@@ -29,11 +29,13 @@ type RunJob struct {
29
29
Delete string `default:"true"`
30
30
Pull string `default:"true"`
31
31
32
- Image string
33
- Network string
34
- Container string
35
- Volume []string
36
- Env []string
32
+ Image string
33
+ Network string
34
+ Container string
35
+ Volume []string
36
+ Environment []string
37
+
38
+ containerID string
37
39
}
38
40
39
41
func NewRunJob (c * docker.Client ) * RunJob {
@@ -92,18 +94,31 @@ func (j *RunJob) Run(ctx *Context) error {
92
94
return err
93
95
}
94
96
} else {
95
- container , err = j .getContainer (j .Container )
97
+ container , err = j .Client . InspectContainer (j .Container )
96
98
if err != nil {
97
99
return err
98
100
}
99
101
}
100
102
103
+ if container != nil {
104
+ j .containerID = container .ID
105
+ }
106
+
107
+ // cleanup container if it is a created one
108
+ if j .Container == "" {
109
+ defer func () {
110
+ if delErr := j .deleteContainer (); delErr != nil {
111
+ ctx .Warn ("failed to delete container: " + delErr .Error ())
112
+ }
113
+ }()
114
+ }
115
+
101
116
startTime := time .Now ()
102
- if err := j .startContainer (ctx . Execution , container ); err != nil {
117
+ if err := j .startContainer (); err != nil {
103
118
return err
104
119
}
105
120
106
- err = j .watchContainer (container . ID )
121
+ err = j .watchContainer ()
107
122
if err == ErrUnexpected {
108
123
return err
109
124
}
@@ -120,14 +135,6 @@ func (j *RunJob) Run(ctx *Context) error {
120
135
ctx .Warn ("failed to fetch container logs: " + logsErr .Error ())
121
136
}
122
137
123
- if j .Container == "" {
124
- defer func () {
125
- if delErr := j .deleteContainer (container .ID ); delErr != nil {
126
- ctx .Warn ("failed to delete container: " + delErr .Error ())
127
- }
128
- }()
129
- }
130
-
131
138
return err
132
139
}
133
140
@@ -163,7 +170,7 @@ func (j *RunJob) buildContainer() (*docker.Container, error) {
163
170
Tty : j .TTY ,
164
171
Cmd : args .GetArgs (j .Command ),
165
172
User : j .User ,
166
- Env : j .Env ,
173
+ Env : j .Environment ,
167
174
},
168
175
NetworkingConfig : & docker.NetworkingConfig {},
169
176
HostConfig : & docker.HostConfig {
@@ -193,12 +200,16 @@ func (j *RunJob) buildContainer() (*docker.Container, error) {
193
200
return c , nil
194
201
}
195
202
196
- func (j * RunJob ) startContainer (e * Execution , c * docker.Container ) error {
197
- return j .Client .StartContainer (c .ID , & docker.HostConfig {})
203
+ func (j * RunJob ) startContainer () error {
204
+ return j .Client .StartContainer (j .containerID , & docker.HostConfig {})
205
+ }
206
+
207
+ func (j * RunJob ) stopContainer (timeout uint ) error {
208
+ return j .Client .StopContainer (j .containerID , timeout )
198
209
}
199
210
200
- func (j * RunJob ) getContainer (id string ) (* docker.Container , error ) {
201
- container , err := j .Client .InspectContainer (id )
211
+ func (j * RunJob ) getContainer () (* docker.Container , error ) {
212
+ container , err := j .Client .InspectContainer (j . containerID )
202
213
if err != nil {
203
214
return nil , err
204
215
}
@@ -210,7 +221,7 @@ const (
210
221
maxProcessDuration = time .Hour * 24
211
222
)
212
223
213
- func (j * RunJob ) watchContainer (containerID string ) error {
224
+ func (j * RunJob ) watchContainer () error {
214
225
var s docker.State
215
226
var r time.Duration
216
227
for {
@@ -221,7 +232,7 @@ func (j *RunJob) watchContainer(containerID string) error {
221
232
return ErrMaxTimeRunning
222
233
}
223
234
224
- c , err := j .Client .InspectContainer (containerID )
235
+ c , err := j .Client .InspectContainer (j . containerID )
225
236
if err != nil {
226
237
return err
227
238
}
@@ -242,12 +253,12 @@ func (j *RunJob) watchContainer(containerID string) error {
242
253
}
243
254
}
244
255
245
- func (j * RunJob ) deleteContainer (containerID string ) error {
256
+ func (j * RunJob ) deleteContainer () error {
246
257
if delete , _ := strconv .ParseBool (j .Delete ); ! delete {
247
258
return nil
248
259
}
249
260
250
261
return j .Client .RemoveContainer (docker.RemoveContainerOptions {
251
- ID : containerID ,
262
+ ID : j . containerID ,
252
263
})
253
264
}
0 commit comments