From acac84a6f142ad68aa165040d3996e6265c2b83f Mon Sep 17 00:00:00 2001 From: Greg Linton Date: Thu, 9 Mar 2017 14:51:04 -0700 Subject: [PATCH 1/2] Updates --- .gitignore | 2 ++ container.go | 52 ++++++++++++++++++++++++++++++++++++---------------- exec.go | 2 ++ volume.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 16 deletions(-) create mode 100644 .gitignore create mode 100644 volume.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..930f9a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor/*/ + diff --git a/container.go b/container.go index 0a0df99..064cf92 100644 --- a/container.go +++ b/container.go @@ -9,24 +9,29 @@ import ( dockContainer "github.com/docker/engine-api/types/container" dockNetwork "github.com/docker/engine-api/types/network" "github.com/docker/engine-api/types/strslice" + "github.com/docker/go-connections/nat" ) type ContainerConfig struct { - ID string `json:"id"` - Network string `json:"network"` - NetName string `json:"networkname"` - Name string `json:"name"` - Labels map[string]string `json:"labels"` - Hostname string `json:"hostname"` - Domainname string `json:"domainname"` - Cmd []string `json:"cmd"` - Image string `json:"image_slug"` - IP string `json:"ip"` - Binds []string `json:"binds"` - Memory int64 `json:"memory"` - MemorySwap int64 `json:"memory_swap"` - Status string `json:"status"` - CPUShares int64 `json:"cpu_shares"` + ID string `json:"id"` + Network string `json:"network"` + NetName string `json:"networkname"` + Name string `json:"name"` + Labels map[string]string `json:"labels"` + Hostname string `json:"hostname"` + Domainname string `json:"domainname"` + Cmd []string `json:"cmd"` + Env []string `json:"env"` + Image string `json:"image_slug"` + IP string `json:"ip"` + Binds []string `json:"binds"` + Memory int64 `json:"memory"` + MemorySwap int64 `json:"memory_swap"` + Status string `json:"status"` + CPUShares int64 `json:"cpu_shares"` + RestartPolicy string `json:"restart_policy"` + RestartAttempts int `json:"restart_attempts"` + Ports []string `json:"ports"` } // create a container from the user specification @@ -35,13 +40,27 @@ func CreateContainer(conf ContainerConfig) (dockType.ContainerJSON, error) { // conf.Cmd = []string{"/bin/sleep", "3650d"} // } + ports, portBindings, _ := nat.ParsePortSpecs(conf.Ports) + + // create a configurable restart policy with a default 'unless stopped' behavior + restartPolicy := dockContainer.RestartPolicy{Name: "unless-stopped"} + switch conf.RestartPolicy { + case "no", "", "always", "on-failure": + restartPolicy = dockContainer.RestartPolicy{ + Name: conf.RestartPolicy, + MaximumRetryCount: conf.RestartAttempts, + } + } + config := &dockContainer.Config{ Hostname: conf.Hostname, Domainname: conf.Domainname, Cmd: conf.Cmd, + Env: conf.Env, Labels: conf.Labels, NetworkDisabled: false, Image: conf.Image, + ExposedPorts: ports, } hostConfig := &dockContainer.HostConfig{ @@ -50,12 +69,13 @@ func CreateContainer(conf ContainerConfig) (dockType.ContainerJSON, error) { // NetworkMode: "host", CapAdd: strslice.StrSlice([]string{"NET_ADMIN"}), NetworkMode: "bridge", - RestartPolicy: dockContainer.RestartPolicy{Name: "unless-stopped"}, + RestartPolicy: restartPolicy, Resources: dockContainer.Resources{ Memory: conf.Memory, MemorySwap: conf.MemorySwap, CPUShares: conf.CPUShares, }, + PortBindings: portBindings, } netConfig := &dockNetwork.NetworkingConfig{} diff --git a/exec.go b/exec.go index f9108a4..c3dcdd8 100644 --- a/exec.go +++ b/exec.go @@ -13,6 +13,7 @@ type ExecConfig struct { ID string User string Cmd []string + Env []string Stdin, Stdout, Stderr, Tty bool } @@ -24,6 +25,7 @@ func ExecStart(execConfig ExecConfig) (dockType.ContainerExecCreateResponse, doc AttachStdin: execConfig.Stdin, AttachStdout: execConfig.Stdout, AttachStderr: execConfig.Stderr, + Env: execConfig.Env, } exec, err := client.ContainerExecCreate(context.Background(), execConfig.ID, config) diff --git a/volume.go b/volume.go new file mode 100644 index 0000000..9ab8f84 --- /dev/null +++ b/volume.go @@ -0,0 +1,43 @@ +package docker + +import ( + dockType "github.com/docker/engine-api/types" + "github.com/docker/engine-api/types/filters" + "golang.org/x/net/context" +) + +// create a new volume +func VolumeCreate(name string) (dockType.Volume, error) { + vol := dockType.VolumeCreateRequest{ + Name: name, + } + + return client.VolumeCreate(context.Background(), vol) +} + +// list the volumes we have +func VolumeList() ([]*dockType.Volume, error) { + volumeList, err := client.VolumeList(context.Background(), filters.Args{}) + return volumeList.Volumes, err +} + +// check to see if a volume exists +func VolumeExists(name string) bool { + volumes, err := VolumeList() + if err != nil { + return false + } + + for _, volume := range volumes { + if volume.Name == name { + return true + } + + } + return false +} + +// remove an existing volume +func VolumeRemove(name string) error { + return client.VolumeRemove(context.Background(), name, true) +} From d1683dd8c7dd48bdd358d2583c26295919f7c2ca Mon Sep 17 00:00:00 2001 From: Greg Linton Date: Fri, 1 Sep 2017 12:42:07 -0600 Subject: [PATCH 2/2] Update imageremove to match version used in nanobox --- image.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/image.go b/image.go index a1d69b3..c71df5d 100644 --- a/image.go +++ b/image.go @@ -100,7 +100,7 @@ func ImageInspect(imageID string) (Image, error) { return img, err } -func ImageRemove(imageID string) error { - _, err := client.ImageRemove(context.Background(), imageID, dockType.ImageRemoveOptions{Force: true, PruneChildren: true}) +func ImageRemove(imageID string, force bool) error { + _, err := client.ImageRemove(context.Background(), imageID, dockType.ImageRemoveOptions{Force: force, PruneChildren: true}) return err }