未验证 提交 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{
},
}
func printUpdateCenter(jenkins *JenkinsServer, roundTripper http.RoundTripper) {
func printUpdateCenter(jenkins *JenkinsServer, roundTripper http.RoundTripper) (
status *client.UpdateCenter, err error) {
jclient := &client.UpdateCenterManager{
JenkinsCore: client.JenkinsCore{
RoundTripper: roundTripper,
},
}
jclient.URL = jenkins.URL
jclient.UserName = jenkins.UserName
jclient.Token = jenkins.Token
jclient.Proxy = jenkins.Proxy
jclient.ProxyAuth = jenkins.ProxyAuth
getCurrentJenkinsAndClient(&(jclient.JenkinsCore))
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)
if status.Jobs != nil {
for i, job := range status.Jobs {
......@@ -64,9 +61,8 @@ func printUpdateCenter(jenkins *JenkinsServer, roundTripper http.RoundTripper) {
fmt.Printf("%s", centerStatus)
}
} else {
log.Fatal(err)
}
return
}
func printJenkinsStatus(jenkins *JenkinsServer, roundTripper http.RoundTripper) {
......
......@@ -4,12 +4,15 @@ import (
"net/http"
"time"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra"
)
// CenterWatchOption as the options of watch command
type CenterWatchOption struct {
WatchOption
UtilNeedRestart bool
UtilInstallComplete bool
RoundTripper http.RoundTripper
CeneterStatus string
......@@ -19,6 +22,10 @@ var centerWatchOption CenterWatchOption
func init() {
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)
}
......@@ -26,14 +33,34 @@ var centerWatchCmd = &cobra.Command{
Use: "watch",
Short: "Watch your update center status",
Long: `Watch your update center status`,
Run: func(_ *cobra.Command, _ []string) {
Run: func(cmd *cobra.Command, _ []string) {
jenkins := getCurrentJenkinsFromOptionsOrDie()
printJenkinsStatus(jenkins, centerWatchOption.RoundTripper)
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)
}
},
}
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 (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
)
......@@ -39,7 +40,7 @@ var _ = Describe("center watch command", func() {
})
Context("basic cases", func() {
It("should success", func() {
It("should success, center watch command", func() {
data, err := generateSampleConfig()
Expect(err).To(BeNil())
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
......@@ -62,5 +63,30 @@ var _ = Describe("center watch command", func() {
_, err = rootCmd.ExecuteC()
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 {
// SetFlag for WatchOption
func (o *WatchOption) SetFlag(cmd *cobra.Command) {
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
......@@ -101,7 +101,7 @@ func (b *InteractiveOption) SetFlag(cmd *cobra.Command) {
// HookOption is the option whether skip command hook
type HookOption struct {
SkipPreHook bool
SkipPreHook bool
SkipPostHook bool
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册