@@ -65,19 +65,19 @@ func main() {
65
65
defer cleanup ()
66
66
if err != nil {
67
67
fmt .Fprintln (sess , err )
68
- log . Println (err )
68
+ ErrorPrint (err . Error () )
69
69
}
70
70
sess .Exit (int (status ))
71
71
})
72
72
73
- log . Printf ("starting ssh server on %s..." , * bindAddress )
73
+ InfoPrint ("starting ssh server on %s..." , * bindAddress )
74
74
log .Fatal (ssh .ListenAndServe (* bindAddress , nil ))
75
75
}
76
76
77
77
func imageExistsLocally (ctx context.Context , imageName string , cli * client.Client ) bool {
78
78
images , err := cli .ImageList (ctx , image.ListOptions {})
79
79
if err != nil {
80
- log . Printf ("Error listing images: %v" , err )
80
+ ErrorPrint ("Error listing images: %v" , err )
81
81
return false
82
82
}
83
83
@@ -106,19 +106,19 @@ func waitForContainerReady(ctx context.Context, sess ssh.Session, cli *client.Cl
106
106
// If a health check is defined, ensure it's healthy
107
107
if containerJSON .State .Health != nil {
108
108
if containerJSON .State .Health .Status == "healthy" {
109
- log . Println ("Container is running and healthy." )
109
+ InfoPrint ("Container %s is running and healthy." , containerID )
110
110
return nil
111
111
} else if containerJSON .State .Health .Status == "unhealthy" {
112
112
return fmt .Errorf ("container is unhealthy" )
113
113
}
114
114
} else {
115
- log . Println ("Container is running." )
115
+ InfoPrint ("Container %s is running." , containerID )
116
116
return nil
117
117
}
118
118
}
119
119
120
120
sess .Write ([]byte ("Waiting for container to become ready...\n " ))
121
- log . Printf ("Waiting for container %s to be ready..." , containerID )
121
+ InfoPrint ("Waiting for container %s to be ready..." , containerID )
122
122
time .Sleep (2 * time .Second )
123
123
}
124
124
@@ -133,7 +133,7 @@ func dockerRun(cfg *container.Config, hostcfg *container.HostConfig, sess ssh.Se
133
133
cleanup = func () {}
134
134
ctx := context .Background ()
135
135
136
- log . Printf ( "User : %s" , sess .User ())
136
+ InfoPrint ( "Image : %s" , sess .User ())
137
137
cImage := sess .User ()
138
138
139
139
networkingConfig := network.NetworkingConfig {}
@@ -143,36 +143,36 @@ func dockerRun(cfg *container.Config, hostcfg *container.HostConfig, sess ssh.Se
143
143
// Variant: "minimal",
144
144
}
145
145
if imageExistsLocally (ctx , cImage , docker ) != true {
146
- sess .Write ([]byte ("Fetching Image from repository .." ))
146
+ sess .Write ([]byte ("Fetching Image from repository ..\n " ))
147
147
reader , pullerr := docker .ImagePull (ctx , cImage , image.PullOptions {})
148
148
if pullerr != nil {
149
- sess .Write ([]byte ("Unable to pull requested image" + string (pullerr .Error ()) + "\n " ))
150
- log . Printf ( "Error pulling image: %v" , pullerr )
149
+ sess .Write ([]byte ("Unable to pull requested image [ " + cImage + "]: [" + string (pullerr .Error ()) + "] \n " ))
150
+ ErrorPrint ( "Unable to pull requested image [%s] : %v", cImage , pullerr )
151
151
cleanup = func () {}
152
152
return
153
153
}
154
154
defer reader .Close ()
155
155
if _ , err := io .Copy (os .Stdout , reader ); err != nil {
156
- log . Printf ( "Error reading pull output: %v" , pullerr )
156
+ ErrorPrint ( "Unable to read pull output: %v" , pullerr )
157
157
}
158
158
}
159
159
160
160
resp , err := docker .ContainerCreate (ctx , cfg , hostcfg , & networkingConfig , & platformConfig , "" )
161
161
if err != nil {
162
- log . Printf ("Unable to create container: %v" , err )
162
+ ErrorPrint ("Unable to create container: %v" , err )
163
163
return
164
164
}
165
- log . Printf ("Created container: %s" , resp .ID )
165
+ InfoPrint ("Created container: %s" , resp .ID )
166
166
cleanup = func () {
167
167
docker .ContainerRemove (ctx , resp .ID , container.RemoveOptions {})
168
168
}
169
169
startErr := docker .ContainerStart (ctx , resp .ID , container.StartOptions {})
170
170
if startErr != nil {
171
- log . Printf ("Unable to start container: %v" , err )
171
+ ErrorPrint ("Unable to start container: %v" , err )
172
172
sess .Write ([]byte ("Unable to pull requested image" + string (startErr .Error ()) + "\n " ))
173
173
return
174
174
}
175
- log . Printf ("Wait for container %s to be ready" , resp .ID )
175
+ InfoPrint ("Wait for container %s to be ready" , resp .ID )
176
176
err = waitForContainerReady (ctx , sess , docker , resp .ID , 30 * time .Second )
177
177
if err != nil {
178
178
sess .Write ([]byte ("container failed to become ready" ))
@@ -189,7 +189,7 @@ func dockerRun(cfg *container.Config, hostcfg *container.HostConfig, sess ssh.Se
189
189
if err != nil {
190
190
return
191
191
}
192
- log . Printf ("Attaching container: %s" , resp .ID )
192
+ InfoPrint ("Attaching container: %s" , resp .ID )
193
193
stream , err := docker .ContainerExecAttach (ctx , execResp .ID , container.ExecStartOptions {
194
194
Tty : true ,
195
195
})
@@ -220,7 +220,7 @@ func dockerRun(cfg *container.Config, hostcfg *container.HostConfig, sess ssh.Se
220
220
Width : uint (win .Width ),
221
221
})
222
222
if err != nil {
223
- log . Println (err )
223
+ ErrorPrint (err . Error () )
224
224
break
225
225
}
226
226
}
@@ -229,12 +229,11 @@ func dockerRun(cfg *container.Config, hostcfg *container.HostConfig, sess ssh.Se
229
229
select {
230
230
case <- outputErr :
231
231
cleanup = func () {
232
- log . Printf ("Killing container: %s" , resp .ID )
232
+ InfoPrint ("Killing container: %s" , resp .ID )
233
233
docker .ContainerKill (ctx , resp .ID , "9" )
234
- log . Printf ("Removing container: %s" , resp .ID )
234
+ InfoPrint ("Removing container: %s" , resp .ID )
235
235
docker .ContainerRemove (ctx , resp .ID , container.RemoveOptions {})
236
236
}
237
- log .Println ("Exit.." )
238
237
return
239
238
}
240
239
}
0 commit comments