@@ -25,6 +25,8 @@ func init() {
25
25
setupCmd .Flags ().String ("wireguard-address" , "" , "Wireguard address" )
26
26
setupCmd .Flags ().String ("docker-network-gateway-address" , "" , "Docker network gateway address" )
27
27
setupCmd .Flags ().String ("docker-network-subnet" , "" , "Docker network subnet" )
28
+ setupCmd .Flags ().String ("swiftwave-service-address" , "" , "Swiftwave service address ip:port" )
29
+ setupCmd .Flags ().Bool ("enable-haproxy" , false , "Enable haproxy" )
28
30
29
31
setupCmd .Flags ().Bool ("master-node" , false , "Setup as a master node" )
30
32
setupCmd .Flags ().String ("master-node-endpoint" , "" , "Master server endpoint" )
@@ -35,6 +37,7 @@ func init() {
35
37
setupCmd .MarkFlagRequired ("wireguard-address" )
36
38
setupCmd .MarkFlagRequired ("docker-network-gateway-address" )
37
39
setupCmd .MarkFlagRequired ("docker-network-subnet" )
40
+ setupCmd .MarkFlagRequired ("swiftwave-service-address" )
38
41
}
39
42
40
43
var rootCmd = & cobra.Command {
@@ -68,7 +71,14 @@ var startCmd = &cobra.Command{
68
71
var setupCmd = & cobra.Command {
69
72
Use : "setup" ,
70
73
Run : func (cmd * cobra.Command , args []string ) {
71
- _ , err := GetConfig ()
74
+ // Migrate the database
75
+ err := MigrateDatabase ()
76
+ if err != nil {
77
+ cmd .PrintErr ("Failed to migrate database" )
78
+ return
79
+ }
80
+ // Try to get the config
81
+ _ , err = GetConfig ()
72
82
if err == nil {
73
83
cmd .Println ("Sorry, you can't change any config" )
74
84
return
@@ -80,13 +90,27 @@ var setupCmd = &cobra.Command{
80
90
return
81
91
}
82
92
93
+ isEnableHaproxy , err := cmd .Flags ().GetBool ("enable-haproxy" )
94
+ if err != nil {
95
+ cmd .PrintErr ("Invalid enable haproxy flag" )
96
+ return
97
+ }
98
+
99
+ // validate swiftwave service address
100
+ _ , _ , err = net .SplitHostPort (cmd .Flag ("swiftwave-service-address" ).Value .String ())
101
+ if err != nil {
102
+ cmd .PrintErr ("Invalid swiftwave service address" )
103
+ return
104
+ }
105
+
83
106
nodeType := WorkerNode
84
107
if isMasterNode {
85
108
nodeType = MasterNode
86
109
}
87
110
88
111
config := AgentConfig {
89
- NodeType : nodeType ,
112
+ NodeType : nodeType ,
113
+ SwiftwaveServiceAddress : cmd .Flag ("swiftwave-service-address" ).Value .String (),
90
114
WireguardConfig : WireguardConfig {
91
115
PrivateKey : cmd .Flag ("wireguard-private-key" ).Value .String (),
92
116
Address : cmd .Flag ("wireguard-address" ).Value .String (),
@@ -100,6 +124,11 @@ var setupCmd = &cobra.Command{
100
124
PublicKey : cmd .Flag ("master-node-public-key" ).Value .String (),
101
125
AllowedIPs : cmd .Flag ("master-node-allowed-ips" ).Value .String (),
102
126
},
127
+ HaproxyConfig : HAProxyConfig {
128
+ Enabled : isEnableHaproxy ,
129
+ Username : GenerateRandomString (10 ),
130
+ Password : GenerateRandomString (30 ),
131
+ },
103
132
}
104
133
105
134
if ! isMasterNode {
@@ -137,6 +166,20 @@ var setupCmd = &cobra.Command{
137
166
}
138
167
}()
139
168
169
+ // Get ip from wireguard address
170
+ ip , _ , err := net .ParseCIDR (config .WireguardConfig .Address )
171
+ if err != nil {
172
+ cmd .PrintErr ("Failed to parse wireguard address" )
173
+ return
174
+ }
175
+
176
+ // Install haproxy
177
+ err = installHAProxy (config .SwiftwaveServiceAddress , fmt .Sprintf ("%s:53" , ip ), config .HaproxyConfig .Username , config .HaproxyConfig .Password )
178
+ if err != nil {
179
+ cmd .PrintErr (err .Error ())
180
+ return
181
+ }
182
+
140
183
err = SetConfig (& config )
141
184
if err != nil {
142
185
cmd .PrintErr (err .Error ())
@@ -162,6 +205,12 @@ var setupCmd = &cobra.Command{
162
205
cmd .PrintErr (err .Error ())
163
206
return
164
207
}
208
+ // Enable haproxy and data plane api
209
+ if config .HaproxyConfig .Enabled {
210
+ enableHAProxy ()
211
+ }
212
+ cmd .Println ("Haproxy and data plane api enabled" )
213
+
165
214
isSuccess = true
166
215
},
167
216
}
@@ -208,6 +257,11 @@ var getConfig = &cobra.Command{
208
257
cmd .Printf (" • Bridge ID ---------- %s\n " , config .DockerNetwork .BridgeId )
209
258
cmd .Printf (" • Gateway Address ---- %s\n " , config .DockerNetwork .GatewayAddress )
210
259
cmd .Printf (" • Subnet ------------- %s\n " , config .DockerNetwork .Subnet )
260
+ cmd .Println ()
261
+ cmd .Println ("Haproxy Configuration:" )
262
+ cmd .Printf (" • Enabled ------------ %t\n " , config .HaproxyConfig .Enabled )
263
+ cmd .Printf (" • Username ---------- %s\n " , config .HaproxyConfig .Username )
264
+ cmd .Printf (" • Password ---------- %s\n " , config .HaproxyConfig .Password )
211
265
},
212
266
}
213
267
@@ -216,7 +270,7 @@ var cleanup = &cobra.Command{
216
270
Run : func (cmd * cobra.Command , args []string ) {
217
271
// Ask for confirmation
218
272
fmt .Println ("This will delete all containers and remove docker and wireguard networks" )
219
- fmt .Println ("Are you sure you want to continue? (y/n)" )
273
+ fmt .Print ("Are you sure you want to continue? (y/n) : " )
220
274
var response string
221
275
_ , err := fmt .Scanln (& response )
222
276
if err != nil {
@@ -274,6 +328,9 @@ var cleanup = &cobra.Command{
274
328
_ = IPTablesClient .ClearChain ("nat" , NatPostroutingChainName )
275
329
_ = IPTablesClient .ClearChain ("nat" , NatInputChainName )
276
330
_ = IPTablesClient .ClearChain ("nat" , NatOutputChainName )
331
+ // Disable haproxy and data plane api
332
+ disableHAProxy ()
333
+ cmd .Println ("Haproxy and data plane api disabled" )
277
334
// Backup and remove the database file
278
335
moveDBFilesToBackup ()
279
336
// Done
0 commit comments