@@ -8,10 +8,10 @@ import (
88 "strings"
99 "time"
1010
11- apiTypes "github.com/docker/docker/api/types"
1211 "github.com/docker/docker/api/types/container"
12+ "github.com/docker/docker/api/types/events"
1313 "github.com/docker/docker/api/types/filters"
14- "github.com/docker/docker /client"
14+ "github.com/moby/moby /client"
1515 "github.com/sirupsen/logrus"
1616
1717 "github.com/wrfly/container-web-tty/config"
@@ -21,7 +21,7 @@ import (
2121type DockerCli struct {
2222 cli * client.Client
2323 containers * types.Containers
24- listOptions apiTypes. ContainerListOptions
24+ listOptions container. ListOptions
2525 lastList time.Time
2626}
2727
@@ -88,16 +88,16 @@ func getContainerIP(networkSettings interface{}) (ips []string) {
8888 return
8989 }
9090 switch networkSettings .(type ) {
91- case * apiTypes. SummaryNetworkSettings :
92- network := networkSettings .(* apiTypes. SummaryNetworkSettings )
91+ case * container. NetworkSettingsSummary :
92+ network := networkSettings .(* container. NetworkSettingsSummary )
9393 for net := range network .Networks {
9494 if network .Networks [net ] == nil {
9595 continue
9696 }
9797 ips = append (ips , network .Networks [net ].IPAddress )
9898 }
99- case * apiTypes .NetworkSettings :
100- network := networkSettings .(* apiTypes .NetworkSettings )
99+ case * container .NetworkSettings :
100+ network := networkSettings .(* container .NetworkSettings )
101101 for net := range network .Networks {
102102 if network .Networks [net ] == nil {
103103 continue
@@ -109,8 +109,8 @@ func getContainerIP(networkSettings interface{}) (ips []string) {
109109 return
110110}
111111
112- func (docker * DockerCli ) watchEvents () {
113- eventChan , errChan := docker .cli .Events (context .Background (), apiTypes. EventsOptions {})
112+ func (d * DockerCli ) watchEvents () {
113+ eventChan , errChan := d .cli .Events (context .Background (), events. ListOptions {})
114114
115115 go func () {
116116 for event := range eventChan {
@@ -121,7 +121,7 @@ func (docker *DockerCli) watchEvents() {
121121 switch event .Action {
122122 case "start" , "destroy" :
123123 ctx , cancel := context .WithTimeout (context .Background (), time .Second * 3 )
124- docker .listContainers (ctx , true )
124+ d .listContainers (ctx , true )
125125 cancel ()
126126 }
127127 }
@@ -130,69 +130,67 @@ func (docker *DockerCli) watchEvents() {
130130 logrus .Errorf ("docker cli watch events error: %s" , <- errChan )
131131}
132132
133- func (docker * DockerCli ) GetInfo (ctx context.Context , cid string ) types.Container {
134- if docker .containers .Len () == 0 {
133+ func (d * DockerCli ) GetInfo (ctx context.Context , cid string ) types.Container {
134+ if d .containers .Len () == 0 {
135135 logrus .Debugf ("zero containers, get cid %s" , cid )
136- docker .List (ctx )
136+ d .List (ctx )
137137 }
138138
139139 // find in containers
140- if container := docker .containers .Find (cid ); container .ID != "" {
140+ if container := d .containers .Find (cid ); container .ID != "" {
141141 if container .Shell == "" {
142- shell := docker .getShell (ctx , cid )
142+ shell := d .getShell (ctx , cid )
143143 container .Shell = shell
144- docker .containers .SetShell (cid , shell )
144+ d .containers .SetShell (cid , shell )
145145 }
146146 logrus .Debugf ("found valid container: %s (%s)" , container .ID , container .Shell )
147147 return container
148148 }
149149
150- // didn't get this container, this is rarelly happens
151- cjson , err := docker .cli .ContainerInspect (ctx , cid )
150+ // didn't get this container, this really happens
151+ resp , err := d .cli .ContainerInspect (ctx , cid )
152152 if err != nil {
153153 logrus .Errorf ("inspect container %s error: %s" , cid , err )
154154 return types.Container {}
155155 }
156156
157- c := docker . convertCjsonToContainre ( cjson )
157+ c := d . convert2Container ( resp )
158158 if c .ID != "" {
159- docker .containers .Append (c )
159+ d .containers .Append (c )
160160 }
161161 return c
162162}
163163
164- func (docker * DockerCli ) convertCjsonToContainre ( cjson apiTypes. ContainerJSON ) types.Container {
165- if cjson .Config == nil {
164+ func (d * DockerCli ) convert2Container ( inspect container. InspectResponse ) types.Container {
165+ if inspect .Config == nil {
166166 // WTF?
167167 return types.Container {}
168168 }
169169
170170 ctx , cancel := context .WithTimeout (context .Background (), time .Second * 3 )
171171 defer cancel ()
172- shell := docker .getShell (ctx , cjson .ID )
173-
174- c := types.Container {
175- ID : cjson .ID ,
176- Name : cjson .Name ,
177- Image : cjson .Image ,
178- Command : fmt .Sprintf ("%s" , cjson .Config .Cmd ),
179- IPs : getContainerIP (cjson .NetworkSettings ),
180- Status : cjson .State .Status ,
181- State : cjson .State .Status ,
172+ shell := d .getShell (ctx , inspect .ID )
173+
174+ return types.Container {
175+ ID : inspect .ID ,
176+ Name : inspect .Name ,
177+ Image : inspect .Image ,
178+ Command : fmt .Sprintf ("%s" , inspect .Config .Cmd ),
179+ IPs : getContainerIP (inspect .NetworkSettings ),
180+ Status : inspect .State .Status ,
181+ State : inspect .State .Status ,
182182 Shell : shell ,
183183 }
184-
185- return c
186184}
187185
188- func (docker * DockerCli ) listContainers (ctx context.Context , force bool ) []types.Container {
189- if time .Now ().Sub (docker .lastList ) < time .Minute && ! force {
190- return docker .containers .List ()
186+ func (d * DockerCli ) listContainers (ctx context.Context , force bool ) []types.Container {
187+ if time .Now ().Sub (d .lastList ) < time .Minute && ! force {
188+ return d .containers .List ()
191189 }
192190
193191 start := time .Now ()
194192 logrus .Debug ("list conatiners" )
195- cs , err := docker .cli .ContainerList (ctx , docker .listOptions )
193+ cs , err := d .cli .ContainerList (ctx , d .listOptions )
196194 if err != nil {
197195 logrus .Errorf ("list containers eror: %s" , err )
198196 return nil
@@ -204,7 +202,7 @@ func (docker *DockerCli) listContainers(ctx context.Context, force bool) []types
204202 shell string
205203 )
206204 for i , container := range cs {
207- if old := docker .containers .Find (container .ID ); old .ID != "" {
205+ if old := d .containers .Find (container .ID ); old .ID != "" {
208206 shell = old .Shell
209207 ips = old .IPs
210208 } else {
@@ -222,28 +220,28 @@ func (docker *DockerCli) listContainers(ctx context.Context, force bool) []types
222220 }
223221 }
224222
225- docker .containers .Set (containers )
223+ d .containers .Set (containers )
226224
227- docker .lastList = time .Now ()
225+ d .lastList = time .Now ()
228226 logrus .Debugf ("list %d containers, use %s" , len (containers ), time .Now ().Sub (start ))
229227 return containers
230228}
231229
232- func (docker * DockerCli ) List (ctx context.Context ) []types.Container {
233- return docker .listContainers (ctx , false )
230+ func (d * DockerCli ) List (ctx context.Context ) []types.Container {
231+ return d .listContainers (ctx , false )
234232}
235233
236- func (docker * DockerCli ) exist (ctx context.Context , cid , path string ) bool {
237- _ , err := docker .cli .ContainerStatPath (ctx , cid , path )
234+ func (d * DockerCli ) exist (ctx context.Context , cid , path string ) bool {
235+ _ , err := d .cli .ContainerStatPath (ctx , cid , path )
238236 if err != nil {
239237 return false
240238 }
241239 return true
242240}
243241
244- func (docker * DockerCli ) getShell (ctx context.Context , cid string ) string {
242+ func (d * DockerCli ) getShell (ctx context.Context , cid string ) string {
245243 for _ , sh := range config .SHELL_LIST {
246- if docker .exist (ctx , cid , sh ) {
244+ if d .exist (ctx , cid , sh ) {
247245 logrus .Debugf ("container [%s] use [%s]" , cid , sh )
248246 return sh
249247 }
@@ -252,23 +250,23 @@ func (docker *DockerCli) getShell(ctx context.Context, cid string) string {
252250 return ""
253251}
254252
255- func (docker * DockerCli ) Start (ctx context.Context , cid string ) error {
256- return docker .cli .ContainerStart (ctx , cid , apiTypes. ContainerStartOptions {})
253+ func (d * DockerCli ) Start (ctx context.Context , cid string ) error {
254+ return d .cli .ContainerStart (ctx , cid , container. StartOptions {})
257255}
258256
259- func (docker * DockerCli ) Stop (ctx context.Context , cid string ) error {
260- return docker .cli .ContainerStop (ctx , cid , container.StopOptions {})
257+ func (d * DockerCli ) Stop (ctx context.Context , cid string ) error {
258+ return d .cli .ContainerStop (ctx , cid , container.StopOptions {})
261259}
262260
263- func (docker * DockerCli ) Restart (ctx context.Context , cid string ) error {
261+ func (d * DockerCli ) Restart (ctx context.Context , cid string ) error {
264262 // restart immediately
265- return docker .cli .ContainerRestart (ctx , cid , container.StopOptions {})
263+ return d .cli .ContainerRestart (ctx , cid , container.StopOptions {})
266264}
267265
268- func buildListOptions (options string ) (apiTypes. ContainerListOptions , error ) {
266+ func buildListOptions (options string ) (container. ListOptions , error ) {
269267 // ["-a", "-f", "key=val"]
270268 // https://docs.docker.com/engine/reference/commandline/ps/#filtering
271- listOptions := apiTypes. ContainerListOptions {}
269+ listOptions := container. ListOptions {}
272270 args := strings .Split (options , " " )
273271 for i , arg := range args {
274272 switch arg {
@@ -302,16 +300,16 @@ func buildListOptions(options string) (apiTypes.ContainerListOptions, error) {
302300 return listOptions , nil
303301}
304302
305- func (docker * DockerCli ) Exec (ctx context.Context , container types.Container ) (types.TTY , error ) {
306- cmds := []string {container .Shell }
307- opts := container .Exec
303+ func (d * DockerCli ) Exec (ctx context.Context , c types.Container ) (types.TTY , error ) {
304+ cmds := []string {c .Shell }
305+ opts := c .Exec
308306 if cmd := opts .Cmd ; cmd != "" {
309307 cmds = append (cmds , "-c" )
310308 cmds = append (cmds , fmt .Sprintf ("\" \" %s\" \" " , cmd ))
311309 }
312310 logrus .Debugf ("exec cmd: %v" , cmds )
313311
314- execConfig := apiTypes. ExecConfig {
312+ execConfig := container. ExecOptions {
315313 AttachStdin : true ,
316314 AttachStderr : true ,
317315 AttachStdout : true ,
@@ -328,7 +326,7 @@ func (docker *DockerCli) Exec(ctx context.Context, container types.Container) (t
328326 strings .Split (opts .Env , " " )... )
329327 }
330328
331- response , err := docker .cli .ContainerExecCreate (ctx , container .ID , execConfig )
329+ response , err := d .cli .ContainerExecCreate (ctx , c .ID , execConfig )
332330 if err != nil {
333331 return nil , err
334332 }
@@ -338,17 +336,16 @@ func (docker *DockerCli) Exec(ctx context.Context, container types.Container) (t
338336 return nil , fmt .Errorf ("exec ID empty" )
339337 }
340338
341- execCheck := apiTypes. ExecStartCheck {
339+ resp , err := d . cli . ContainerExecAttach ( ctx , execID , container. ExecAttachOptions {
342340 Tty : true ,
343- }
344- resp , err := docker .cli .ContainerExecAttach (ctx , execID , execCheck )
341+ })
345342 if err != nil {
346343 return nil , err
347344 }
348345
349346 resizeFunc := func (width int , height int ) error {
350- return docker .cli .ContainerExecResize (ctx , execID ,
351- apiTypes .ResizeOptions {
347+ return d .cli .ContainerExecResize (ctx , execID ,
348+ container .ResizeOptions {
352349 Width : uint (width ),
353350 Height : uint (height ),
354351 })
@@ -357,12 +354,12 @@ func (docker *DockerCli) Exec(ctx context.Context, container types.Container) (t
357354 return newExecInjector (resp , resizeFunc ), nil
358355}
359356
360- func (docker * DockerCli ) Close () error {
361- return docker .cli .Close ()
357+ func (d * DockerCli ) Close () error {
358+ return d .cli .Close ()
362359}
363360
364- func (docker * DockerCli ) Logs (ctx context.Context , opts types.LogOptions ) (io.ReadCloser , error ) {
365- rc , err := docker .cli .ContainerLogs (ctx , opts .ID , apiTypes. ContainerLogsOptions {
361+ func (d * DockerCli ) Logs (ctx context.Context , opts types.LogOptions ) (io.ReadCloser , error ) {
362+ rc , err := d .cli .ContainerLogs (ctx , opts .ID , container. LogsOptions {
366363 ShowStderr : true ,
367364 ShowStdout : true ,
368365 Follow : opts .Follow ,
0 commit comments