|
| 1 | +package pipeline |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/AlecAivazis/survey/v2" |
| 6 | + jCLI "github.com/jenkins-zh/jenkins-cli/client" |
| 7 | + "github.com/linuxsuren/jcli-ks-plugin/cmd/common" |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +func newBackupCommand() (cmd *cobra.Command) { |
| 16 | + opt := &backupOption{} |
| 17 | + |
| 18 | + cmd = &cobra.Command{ |
| 19 | + Use: "backup", |
| 20 | + Aliases: []string{"b"}, |
| 21 | + Short: "Backup KubeSphere Pipeline from Jenkins", |
| 22 | + Long: `Backup Ku beSphere Pipeline from Jenkins |
| 23 | +It only backups the config of jobs |
| 24 | +This command rely on kubectl`, |
| 25 | + PreRunE: opt.preRunE, |
| 26 | + RunE: opt.runE, |
| 27 | + } |
| 28 | + |
| 29 | + flags := cmd.Flags() |
| 30 | + flags.StringVarP(&opt.output, "output", "o", "", "The output directory which store the backup files") |
| 31 | + flags.StringArrayVarP(&opt.jobs, "jobs", "j", []string{}, |
| 32 | + "Which jobs do you want to backup") |
| 33 | + return |
| 34 | +} |
| 35 | + |
| 36 | +type backupOption struct { |
| 37 | + jobs []string |
| 38 | + output string |
| 39 | +} |
| 40 | + |
| 41 | +func (o *backupOption) preRunE(cmd *cobra.Command, args []string) (err error) { |
| 42 | + if len(o.jobs) == 0 { |
| 43 | + // search jobs from Jenkins, then let users to choose |
| 44 | + jclient := &jCLI.JobClient{ |
| 45 | + JenkinsCore: jCLI.JenkinsCore{ |
| 46 | + RoundTripper: nil, |
| 47 | + }, |
| 48 | + } |
| 49 | + if _, err = getCurrentJenkinsAndClient(&(jclient.JenkinsCore)); err != nil { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + var keyword string |
| 54 | + if len(args) > 0 { |
| 55 | + keyword = args[0] |
| 56 | + } |
| 57 | + |
| 58 | + var items []jCLI.JenkinsItem |
| 59 | + var allJobs []string |
| 60 | + if items, err = jclient.SearchViaBlue(keyword, 0, 1000); err == nil { |
| 61 | + allJobs = make([]string, len(items)) |
| 62 | + |
| 63 | + for i, item := range items { |
| 64 | + allJobs[i] = item.FullName |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + prompt := &survey.MultiSelect{ |
| 69 | + Message: "Please select the pipelines that you want to backup:", |
| 70 | + Options: allJobs, |
| 71 | + } |
| 72 | + if err = survey.AskOne(prompt, &o.jobs); err == nil { |
| 73 | + for i, item := range o.jobs { |
| 74 | + o.jobs[i] = strings.Join(strings.Split("/"+item, "/"), "/job/") |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if o.output == "" { |
| 80 | + o.output = path.Dir(".") |
| 81 | + } |
| 82 | + return |
| 83 | +} |
| 84 | + |
| 85 | +func (o *backupOption) runE(_ *cobra.Command, _ []string) (err error) { |
| 86 | + var podName string |
| 87 | + if podName, err = getJenkinsPodName(); err != nil { |
| 88 | + err = fmt.Errorf("cannot get ks-jenkins pod, %v", err) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + for _, job := range o.jobs { |
| 93 | + job = strings.TrimPrefix(job, "/") |
| 94 | + job = strings.TrimSuffix(job, "/") |
| 95 | + job = strings.ReplaceAll(job, "job/", "jobs/") |
| 96 | + |
| 97 | + if err = os.MkdirAll(path.Dir(podName), 0666); err != nil { |
| 98 | + err = fmt.Errorf("cannot mkdir %s, %v", podName, err) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + cmd := fmt.Sprintf("kubectl cp -n kubesphere-devops-system %s:var/jenkins_home/%s/config.xml %s/config.xml", podName, job, job) |
| 103 | + fmt.Println("start to backup", fmt.Sprintf("/var/jenkins_home/%s/config.xml", job)) |
| 104 | + err = common.ExecCommand("kubectl", strings.Split(cmd, " ")[1:]...) |
| 105 | + if err != nil { |
| 106 | + return |
| 107 | + } |
| 108 | + } |
| 109 | + return |
| 110 | +} |
| 111 | + |
| 112 | +func getJenkinsPodName() (name string, err error) { |
| 113 | + var data []byte |
| 114 | + 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", " ")...) |
| 115 | + if data, err = cmd.Output(); err == nil { |
| 116 | + name = strings.TrimSpace(string(data)) |
| 117 | + } |
| 118 | + return |
| 119 | +} |
0 commit comments