github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/hack/integration-cli-on-swarm/host/dockercmd.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "strings" 8 "time" 9 10 "github.com/docker/docker/client" 11 ) 12 13 func system(commands [][]string) error { 14 for _, c := range commands { 15 cmd := exec.Command(c[0], c[1:]...) 16 cmd.Stdout = os.Stdout 17 cmd.Stderr = os.Stderr 18 cmd.Env = os.Environ() 19 if err := cmd.Run(); err != nil { 20 return err 21 } 22 } 23 return nil 24 } 25 26 func pushImage(unusedCli *client.Client, remote, local string) error { 27 // FIXME: eliminate os/exec (but it is hard to pass auth without os/exec ...) 28 return system([][]string{ 29 {"docker", "image", "tag", local, remote}, 30 {"docker", "image", "push", remote}, 31 }) 32 } 33 34 func deployStack(unusedCli *client.Client, stackName, composeFilePath string) error { 35 // FIXME: eliminate os/exec (but stack is implemented in CLI ...) 36 return system([][]string{ 37 {"docker", "stack", "deploy", 38 "--compose-file", composeFilePath, 39 "--with-registry-auth", 40 stackName}, 41 }) 42 } 43 44 func hasStack(unusedCli *client.Client, stackName string) bool { 45 // FIXME: eliminate os/exec (but stack is implemented in CLI ...) 46 out, err := exec.Command("docker", "stack", "ls").CombinedOutput() 47 if err != nil { 48 panic(fmt.Errorf("`docker stack ls` failed with: %s", string(out))) 49 } 50 // FIXME: not accurate 51 return strings.Contains(string(out), stackName) 52 } 53 54 func removeStack(unusedCli *client.Client, stackName string) error { 55 // FIXME: eliminate os/exec (but stack is implemented in CLI ...) 56 if err := system([][]string{ 57 {"docker", "stack", "rm", stackName}, 58 }); err != nil { 59 return err 60 } 61 // FIXME 62 time.Sleep(10 * time.Second) 63 return nil 64 }