未验证 提交 fc819728 编写于 作者: LinuxSuRen's avatar LinuxSuRen 提交者: GitHub

Merge pull request #141 from LinuxSuRen/fix/center-watch

Add support to finish the watch by conditions
...@@ -34,20 +34,17 @@ var centerCmd = &cobra.Command{ ...@@ -34,20 +34,17 @@ var centerCmd = &cobra.Command{
}, },
} }
func printUpdateCenter(jenkins *JenkinsServer, roundTripper http.RoundTripper) { func printUpdateCenter(jenkins *JenkinsServer, roundTripper http.RoundTripper) (
status *client.UpdateCenter, err error) {
jclient := &client.UpdateCenterManager{ jclient := &client.UpdateCenterManager{
JenkinsCore: client.JenkinsCore{ JenkinsCore: client.JenkinsCore{
RoundTripper: roundTripper, RoundTripper: roundTripper,
}, },
} }
jclient.URL = jenkins.URL getCurrentJenkinsAndClient(&(jclient.JenkinsCore))
jclient.UserName = jenkins.UserName
jclient.Token = jenkins.Token
jclient.Proxy = jenkins.Proxy
jclient.ProxyAuth = jenkins.ProxyAuth
var centerStatus string var centerStatus string
if status, err := jclient.Status(); err == nil { if status, err = jclient.Status(); err == nil {
centerStatus += fmt.Sprintf("RestartRequiredForCompletion: %v\n", status.RestartRequiredForCompletion) centerStatus += fmt.Sprintf("RestartRequiredForCompletion: %v\n", status.RestartRequiredForCompletion)
if status.Jobs != nil { if status.Jobs != nil {
for i, job := range status.Jobs { for i, job := range status.Jobs {
...@@ -64,9 +61,8 @@ func printUpdateCenter(jenkins *JenkinsServer, roundTripper http.RoundTripper) { ...@@ -64,9 +61,8 @@ func printUpdateCenter(jenkins *JenkinsServer, roundTripper http.RoundTripper) {
fmt.Printf("%s", centerStatus) fmt.Printf("%s", centerStatus)
} }
} else {
log.Fatal(err)
} }
return
} }
func printJenkinsStatus(jenkins *JenkinsServer, roundTripper http.RoundTripper) { func printJenkinsStatus(jenkins *JenkinsServer, roundTripper http.RoundTripper) {
......
...@@ -4,12 +4,15 @@ import ( ...@@ -4,12 +4,15 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// CenterWatchOption as the options of watch command // CenterWatchOption as the options of watch command
type CenterWatchOption struct { type CenterWatchOption struct {
WatchOption WatchOption
UtilNeedRestart bool
UtilInstallComplete bool
RoundTripper http.RoundTripper RoundTripper http.RoundTripper
CeneterStatus string CeneterStatus string
...@@ -19,6 +22,10 @@ var centerWatchOption CenterWatchOption ...@@ -19,6 +22,10 @@ var centerWatchOption CenterWatchOption
func init() { func init() {
centerCmd.AddCommand(centerWatchCmd) centerCmd.AddCommand(centerWatchCmd)
centerWatchCmd.Flags().BoolVarP(&centerWatchOption.UtilNeedRestart, "util-need-restart", "", false,
"The watch will be continue util Jenkins needs restart")
centerWatchCmd.Flags().BoolVarP(&centerWatchOption.UtilInstallComplete, "util-install-complete", "", false,
"The watch will be continue util all Jenkins plugins installation is completed")
centerWatchOption.SetFlag(centerWatchCmd) centerWatchOption.SetFlag(centerWatchCmd)
} }
...@@ -26,14 +33,34 @@ var centerWatchCmd = &cobra.Command{ ...@@ -26,14 +33,34 @@ var centerWatchCmd = &cobra.Command{
Use: "watch", Use: "watch",
Short: "Watch your update center status", Short: "Watch your update center status",
Long: `Watch your update center status`, Long: `Watch your update center status`,
Run: func(_ *cobra.Command, _ []string) { Run: func(cmd *cobra.Command, _ []string) {
jenkins := getCurrentJenkinsFromOptionsOrDie() jenkins := getCurrentJenkinsFromOptionsOrDie()
printJenkinsStatus(jenkins, centerWatchOption.RoundTripper) printJenkinsStatus(jenkins, centerWatchOption.RoundTripper)
for ; centerWatchOption.Count >= 0; centerWatchOption.Count-- { for ; centerWatchOption.Count >= 0; centerWatchOption.Count-- {
printUpdateCenter(jenkins, centerOption.RoundTripper) if status, err := printUpdateCenter(jenkins, centerOption.RoundTripper); err != nil {
cmd.PrintErr(err)
break
} else if (centerWatchOption.UtilNeedRestart && status.RestartRequiredForCompletion) ||
(centerWatchOption.UtilInstallComplete && allPluginsCompleted(status)) {
return
}
time.Sleep(time.Duration(centerOption.Interval) * time.Second) time.Sleep(time.Duration(centerOption.Interval) * time.Second)
} }
}, },
} }
func allPluginsCompleted(status *client.UpdateCenter) (completed bool) {
if status == nil || status.Jobs == nil {
return
}
for _, job := range status.Jobs {
if job.Type == "InstallationJob" && !job.Status.Success {
return
}
}
completed = true
return
}
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/mock/mhttp" "github.com/jenkins-zh/jenkins-cli/mock/mhttp"
) )
...@@ -39,7 +40,7 @@ var _ = Describe("center watch command", func() { ...@@ -39,7 +40,7 @@ var _ = Describe("center watch command", func() {
}) })
Context("basic cases", func() { Context("basic cases", func() {
It("should success", func() { It("should success, center watch command", func() {
data, err := generateSampleConfig() data, err := generateSampleConfig()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664) err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
...@@ -62,5 +63,30 @@ var _ = Describe("center watch command", func() { ...@@ -62,5 +63,30 @@ var _ = Describe("center watch command", func() {
_, err = rootCmd.ExecuteC() _, err = rootCmd.ExecuteC()
Expect(err).To(BeNil()) Expect(err).To(BeNil())
}) })
It("allPluginsCompleted", func() {
Expect(allPluginsCompleted(nil)).To(Equal(false))
status := &client.UpdateCenter{}
Expect(allPluginsCompleted(status)).To(Equal(false))
// all install job is completed
status.Jobs = []client.InstallationJob{client.InstallationJob{
UpdateCenterJob: client.UpdateCenterJob{Type: "InstallationJob"},
Status: client.InstallationJobStatus{
Success: true,
},
}}
Expect(allPluginsCompleted(status)).To(Equal(true))
// there's one install job is not completed
status.Jobs = append(status.Jobs, client.InstallationJob{
UpdateCenterJob: client.UpdateCenterJob{Type: "InstallationJob"},
Status: client.InstallationJobStatus{
Success: false,
},
})
Expect(allPluginsCompleted(status)).To(Equal(false))
})
}) })
}) })
...@@ -86,7 +86,7 @@ type WatchOption struct { ...@@ -86,7 +86,7 @@ type WatchOption struct {
// SetFlag for WatchOption // SetFlag for WatchOption
func (o *WatchOption) SetFlag(cmd *cobra.Command) { func (o *WatchOption) SetFlag(cmd *cobra.Command) {
cmd.Flags().IntVarP(&o.Interval, "interval", "i", 1, "Interval of watch") cmd.Flags().IntVarP(&o.Interval, "interval", "i", 1, "Interval of watch")
cmd.Flags().IntVarP(&o.Interval, "count", "", 9999, "Count of watch") cmd.Flags().IntVarP(&o.Count, "count", "", 9999, "Count of watch")
} }
// InteractiveOption allow user to choose whether the mode is interactive // InteractiveOption allow user to choose whether the mode is interactive
...@@ -101,7 +101,7 @@ func (b *InteractiveOption) SetFlag(cmd *cobra.Command) { ...@@ -101,7 +101,7 @@ func (b *InteractiveOption) SetFlag(cmd *cobra.Command) {
// HookOption is the option whether skip command hook // HookOption is the option whether skip command hook
type HookOption struct { type HookOption struct {
SkipPreHook bool SkipPreHook bool
SkipPostHook bool SkipPostHook bool
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册