-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomContainerizingKuberizer.go
More file actions
301 lines (272 loc) · 13.8 KB
/
Copy pathCustomContainerizingKuberizer.go
File metadata and controls
301 lines (272 loc) · 13.8 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strings"
)
func main() {
//Pull JSON And Store It In A Variable
url := "https://swapi.co/api/people"
req, _ := http.NewRequest("GET", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
//Parse Through JSON
var character Character
json.Unmarshal(body, &character)
people := character.Results
//Create the dockerAuto Directory Tree and Building It IF It Doesn't Exist.
dockerAutoPath := "/opt/dockerAuto"
dockerBaseImagePath := "/opt/dockerAuto/dockerBaseImage"
dockerCustomImagePath_ROOT := "/opt/dockerAuto/dockerGenImages"
//dockerApplicationPATH := "/opt/dockerAppliactions"
if _, err := os.Stat(dockerAutoPath); os.IsNotExist(err) {
os.Mkdir(dockerAutoPath, 0764)
os.Chmod(dockerAutoPath, 0764)
if _, err := os.Stat(dockerCustomImagePath_ROOT); os.IsNotExist(err) {
os.Mkdir(dockerCustomImagePath_ROOT, 0764)
os.Chmod(dockerCustomImagePath_ROOT, 0764)
}
if _, err := os.Stat(dockerBaseImagePath); os.IsNotExist(err) {
os.Mkdir(dockerBaseImagePath, 0764)
os.Chmod(dockerBaseImagePath, 0764)
}
} else if _, err := os.Stat(dockerCustomImagePath_ROOT); os.IsNotExist(err) {
os.Mkdir(dockerCustomImagePath_ROOT, 0764)
os.Chmod(dockerCustomImagePath_ROOT, 0764)
} else if _, err := os.Stat(dockerBaseImagePath); os.IsNotExist(err) {
os.Mkdir(dockerBaseImagePath, 0764)
os.Chmod(dockerBaseImagePath, 0764)
}
//List Of Illegal Characters For Variables.(Allows Spaces)
//Use formatedJsonNameNOSpaces On Variable Directly
badCharacterList := []string{"(", ")", `\`, `\`, ",", ".", "!", "@", "#", "$", "%", "^", "*", "+", "=", "[", "]", ";",
`:`, "<", ">", "{", "}", "\\", "?", "/", "|", "`", "~", "_"}
//Change To dockerBaseImagePath Directory.
if err := os.Chdir(dockerBaseImagePath); err != nil {
panic(err)
}
//Creating Base Dockerfile from Ubuntu Image and Customizing.
baseImageDockerFile, err := os.Create("Dockerfile")
os.Chmod("Dockerfile", 0764)
if err != nil {
log.Fatal("Cannot create Dockerfile", err)
}
defer baseImageDockerFile.Close()
//Creating Base Dockerfile from Ubuntu Image and Customizing.
//Name Must Stay Dockerfile (docker syntax rule).
fmt.Fprintln(baseImageDockerFile, "FROM ubuntu")
fmt.Fprintln(baseImageDockerFile, "RUN apt update -y && apt upgrade -y")
fmt.Fprintln(baseImageDockerFile, "RUN apt install golang-go -y")
fmt.Fprintln(baseImageDockerFile, "RUN apt install nano -y")
fmt.Fprintln(baseImageDockerFile, "ENV TESTVAR1='This is an ENV var from dockerfile!'")
baseImageDockerFile.Close()
//Changing to the generated base Image directory.
if err := os.Chdir(dockerBaseImagePath); err != nil {
panic(err)
}
dockerBaseImageNAME := "dockerbase-image"
dockerBaseImageVERSION := "v1"
//Creating a bash script that runs the docker commands to make the image.
bashBaseImageBuildScript, err := os.Create("bashBaseImageBuildScript.sh")
os.Chmod("bashBaseImageBuildScript.sh", 0764)
if err != nil {
log.Fatal("Cannot create bashBaseImageBuildScript.sh", err)
}
defer baseImageDockerFile.Close()
//Creating a bash script that runs the docker commands to make the image.
fmt.Fprintln(bashBaseImageBuildScript, "#!/bin/bash")
fmt.Fprintln(bashBaseImageBuildScript, "docker build -t"+dockerBaseImageNAME+":"+dockerBaseImageVERSION, ".")
fmt.Fprintln(bashBaseImageBuildScript, "exit 0")
bashBaseImageBuildScript.Close()
//Executing a bash script that runs the docker commands to make the image.
BASHScriptEXE(dockerBaseImagePath + "/bashBaseImageBuildScript.sh")
//Change To /opt/dockerTesting/dockerGenImages Directory.
if err := os.Chdir(dockerCustomImagePath_ROOT); err != nil {
panic(err)
}
//Loop Through JSON
for i := 0; i < len(people); i++ {
//Create Variables From JSON
// Using Name Method From Custom Type
jsonName := people[i].Name
//Removing Illegal Characters From Variable
for _, badCharacter := range badCharacterList {
jsonName = strings.Replace(jsonName, badCharacter, "", -1)
}
//Stores Data That Doesn't Contain Illegal Characters, But Allows Spaces.
formatedJsonNameSpaces := strings.Title(jsonName)
//Stores Data That Doesn't Contain Illegal Characters, But DOES NOT Allow Spaces.
formatedJsonNameNOSpaces := strings.Replace(formatedJsonNameSpaces, " ", "", -1)
//##################################################################################################################
// VARIABLES BEING GENERATED START
//##################################################################################################################
//password := "SYS"+formatedJsonNameNOSpaces+"admiN"
watchman_group_name := formatedJsonNameSpaces
namesync := formatedJsonNameNOSpaces
namesyncLower := strings.ToLower(namesync)
//##################################################################################################################
// VARIABLES BEING GENERATED END
//##################################################################################################################
if err := os.Chdir("/opt/dockerAuto/dockerGenImages"); err != nil {
panic(err)
}
//Creating The Root Directory Fot The Generated Custom Images
dockerCustomImagePath_IMAGES := dockerCustomImagePath_ROOT + "/" + namesyncLower
if _, err := os.Stat(dockerCustomImagePath_IMAGES); os.IsNotExist(err) {
os.Mkdir(dockerCustomImagePath_IMAGES, 0764)
os.Chmod(dockerCustomImagePath_IMAGES, 0764)
}
//Change To Directory Were Custom Images Will Be Stored.
if err := os.Chdir(dockerCustomImagePath_IMAGES); err != nil {
panic(err)
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
dockerApplication_goServer, err := os.Create("goServer2.go")
os.Chmod("goServer2.go", 0764)
if err != nil {
log.Fatal("Cannot create goServer2.go", err)
}
defer dockerApplication_goServer.Close()
fmt.Fprintln(dockerApplication_goServer, "package main")
fmt.Fprintln(dockerApplication_goServer, "import (")
fmt.Fprintln(dockerApplication_goServer, `"fmt"`)
fmt.Fprintln(dockerApplication_goServer, `"net/http"`)
fmt.Fprintln(dockerApplication_goServer, ")")
fmt.Fprintln(dockerApplication_goServer, "func main() {")
fmt.Fprintln(dockerApplication_goServer, "http.HandleFunc(`/`, func(w http.ResponseWriter, r *http.Request) {")
fmt.Fprintln(dockerApplication_goServer, "fmt.Fprint(w, `")
fmt.Fprintln(dockerApplication_goServer, "<!doctype html>")
fmt.Fprintln(dockerApplication_goServer, "<html lang='en'>")
fmt.Fprintln(dockerApplication_goServer, "<head>")
fmt.Fprintln(dockerApplication_goServer, "<meta charset='utf-8'>")
fmt.Fprintln(dockerApplication_goServer, "<meta name='viewport' content='width=device-width, initial-scale=1'>")
fmt.Fprintln(dockerApplication_goServer, "<title>"+watchman_group_name, "MicroMDM</title>")
fmt.Fprintln(dockerApplication_goServer, "<style>")
fmt.Fprintln(dockerApplication_goServer, "body {")
fmt.Fprintln(dockerApplication_goServer, "font-family: -apple-system, BlinkMacSystemFont, sans-serif;")
fmt.Fprintln(dockerApplication_goServer, "}")
fmt.Fprintln(dockerApplication_goServer, "</style>")
fmt.Fprintln(dockerApplication_goServer, "</head>")
fmt.Fprintln(dockerApplication_goServer, "<body>")
fmt.Fprintln(dockerApplication_goServer, "<h3>Welcome to", watchman_group_name, "MicroMDM!</h3>")
fmt.Fprintln(dockerApplication_goServer, "<h1 style='background-color:rgb(0, 255, 255);'>"+watchman_group_name, "MicroMDM</h1>")
fmt.Fprintln(dockerApplication_goServer, "<img class='right' src='https://657cea1304d5d92ee105-33ee89321dddef28209b83f19f06774f.ssl.cf1.rackcdn.com/gophercloud-edf0a107430a35b63fae80ea5d465fe648e194637e78f52455482f49c543769d.png'>")
fmt.Fprintln(dockerApplication_goServer, "</body>")
fmt.Fprintln(dockerApplication_goServer, "<h1>Doing Stuff In The Cloud ;D</h1>")
fmt.Fprintln(dockerApplication_goServer, "</html>`)")
fmt.Fprintln(dockerApplication_goServer, "})")
fmt.Fprintln(dockerApplication_goServer, "fmt.Println(http.ListenAndServe(`:8080`, nil))")
fmt.Fprintln(dockerApplication_goServer, "}")
dockerApplication_goServer.Close()
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Creating Custon Dockerfile file from JSON DATA.
//Name Must be Dockerfile (Docker syntax rule).
customImageDockerFile, err := os.Create("Dockerfile")
os.Chmod("Dockerfile", 0764)
if err != nil {
log.Fatal("Cannot create Dockerfile", err)
}
defer customImageDockerFile.Close()
//Creating Custom Docker File From Json Data.
fmt.Fprintln(customImageDockerFile, "FROM", dockerBaseImageNAME+":"+dockerBaseImageVERSION)
fmt.Fprintln(customImageDockerFile, "RUN mkdir /app")
fmt.Fprintln(customImageDockerFile, "COPY goServer2.go /app/goServer2.go")
fmt.Fprintln(customImageDockerFile, "WORKDIR /app")
fmt.Fprintln(customImageDockerFile, `CMD ["go", "run", "goServer2.go"]`)
customImageDockerFile.Close()
dockerCustomImageNAME := namesyncLower + "-image"
dockerCustomImageVERSION := "v2"
bashCustomImageBuildScript, err := os.Create("bashCustomImageBuildScript.sh")
os.Chmod("bashCustomImageBuildScript.sh", 0764)
if err != nil {
log.Fatal("Cannot create bashCustomImageBuildScript.sh", err)
}
defer bashCustomImageBuildScript.Close()
//Creating a bash script that runs the docker commands to make the image.
fmt.Fprintln(bashCustomImageBuildScript, "#!/bin/bash")
fmt.Fprintln(bashCustomImageBuildScript, "docker build -t"+dockerCustomImageNAME+":"+dockerCustomImageVERSION, ".")
fmt.Fprintln(bashCustomImageBuildScript, "exit 0")
bashCustomImageBuildScript.Close()
//Executing a bash script that runs the docker commands to make the image.
BASHScriptEXE(dockerCustomImagePath_IMAGES + "/bashCustomImageBuildScript.sh")
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Building Cluster
gcloudProject := "terraform-gcloud4"
clusterName := namesyncLower + "-cluster"
clusterZone := "us-central1-a"
externalPort := "8080"
containerAppPort := "8080"
bashKuberizeScript, err := os.Create("bashKuberizeScript.sh")
os.Chmod("bashKuberizeScript.sh", 0764)
if err != nil {
log.Fatal("Cannot create bashKuberizeScript.sh", err)
}
defer bashKuberizeScript.Close()
//Creating a bash script that runs the docker commands to make the image.
fmt.Fprintln(bashKuberizeScript, "#!/bin/bash")
fmt.Fprintln(bashKuberizeScript, `gcloud container clusters create`, clusterName, `--zone`, clusterZone)
fmt.Fprintln(bashKuberizeScript, `docker tag`, dockerCustomImageNAME+":"+dockerCustomImageVERSION, `gcr.io/`+gcloudProject+`/`+dockerCustomImageNAME+":"+dockerCustomImageVERSION)
fmt.Fprintln(bashKuberizeScript, `docker push gcr.io/`+gcloudProject+`/`+dockerCustomImageNAME+":"+dockerCustomImageVERSION)
fmt.Fprintln(bashKuberizeScript, `kubectl run`, clusterName, `--image=gcr.io/`+gcloudProject+`/`+dockerCustomImageNAME+":"+dockerCustomImageVERSION)
fmt.Fprintln(bashKuberizeScript, `kubectl expose deployment`, clusterName, `--type LoadBalancer --port`, externalPort, `--target-port`, containerAppPort)
fmt.Fprintln(bashKuberizeScript, "exit 0")
bashKuberizeScript.Close()
//Executing a bash script that runs the docker commands to make the image.
BASHScriptEXE(dockerCustomImagePath_IMAGES + "/bashKuberizeScript.sh")
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
func BASHScriptEXE(bashScript string) {
//BASHScriptEXE executes shell scripts, WITHOUT arguments.
//The Job WILL FAIL IF YOU ADD Them!
//EX. /Users/prestongoode/Documents/testFrom.sh.
//This Will Execute The Script At The End of The Path.
//YOU MUST SPECIFY THE ENTIRE PATH TO THE SCRIPT, INCLUDING THE SCRIPT!!
cmd := exec.Command(bashScript)
cmd.Stdin = strings.NewReader("")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
fmt.Printf(out.String())
}
//JSON Struct For Unmarshal
type Characters struct {
Characters []Character `json:"users"`
}
type Character struct {
Count int `json:"count"`
Next string `json:"next"`
Previous interface{} `json:"previous"`
Results []struct {
Name string `json:"name"`
Height string `json:"height"`
Mass string `json:"mass"`
HairColor string `json:"hair_color"`
SkinColor string `json:"skin_color"`
EyeColor string `json:"eye_color"`
BirthYear string `json:"birth_year"`
Gender string `json:"gender"`
Homeworld string `json:"homeworld"`
Films []string `json:"films"`
Species []string `json:"species"`
Vehicles []string `json:"vehicles"`
Starships []string `json:"starships"`
//Created time.Time `json:"created"`
//Edited time.Time `json:"edited"`
URL string `json:"url"`
} `json:"results"`
}