-
Notifications
You must be signed in to change notification settings - Fork 4
/
helper.go
226 lines (183 loc) · 4.23 KB
/
helper.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
package aws
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/appscode/go/crypto/rand"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/docker/docker/pkg/archive"
"github.com/mholt/archiver"
)
func lastline(data []byte) string {
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
return lines[len(lines)-1]
}
func generateId(prefix string, size int) string {
return prefix + "-" + rand.Characters(size)
}
func stackParameters(stack *cloudformation.Stack) map[string]string {
parameters := make(map[string]string)
for _, parameter := range stack.Parameters {
parameters[*parameter.ParameterKey] = *parameter.ParameterValue
}
return parameters
}
func stackOutputs(stack *cloudformation.Stack) map[string]string {
outputs := make(map[string]string)
for _, output := range stack.Outputs {
outputs[*output.OutputKey] = *output.OutputValue
}
return outputs
}
func coalesce(s *dynamodb.AttributeValue, def string) string {
if s != nil {
return *s.S
} else {
return def
}
}
func coalesceInt(s *dynamodb.AttributeValue, def int) int {
if s != nil {
num, _ := strconv.Atoi(*s.N)
return num
} else {
return def
}
}
func coalesceBytes(s *dynamodb.AttributeValue) (data []byte) {
if s != nil {
return s.B
} else {
return data
}
}
func camelize(dasherized string) string {
tokens := strings.Split(dasherized, "-")
for i, token := range tokens {
switch token {
case "az":
tokens[i] = "AZ"
default:
tokens[i] = strings.Title(token)
}
}
return strings.Join(tokens, "")
}
func cfParams(source map[string]string) map[string]string {
params := make(map[string]string)
for key, value := range source {
var val string
switch value {
case "":
val = "false"
case "true":
val = "true"
default:
val = value
}
params[camelize(key)] = val
}
return params
}
func timestampNow() int64 {
return time.Now().Unix()
}
func convertGzipToZip(app, src string) (string, error) {
dir, err := ioutil.TempDir("", app+"-")
if err != nil {
return dir, err
}
log.Debugf("untar %s to %s", src, dir)
if err = untarPath(src, dir); err != nil {
return dir, err
}
fileInfos, err := ioutil.ReadDir(dir)
if err != nil {
return dir, fmt.Errorf("listing files: %v", err)
}
files := make([]string, len(fileInfos))
for i, f := range fileInfos {
files[i] = filepath.Join(dir, f.Name())
}
zipPath := dir + ".zip"
if err := archiver.Zip.Make(zipPath, files); err != nil {
return dir, fmt.Errorf("creating a zip archive err: %v", err)
}
return zipPath, nil
}
func buildTemplate(name, section string, data interface{}) (string, error) {
d, err := Asset(fmt.Sprintf("cloud/aws/templates/%s.tmpl", name))
if err != nil {
return "", err
}
tmpl, err := template.New(section).Funcs(templateHelpers()).Parse(string(d))
if err != nil {
return "", err
}
var formation bytes.Buffer
err = tmpl.Execute(&formation, data)
if err != nil {
return "", err
}
return formation.String(), nil
}
func untarPath(src, dst string) error {
fd, err := os.Open(src)
if err != nil {
return err
}
defer fd.Close()
defaultArchiver := archive.Archiver{Untar: archive.Untar, UIDMaps: nil, GIDMaps: nil}
return defaultArchiver.Untar(fd, dst, &archive.TarOptions{NoLchown: true})
}
func toJson(object interface{}) string {
dump, err := json.MarshalIndent(object, " ", " ")
if err != nil {
log.Fatal(fmt.Errorf("dumping json: %v", err))
}
return string(dump)
}
func templateHelpers() template.FuncMap {
return template.FuncMap{
"env": func(s string) string {
return os.Getenv(s)
},
"upper": func(s string) string {
return upperName(s)
},
"value": func(s string) template.HTML {
return template.HTML(fmt.Sprintf("%q", s))
},
}
}
func upperName(name string) string {
// myapp -> Myapp; my-app -> MyApp
us := strings.ToUpper(name[0:1]) + name[1:]
for {
i := strings.Index(us, "-")
if i == -1 {
break
}
s := us[0:i]
if len(us) > i+1 {
s += strings.ToUpper(us[i+1 : i+2])
}
if len(us) > i+2 {
s += us[i+2:]
}
us = s
}
return us
}
func generatePassword() string {
return rand.GeneratePassword()
}