|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/labstack/gommon/color" |
| 13 | +) |
| 14 | + |
| 15 | +func startDeepDetect(path string) string { |
| 16 | + // Starting the DeepDetect docker image |
| 17 | + name := randString(11, charset) |
| 18 | + path, _ = filepath.Abs(path) |
| 19 | + cmd := "docker" |
| 20 | + args := []string{"run", "-d", "-p", "8080:8080", "-v", path + ":" + path, "--name", name, "beniz/deepdetect_cpu"} |
| 21 | + if err := exec.Command(cmd, args...).Run(); err != nil { |
| 22 | + fmt.Fprintln(os.Stderr, err) |
| 23 | + stopDeepDetect(name) |
| 24 | + os.Exit(1) |
| 25 | + } |
| 26 | + color.Println(color.Yellow("[") + color.Cyan("CONTAINER: "+name) + color.Yellow("] ") + color.Green("Successfully started DeepDetect. ")) |
| 27 | + // Starting the image classification service |
| 28 | + time.Sleep(time.Second) |
| 29 | + url := "http://localhost:8080/services/imageserv" |
| 30 | + var jsonStr = []byte(`{"mllib":"caffe","description":"image classification service","type":"supervised","parameters":{"input":{"connector":"image"},"mllib":{"nclasses":1000}},"model":{"repository":"/opt/models/ggnet/"}}`) |
| 31 | + req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr)) |
| 32 | + client := &http.Client{} |
| 33 | + resp, err := client.Do(req) |
| 34 | + if err != nil { |
| 35 | + stopDeepDetect(name) |
| 36 | + panic(err) |
| 37 | + } |
| 38 | + defer resp.Body.Close() |
| 39 | + if resp.Status != "201 Created" { |
| 40 | + stopDeepDetect(name) |
| 41 | + os.Exit(1) |
| 42 | + } |
| 43 | + color.Println(color.Yellow("[") + color.Cyan("CONTAINER: "+name) + color.Yellow("] ") + color.Green("Successfully started the image classification service. ")) |
| 44 | + return name |
| 45 | +} |
| 46 | + |
| 47 | +func stopDeepDetect(name string) { |
| 48 | + // Stopping the DeepDetect docker image |
| 49 | + cmd := "docker" |
| 50 | + args := []string{"stop", name} |
| 51 | + if err := exec.Command(cmd, args...).Run(); err != nil { |
| 52 | + fmt.Fprintln(os.Stderr, err) |
| 53 | + os.Exit(1) |
| 54 | + } |
| 55 | +} |
0 commit comments