forked from ernado-legacy/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployer.go
160 lines (146 loc) · 3.42 KB
/
deployer.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"bytes"
"fmt"
"github.com/GeertJohan/go.rice"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
const (
systemFolder = "/etc/systemd/system/"
dockerService = "docker.service"
flannelService = "flannel.service"
flannelPath = "/opt/bin/flanneld"
flannelCDN = "https://24827.selcdn.ru/cydev/flanneld"
envFile = "/run/flannel/subnet.env"
poolTime = time.Millisecond * 200
maxTimeout = 60 * time.Second
)
var (
templates *rice.Box
)
func downloadFlannel() {
if exec.Command("systemctl", "status", "flannel").Run() == nil {
fmt.Println("flannel service already exists")
return
}
fmt.Printf("downloading flannel...")
if err := os.MkdirAll(filepath.Dir(flannelPath), 0777); err != nil {
log.Fatal(err)
}
dst, err := os.Create(flannelPath)
check(err)
defer dst.Close()
req, _ := http.NewRequest("GET", flannelCDN, nil)
res, err := http.DefaultClient.Do(req)
check(err)
if res.StatusCode != http.StatusOK {
log.Fatalf("Bad status %s", res.Status)
}
src := res.Body
defer src.Close()
if _, err := io.Copy(dst, src); err != nil {
log.Fatal(err)
}
must(fmt.Sprintf("chmod +x %s", flannelPath))
fmt.Printf("ok\n")
}
func must(args string) {
cmd := exec.Command("/bin/bash", "-c", args)
cmd.Stderr = os.Stderr
check(cmd.Run())
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func stopDocker() {
fmt.Print("stoping docker...")
must("systemctl stop docker")
fmt.Print("ok\n")
}
func configureDockerNetwork() {
buffer := new(bytes.Buffer)
cmd := exec.Command("ifconfig")
cmd.Stdout = buffer
check(cmd.Run())
if strings.Contains(buffer.String(), "docker0") {
fmt.Print("removing docker0...")
must("ip link set dev docker0 down")
must("brctl delbr docker0")
fmt.Print("ok\n")
} else {
fmt.Println("docker0 already removed")
}
fmt.Print("restarting docker...")
must("systemctl restart docker")
must("docker ps")
fmt.Print("ok\n")
}
func createDockerService() {
fmt.Printf("creating docker service file...")
dst, err := os.Create(filepath.Join(systemFolder, dockerService))
check(err)
defer dst.Close()
src, err := templates.Open(dockerService)
_, err = io.Copy(dst, src)
check(err)
must("systemctl daemon-reload")
fmt.Printf("ok\n")
}
func createFlannelService() {
if exec.Command("systemctl", "status", "flannel").Run() == nil {
fmt.Println("flannel service already exists")
return
}
fmt.Print("creating docker service file...")
dst, err := os.Create(filepath.Join(systemFolder, flannelService))
check(err)
defer dst.Close()
src, err := templates.Open(flannelService)
_, err = io.Copy(dst, src)
check(err)
must("systemctl daemon-reload")
fmt.Printf("ok\n")
}
func waitForFile() {
fmt.Print("waiting for flannel subnet file...")
start := time.Now()
for {
_, err := os.Stat(envFile)
if err == nil {
fmt.Print("ok\n")
return
}
time.Sleep(poolTime)
if time.Now().Sub(start) > maxTimeout {
fmt.Print("timed out\n")
log.Fatalln("Waiting for flannel subnet file timed out")
}
}
}
func startFlannel() {
fmt.Print("starting flannel...")
must("systemctl enable flannel")
must("systemctl start flannel")
fmt.Printf("ok\n")
}
func main() {
templates = rice.MustFindBox("templates")
fmt.Println("deploying flannel on coreos")
stopDocker()
createDockerService()
downloadFlannel()
createFlannelService()
startFlannel()
waitForFile()
configureDockerNetwork()
fmt.Println("operations complete")
}