-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
231 lines (205 loc) · 6.45 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path"
"strings"
"github.com/tidwall/gjson"
)
var (
// Variables gotten from Environment
bamboo_buildNumber = os.Getenv("bamboo_buildNumber")
bamboo_CONSUL_URL = os.Getenv("bamboo_CONSUL_URL")
bamboo_deploy_release = os.Getenv("bamboo_deploy_release")
bamboo_NEW_RELIC_API_URL = os.Getenv("bamboo_NEW_RELIC_API_URL")
cluster_ip = os.Getenv("cluster_ip")
CONSUL_APPLICATION = os.Getenv("bamboo_CONSUL_APPLICATION")
CONSUL_ENVIRONMENT = os.Getenv("bamboo_CONSUL_ENVIRONMENT")
DEPLOYMENT_DATACENTER = os.Getenv("bamboo_DEPLOYMENT_DATACENTER")
CONSUL_PASSWORD = os.Getenv("bamboo_CONSUL_PASSWORD")
CONSUL_USERNAME = os.Getenv("bamboo_CONSUL_USERNAME")
NEW_RELIC_API_KEY_PASSWORD = os.Getenv("bamboo_NEW_RELIC_API_KEY_PASSWORD")
NEW_RELIC_LICENSE_KEY = os.Getenv("bamboo_NEW_RELIC_LICENSE_KEY_PASSWORD")
NEW_RELIC_ADMIN_KEY_PASSWORD = os.Getenv("bamboo_NEW_RELIC_ADMIN_KEY_PASSWORD")
ssh_key = os.Getenv("ssh_key")
bamboo_AWS_HOSTNAME = os.Getenv("bamboo_AWS_HOSTNAME")
// Static configs
application_name string
build_id string
CONSUL_FULL_URL *url.URL
CONSUL_URL *url.URL
deploy_build string
deploy_namespace string
git_repo *url.URL
hostnames []string
M_ALL bool
M_AUTOSCALER bool
M_CLINIC bool
M_DEPLOY bool
M_GERNICSERVICE bool
M_INGRESS bool
M_CRONJOB bool
M_SERVICE bool
NEW_RELIC_API_URL *url.URL
O_FILENAME string
O_LIMIT string
O_OUTPUT string
clinic_name string
clinic_hostname string
D_HOSTNAMES string
)
func checkErr(err error) {
if err != nil {
log.Printf("WARN: %#v", err)
}
}
func init() {
var err error
CONSUL_URL, err = url.Parse(bamboo_CONSUL_URL)
NEW_RELIC_API_URL, err = url.Parse(bamboo_NEW_RELIC_API_URL)
if CONSUL_URL.Host != "" {
CONSUL_FULL_URL, err = url.Parse(fmt.Sprintf("%s://%s:%s@%s/", CONSUL_URL.Scheme, CONSUL_USERNAME, CONSUL_PASSWORD, CONSUL_URL.Host))
}
git_repo, err = url.Parse(os.Getenv("git_repo"))
_ = err
_ = git_repo
_ = NEW_RELIC_API_URL
_ = CONSUL_FULL_URL
flag.BoolVar(&M_ALL, "all", false, "Outputs deploymen, service, autoscaler and ingress")
flag.BoolVar(&M_AUTOSCALER, "autoscaler", false, "Create autoscaler")
flag.BoolVar(&M_DEPLOY, "deploy", false, "Create deployments")
flag.BoolVar(&M_INGRESS, "ingress", false, "Create ingress rules")
flag.BoolVar(&M_CRONJOB, "cronjob", false, "Create cronjob rules")
flag.BoolVar(&M_SERVICE, "service", false, "Create services")
flag.BoolVar(&M_GERNICSERVICE, "genericservice", false, "Create generic services")
flag.StringVar(&build_id, "build_id", "", "build_id from bamboo")
flag.StringVar(&clinic_hostname, "clinic_hostname", "", "Clinic hostname")
flag.StringVar(&clinic_name, "clinic_name", "", "Clinic name")
flag.StringVar(&D_HOSTNAMES, "hostname", "", "Hostnames for ingress. comma separated")
flag.StringVar(&deploy_namespace, "namespace", "", "namespace")
flag.StringVar(&O_FILENAME, "file", "", "Filename to parse")
flag.StringVar(&O_LIMIT, "limit", "", "Limit the run to certain app name")
flag.StringVar(&O_OUTPUT, "output", "./", "Output folder")
flag.Parse()
if M_ALL {
M_DEPLOY = true
M_SERVICE = true
M_GERNICSERVICE = true
M_AUTOSCALER = true
M_INGRESS = true
M_CRONJOB = true
}
if clinic_hostname != "" {
M_CLINIC = true
}
}
func CreateFH(Filename string) (fp *os.File) {
PFilename := fmt.Sprintf("%s/%s", path.Clean(O_OUTPUT), Filename)
fp, err := os.OpenFile(PFilename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("ERROR: create file: ", err)
os.Exit(1)
}
return fp
}
func Check_if_limit(AppObj App) bool {
if O_LIMIT != "" {
if AppObj.Name == O_LIMIT {
return true
} else {
return false
}
} else {
return true
}
}
func Check_if_limit_cronjob(CronjobObj Cronjob) bool {
if O_LIMIT != "" {
if CronjobObj.Name == O_LIMIT {
return true
} else {
return false
}
} else {
return true
}
}
func main() {
if M_CLINIC {
createClinic(clinic_hostname)
}
if O_FILENAME != "" {
hostnames = strings.Split(D_HOSTNAMES, ",")
if build_id == "" || deploy_namespace == "" {
log.Fatalf("Missing CMD line options build (\"%s\"), or namespace (\"%s\")", build_id, deploy_namespace)
os.Exit(1)
}
file, err := ioutil.ReadFile(O_FILENAME)
if err != nil {
log.Fatalf("ERROR: File error: %v\n", err)
os.Exit(1)
}
//Get the application name from Json object
application_name = gjson.GetBytes(file, "application").Str
// Do cronjob section
Cronjobs := gjson.GetBytes(file, "cronjobs")
if Cronjobs.Index == 0 {
log.Printf("No cronjobs found in service definition file")
}
Cronjobs.ForEach(func(key, value gjson.Result) bool {
var CronjobObj Cronjob
err := json.Unmarshal([]byte(value.String()), &CronjobObj)
if err != nil {
fmt.Fprintf(os.Stderr, "Json Error: %s\n", err)
os.Exit(1)
} else if Check_if_limit_cronjob(CronjobObj) {
if M_CRONJOB {
createCronjob(CronjobObj)
}
}
return true // keep iterating
})
// Do services section
Services := gjson.GetBytes(file, "services")
if Services.Index == 0 {
log.Fatal("ERROR: Json decode error, no services found\n")
os.Exit(1)
}
Services.ForEach(func(key, value gjson.Result) bool {
var AppObj App
err := json.Unmarshal([]byte(value.String()), &AppObj)
if err != nil {
fmt.Fprintf(os.Stderr, "Json Error: %s\n", err)
os.Exit(1)
} else if Check_if_limit(AppObj) {
if M_DEPLOY {
createDeploy(AppObj)
}
if M_SERVICE {
createService(AppObj)
}
if M_GERNICSERVICE {
createGenericService(AppObj)
}
// Set a default
if AppObj.Scalability.TargetCPUUtilizationPercentage < 1 {
AppObj.Scalability.TargetCPUUtilizationPercentage = 70
}
if M_AUTOSCALER {
if AppObj.Resources.Requests.Cpu != "" && AppObj.Scalability.MinReplicas > 0 && AppObj.Scalability.MaxReplicas > 1 && AppObj.Scalability.TargetCPUUtilizationPercentage > 0 {
createAutoScaler(AppObj)
}
}
if M_INGRESS {
createIngress(AppObj)
}
}
return true // keep iterating
})
} // end if O_FILENAME
} // end main