forked from clearraining/tutum-agent
/
agent.go
170 lines (145 loc) · 5.02 KB
/
agent.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
package main
import (
"os"
"path"
"runtime"
"syscall"
"time"
. "github.com/tutumcloud/tutum-agent/agent"
"github.com/tutumcloud/tutum-agent/utils"
)
func init() {
runtime.GOMAXPROCS(4)
}
func main() {
dockerBinPath := path.Join(DockerDir, DockerBinaryName)
dockerNewBinPath := path.Join(DockerDir, DockerNewBinaryName)
dockerNewBinSigPath := path.Join(DockerDir, DockerNewBinarySigName)
configFilePath := path.Join(TutumHome, ConfigFileName)
keyFilePath := path.Join(TutumHome, KeyFileName)
certFilePath := path.Join(TutumHome, CertFileName)
caFilePath := path.Join(TutumHome, CAFileName)
ngrokPath := path.Join(DockerDir, NgrokBinaryName)
ngrokLogPath := path.Join(LogDir, NgrokLogName)
ngrokConfPath := path.Join(TutumHome, NgrokConfName)
_ = os.MkdirAll(TutumHome, 0755)
_ = os.MkdirAll(DockerDir, 0755)
_ = os.MkdirAll(LogDir, 0755)
ParseFlag()
SetLogger(path.Join(LogDir, TutumLogFileName))
CreatePidFile(TutumPidFile)
PrepareFiles(configFilePath, dockerBinPath, keyFilePath, certFilePath)
SetConfigFile(configFilePath)
regUrl := utils.JoinURL(Conf.TutumHost, RegEndpoint)
if Conf.TutumUUID == "" {
os.RemoveAll(keyFilePath)
os.RemoveAll(certFilePath)
os.RemoveAll(caFilePath)
if !*FlagStandalone {
Logger.Printf("Registering in Tutum via POST: %s...\n", regUrl)
PostToTutum(regUrl, caFilePath, configFilePath)
}
}
if *FlagStandalone {
commonName := Conf.CertCommonName
if commonName == "" {
commonName = "*"
}
CreateCerts(keyFilePath, certFilePath, commonName)
} else {
CreateCerts(keyFilePath, certFilePath, Conf.CertCommonName)
}
if !*FlagStandalone {
Logger.Printf("Registering in Tutum via PATCH: %s...\n",
regUrl+Conf.TutumUUID)
err := PatchToTutum(regUrl, caFilePath, certFilePath, configFilePath)
if err != nil {
Logger.Printf("TutumUUID (%s) is invalid, trying to allocate a new one...\n", Conf.TutumUUID)
Conf.TutumUUID = ""
SaveConf(configFilePath, Conf)
os.RemoveAll(keyFilePath)
os.RemoveAll(certFilePath)
os.RemoveAll(caFilePath)
Logger.Printf("Registering in Tutum via POST: %s...\n", regUrl)
PostToTutum(regUrl, caFilePath, configFilePath)
CreateCerts(keyFilePath, certFilePath, Conf.CertCommonName)
Logger.Printf("Registering in Tutum via PATCH: %s...\n",
regUrl+Conf.TutumUUID)
if err = PatchToTutum(regUrl, caFilePath, certFilePath, configFilePath); err != nil {
SendError(err, "Registion HTTP error", nil)
}
}
}
if err := SaveConf(configFilePath, Conf); err != nil {
SendError(err, "Failed to save config to the conf file", nil)
Logger.Fatalln(err)
}
DownloadDocker(DockerBinaryURL, dockerBinPath)
HandleSig()
syscall.Setpriority(syscall.PRIO_PROCESS, os.Getpid(), RenicePriority)
Logger.Println("Starting docker daemon...")
StartDocker(dockerBinPath, keyFilePath, certFilePath, caFilePath)
if !*FlagStandalone {
if *FlagSkipNatTunnel {
Logger.Println("Skip NAT tunnel")
} else {
Logger.Println("Loading NAT tunnel module...")
go NatTunnel(regUrl, ngrokPath, ngrokLogPath, ngrokConfPath, NodePublicIp)
}
}
if !*FlagStandalone {
Logger.Println("Verifying the registration with Tutum...")
go VerifyRegistration(regUrl)
}
Logger.Println("Docker server started. Entering maintenance loop")
for {
time.Sleep(HeartBeatInterval * time.Second)
UpdateDocker(dockerBinPath, dockerNewBinPath, dockerNewBinSigPath, keyFilePath, certFilePath, caFilePath)
// try to restart docker daemon if it dies somehow
if DockerProcess == nil {
time.Sleep(HeartBeatInterval * time.Second)
if DockerProcess == nil && ScheduleToTerminateDocker == false {
Logger.Println("Respawning docker daemon")
StartDocker(dockerBinPath, keyFilePath, certFilePath, caFilePath)
}
}
}
}
func PrepareFiles(configFilePath, dockerBinPath, keyFilePath, certFilePath string) {
Logger.Println("Checking if config file exists...")
if !utils.FileExist(configFilePath) {
LoadDefaultConf()
if err := SaveConf(configFilePath, Conf); err != nil {
SendError(err, "Failed to save config to the conf file", nil)
Logger.Fatalln(err)
}
}
Logger.Println("Loading Configuration file...")
conf, err := LoadConf(configFilePath)
if err != nil {
SendError(err, "Failed to load configuration file", nil)
Logger.Fatalln("Failed to load configuration file:", err)
} else {
Conf = *conf
}
if *FlagDockerHost != "" {
Logger.Printf("Override 'DockerHost' from command line flag: %s\n", *FlagDockerHost)
Conf.DockerHost = *FlagDockerHost
}
if *FlagTutumHost != "" {
Logger.Printf("Override 'TutumHost' from command line flag: %s\n", *FlagTutumHost)
Conf.TutumHost = *FlagTutumHost
}
if *FlagTutumToken != "" {
Logger.Printf("Override 'TutumToken' from command line flag: %s\n", *FlagTutumToken)
Conf.TutumToken = *FlagTutumToken
}
if *FlagTutumUUID != "" {
Logger.Printf("Override 'TutumUUID' from command line flag: %s\n", *FlagTutumUUID)
Conf.TutumUUID = *FlagTutumUUID
}
if *FlagDockerOpts != "" {
Logger.Printf("Override 'DockerOpts' from command line flag: %s\n", *FlagDockerOpts)
Conf.DockerOpts = *FlagDockerOpts
}
}