Skip to content

Commit

Permalink
Merge branch 'master' into dependabot-go_modules-github.com-spf13-cob…
Browse files Browse the repository at this point in the history
…ra-1.1.3
  • Loading branch information
LinuxSuRen committed Apr 1, 2021
2 parents 3819185 + 71189e8 commit 4b12075
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v2.3.4
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2.4.1
uses: goreleaser/goreleaser-action@v2.5.0
with:
version: latest
args: check
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
with:
go-version: 1.13.x
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2.4.1
uses: goreleaser/goreleaser-action@v2.5.0
with:
version: latest
args: release --rm-dist
Expand Down
5 changes: 3 additions & 2 deletions cmd/config/exec.go → cmd/common/exec.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package common

import (
"io"
Expand All @@ -7,7 +7,8 @@ import (
"sync"
)

func execCommand(name string, arg ...string) (err error) {
// ExecCommand exec command
func ExecCommand(name string, arg ...string) (err error) {
command := exec.Command(name, arg...)

//var stdout []byte
Expand Down
1 change: 1 addition & 0 deletions cmd/config/config_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import "github.com/spf13/cobra"

// NewConfigCmd creates config command
func NewConfigCmd() (cmd *cobra.Command) {
cmd = &cobra.Command{
Use: "config",
Expand Down
3 changes: 2 additions & 1 deletion cmd/config/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/ghodss/yaml"
"github.com/linuxsuren/jcli-ks-plugin/cmd/common"
kstypes "github.com/linuxsuren/ks/kubectl-plugin/types"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -81,6 +82,6 @@ func (o *updateOption) preRunE(_ *cobra.Command, args []string) (err error) {
}

func (o *updateOption) runE(_ *cobra.Command, args []string) (err error) {
err = execCommand("jcli", "config", "update", "--token", o.token, o.name)
err = common.ExecCommand("jcli", "config", "update", "--token", o.token, o.name)
return
}
119 changes: 119 additions & 0 deletions cmd/pipeline/backup.go
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
}
85 changes: 85 additions & 0 deletions cmd/pipeline/inner.go
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
}
14 changes: 14 additions & 0 deletions cmd/pipeline/pipeline_root.go
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
}
61 changes: 61 additions & 0 deletions cmd/pipeline/restore.go
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
}
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/linuxsuren/jcli-ks-plugin/cmd/config"
"github.com/linuxsuren/jcli-ks-plugin/cmd/pipeline"
"github.com/spf13/cobra"
)

Expand All @@ -12,6 +13,6 @@ func NewKSPlugin() (cmd *cobra.Command) {
Short: "jcli plugin for KubeSphere",
}

cmd.AddCommand(config.NewConfigCmd())
cmd.AddCommand(config.NewConfigCmd(), pipeline.NewPipelineRootCommand())
return
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ module github.com/linuxsuren/jcli-ks-plugin
go 1.15

require (
github.com/AlecAivazis/survey/v2 v2.2.9
github.com/ghodss/yaml v1.0.0
github.com/linuxsuren/ks v0.0.26
github.com/linuxsuren/ks v0.0.27
github.com/spf13/cobra v1.1.3
github.com/jenkins-zh/jenkins-cli v0.0.37-0.20210331140915-f101c983193d
github.com/mitchellh/go-homedir v1.1.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/apimachinery v0.20.4
k8s.io/client-go v0.19.4
)

0 comments on commit 4b12075

Please sign in to comment.