// Launch runs a new docker container with the given process data. func Launch(p charm.Process) (ProcDetails, error) { args, err := launchArgs(p) if err != nil { return ProcDetails{}, err } d := deputy.Deputy{ Errors: deputy.FromStderr, } cmd := execCommand("docker", args...) out := &bytes.Buffer{} cmd.Stdout = out if err := d.Run(cmd); err != nil { return ProcDetails{}, err } id := string(bytes.TrimSpace(out.Bytes())) status, err := inspect(id) if err != nil { return ProcDetails{}, fmt.Errorf("can't get status for container %q: %s", id, err) } return ProcDetails{ ID: strings.TrimPrefix(status.Name, "/"), Status: ProcStatus{ State: status.brief(), }, }, nil }
// inspect calls docker inspect and returns the unmarshaled json response. func inspect(id string) (status, error) { cmd := execCommand("docker", "inspect", id) out := &bytes.Buffer{} cmd.Stdout = out d := deputy.Deputy{ Errors: deputy.FromStderr, } if err := d.Run(cmd); err != nil { return status{}, err } return statusFromInspect(id, out.Bytes()) }
func Example() { // Make a new deputy that'll return the data written to stderr as the error // message, log everything written to stdout to this application's log, and // timeout after 30 seconds. d := deputy.Deputy{ Errors: deputy.FromStderr, StdoutLog: func(b []byte) { log.Print(string(b)) }, Timeout: time.Second * 30, } if err := d.Run(exec.Command("foo")); err != nil { log.Print(err) } }
// Destroy stops and removes the docker container with the given id. func Destroy(id string) error { d := deputy.Deputy{ Errors: deputy.FromStderr, } cmd := execCommand("docker", "stop", id) if err := d.Run(cmd); err != nil { return fmt.Errorf("error while stopping container %q: %s", err) } cmd = execCommand("docker", "rm", id) if err := d.Run(cmd); err != nil { return fmt.Errorf("error while removing container %q: %s", err) } return nil }
func makeCatalogs(makeCatalogsPath, repoPath string, execTimeout time.Duration) { makecatalogsCmd := exec.Command( makeCatalogsPath, repoPath, ) d := deputy.Deputy{ Errors: deputy.FromStderr, StdoutLog: func(b []byte) { log.Println(string(b)) }, Timeout: time.Second * execTimeout, } if err := d.Run(makecatalogsCmd); err != nil { log.Println(err) return } }
func execute(cmd string) (string, error) { if len(cmd) == 0 { return "", ErrNoCommand } var value string d := deputy.Deputy{ Errors: deputy.FromStderr, StdoutLog: func(b []byte) { value = string(b) }, } if err := d.Run(exec.Command("sh", "-c", cmd)); err != nil { return "", err } return value, nil }
func runAutopkg(recipe, reportsPath, cmdPath string, check bool, execTimeout time.Duration) autopkgReport { autopkgCmd := exec.Command(cmdPath, "run", "--report-plist="+reportsPath+"/"+recipe) if check { autopkgCmd.Args = append(autopkgCmd.Args, "--check") } autopkgCmd.Args = append(autopkgCmd.Args, recipe) d := deputy.Deputy{ Errors: deputy.FromStderr, StdoutLog: func(b []byte) { log.Print(string(b)) }, Timeout: time.Second * execTimeout, } if err := d.Run(autopkgCmd); err != nil { log.Println(err) return autopkgReport{} } report, err := readReportPlist(reportsPath + "/" + recipe) if err != nil { log.Println(err) return autopkgReport{} } return report }