func main() { fmt.Println("dockerapi started...") time.Sleep(time.Millisecond * 5000) fmt.Println("action: " + ACTION) fmt.Println("containers: " + CONTAINERS) var containers = strings.Split(CONTAINERS, ",") var docker *dockerapi.Client var err error docker, err = dockerapi.NewClient(DOCKER_HOST) if err != nil { fmt.Println(err.Error()) } err = docker.Ping() for i := range containers { fmt.Println(ACTION + " issued for " + containers[i]) switch ACTION { case "stop": err = docker.StopContainer(containers[i], 5) case "start": err = docker.StartContainer(containers[i], nil) default: fmt.Println(ACTION + " unsupported action") } if err != nil { fmt.Println(err.Error()) } time.Sleep(time.Millisecond * 2000) } }
// cleanUp stops and removes the deployed router func cleanUp(dockerCli *dockerClient.Client, routerId string) { dockerCli.StopContainer(routerId, 5) dockerCli.RemoveContainer(dockerClient.RemoveContainerOptions{ ID: routerId, Force: true, }) }
// cleanUp stops and removes the deployed router func cleanUp(t *testing.T, dockerCli *dockerClient.Client, routerId string) { dockerCli.StopContainer(routerId, 5) if t.Failed() { dockerCli.Logs(dockerClient.LogsOptions{ Container: routerId, OutputStream: os.Stdout, ErrorStream: os.Stderr, Stdout: true, Stderr: true, }) } dockerCli.RemoveContainer(dockerClient.RemoveContainerOptions{ ID: routerId, Force: true, }) }
func (c *Container) renameOld(client *dockerClient.Client, opts *dockerClient.CreateContainerOptions) error { if len(opts.Name) == 0 { return nil } existing, err := inspect(client, opts.Name) if _, ok := err.(*dockerClient.NoSuchContainer); ok { return nil } if err != nil { return nil } if c.Container != nil && existing.ID == c.Container.ID { return nil } var newName string if label, ok := existing.Config.Labels[config.HASH]; ok { newName = fmt.Sprintf("%s-%s", existing.Name, label) } else { newName = fmt.Sprintf("%s-unknown-%s", existing.Name, util.RandSeq(12)) } if existing.State.Running { err := client.StopContainer(existing.ID, 2) if err != nil { return err } _, err = client.WaitContainer(existing.ID) if err != nil { return err } } log.Debugf("Renaming %s to %s", existing.Name, newName) return client.RenameContainer(dockerClient.RenameContainerOptions{ID: existing.ID, Name: newName}) }
// StopContainers stops all the containers in ids slice func StopContainers(client *docker.Client, ids []string) { for _, id := range ids { client.StopContainer(id, 10) } }