/
util.go
242 lines (193 loc) · 5.93 KB
/
util.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
package ops
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/mitchellh/go-homedir"
"github.com/orion-labs/genkeyset/pkg/genkeyset"
"github.com/pkg/errors"
"io/ioutil"
"os"
"regexp"
"text/template"
"time"
)
const DEFAULT_TEMPLATE_FILE = ".orion-ptt-system.tmpl"
const DEFAULT_NETWORK_CONFIG_FILE = ".orion-ptt-system-network.json"
// RetryUntil takes a function, and calls it every 20 seconds until it succeeds. Useful for polling endpoints in k8s that will eventually start working. Returns an error if the provided timeoutMinutes elapses. Otherwise returns the elapsed duration from start to finish.
func RetryUntil(thing func() (err error), timeoutMinutes int) (elapsed time.Duration, err error) {
start := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(int32(timeoutMinutes))*time.Minute)
defer cancel()
statusReady := false
for {
select {
case <-time.After(20 * time.Second):
err = thing()
if err != nil {
ts := time.Now()
h, m, s := ts.Clock()
// print the timestamp, and the error from the thing() function
fmt.Printf(" %02d:%02d:%02d %s.\n", h, m, s, err)
} else {
statusReady = true
}
case <-ctx.Done():
err = errors.New("Timeout exceeded")
finish := time.Now()
elapsed = finish.Sub(start)
return elapsed, err
}
if statusReady {
break
}
}
finish := time.Now()
elapsed = finish.Sub(start)
return elapsed, err
}
// CreateConfig Creates an orion-ptt-system kots config file from a local template. The template itself is not distributed with this package to avoid leaking sensitive information. To get one, you'll have to purchase an Orion PTT System license.
func (s *Stack) CreateConfig() (content string, err error) {
keyset, err := genkeyset.GenerateKeySet(3)
if err != nil {
err = errors.Wrapf(err, "failed to generate keyset")
return content, err
}
jsonbuf, err := json.Marshal(keyset)
if err != nil {
err = errors.Wrapf(err, "failed to marshall JWK KeySet into json")
return content, err
}
h, err := homedir.Dir()
if err != nil {
err = errors.Wrapf(err, "failed to detect homedir")
return content, err
}
templatePath := s.Config.ConfigTemplate
defaultPath := fmt.Sprintf("%s/%s", h, DEFAULT_TEMPLATE_FILE)
isS3, s3Meta := S3Url(templatePath)
// Look at the templatePath. If it's an s3 url, fetch it, and stick it in the default location
if isS3 {
fmt.Printf("Fetching config template from S3.\n")
err = FetchFileS3(s3Meta, defaultPath)
if err != nil {
err = errors.Wrapf(err, "failed to fetch template from %s", templatePath)
return content, err
}
templatePath = defaultPath
} else if isGit(templatePath) {
repo, path := SplitRepoPath(templatePath)
fmt.Printf("pulling templates from git. Repo: %s Path: %s\n", repo, path)
gitContent, err := GitContent(repo, path)
if err != nil {
err = errors.Wrapf(err, "error cloning %s", repo)
return content, err
}
err = ioutil.WriteFile(defaultPath, gitContent, 0644)
if err != nil {
err = errors.Wrapf(err, "failed to write file to %s", defaultPath)
return content, err
}
templatePath = defaultPath
} else {
fmt.Printf("Using local config template file %s.\n", templatePath)
}
// read template from local file, which might have been written by us, or might have been placed there manually . Either way we don't really care. It's just a file at this point.
tmplBytes, err := ioutil.ReadFile(templatePath)
if err != nil {
err = errors.Wrapf(err, "failed reading template file %q", templatePath)
return content, err
}
tmpl, err := template.New("stack config").Parse(string(tmplBytes))
if err != nil {
err = errors.Wrapf(err, "failed to create template")
}
contentBytes := make([]byte, 0)
buf := bytes.NewBuffer(contentBytes)
data := OnpremConfig{
Keystore: string(jsonbuf),
StackName: s.Config.StackName,
Domain: s.Config.DNSDomain,
}
err = tmpl.Execute(buf, data)
if err != nil {
err = errors.Wrapf(err, "failed to execute template")
return content, err
}
content = buf.String()
return content, err
}
// FetchFileS3 fetches the config template from an s3 url.
func FetchFileS3(s3Meta S3Meta, filePath string) (err error) {
awsSession, err := DefaultSession()
if err != nil {
err = errors.Wrapf(err, "failed to create s3 session")
return err
}
downloader := s3manager.NewDownloader(awsSession)
downloadOptions := &s3.GetObjectInput{
Bucket: aws.String(s3Meta.Bucket),
Key: aws.String(s3Meta.Key),
}
outFile, err := os.Create(filePath)
if err != nil {
err = errors.Wrapf(err, "failed creating file %s", filePath)
return err
}
defer outFile.Close()
_, err = downloader.Download(outFile, downloadOptions)
if err != nil {
err = errors.Wrapf(err, "download failed")
return err
}
return err
}
// S3Meta a struct for holding metadata for S3 Objects. There's probably already a struct that holds this, but this is all I need.
type S3Meta struct {
Bucket string
Region string
Key string
Url string
}
// S3Url returns true, and a metadata struct if the url given appears to be in s3
func S3Url(url string) (ok bool, meta S3Meta) {
// Check to see if it's an s3 URL.
s3Url := regexp.MustCompile(`https?://(.*)\.s3\.(.*)\.amazonaws.com/?(.*)?`)
matches := s3Url.FindAllStringSubmatch(url, -1)
if len(matches) == 0 {
return ok, meta
}
match := matches[0]
if len(match) == 3 {
meta = S3Meta{
Bucket: match[1],
Region: match[2],
Url: url,
}
ok = true
return ok, meta
} else if len(match) == 4 {
meta = S3Meta{
Bucket: match[1],
Region: match[2],
Key: match[3],
Url: url,
}
ok = true
return ok, meta
}
return ok, meta
}
// StringInSlice returns true if the given string is in the given slice
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}