github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/engine/docker/docker.go (about) 1 package docker 2 3 import ( 4 "context" 5 "net/http" 6 "strings" 7 8 dockerapi "github.com/docker/docker/client" 9 10 "github.com/projecteru2/core/engine" 11 enginetypes "github.com/projecteru2/core/engine/types" 12 "github.com/projecteru2/core/log" 13 coretypes "github.com/projecteru2/core/types" 14 "github.com/projecteru2/core/utils" 15 ) 16 17 const ( 18 // TCPPrefixKey indicate tcp prefix 19 TCPPrefixKey = "tcp://" 20 // SockPrefixKey indicate sock prefix 21 SockPrefixKey = "unix://" 22 // Type indicate docker 23 Type = "docker" 24 ) 25 26 // Engine is engine for docker 27 type Engine struct { 28 client dockerapi.APIClient 29 config coretypes.Config 30 } 31 32 // MakeClient make docker cli 33 func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint, ca, cert, key string) (engine.API, error) { 34 var client *http.Client 35 var err error 36 logger := log.WithFunc("engine.docker.MakeClient") 37 if strings.HasPrefix(endpoint, "unix://") { 38 client = utils.GetUnixSockClient() 39 } else { 40 client, err = utils.GetHTTPSClient(ctx, config.CertPath, nodename, ca, cert, key) 41 if err != nil { 42 logger.Errorf(ctx, err, "GetHTTPSClient for %s %s", nodename, endpoint) 43 return nil, err 44 } 45 } 46 47 logger.Debugf(ctx, "Create new http.Client for %s, %s", endpoint, config.Docker.APIVersion) 48 return makeDockerClient(ctx, config, client, endpoint) 49 } 50 51 // Info show node info 52 // 2 seconds timeout 53 // used to be 5, but client won't wait that long 54 func (e *Engine) Info(ctx context.Context) (*enginetypes.Info, error) { 55 r, err := e.client.Info(ctx) 56 if err != nil { 57 return nil, err 58 } 59 return &enginetypes.Info{Type: Type, ID: r.ID, NCPU: r.NCPU, MemTotal: r.MemTotal}, nil 60 } 61 62 // Ping test connection 63 func (e *Engine) Ping(ctx context.Context) error { 64 _, err := e.client.Ping(ctx) 65 return err 66 } 67 68 // CloseConn close connection 69 func (e *Engine) CloseConn() error { 70 return e.client.Close() 71 }