ping docker daemon on startup [ci skip]
This commit is contained in:
@@ -51,6 +51,16 @@ func (c *daemonCommand) run(*kingpin.ParseContext) error {
|
|||||||
// setup the global logrus logger.
|
// setup the global logrus logger.
|
||||||
setupLogger(config)
|
setupLogger(config)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(nocontext)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// listen for termination signals to gracefully shutdown
|
||||||
|
// the runner daemon.
|
||||||
|
ctx = signal.WithContextFunc(ctx, func() {
|
||||||
|
println("received signal, terminating process")
|
||||||
|
cancel()
|
||||||
|
})
|
||||||
|
|
||||||
cli := client.New(
|
cli := client.New(
|
||||||
config.Client.Address,
|
config.Client.Address,
|
||||||
config.Client.Secret,
|
config.Client.Secret,
|
||||||
@@ -69,7 +79,22 @@ func (c *daemonCommand) run(*kingpin.ParseContext) error {
|
|||||||
|
|
||||||
engine, err := engine.NewEnv()
|
engine, err := engine.NewEnv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
logrus.WithError(err).
|
||||||
|
Fatalln("cannot load the docker engine")
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
err := engine.Ping(ctx)
|
||||||
|
if err == context.Canceled {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
logrus.WithError(err).
|
||||||
|
Errorln("cannot ping the docker daemon")
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
} else {
|
||||||
|
logrus.Debugln("successfully pinged the docker daemon")
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
remote := remote.New(cli)
|
remote := remote.New(cli)
|
||||||
@@ -117,16 +142,6 @@ func (c *daemonCommand) run(*kingpin.ParseContext) error {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(nocontext)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
// listen for termination signals to gracefully shutdown
|
|
||||||
// the runner daemon.
|
|
||||||
ctx = signal.WithContextFunc(ctx, func() {
|
|
||||||
println("received signal, terminating process")
|
|
||||||
cancel()
|
|
||||||
})
|
|
||||||
|
|
||||||
var g errgroup.Group
|
var g errgroup.Group
|
||||||
server := server.Server{
|
server := server.Server{
|
||||||
Addr: config.Server.Port,
|
Addr: config.Server.Port,
|
||||||
|
|||||||
@@ -17,17 +17,18 @@ import (
|
|||||||
"docker.io/go-docker/api/types/volume"
|
"docker.io/go-docker/api/types/volume"
|
||||||
)
|
)
|
||||||
|
|
||||||
type engine struct {
|
// Docker implements a Docker pipeline engine.
|
||||||
|
type Docker struct {
|
||||||
client docker.APIClient
|
client docker.APIClient
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new engine.
|
// New returns a new engine.
|
||||||
func New(client docker.APIClient) Engine {
|
func New(client docker.APIClient) *Docker {
|
||||||
return &engine{client}
|
return &Docker{client}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEnv returns a new Engine from the environment.
|
// NewEnv returns a new Engine from the environment.
|
||||||
func NewEnv() (Engine, error) {
|
func NewEnv() (*Docker, error) {
|
||||||
cli, err := docker.NewEnvClient()
|
cli, err := docker.NewEnvClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -35,8 +36,14 @@ func NewEnv() (Engine, error) {
|
|||||||
return New(cli), nil
|
return New(cli), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ping pings the Docker daemon.
|
||||||
|
func (e *Docker) Ping(ctx context.Context) error {
|
||||||
|
_, err := e.client.Ping(ctx)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Setup the pipeline environment.
|
// Setup the pipeline environment.
|
||||||
func (e *engine) Setup(ctx context.Context, spec *Spec) error {
|
func (e *Docker) Setup(ctx context.Context, spec *Spec) error {
|
||||||
// creates the default temporary (local) volumes
|
// creates the default temporary (local) volumes
|
||||||
// that are mounted into each container step.
|
// that are mounted into each container step.
|
||||||
for _, vol := range spec.Volumes {
|
for _, vol := range spec.Volumes {
|
||||||
@@ -68,7 +75,7 @@ func (e *engine) Setup(ctx context.Context, spec *Spec) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Destroy the pipeline environment.
|
// Destroy the pipeline environment.
|
||||||
func (e *engine) Destroy(ctx context.Context, spec *Spec) error {
|
func (e *Docker) Destroy(ctx context.Context, spec *Spec) error {
|
||||||
removeOpts := types.ContainerRemoveOptions{
|
removeOpts := types.ContainerRemoveOptions{
|
||||||
Force: true,
|
Force: true,
|
||||||
RemoveLinks: false,
|
RemoveLinks: false,
|
||||||
@@ -109,7 +116,7 @@ func (e *engine) Destroy(ctx context.Context, spec *Spec) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run runs the pipeline step.
|
// Run runs the pipeline step.
|
||||||
func (e *engine) Run(ctx context.Context, spec *Spec, step *Step, output io.Writer) (*State, error) {
|
func (e *Docker) Run(ctx context.Context, spec *Spec, step *Step, output io.Writer) (*State, error) {
|
||||||
// create the container
|
// create the container
|
||||||
err := e.create(ctx, spec, step, output)
|
err := e.create(ctx, spec, step, output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -133,7 +140,7 @@ func (e *engine) Run(ctx context.Context, spec *Spec, step *Step, output io.Writ
|
|||||||
// emulate docker commands
|
// emulate docker commands
|
||||||
//
|
//
|
||||||
|
|
||||||
func (e *engine) create(ctx context.Context, spec *Spec, step *Step, output io.Writer) error {
|
func (e *Docker) create(ctx context.Context, spec *Spec, step *Step, output io.Writer) error {
|
||||||
// parse the docker image name. We need to extract the
|
// parse the docker image name. We need to extract the
|
||||||
// image domain name and match to registry credentials
|
// image domain name and match to registry credentials
|
||||||
// stored in the .docker/config.json object.
|
// stored in the .docker/config.json object.
|
||||||
@@ -212,13 +219,13 @@ func (e *engine) create(ctx context.Context, spec *Spec, step *Step, output io.W
|
|||||||
}
|
}
|
||||||
|
|
||||||
// helper function emulates the `docker start` command.
|
// helper function emulates the `docker start` command.
|
||||||
func (e *engine) start(ctx context.Context, id string) error {
|
func (e *Docker) start(ctx context.Context, id string) error {
|
||||||
return e.client.ContainerStart(ctx, id, types.ContainerStartOptions{})
|
return e.client.ContainerStart(ctx, id, types.ContainerStartOptions{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper function emulates the `docker wait` command, blocking
|
// helper function emulates the `docker wait` command, blocking
|
||||||
// until the container stops and returning the exit code.
|
// until the container stops and returning the exit code.
|
||||||
func (e *engine) wait(ctx context.Context, id string) (*State, error) {
|
func (e *Docker) wait(ctx context.Context, id string) (*State, error) {
|
||||||
wait, errc := e.client.ContainerWait(ctx, id, "")
|
wait, errc := e.client.ContainerWait(ctx, id, "")
|
||||||
select {
|
select {
|
||||||
case <-wait:
|
case <-wait:
|
||||||
@@ -243,7 +250,7 @@ func (e *engine) wait(ctx context.Context, id string) (*State, error) {
|
|||||||
|
|
||||||
// helper function emulates the `docker logs -f` command, streaming
|
// helper function emulates the `docker logs -f` command, streaming
|
||||||
// all container logs until the container stops.
|
// all container logs until the container stops.
|
||||||
func (e *engine) tail(ctx context.Context, id string, output io.Writer) error {
|
func (e *Docker) tail(ctx context.Context, id string, output io.Writer) error {
|
||||||
opts := types.ContainerLogsOptions{
|
opts := types.ContainerLogsOptions{
|
||||||
Follow: true,
|
Follow: true,
|
||||||
ShowStdout: true,
|
ShowStdout: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user