func main() { // On panic, recover the error and display it defer func() { if err := recover(); err != nil { print.Error("ERROR: %s\n", err) } }() handleCmd() }
// Remove container func (container Container) rm() { if container.exists() { if container.running() { print.Error("Container %s is running and cannot be removed.\n", container.Name) } else { fmt.Printf("Removing container %s ... ", container.Name) args := []string{"rm", container.Name} executeCommand("docker", args) } } }
// Start container func (container Container) start() { if !isManualTargetting() && container.Manual { return } if container.exists() { if !container.running() { fmt.Printf("Starting container %s ... ", container.Name) args := []string{"start", container.Name} executeCommand("docker", args) } } else { print.Error("Container %s does not exist.\n", container.Name) } }
// Run container func (container Container) run() { if !isManualTargetting() && container.Manual { return } if container.exists() { print.Notice("Container %s does already exist. Use --force to recreate.\n", container.Name) if !container.running() { container.start() } } else { fmt.Printf("Running container %s ... ", container.Name) // Assemble command arguments args := []string{"run"} // Cidfile if len(container.Run.Cidfile) > 0 { args = append(args, "--cidfile", os.ExpandEnv(container.Run.Cidfile)) } // CPU shares if container.Run.CpuShares > 0 { args = append(args, "--cpu-shares", os.ExpandEnv(strconv.Itoa(container.Run.CpuShares))) } // Detach if container.Run.Detach { args = append(args, "--detach") } // Dns for _, dns := range container.Run.Dns { args = append(args, "--dns", os.ExpandEnv(dns)) } // Entrypoint if len(container.Run.Entrypoint) > 0 { args = append(args, "--entrypoint", os.ExpandEnv(container.Run.Entrypoint)) } // Env for _, env := range container.Run.Env { args = append(args, "--env", os.ExpandEnv(env)) } // Expose for _, expose := range container.Run.Expose { args = append(args, "--expose", os.ExpandEnv(expose)) } // Host if len(container.Run.Host) > 0 { args = append(args, "--hostname", os.ExpandEnv(container.Run.Host)) } // Interactive if container.Run.Interactive { args = append(args, "--interactive") } // Link for _, link := range container.Run.Link { args = append(args, "--link", link) } // LxcConf for _, lxcConf := range container.Run.LxcConf { args = append(args, "--lxc-conf", os.ExpandEnv(lxcConf)) } // MappedVolumesFrom for _, mappedVolumesFrom := range container.Run.MappedVolumesFrom { mappedVolumesFrom = os.ExpandEnv(mappedVolumesFrom) x := strings.Split(mappedVolumesFrom, ":") from, volume, dest := x[0], x[1], x[2] src_volume_dir := getSourceForVolume(from, volume) vol_map := strings.Join([]string{src_volume_dir, dest}, ":") args = append(args, "--volume", vol_map) } // Memory if len(container.Run.Memory) > 0 { args = append(args, "--memory", os.ExpandEnv(container.Run.Memory)) } // Privileged if container.Run.Privileged { args = append(args, "--privileged") } // Publish for _, port := range container.Run.Publish { args = append(args, "--publish", os.ExpandEnv(port)) } // PublishAll if container.Run.PublishAll { args = append(args, "--publish-all") } // Rm if container.Run.Rm { args = append(args, "--rm") } // Tty if container.Run.Tty { args = append(args, "--tty") } // User if len(container.Run.User) > 0 { args = append(args, "--user", os.ExpandEnv(container.Run.User)) } // Volumes for _, volume := range container.Run.Volume { paths := strings.Split(volume, ":") if !path.IsAbs(paths[0]) { cwd, _ := os.Getwd() paths[0] = cwd + "/" + paths[0] } args = append(args, "--volume", os.ExpandEnv(strings.Join(paths, ":"))) } // VolumesFrom for _, volumeFrom := range container.Run.VolumesFrom { args = append(args, "--volumes-from", os.ExpandEnv(volumeFrom)) } // Workdir if len(container.Run.Workdir) > 0 { args = append(args, "--workdir", os.ExpandEnv(container.Run.Workdir)) } // Name args = append(args, "--name", container.Name) // Image args = append(args, container.Image) // Command if container.Run.Command != nil { switch cmd := container.Run.Command.(type) { case string: if len(cmd) > 0 { args = append(args, cmd) } case []interface{}: cmds := make([]string, len(cmd)) for i, v := range cmd { cmds[i] = v.(string) } args = append(args, cmds...) default: print.Error("cmd is of unknown type!") } } // Execute command executeCommand("docker", args) } }