-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathsetup.go
185 lines (145 loc) · 4.35 KB
/
setup.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package bridge
import (
"os"
// "runtime"
"encoding/json"
"io/ioutil"
"path/filepath"
"time"
"github.com/jcelliott/lumber"
"github.com/nanobox-io/golang-docker-client"
container_generator "github.com/nanobox-io/nanobox/generators/containers"
"github.com/nanobox-io/nanobox/util"
"github.com/nanobox-io/nanobox/util/display"
// "github.com/nanobox-io/nanobox/util/fileutil"
"github.com/nanobox-io/nanobox/util/config"
"github.com/nanobox-io/nanobox/util/hookit"
"github.com/nanobox-io/nanobox/util/locker"
)
var keys map[string]string
func Setup() error {
display.OpenContext("Building bridge")
defer display.CloseContext()
// if the container exists and openvpn is running
if Connected() {
return nil
}
// create a component
if err := setupContainer(); err != nil {
return err
}
// configure bridge client
if err := configureBridge(); err != nil {
return err
}
// start bridge client
if err := Start(); err != nil {
return err
}
return nil
}
// sets up the dev container and network stack
func setupContainer() error {
// we don't need to setup if bridge is already running
if isBridgeExists() {
return nil
}
// establish a local lock to ensure we're the only ones bringing up the
// dev container. Also, we need to ensure the lock is released even in we error
locker.LocalLock()
defer locker.LocalUnlock()
// generate a container config
config := container_generator.BridgeConfig()
//
if err := downloadImage(config.Image); err != nil {
return err
}
display.StartTask("Starting docker container")
container, err := docker.CreateContainer(config)
if err != nil {
display.ErrorTask()
return util.ErrorAppend(err, "failed to create bridge container")
}
display.StopTask()
display.StartTask("Setting up container")
// run the configure hook
if _, err := hookit.DebugExec(container.ID, "configure", "{\"platform\":\"local\",\"config\":{}}", "info"); err != nil {
return util.ErrorAppend(err, "failed to run configure hook")
}
// run the start hook
if _, err := hookit.DebugExec(container.ID, "start", "{}", "info"); err != nil {
return util.ErrorAppend(err, "failed to run start hook")
}
// run the start hook
output, err := hookit.DebugExec(container.ID, "keys", "{}", "info")
if err != nil {
return util.ErrorAppend(err, "failed to run start hook")
}
if err := json.Unmarshal([]byte(output), &keys); err != nil {
return util.ErrorAppend(err, "failed to decode the keys")
}
display.StopTask()
return nil
}
// isBridgeExists returns true if a service is already running
func isBridgeExists() bool {
_, err := docker.GetContainer(container_generator.BridgeName())
// if the container doesn't exist then just return false
return err == nil
}
// downloadImage downloads the dev docker image
func downloadImage(image string) error {
if docker.ImageExists(image) {
return nil
}
display.StartTask("Pulling %s image", image)
defer display.StopTask()
// generate a docker percent display
dockerPercent := &display.DockerPercentDisplay{
Output: display.NewStreamer("info"),
// Prefix: image,
}
imagePull := func() error {
_, err := docker.ImagePull(image, dockerPercent)
return err
}
if err := util.Retry(imagePull, 5, time.Second); err != nil {
display.ErrorTask()
lumber.Error("dev:Setup:downloadImage.ImagePull(%s, nil): %s", image, err.Error())
return util.ErrorAppend(err, "failed to pull docker image (%s)", image)
}
return nil
}
// ca.crt
// client.key
// client.crt
func configureBridge() error {
display.StartTask("Configuring")
defer display.StopTask()
// make the openvpn folder
vpnDir := filepath.ToSlash(filepath.Join(config.EtcDir(), "openvpn"))
if err := os.MkdirAll(vpnDir, 0755); err != nil {
lumber.Fatal("[bridge] os.Mkdir() failed", err.Error())
}
// create config file
if err := ioutil.WriteFile(ConfigFile(), []byte(BridgeConfig()), 0644); err != nil {
return err
}
// make sure to not overwrite the keys if we didnt create the container on this run
if keys["ca.crt"] == "" {
return nil
}
// create ca.crt
if err := ioutil.WriteFile(CaCrt(), []byte(keys["ca.crt"]), 0644); err != nil {
return err
}
// create client.key
if err := ioutil.WriteFile(ClientKey(), []byte(keys["client.key"]), 0644); err != nil {
return err
}
// create client.crt
if err := ioutil.WriteFile(ClientCrt(), []byte(keys["client.crt"]), 0644); err != nil {
return err
}
return nil
}