func tailConsole(cfg *config.Config) error { if !cfg.Console.Tail { return nil } client, err := docker.NewSystemClient() if err != nil { return err } console, ok := cfg.SystemContainers[config.CONSOLE_CONTAINER] if !ok { log.Error("Console not found") return nil } c := docker.NewContainerFromService(config.DOCKER_SYSTEM_HOST, config.CONSOLE_CONTAINER, console) if c.Err != nil { return c.Err } log.Infof("Tailing console : %s", c.Name) return client.Logs(dockerClient.LogsOptions{ Container: c.Name, Stdout: true, Stderr: true, Follow: true, OutputStream: os.Stdout, ErrorStream: os.Stderr, }) }
func osMetaDataGet(c *cli.Context) { images, err := getImages() if err != nil { log.Fatal(err) } client, err := docker.NewSystemClient() if err != nil { log.Fatal(err) } for _, image := range images.Available { _, err := client.InspectImage(image) if err == dockerClient.ErrNoSuchImage { fmt.Println(image, "remote") } else { fmt.Println(image, "local") } } }
func loadImages(cfg *config.CloudConfig) error { images, err := findImages(cfg) if err != nil || len(images) == 0 { return err } client, err := docker.NewSystemClient() if err != nil { return err } for _, image := range images { if hasImage(image) { continue } inputFileName := path.Join(config.IMAGES_PATH, image) input, err := os.Open(inputFileName) if err != nil { return err } defer input.Close() log.Infof("Loading images from %s", inputFileName) err = client.LoadImage(dockerClient.LoadImageOptions{ InputStream: input, }) log.Infof("Done loading images from %s", inputFileName) if err != nil { return err } } return nil }
func runDocker(name string) error { if os.ExpandEnv("${IN_DOCKER}") == "true" { return nil } client, err := docker.NewSystemClient() if err != nil { return err } cmd := []string{name} if name == "" { name = filepath.Base(os.Args[0]) cmd = os.Args } exiting, err := client.InspectContainer(name) if exiting != nil { err := client.RemoveContainer(dockerClient.RemoveContainerOptions{ ID: exiting.ID, Force: true, }) if err != nil { return err } } currentContainerId, err := getCurrentContainerId() if err != nil { return err } currentContainer, err := client.InspectContainer(currentContainerId) if err != nil { return err } powerContainer, err := client.CreateContainer(dockerClient.CreateContainerOptions{ Name: name, Config: &dockerClient.Config{ Image: currentContainer.Config.Image, Cmd: cmd, Env: []string{ "IN_DOCKER=true", }, }, HostConfig: &dockerClient.HostConfig{ PidMode: "host", VolumesFrom: []string{ currentContainer.ID, }, Privileged: true, }, }) if err != nil { return err } go func() { client.AttachToContainer(dockerClient.AttachToContainerOptions{ Container: powerContainer.ID, OutputStream: os.Stdout, ErrorStream: os.Stderr, Stderr: true, Stdout: true, }) }() err = client.StartContainer(powerContainer.ID, powerContainer.HostConfig) if err != nil { return err } _, err = client.WaitContainer(powerContainer.ID) if err != nil { log.Fatal(err) } os.Exit(0) return nil }
func shutDownContainers() error { var err error shutDown := true timeout := 2 for i, arg := range os.Args { if arg == "-f" || arg == "--f" || arg == "--force" { shutDown = false } if arg == "-t" || arg == "--t" || arg == "--timeout" { if len(os.Args) > i+1 { t, err := strconv.Atoi(os.Args[i+1]) if err != nil { return err } timeout = t } else { log.Error("please specify a timeout") } } } if !shutDown { return nil } client, err := docker.NewSystemClient() if err != nil { return err } opts := dockerClient.ListContainersOptions{ All: true, Filters: map[string][]string{ "status": []string{"running"}, }, } containers, err := client.ListContainers(opts) if err != nil { return err } currentContainerId, err := getCurrentContainerId() if err != nil { return err } var stopErrorStrings []string for _, container := range containers { if container.ID == currentContainerId { continue } log.Infof("Stopping %s : %v", container.ID[:12], container.Names) stopErr := client.StopContainer(container.ID, uint(timeout)) if stopErr != nil { stopErrorStrings = append(stopErrorStrings, " ["+container.ID+"] "+stopErr.Error()) } } var waitErrorStrings []string for _, container := range containers { if container.ID == currentContainerId { continue } _, waitErr := client.WaitContainer(container.ID) if waitErr != nil { waitErrorStrings = append(waitErrorStrings, " ["+container.ID+"] "+waitErr.Error()) } } if len(waitErrorStrings) != 0 || len(stopErrorStrings) != 0 { return errors.New("error while stopping \n1. STOP Errors [" + strings.Join(stopErrorStrings, ",") + "] \n2. WAIT Errors [" + strings.Join(waitErrorStrings, ",") + "]") } return nil }