Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Jenkins 中文社区
jenkins-cli
提交
fc819728
J
jenkins-cli
项目概览
Jenkins 中文社区
/
jenkins-cli
通知
49
Star
8
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins-cli
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
fc819728
编写于
9月 07, 2019
作者:
LinuxSuRen
提交者:
GitHub
9月 07, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #141 from LinuxSuRen/fix/center-watch
Add support to finish the watch by conditions
上级
6709a005
3a3cb765
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
63 addition
and
14 deletion
+63
-14
app/cmd/center.go
app/cmd/center.go
+5
-9
app/cmd/center_watch.go
app/cmd/center_watch.go
+29
-2
app/cmd/center_watch_test.go
app/cmd/center_watch_test.go
+27
-1
app/cmd/common.go
app/cmd/common.go
+2
-2
未找到文件。
app/cmd/center.go
浏览文件 @
fc819728
...
@@ -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
)
{
...
...
app/cmd/center_watch.go
浏览文件 @
fc819728
...
@@ -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
}
app/cmd/center_watch_test.go
浏览文件 @
fc819728
...
@@ -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
))
})
})
})
})
})
app/cmd/common.go
浏览文件 @
fc819728
...
@@ -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
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录