k3d/pkg/runtimes/docker/container.go
2019-10-10 13:36:11 +02:00

123 lines
3.5 KiB
Go

/*
Copyright © 2019 Thorsten Klein <iwilltry42@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package docker
import (
"context"
"io"
"io/ioutil"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
)
// createContainer creates a new docker container from translated specs
func createContainer(dockerNode *NodeInDocker, name string) error {
log.Debugf("Creating docker container with translated config\n%+v\n", dockerNode) // TODO: remove?
// initialize docker client
ctx := context.Background()
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Errorln("Failed to create docker client")
return err
}
// check that we have the image locally
// start container
create: // label used to restart process, if we're only missing the image
resp, err := docker.ContainerCreate(ctx, &dockerNode.ContainerConfig, &dockerNode.HostConfig, &dockerNode.NetworkingConfig, name)
if err != nil {
if client.IsErrNotFound(err) {
if err := pullImage(&ctx, docker, dockerNode.ContainerConfig.Image); err != nil {
log.Errorln("Failed to create container")
return err
}
goto create
}
log.Errorln("Failed to create container")
return err
}
log.Infoln("Created container", resp.ID)
return nil
}
// removeContainer deletes a running container (like docker rm -f)
func removeContainer(ID string) error {
// (0) create docker client
ctx := context.Background()
docker, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Errorln("Failed to create docker client")
return err
}
// (1) define remove options
options := types.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
}
// (2) remove container
if err := docker.ContainerRemove(ctx, ID, options); err != nil {
log.Errorf("Failed to delete container '%s'", ID)
return err
}
log.Infoln("Deleted", ID)
return nil
}
func pullImage(ctx *context.Context, docker *client.Client, image string) error {
resp, err := docker.ImagePull(*ctx, image, types.ImagePullOptions{})
if err != nil {
log.Errorf("Failed to pull image '%s'", image)
return err
}
defer resp.Close()
log.Infof("Pulling image '%s'", image)
// in debug mode (--verbose flag set), output pull progress
var writer io.Writer = ioutil.Discard
if log.GetLevel() == log.DebugLevel {
writer = os.Stdout
}
_, err = io.Copy(writer, resp)
if err != nil {
log.Warningf("Couldn't get docker output")
log.Warningln(err)
}
return nil
}