-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstancesetup.go
211 lines (167 loc) · 6.37 KB
/
instancesetup.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
github.com/tcrain/cons - Experimental project for testing and scaling consensus algorithms.
Copyright (C) 2020 The project authors - tcrain
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"flag"
"fmt"
"github.com/tcrain/cons/cloudsetup"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
"log"
"strings"
)
func main() {
var project string
var credentialPath string
var instanceType string
var imageName string
var doLaunchInstance bool
var doShutdownInstance bool
var instanceName string
var doDeleteDisk bool
var doCreateImage bool
var initialSetupZone string
var doRebootRegions bool
var doLaunchRegions bool
var singleZoneRegion bool
var notSingleZoneRegion bool
var launchRegions string
var nodeCount int
var undoLaunchRegions bool
var doGetIPs bool
var onlyExternalIps bool
var doGetInstanceIP bool
var undoCreateImage bool
var doCreateInstanceTemplate bool
var doDeleteInstanceTemplate bool
flag.BoolVar(&doCreateInstanceTemplate, "cit", false, "Create instance template,"+
"use with options: p, i")
flag.BoolVar(&doDeleteInstanceTemplate, "dit", false, "Remove instance template,"+
"use with options: p")
flag.StringVar(&project, "p", cloudsetup.ProjectID, "Cloud project ID")
flag.StringVar(&credentialPath, "c", cloudsetup.JsonPath, "Path to credentials file")
flag.StringVar(&instanceType, "i", cloudsetup.DefaultInstanceType, "Instance type to use")
flag.StringVar(&instanceName, "in", cloudsetup.InitInstance, "Init instance name")
flag.StringVar(&imageName, "im", cloudsetup.GlobalImageName, "Image name to use when launching a node")
flag.BoolVar(&doShutdownInstance, "sd", false, "Shutdown an instance,"+
"use this with options: "+
"im, p, z")
flag.BoolVar(&doDeleteDisk, "dd", false, "Delete a disk, "+
"use this with options: im, p, z")
flag.BoolVar(&doLaunchInstance, "li", false, "Launch an instance,"+
"the output of this command is the ip of the image, use this with options: "+
"in, i, im, p, z")
flag.BoolVar(&doGetInstanceIP, "ii", false, "Get the ip of an instance launched with -li,"+
"the output of this command is the ip of the image, use this with options: "+
"in, i, im, p, z")
flag.BoolVar(&doCreateImage, "s", false, "This should be called after an instance is setup,"+
"this will use the instance to create an image, use this with options:"+
"i, im, in, p")
flag.StringVar(&initialSetupZone, "z", cloudsetup.DefaultZone, "Zone to perform initial setup in")
flag.BoolVar(&undoCreateImage, "us", false, "Undo create image setup, use this with options:"+
"in, p")
flag.BoolVar(&doLaunchRegions, "lr", false, "Launch instance groups in multiple zones, use this with options:"+
"n, p, r")
flag.BoolVar(&singleZoneRegion, "lrz", false, "For instance group commands use a single zone:"+
"n, p, r")
flag.BoolVar(&doRebootRegions, "rb", false, "Reboot instance groups in multiple zones, use this with options:"+
"n, p, r")
flag.BoolVar(¬SingleZoneRegion, "nlrz", false, "Unused")
flag.StringVar(&launchRegions, "r", cloudsetup.DefaultRegion, "Regions to launch instance groups on")
flag.IntVar(&nodeCount, "n", 1, "Nodes per region to launch")
flag.BoolVar(&undoLaunchRegions, "ur", false, "Undo region launch, use this with options:"+
"p, r")
flag.BoolVar(&doGetIPs, "gi", false, "Get the IPs for the running instance groups,"+
"use with options: p, r")
flag.BoolVar(&onlyExternalIps, "eip", false, "Get external ips only, use with option: gi")
flag.Parse()
ctx := context.Background()
service, err := compute.NewService(ctx, option.WithCredentialsFile(credentialPath))
if err != nil {
log.Fatal(err)
}
if doLaunchInstance {
if err := cloudsetup.DoLaunchInstance(instanceName, imageName, instanceType, project,
initialSetupZone, service); err != nil {
log.Fatal(err)
}
}
if doGetInstanceIP {
if err := cloudsetup.DoGetInstanceIP(instanceName, imageName, instanceType, project,
initialSetupZone, service); err != nil {
log.Fatal(err)
}
}
if doShutdownInstance {
if err := cloudsetup.DoShutdownInstance(instanceName, project, initialSetupZone, service); err != nil {
log.Fatal(err)
}
}
if doDeleteDisk {
if err := cloudsetup.DoDeleteDisk(instanceName, project, initialSetupZone, service); err != nil {
log.Fatal(err)
}
}
if doCreateImage {
if err := cloudsetup.MakeImageFromInstance(imageName, instanceName, project, initialSetupZone,
service); err != nil {
log.Fatal(err)
}
}
if undoCreateImage {
if err := cloudsetup.DeleteImage(imageName, project, initialSetupZone, service); err != nil {
log.Fatal(err)
}
}
if doCreateInstanceTemplate {
if err := cloudsetup.CreateInstanceTemplate(cloudsetup.TemplateName, cloudsetup.GlobalImageName,
instanceType, project, service); err != nil {
log.Fatal(err)
}
}
if doDeleteInstanceTemplate {
if err := cloudsetup.DeleteInstanceTemplate(cloudsetup.TemplateName, project, service); err != nil {
log.Fatal(err)
}
}
regions := strings.Fields(launchRegions)
if doGetIPs {
ips, err := cloudsetup.GetInstanceGroupsIPs(singleZoneRegion, onlyExternalIps, cloudsetup.InstanceGroupName, project, regions, service)
if err != nil {
log.Fatal(err)
}
fmt.Println(strings.Join(ips, "\n"))
}
if doLaunchRegions {
if err := cloudsetup.LaunchInstanceGroups(singleZoneRegion, nodeCount, cloudsetup.BaseInstanceName,
cloudsetup.InstanceGroupName, cloudsetup.TemplateName,
project, regions, service); err != nil {
log.Fatal(err)
}
}
if undoLaunchRegions {
if errs := cloudsetup.ShutdownInstanceGroups(singleZoneRegion, cloudsetup.InstanceGroupName,
project, regions, service); len(errs) > 0 {
log.Fatal(errs)
}
}
if doRebootRegions {
if errs := cloudsetup.DoRebootRegions(singleZoneRegion, cloudsetup.InstanceGroupName,
project, regions, service); len(errs) > 0 {
log.Fatal(errs)
}
}
}