This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_init.go
240 lines (217 loc) · 5.88 KB
/
client_init.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2020 DigitalOcean
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"crypto/rand"
"crypto/rsa"
"encoding/gob"
"fmt"
"github.com/digitalocean/godo"
"github.com/google/uuid"
"golang.org/x/crypto/ssh"
"os"
"path/filepath"
"strings"
)
// Used to define the DO client.
var client *godo.Client
// Used to get the user to input their token.
func setToken() string {
for {
text := GetInput("What is your DigitalOcean token? You will need a read/write API key which you can generate from the \"API Keys\" panel when you are signed in with your DigitalOcean account: ")
if text == "" {
continue
}
client = godo.NewFromToken(text)
_, resp, err := client.Tags.List(context(), &godo.ListOptions{})
if err == nil {
return text
}
if resp != nil && resp.StatusCode == 401 {
continue
}
panic(err)
}
}
// Tries to load the config. Returns false if requires init.
func loadConfig() (string, bool) {
homedir, err := os.UserHomeDir()
if err != nil {
// For every platform we support, we expect a home folder to exist.
// There's not much we can do here except crash.
panic(err)
}
fp := filepath.Join(homedir, ".do-disposable")
f, err := os.Open(fp)
// Handle file being existent/readable.
if err == nil {
// Attempt to decode the Gob file.
decoder := gob.NewDecoder(f)
err = decoder.Decode(&config)
if err != nil {
// The user has either manually tried to modify this or something went wrong in the application.
panic(err)
}
// Create the client with this token.
client = godo.NewFromToken(config.Token)
// Return true here.
return fp, true
}
if os.IsNotExist(err) {
// The file doesn't exist. Return false.
return fp, false
} else {
// The error here is a configuration issue with the users system. We will crash.
panic(err)
}
}
// Quick hack to get the tag from a description.
func getTag(x string) string {
l := strings.Split(x, " ")
return l[len(l)-1][1:len(l[len(l)-1])-1]
}
// Used to write the config.
func writeConfig(fp string) {
f, err := os.Create(fp)
if err != nil {
panic(err)
}
e := gob.NewEncoder(f)
err = e.Encode(config)
if err != nil {
panic(err)
}
}
// Used to set the default region.
func setDefaultRegion() []godo.Region {
regions, _, err := client.Regions.List(context(), &godo.ListOptions{})
if err != nil {
// Hmmmmm this is odd.
panic(err)
}
descs := make([]string, len(regions))
nyc3Exists := false
for i, v := range regions {
if !v.Available {
// Not relevant to us. Continue.
continue
}
descs[i] = v.Name + " [" + v.Slug + "]"
if v.Slug == "nyc3" {
nyc3Exists = true
}
}
DefaultRegion := "New York 3 [nyc3]"
if !nyc3Exists {
DefaultRegion = descs[0]
}
config.DefaultRegion = getTag(FormatList("What's the default region you wish to use?", descs, &DefaultRegion))
return regions
}
// Used to set the droplet size.
func setSize(regions []godo.Region) {
var err error
if regions == nil {
regions, _, err = client.Regions.List(context(), &godo.ListOptions{})
if err != nil {
// Hmmmmm this is odd.
panic(err)
}
}
var sizes []string
for _, v := range regions {
if v.Slug == config.DefaultRegion {
sizes = v.Sizes
}
}
doSizes := map[string]godo.Size{}
doGlobalSizes, _, err := client.Sizes.List(context(), &godo.ListOptions{})
if err != nil {
// Very odd.
panic(err)
}
for _, v := range doGlobalSizes {
if !v.Available {
// Ignore this.
continue
}
doSizes[v.Slug] = v
}
dropletDescs := make([]string, 0, len(sizes))
for _, v := range sizes {
x, ok := doSizes[v]
if !ok {
continue
}
dropletDescs = append(dropletDescs, fmt.Sprintf("%d GB storage/%d vCPUS/%d MB RAM/$%f per hour [%s]", x.Disk, x.Vcpus, x.Memory, x.PriceHourly, v))
}
config.DefaultSize = getTag(FormatList("What's the default droplet size you wish to use?", dropletDescs, &dropletDescs[0]))
}
// Used to get the public SSH key of the user.
func getPublicKey() string {
pub, err := ssh.NewPublicKey(&config.PrivateKey.PublicKey)
if err != nil {
panic(err)
}
return string(ssh.MarshalAuthorizedKey(pub))
}
// Used to generate/upload the SSH key.
func genSSHKey() {
print("Generating application specific SSH key... ")
var err error
config.PrivateKey, err = rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
panic(err)
}
err = config.PrivateKey.Validate()
if err != nil {
panic(err)
}
println("done!")
// Save the SSH key to the user.
print("Saving application specific SSH key to user... ")
info, _, err := client.Keys.Create(context(), &godo.KeyCreateRequest{
Name: "do-disposable ["+uuid.New().String()+"]",
PublicKey: getPublicKey(),
})
if err != nil {
panic(err)
}
config.KeyID = info.ID
println("done!")
}
// Used to get the user to input their config options and then save it as a new config.
func inputSaveConfig(fp string) {
// Create the base config structure.
config = &configStructure{}
// Set the users token.
config.Token = setToken()
// Create the client with this token.
client = godo.NewFromToken(config.Token)
// Get the default region from the user.
regions := setDefaultRegion()
// Get the default droplet size from the user.
setSize(regions)
// Generate the SSH key.
genSSHKey()
// Write the config.
writeConfig(fp)
}
// Used to initialise the DigitalOcean client and config.
func clientInit() {
_, exists := loadConfig()
if !exists {
println("Configuration is not set. Please run do-disposable auth.")
os.Exit(1)
}
}