-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
110 lines (91 loc) · 3.58 KB
/
main.go
File metadata and controls
110 lines (91 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"log"
"github.com/alecthomas/kingpin"
yaml "gopkg.in/yaml.v2"
)
//The SIGTERM signal is a generic signal used to cause program termination. Unlike SIGKILL, this signal can be blocked, handled, and ignored. It is the normal way to politely ask a program to terminate.
const sigterm = "143"
func main() {
app := kingpin.New("serc", "A docker images running time.")
app.HelpFlag.Short('h')
dchub := app.Command("docker", "Run docker image from dockerhub.")
dockerImagePath := dchub.Arg("imgpath", "Path to the directory of projects").Required().String()
dockerImageConfig := dchub.Arg("config", "Path to your docker image config file.").Required().String()
dockerServerAddr := dchub.Arg("addr", "Server address(domain, ip) on which to run the docker image.").Default("localhost").String()
dockerPort := dchub.Arg("port", "Docker port.").Default("8080").String()
dockerExposePort := dchub.Arg("expose-port", "Port on which the server will be exposed.").Default("8080").String()
cpusAmm := dchub.Arg("cpus", "The ammount of cpu to use.").Default("2").Strings()
if kingpin.MustParse(app.Parse(os.Args[1:])) == dchub.FullCommand() {
images, err := loadImagesFromConfig(*dockerImageConfig)
if err != nil {
log.Fatal("Couldn't load the images from config file: ", err)
}
if err := buildImages(*dockerImagePath, images); err != nil {
log.Printf("Couldn't build the images from the directory: %v", err)
}
for _, image := range images {
fmt.Println(image)
for _, cpu := range *cpusAmm {
go checkServer(*dockerServerAddr, *dockerPort, image, cpu)
if err := exec.Command("sudo", "docker", "run", "-p", fmt.Sprintf("%v:%v", *dockerExposePort, *dockerPort), fmt.Sprintf("--cpus=%v", cpu), image).Run(); err != nil && err.Error() != ("exit status "+sigterm) {
log.Fatal(err)
}
}
}
}
}
func checkServer(addr string, port string, dockerImage string, cpu string) {
begin := time.Now()
for {
resp, err := http.Get(fmt.Sprintf("http://%v:%v/", addr, port))
if err == nil && resp.StatusCode == http.StatusOK {
end := time.Now()
elapsed := end.Sub(begin)
fmt.Printf("(%v cpu): %vs \n", cpu, elapsed.Seconds())
out, err := exec.Command("sudo", "docker", "ps", "-q", fmt.Sprintf("--filter=ancestor=%v", dockerImage)).Output()
if err != nil {
fmt.Printf("Couldn't get the running docker container ID: %v", err)
}
if err := exec.Command("docker", "stop", strings.TrimRight(string(out), "\n")).Run(); err != nil {
fmt.Printf("Couldn't stop the running docker container: %v", err)
}
break
}
}
}
func loadImagesFromConfig(filename string) (map[string]string, error) {
imagesFile, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("can't open the images file- %v, filename- %v", err, filename)
}
images := make(map[string]string)
err = yaml.Unmarshal(imagesFile, &images)
if err != nil {
return nil, fmt.Errorf("can't read the images file- %v, filename- %v", err, filename)
}
return images, nil
}
func buildImages(path string, images map[string]string) error {
filesInDir, err := ioutil.ReadDir(path)
if err != nil {
return fmt.Errorf("can't open the project files directory: %v", err)
}
for amm, file := range filesInDir {
if file.IsDir() {
if err := exec.Command("sudo", "docker", "build", filepath.Join(path, file.Name()), "--tag", file.Name()).Run(); err != nil {
return fmt.Errorf("can't build the docker images: %v", err)
}
images[fmt.Sprintf("imagefromdir%v", amm)] = file.Name()
}
}
return nil
}