-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
283 lines (218 loc) · 6.44 KB
/
options.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package sad
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"regexp"
"strconv"
"strings"
)
// OptionEnvVarPrefix represents the prefix that all environment variables representing options should have to be read properly.
var OptionEnvVarPrefix = "SAD_"
// DeploymentEnvVarPrefix represents the prefix that all dynamic environment variables which will be injected into the deployment should have to be read properly.
var DeploymentEnvVarPrefix = OptionEnvVarPrefix + "DEPLOY_"
// Options for deployment.
type Options struct {
Registry string
Image string
Digest string
Server net.IP
Username string
RootDir string
PrivateKey RSAPrivateKey
Channel string
EnvVars []string
Debug bool
}
// Merge merges the other options into the existing options
// When both fields are populated, the field from the existing options is kept.
func (o *Options) Merge(other *Options) {
if o.Registry == "" {
o.Registry = other.Registry
}
if o.Image == "" {
o.Image = other.Image
}
if o.Digest == "" {
o.Digest = other.Digest
}
if o.Server == nil {
o.Server = other.Server
}
if o.Username == "" {
o.Username = other.Username
}
if o.RootDir == "" {
o.RootDir = other.RootDir
}
if o.PrivateKey.PrivateKey == nil {
o.PrivateKey = other.PrivateKey
}
if o.Channel == "" {
o.Channel = other.Channel
}
if len(o.EnvVars) == 0 {
o.EnvVars = other.EnvVars
}
if !o.Debug {
o.Debug = other.Debug
}
}
// MergeDefaults merges default option values into the given options.
func (o *Options) MergeDefaults() {
defaults := Options{
Channel: "beta",
RootDir: "/",
Debug: false,
}
o.Merge(&defaults)
}
// Verify verifies that the options are valid.
// Returns an error with information about why the options are invalid.
func (o *Options) Verify() error {
errorMap := make(map[string]string)
empty := "<empty>"
if o.Image == "" {
errorMap["image"] = fmt.Sprintf("is %s", empty)
}
if o.Digest == "" {
errorMap["digest"] = fmt.Sprintf("is %s", empty)
}
if o.Server == nil {
errorMap["server"] = "is nil"
}
if o.Username == "" {
errorMap["username"] = fmt.Sprintf("is %s", empty)
}
if o.RootDir == "" {
errorMap["root directory"] = fmt.Sprintf("is %s", empty)
}
if o.PrivateKey.PrivateKey == nil {
errorMap["private key"] = "is nil"
}
if o.Channel == "" {
errorMap["channel"] = fmt.Sprintf("is %s", empty)
}
if len(errorMap) != 0 {
errorString := "invalid options! "
for field, message := range errorMap {
errorString += fmt.Sprintf("%s %s, ", field, message)
}
errorString = errorString[:len(errorString)-2]
return fmt.Errorf(errorString)
}
return nil
}
// FromStrings converts strings into options.
func (o *Options) FromStrings(registry string, image string, digest string, server string, username string, rootDir string, privateKey string, channel string, envVars string, debug string) error {
o.Registry = registry
o.Image = image
o.Digest = digest
if server != "" {
o.Server = net.ParseIP(server)
}
o.Username = username
o.RootDir = rootDir
if privateKey != "" {
rsaPrivateKey := RSAPrivateKey{}
err := rsaPrivateKey.ParseBase64PEMString(privateKey)
if err != nil {
return err
}
o.PrivateKey = rsaPrivateKey
}
o.Channel = channel
if envVars != "" {
envVarsArr := strings.Split(envVars, ",")
o.EnvVars = envVarsArr
}
if debug != "" {
debugBool, err := strconv.ParseBool(debug)
if err != nil {
return err
}
o.Debug = debugBool
}
return nil
}
// FromJSON parses options from a JSON file.
func (o *Options) FromJSON(path string) error {
file, err := ioutil.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
if len(file) == 0 {
return nil
}
return json.Unmarshal(file, o)
}
// FromEnv parses options from environment variables.
// All variables should be prefixed and they should correspond to the available options with underscores separating words such as "PRIVATE_KEY".
// The private key should be a base64 encoded string.
// The environment variables should be a comma-separated string.
func (o *Options) FromEnv() error {
prefix := OptionEnvVarPrefix
registry := os.Getenv(prefix + "REGISTRY")
image := os.Getenv(prefix + "IMAGE")
digest := os.Getenv(prefix + "DIGEST")
server := os.Getenv(prefix + "SERVER")
username := os.Getenv(prefix + "USERNAME")
rootDir := os.Getenv(prefix + "ROOT_DIR")
privateKey := os.Getenv(prefix + "PRIVATE_KEY")
channel := os.Getenv(prefix + "CHANNEL")
envVars := os.Getenv(prefix + "ENV_VARS")
debug := os.Getenv(prefix + "DEBUG")
err := o.FromStrings(registry, image, digest, server, username, rootDir, privateKey, channel, envVars, debug)
if err != nil {
return err
}
return nil
}
// GetDeploymentName gets the full name of the deployment.
// The name is based on the image and the channel.
// All non-alphanumeric characters are replaced by dashes.
func (o *Options) GetDeploymentName() (string, error) {
deploymentName := fmt.Sprintf("%s-%s", o.Image, o.Channel)
deploymentName, err := replaceNonAlphanumeric(deploymentName, "-")
if err != nil {
return "", fmt.Errorf("error replacing non-alphanumeric characters in deployment name: %s", err)
}
return deploymentName, nil
}
// GetImageSpecifier gets the full image specifier for the deployment.
// The specifier is based on the image and the digest.
func (o *Options) GetImageSpecifier() string {
separator := ""
if o.Registry != "" {
separator = "/"
}
specifier := fmt.Sprintf("%s%s%s@%s", o.Registry, separator, o.Image, o.Digest)
return specifier
}
// GetDeploymentEnvValues gets the values of the environment variables specified in the EnvVars field to be injected into the deployment.
// Returns a map of the variable names to values, or an error if any of the variables are blank or unset.
func (o *Options) GetDeploymentEnvValues() (map[string]string, error) {
m := make(map[string]string)
for _, variableName := range o.EnvVars {
variableNameWithPrefix := DeploymentEnvVarPrefix + variableName
value := os.Getenv(variableNameWithPrefix)
if value == "" {
return nil, fmt.Errorf("environment variable %s is blank or unset", variableNameWithPrefix)
}
m[variableName] = value
}
return m, nil
}
func replaceNonAlphanumeric(input string, replaceWith string) (string, error) {
regStr := "[^a-zA-Z0-9]+"
reg, err := regexp.Compile(regStr)
if err != nil {
return "", fmt.Errorf("error compiling regex %s: %s", regStr, err)
}
return reg.ReplaceAllString(input, replaceWith), nil
}