@@ -4,15 +4,15 @@ import (
4
4
"fmt"
5
5
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
6
6
. "github.com/jenkins-zh/jenkins-cli/app/config"
7
+ "github.com/jenkins-zh/jenkins-cli/app/i18n"
7
8
"github.com/jenkins-zh/jenkins-cli/client"
8
9
"github.com/jenkins-zh/jenkins-cli/util"
10
+ "github.com/spf13/cobra"
9
11
"go.uber.org/zap"
10
12
"io/ioutil"
13
+ "net/url"
11
14
"os"
12
-
13
- "github.com/jenkins-zh/jenkins-cli/app/i18n"
14
-
15
- "github.com/spf13/cobra"
15
+ "strings"
16
16
)
17
17
18
18
// ComputerLaunchOption option for config list command
@@ -28,14 +28,21 @@ type ComputerLaunchOption struct {
28
28
Output string
29
29
}
30
30
31
+ const (
32
+ // AgentJNLP is the agent type of jnlp
33
+ AgentJNLP = "jnlp"
34
+ )
35
+
31
36
var computerLaunchOption ComputerLaunchOption
32
37
33
38
func init () {
34
39
computerCmd .AddCommand (computerLaunchCmd )
35
- computerLaunchCmd .Flags ().StringVarP (& computerLaunchOption .Type , "type" , "" , "" ,
40
+ computerLaunchCmd .Flags ().StringVarP (& computerLaunchOption .Type , "type" , "" , AgentJNLP ,
36
41
i18n .T ("The type of agent, include jnlp" ))
37
42
computerLaunchCmd .Flags ().BoolVarP (& computerLaunchOption .ShowProgress , "show-progress" , "" , true ,
38
43
i18n .T ("Show the progress of downloading agent.jar" ))
44
+
45
+ healthCheckRegister .Register (getCmdPath (computerLaunchCmd ), & computerLaunchOption )
39
46
}
40
47
41
48
var computerLaunchCmd = & cobra.Command {
@@ -50,17 +57,24 @@ jcli agent launch agent-name --type jnlp`,
50
57
computerLaunchOption .ComputerClient , computerLaunchOption .CurrentJenkins =
51
58
GetComputerClient (computerLaunchOption .CommonOption )
52
59
53
- if computerLaunchOption .Type != "jnlp" {
60
+ if computerLaunchOption .Type != AgentJNLP {
54
61
return
55
62
}
56
63
57
64
var f * os.File
58
65
if f , err = ioutil .TempFile ("/tmp" , "agent.jar" ); err == nil {
59
66
computerLaunchOption .Output = f .Name ()
67
+ agentURL := fmt .Sprintf ("%s/jnlpJars/agent.jar" , computerLaunchOption .ComputerClient .URL )
68
+ logger .Debug ("start to download agent.jar" , zap .String ("url" , agentURL ))
69
+ logger .Debug ("proxy setting" , zap .String ("sever" , computerLaunchOption .CurrentJenkins .Proxy ),
70
+ zap .String ("auth" , computerLaunchOption .CurrentJenkins .ProxyAuth ))
71
+
60
72
downloader := util.HTTPDownloader {
61
73
RoundTripper : computerLaunchOption .RoundTripper ,
62
74
TargetFilePath : computerLaunchOption .Output ,
63
- URL : fmt .Sprintf ("%s/jnlpJars/agent.jar" , computerLaunchOption .ComputerClient .URL ),
75
+ URL : agentURL ,
76
+ Proxy : computerLaunchOption .CurrentJenkins .Proxy ,
77
+ ProxyAuth : computerLaunchOption .CurrentJenkins .ProxyAuth ,
64
78
ShowProgress : computerLaunchOption .ShowProgress ,
65
79
}
66
80
err = downloader .DownloadFile ()
@@ -69,11 +83,15 @@ jcli agent launch agent-name --type jnlp`,
69
83
},
70
84
RunE : func (_ * cobra.Command , args []string ) (err error ) {
71
85
name := args [0 ]
86
+ logger .Info ("prepare to start agent" , zap .String ("name" , name ), zap .String ("type" , computerLaunchOption .Type ))
87
+
72
88
switch computerLaunchOption .Type {
73
89
case "" :
74
90
err = computerLaunchOption .Launch (name )
75
- case "jnlp" :
91
+ case AgentJNLP :
76
92
err = computerLaunchOption .LaunchJnlp (name )
93
+ default :
94
+ err = fmt .Errorf ("unsupported agent type %s" , computerLaunchOption .Type )
77
95
}
78
96
return
79
97
},
@@ -89,6 +107,8 @@ func (o *ComputerLaunchOption) Launch(name string) (err error) {
89
107
func (o * ComputerLaunchOption ) LaunchJnlp (name string ) (err error ) {
90
108
var secret string
91
109
if secret , err = o .ComputerClient .GetSecret (name ); err == nil {
110
+ logger .Info ("get agent secret" , zap .String ("value" , secret ))
111
+
92
112
var binary string
93
113
binary , err = util .LookPath ("java" , centerStartOption .LookPathContext )
94
114
if err == nil {
@@ -97,10 +117,29 @@ func (o *ComputerLaunchOption) LaunchJnlp(name string) (err error) {
97
117
"-jnlpUrl" , fmt .Sprintf ("%s/computer/%s/slave-agent.jnlp" , o .ComputerClient .URL , name ),
98
118
"-secret" , secret , "-workDir" , "/tmp" }
99
119
100
- logger .Debug ("start a jnlp agent" , zap .Any ("command" , agentArgs ))
120
+ if o .CurrentJenkins .ProxyAuth != "" {
121
+ proxyURL , _ := url .Parse (o .CurrentJenkins .Proxy )
122
+ agentArgs = append (agentArgs , "-proxyCredentials" , o .CurrentJenkins .ProxyAuth )
123
+
124
+ proxyAuth := strings .SplitN (o .CurrentJenkins .ProxyAuth , ":" , 2 )
125
+ if len (proxyAuth ) == 2 {
126
+ env = append (env , fmt .Sprintf ("http_proxy=http://%s:%s@%s" , url .QueryEscape (proxyAuth [0 ]), url .QueryEscape (proxyAuth [1 ]), proxyURL .Host ))
127
+ }
128
+ }
129
+
130
+ logger .Debug ("start a jnlp agent" , zap .Any ("command" , strings .Join (agentArgs , " " )))
101
131
102
132
err = util .Exec (binary , agentArgs , env , o .SystemCallExec )
103
133
}
104
134
}
105
135
return
106
136
}
137
+
138
+ // Check do the health check of casc cmd
139
+ func (o * ComputerLaunchOption ) Check () (err error ) {
140
+ opt := PluginOptions {
141
+ CommonOption : common.CommonOption {RoundTripper : o .RoundTripper },
142
+ }
143
+ _ , err = opt .FindPlugin ("pipeline-restful-api" )
144
+ return
145
+ }
0 commit comments