-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
113 lines (89 loc) · 2 KB
/
main.go
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
111
112
113
package main
import (
"fmt"
"log"
"os"
"strings"
docker "github.com/fsouza/go-dockerclient"
)
const VERSION = "0.3.1"
func requireEnvVar(name string) {
if os.Getenv(name) == "" {
err := fmt.Errorf("Please set %s environment variable", name)
log.Fatalln(err)
}
}
func getConfig() *Config {
if os.Getenv("CONFIG") != "" {
config, err := NewConfigFromFile(os.Getenv("CONFIG"))
if err != nil {
log.Fatalln(err)
}
return config
}
requireEnvVar("DOCKER_HOST")
requireEnvVar("SHARED_PATH")
return NewConfig()
}
func pullImage(name string, client *docker.Client) error {
chunks := strings.Split(name, ":")
imgName := chunks[0]
imgTag := "latest"
if len(chunks) == 2 {
imgTag = chunks[1]
}
auth := docker.AuthConfiguration{}
opts := docker.PullImageOptions{
Repository: imgName,
Tag: imgTag,
OutputStream: os.Stdout,
}
return client.PullImage(opts, auth)
}
func checkImages(client *docker.Client, config *Config) error {
images, err := client.ListImages(docker.ListImagesOptions{})
if err != nil {
log.Fatalln(err)
}
imagesWithTags := map[string]bool{}
for _, image := range images {
for _, tag := range image.RepoTags {
imagesWithTags[tag] = true
}
}
fmt.Println("checking images...")
for _, lang := range Extensions {
if imagesWithTags[lang.Image] == true {
log.Printf("image %s exists", lang.Image)
} else {
if config.FetchImages {
log.Println("pulling", lang.Image, "image...")
err := pullImage(lang.Image, client)
if err != nil {
log.Fatalln(err)
}
} else {
return fmt.Errorf("image %s does not exist", lang.Image)
}
}
}
return nil
}
func main() {
log.Printf("bitrun api v%s\n", VERSION)
config := getConfig()
err := LoadLanguages(config.LanguagesPath)
if err != nil {
log.Fatalln(err)
}
client, err := docker.NewClient(config.DockerHost)
if err != nil {
log.Fatalln(err)
}
err = checkImages(client, config)
if err != nil {
log.Fatalln(err)
}
go RunPool(config, client)
RunApi(config, client)
}