generated from LinuxSuRen/github-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into dependabot-go_modules-github.com-spf13-cob…
…ra-1.1.3
- Loading branch information
Showing
12 changed files
with
363 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package pipeline | ||
|
||
import ( | ||
"fmt" | ||
"github.com/AlecAivazis/survey/v2" | ||
jCLI "github.com/jenkins-zh/jenkins-cli/client" | ||
"github.com/linuxsuren/jcli-ks-plugin/cmd/common" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"strings" | ||
) | ||
|
||
func newBackupCommand() (cmd *cobra.Command) { | ||
opt := &backupOption{} | ||
|
||
cmd = &cobra.Command{ | ||
Use: "backup", | ||
Aliases: []string{"b"}, | ||
Short: "Backup KubeSphere Pipeline from Jenkins", | ||
Long: `Backup Ku beSphere Pipeline from Jenkins | ||
It only backups the config of jobs | ||
This command rely on kubectl`, | ||
PreRunE: opt.preRunE, | ||
RunE: opt.runE, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.StringVarP(&opt.output, "output", "o", "", "The output directory which store the backup files") | ||
flags.StringArrayVarP(&opt.jobs, "jobs", "j", []string{}, | ||
"Which jobs do you want to backup") | ||
return | ||
} | ||
|
||
type backupOption struct { | ||
jobs []string | ||
output string | ||
} | ||
|
||
func (o *backupOption) preRunE(cmd *cobra.Command, args []string) (err error) { | ||
if len(o.jobs) == 0 { | ||
// search jobs from Jenkins, then let users to choose | ||
jclient := &jCLI.JobClient{ | ||
JenkinsCore: jCLI.JenkinsCore{ | ||
RoundTripper: nil, | ||
}, | ||
} | ||
if _, err = getCurrentJenkinsAndClient(&(jclient.JenkinsCore)); err != nil { | ||
return | ||
} | ||
|
||
var keyword string | ||
if len(args) > 0 { | ||
keyword = args[0] | ||
} | ||
|
||
var items []jCLI.JenkinsItem | ||
var allJobs []string | ||
if items, err = jclient.SearchViaBlue(keyword, 0, 1000); err == nil { | ||
allJobs = make([]string, len(items)) | ||
|
||
for i, item := range items { | ||
allJobs[i] = item.FullName | ||
} | ||
} | ||
|
||
prompt := &survey.MultiSelect{ | ||
Message: "Please select the pipelines that you want to backup:", | ||
Options: allJobs, | ||
} | ||
if err = survey.AskOne(prompt, &o.jobs); err == nil { | ||
for i, item := range o.jobs { | ||
o.jobs[i] = strings.Join(strings.Split("/"+item, "/"), "/job/") | ||
} | ||
} | ||
} | ||
|
||
if o.output == "" { | ||
o.output = path.Dir(".") | ||
} | ||
return | ||
} | ||
|
||
func (o *backupOption) runE(_ *cobra.Command, _ []string) (err error) { | ||
var podName string | ||
if podName, err = getJenkinsPodName(); err != nil { | ||
err = fmt.Errorf("cannot get ks-jenkins pod, %v", err) | ||
return | ||
} | ||
|
||
for _, job := range o.jobs { | ||
job = strings.TrimPrefix(job, "/") | ||
job = strings.TrimSuffix(job, "/") | ||
job = strings.ReplaceAll(job, "job/", "jobs/") | ||
|
||
if err = os.MkdirAll(path.Dir(podName), 0666); err != nil { | ||
err = fmt.Errorf("cannot mkdir %s, %v", podName, err) | ||
return | ||
} | ||
|
||
cmd := fmt.Sprintf("kubectl cp -n kubesphere-devops-system %s:var/jenkins_home/%s/config.xml %s/config.xml", podName, job, job) | ||
fmt.Println("start to backup", fmt.Sprintf("/var/jenkins_home/%s/config.xml", job)) | ||
err = common.ExecCommand("kubectl", strings.Split(cmd, " ")[1:]...) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
return | ||
} | ||
|
||
func getJenkinsPodName() (name string, err error) { | ||
var data []byte | ||
cmd := exec.Command("kubectl", strings.Split("-n kubesphere-devops-system get pod -l app=ks-jenkins -o custom-columns=NAME:.metadata.name --no-headers=true", " ")...) | ||
if data, err = cmd.Output(); err == nil { | ||
name = strings.TrimSpace(string(data)) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package pipeline | ||
|
||
import ( | ||
"fmt" | ||
"github.com/jenkins-zh/jenkins-cli/app/cmd/keyring" | ||
"github.com/jenkins-zh/jenkins-cli/client" | ||
"github.com/mitchellh/go-homedir" | ||
"gopkg.in/yaml.v2" | ||
"io/ioutil" | ||
"os" | ||
|
||
appCfg "github.com/jenkins-zh/jenkins-cli/app/config" | ||
) | ||
|
||
/** | ||
* These code lines should be moved into github.com/jenkins-zh/jenkins-cli at sometime | ||
*/ | ||
|
||
var config *appCfg.Config | ||
|
||
func getCurrentJenkins() (cfg *appCfg.JenkinsServer, err error) { | ||
if err = loadDefaultConfig(); err == nil { | ||
cfg = findJenkinsByName(config.Current) | ||
} | ||
return | ||
} | ||
|
||
func getClient(jenkins *appCfg.JenkinsServer, jClient *client.JenkinsCore) { | ||
jClient.URL = jenkins.URL | ||
jClient.UserName = jenkins.UserName | ||
jClient.Token = jenkins.Token | ||
jClient.Proxy = jenkins.Proxy | ||
jClient.ProxyAuth = jenkins.ProxyAuth | ||
jClient.InsecureSkipVerify = jenkins.InsecureSkipVerify | ||
} | ||
|
||
func getCurrentJenkinsAndClient(jClient *client.JenkinsCore) (jenkins *appCfg.JenkinsServer, err error) { | ||
if jenkins, err = getCurrentJenkins(); err == nil && jenkins != nil { | ||
getClient(jenkins, jClient) | ||
} | ||
return | ||
} | ||
|
||
func findJenkinsByName(name string) (jenkinsServer *appCfg.JenkinsServer) { | ||
if config == nil { | ||
return | ||
} | ||
|
||
for _, cfg := range config.JenkinsServers { | ||
if cfg.Name == name { | ||
jenkinsServer = &cfg | ||
break | ||
} | ||
} | ||
return | ||
} | ||
|
||
func getDefaultConfigPath() (configPath string, err error) { | ||
var userHome string | ||
userHome, err = homedir.Dir() | ||
if err == nil { | ||
configPath = fmt.Sprintf("%s/.jenkins-cli.yaml", userHome) | ||
} | ||
return | ||
} | ||
|
||
func loadDefaultConfig() (err error) { | ||
var configPath string | ||
if configPath, err = getDefaultConfigPath(); err == nil { | ||
if _, err = os.Stat(configPath); err == nil { | ||
err = loadConfig(configPath) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func loadConfig(path string) (err error) { | ||
var content []byte | ||
if content, err = ioutil.ReadFile(path); err == nil { | ||
err = yaml.Unmarshal([]byte(content), &config) | ||
|
||
keyring.LoadTokenFromKeyring(config) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package pipeline | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
// NewPipelineRootCommand returns the root command of pipeline | ||
func NewPipelineRootCommand() (cmd *cobra.Command) { | ||
cmd = &cobra.Command{ | ||
Use: "pipeline", | ||
Aliases: []string{"pip"}, | ||
} | ||
|
||
cmd.AddCommand(newBackupCommand(), newRestoreCommand()) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package pipeline | ||
|
||
import ( | ||
"fmt" | ||
"github.com/linuxsuren/jcli-ks-plugin/cmd/common" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func newRestoreCommand() (cmd *cobra.Command) { | ||
opt := &restoreOption{} | ||
|
||
cmd = &cobra.Command{ | ||
Use: "restore", | ||
Short: "Restore KubeSphere Pipeline to Jenkins", | ||
Long: `Restore KubeSphere Pipeline to Jenkins | ||
It only restore the config of jobs | ||
This command rely on kubectl`, | ||
PreRunE: opt.preRunE, | ||
RunE: opt.runE, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.StringVarP(&opt.input, "input", "i", "", "The input directory which store the backup files") | ||
return | ||
} | ||
|
||
type restoreOption struct { | ||
input string | ||
} | ||
|
||
func (o *restoreOption) preRunE(cmd *cobra.Command, args []string) (err error) { | ||
if o.input == "" { | ||
o.input = path.Dir(".") | ||
} | ||
return | ||
} | ||
|
||
func (o *restoreOption) runE(cmd *cobra.Command, args []string) (err error) { | ||
var podName string | ||
if podName, err = getJenkinsPodName(); err != nil { | ||
err = fmt.Errorf("cannot get ks-jenkins pod, %v", err) | ||
return | ||
} | ||
|
||
err = filepath.Walk(fmt.Sprintf("%s/jobs", o.input), func(path string, info os.FileInfo, err error) error { | ||
if strings.HasSuffix(path, "config.xml") { | ||
cmd := fmt.Sprintf("kubectl cp -n kubesphere-devops-system %s %s:var/jenkins_home/%s", path, podName, path) | ||
fmt.Println("start to restore", path) | ||
err = common.ExecCommand("kubectl", strings.Split(cmd, " ")[1:]...) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.