ETRI_SMARTFARM_POC/containermgmt/manager.go

54 lines
871 B
Go
Raw Normal View History

2021-11-16 12:28:52 +00:00
package containermgmt
2021-11-08 01:47:27 +00:00
import (
"fmt"
"log"
"os/exec"
"strings"
)
2021-11-16 12:28:52 +00:00
// type Container struct {
// ID string `json:"image"`
// Name string `json:"name"`
// Addr string `json:addr`
// }
2021-11-08 01:47:27 +00:00
2021-11-16 12:28:52 +00:00
func IsExist(name string) bool {
2021-11-08 01:47:27 +00:00
cmd := strings.Split("container\\ls\\--format\\'{{.Image}} {{.Names}}'\\-a", "\\")
bout, err := exec.Command("docker", cmd...).Output()
if err != nil {
log.Fatalln(err)
}
sout := strings.Split(string(bout), "\n")
for _, e := range sout {
l := strings.Split(e, " ")
if len(l) < 2 {
continue
}
2021-11-16 12:28:52 +00:00
if name == l[0] {
return true
2021-11-08 01:47:27 +00:00
}
}
2021-11-16 12:28:52 +00:00
return false
}
2021-11-08 01:47:27 +00:00
2021-11-16 12:28:52 +00:00
func CreateContainer(name string) error {
2021-11-08 01:47:27 +00:00
2021-11-16 12:28:52 +00:00
if IsExist(name) {
return nil
}
args := strings.Split(fmt.Sprintf("container\\run\\-d\\%s", name), "\\")
fmt.Println(args)
_, err := exec.Command("docker", args...).Output()
if err != nil {
return err
}
2021-11-08 01:47:27 +00:00
2021-11-16 12:28:52 +00:00
return nil
2021-11-08 01:47:27 +00:00
}