-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (123 loc) · 3.04 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/docker/docker/client"
"github.com/spf13/viper"
)
// AuthInfo stores a users' docker registry/hub info
var AuthInfo sync.Map
var running = map[string]string{}
// E is a generic error handler
func E(err error) {
if err != nil {
panic(err)
}
}
func main() {
log.Println("Version 0.1.2")
dockerconf := map[string][]string{}
conf := map[string]string{}
labelconf := map[string]string{}
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
var mode string
fs.StringVar(&mode, "mode", "docker", "Mode: docker (default), static")
err := fs.Parse(os.Args[1:])
if err != nil {
panic(err)
}
// log.Printf("Flags: %s %v", mode, fs)
env := os.Environ()
for _, e := range env {
if strings.HasPrefix(e, "PATH=") {
log.Printf("Env: %s", e)
}
}
var d Docker
ctx := context.Background()
switch mode {
case "docker":
cli, err := client.NewEnvClient()
E(err)
d = Docker{mode: ModeDocker, cli: cli}
case "static":
d = Docker{mode: ModeStatic}
}
if len(fs.Args()) > 0 {
switch fs.Args()[0] {
case "start":
traefik(ctx, d, dockerconf)
default:
log.Printf("can't process %v", fs.Args())
}
return
}
// deployment command (with docker) is default (legacy systems)
log.Println("Reading configuration...")
viper.SetConfigName("config")
viper.AddConfigPath(".") // optionally look for config in the working directory
err = viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %v", err))
}
conf = viper.GetStringMapString("traefiker")
dockerconf = viper.GetStringMapStringSlice("docker")
if conf["network"] != "" && len(dockerconf["networks"]) == 0 {
dockerconf["networks"] = []string{conf["network"]}
}
labelconf = viper.GetStringMapString("labels")
// hotfix for entryPoints
for k, v := range labelconf {
if strings.Contains(k, "entrypoints") {
k2 := strings.Replace(k, "entrypoints", "entryPoints", -1)
delete(labelconf, k)
labelconf[k2] = v
}
}
log.Println("Connecting to docker...")
old := map[string]string{}
for _, s := range d.List() {
if s.Image == conf["name"] || strings.HasPrefix(s.Names[0], "/"+conf["name"]+"_") {
old[s.ID] = s.ID
}
log.Println(s.Image)
running[s.Image] = s.Names[0]
}
log.Println("Creating docker container...")
name, err := d.BuildDockerImage(ctx, conf)
E(err)
ii := 1
if conf["instances"] != "" {
ii, err = strconv.Atoi(conf["instances"])
if err != nil {
log.Printf("instances must be an integer, defaulting to 1")
}
}
for i := 0; i < ii; i++ {
d.Run(ctx, name, "", labelconf, dockerconf)
}
time.Sleep(2 * time.Second)
newid := ""
for _, s := range d.List() {
if _, ok := old[s.ID]; !ok {
if s.Image == name {
newid = s.ID
}
}
}
if newid == "" {
log.Println("Unsuccessful build, container is not running")
os.Exit(-1)
}
for _, id := range old {
log.Println("Stopping", id)
d.StopContainer(ctx, id)
}
}