-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainscript.go
More file actions
382 lines (343 loc) · 12.9 KB
/
Copy pathmainscript.go
File metadata and controls
382 lines (343 loc) · 12.9 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package main
import (
"bufio"
"bytes"
"fmt"
"github.com/joho/godotenv"
"github.com/urfave/cli"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"strings"
)
// toDO create bash aliases: fill ~/.bash_aliases. Restart bash with "$ . ~/.bashrc"
// toDo refactor for posibility of using with composer.json ????
var app = cli.NewApp()
func commands() {
app.Commands = []cli.Command{
//{
// Name: "init", //toDo create init command for fill .env by command script
// Usage: "Write env variables if you don't have it",
// Action: func(c *cli.Context) {},
//},
{
Name: "create_project",
Aliases: []string{"cp"},
Usage: "Creates Project. Creates file docker-compose.yml with needed containers which you choose in .env file. WARNING! Your changes in docker-compose.yml will be overrided!",
Action: func(c *cli.Context) {
// toDO: заполнить .env или пропустить шаг
// warning
reader := bufio.NewReader(os.Stdin)
// toDo: проверить есть ли файлы, и спрашивать только если они есть
fmt.Print("Your changes in docker-compose.yml will be lost. Continue? (y/n) ")
text, _ := reader.ReadString('\n')
if string(text[0]) != "y" {
fmt.Println("Aborted.")
os.Exit(1)
}
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
abspath := os.Getenv("DEPLOY_LOCAL_DOCKER_PATH")
// create config file
var fileCompose bytes.Buffer
// add start part to config file
// todo: исправить текст ошибки, указать что надо проверить путь в .env
fileStart, err := ioutil.ReadFile(fmt.Sprintf("%s/internal/config/start", abspath))
if err != nil {
log.Fatal(fmt.Sprintf("Error loading start part of file from %s/internal/config/start. Aborted.", abspath))
}
fileCompose.Write(fileStart)
fileCompose.Write([]byte("\n\n"))
// add nginx_php
fileNginx, err := ioutil.ReadFile(fmt.Sprintf("%s/internal/config/nginx_php", abspath))
if err != nil {
log.Fatal(fmt.Sprintf("Error loading nginx file from %s/internal/config/nginx_php. Aborted.", abspath))
}
fileCompose.Write(fileNginx)
fileCompose.Write([]byte("\n\n"))
// add config files for nginx_php
_ = os.Mkdir(fmt.Sprintf("%s/nginx_php", abspath), 0755)
_ = os.Mkdir(fmt.Sprintf("%s/nginx_php/config", abspath), 0755)
_ = os.Mkdir(fmt.Sprintf("%s/database", abspath), 0755)
file, _ := ioutil.ReadFile(fmt.Sprintf("%s/internal/config/modules/trafex_php_nginx/Dockerfile", abspath))
ioutil.WriteFile(fmt.Sprintf("%s/nginx_php/Dockerfile", abspath), file, 0644)
file, _ = ioutil.ReadFile(fmt.Sprintf("%s/internal/config/modules/trafex_php_nginx/config/fpm-pool.conf", abspath))
ioutil.WriteFile(fmt.Sprintf("%s/nginx_php/config/fpm-pool.conf", abspath), file, 0644)
file, _ = ioutil.ReadFile(fmt.Sprintf("%s/internal/config/modules/trafex_php_nginx/config/nginx.conf", abspath))
ioutil.WriteFile(fmt.Sprintf("%s/nginx_php/config/nginx.conf", abspath), file, 0644)
file, _ = ioutil.ReadFile(fmt.Sprintf("%s/internal/config/modules/trafex_php_nginx/config/php.ini", abspath))
ioutil.WriteFile(fmt.Sprintf("%s/nginx_php/config/php.ini", abspath), file, 0644)
file, _ = ioutil.ReadFile(fmt.Sprintf("%s/internal/config/modules/trafex_php_nginx/config/supervisord.conf", abspath))
ioutil.WriteFile(fmt.Sprintf("%s/nginx_php/config/supervisord.conf", abspath), file, 0644)
// Add framework/css nginx site config:
// question to user
reader = bufio.NewReader(os.Stdin)
fmt.Print("\nPlease write needed framework/cms nginx config name, or leave it empty for default. \n(Available list: ")
files, _ := ioutil.ReadDir(fmt.Sprintf("%s/internal/config/modules/nginx/sites_conf", abspath))
for _, filesite := range files {
fmt.Print(filesite.Name() + ", ")
}
fmt.Print("):")
// read answer
text, _ = reader.ReadString('\n')
// delete \n in the string's end
text = strings.TrimSuffix(text, "\n")
// read nginx site config file. Chosen file or default if didn't get any choose from user
fileNginxConf := getfile(text)
filestring := string(fileNginxConf)
// vars replace in site.conf
filestring = strings.Replace(filestring, "${APPNAME}", os.Getenv("APPNAME"), -1)
filestring = strings.Replace(filestring, "${ENV}", os.Getenv("ENV"), -1)
filestring = strings.Replace(filestring, "${SITE_WORKDIR_IN_CONTAINER}", os.Getenv("SITE_WORKDIR_IN_CONTAINER"), -1)
// write site.conf
ioutil.WriteFile(fmt.Sprintf("%s/nginx_php/site.conf", abspath), []byte(filestring), 0644)
// add db
if strings.ToLower(os.Getenv("DB_DRIVER")) == "mysql" {
fileMySql, err := ioutil.ReadFile(fmt.Sprintf("%s/internal/config/mysql", abspath))
if err != nil {
log.Fatal(err)
}
fileCompose.Write(fileMySql)
} else {
// add file with the same name as lowercased DB_DRIVER value
fileDb, err := ioutil.ReadFile(fmt.Sprintf("%s/internal/config/%s", abspath, strings.ToLower(os.Getenv("DB_DRIVER"))))
//fileDb, err = checkTabs(fileDb)
if err != nil {
log.Fatal(err)
}
fileCompose.Write(fileDb)
}
fileCompose.Write([]byte("\n\n"))
// add other services which placed to OTHER_CONTAINERS ("space" delimitter. Example: OTHER_CONTAINERS=redis memcached phpmyadmin mailcatcher)
// script will search files with same name (lowercase) as container names in OTHER_CONTAINERS
if os.Getenv("OTHER_CONTAINERS") != "" {
files := strings.Split(os.Getenv("OTHER_CONTAINERS"), " ")
for _, file := range files {
file, err := ioutil.ReadFile(fmt.Sprintf("%s/internal/config/%s", abspath, strings.ToLower(file)))
if err != nil {
log.Fatal(err)
}
fileCompose.Write(file)
fileCompose.Write([]byte("\n\n"))
}
}
// save docker-compose.yml
err1 := ioutil.WriteFile(fmt.Sprintf("%s/docker-compose.yml", abspath), fileCompose.Bytes(), 0644)
if err1 != nil {
log.Fatal(err1)
}
fmt.Println("\nSuccess. \nSee docker-compose.yml for additional details.\nPHP config in \"php\" folder. Nginx config in \"nginx\" folder.")
},
},
{
Name: "list_container",
Aliases: []string{"ps"},
Usage: "Shows list of ALL runned containers",
Action: func(c *cli.Context) {
cmd := exec.Command("/bin/sh", "-c", "docker ps")
cmd.Stdout = os.Stdout
cmd.Run()
},
},
{
Name: "composer_inst",
Aliases: []string{"ci"},
Usage: "Usage: ci [path]. COMPOSER INSTALL. Default runs in PROJECT_ROOT. You can add another PATH with second argument. WARNING: Use only absolute path in container!",
Action: func(c *cli.Context) {
// get current user. It needed because in other case files after "composer install" will be owned by root:root
user, err := user.Current()
if err != nil {
panic(err)
}
// load .env
err = godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
if c.Args().First() == "" {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("docker exec -u %s:%s -i %s_nginx_php composer install", user.Uid, user.Gid, os.Getenv("APPNAME")))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
} else {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("docker exec -u %s:%s -w %s -i %s_nginx_php composer install", user.Uid, user.Gid, c.Args().First(), os.Getenv("APPNAME")))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
},
},
{
Name: "command",
Aliases: []string{"com"},
Usage: "Run command in container. Usage: com <container_name> \"<command>\". Few words command ONLY LIKE \"COMMAND NO ONE WORD\"! ",
Action: func(c *cli.Context) {
// load .env
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// get current user. It needed because in other case files after "composer install" will be owned by root:root
user, err := user.Current()
if err != nil {
panic(err)
}
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("docker exec -u %s:%s -i %s_nginx_php %s", user.Uid, user.Gid, os.Getenv("APPNAME"), c.Args().First()))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
},
},
{
Name: "stopall",
Aliases: []string{"st"},
Usage: "Stops ALL runned Docker containers",
Action: func(c *cli.Context) {
exec.Command("/bin/sh", "-c", "docker stop $(docker ps -aq)").Run()
fmt.Println("All containers stopped.")
fmt.Println("DOCKER PS:")
cmd := exec.Command("/bin/sh", "-c", "docker ps")
cmd.Stdout = os.Stdout
cmd.Run()
},
},
{
Name: "logs",
Aliases: []string{"lg"},
Usage: "Shows nginx/php error logs for this project.",
Action: func(c *cli.Context) {
// load .env
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("docker logs -f %s_nginx_php", os.Getenv("APPNAME")))
cmd.Stdout = os.Stdout
cmd.Run()
fmt.Print("\n")
},
},
{
Name: "dump_upload",
Aliases: []string{"du"},
Usage: "Dump Upload. Uploads sql dump to mysql container. Place your dump.sql file to ./database folder before running",
Action: func(c *cli.Context) {
// load .env
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("cat %s/database/dump.sql | docker exec -i %s_mysql /usr/bin/mysql -u %s --password=%s %s", os.Getenv("DEPLOY_LOCAL_DOCKER_PATH"), os.Getenv("APPNAME"), os.Getenv("SQL_USER"), os.Getenv("SQL_USER"), os.Getenv("APPNAME")))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
},
},
{
Name: "up",
Usage: "docker-compose up -d. Runs all configured containers for this project.",
Action: func(c *cli.Context) {
// load .env
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("docker-compose -f %s/docker-compose.yml up -d --build", os.Getenv("DEPLOY_LOCAL_DOCKER_PATH")))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
},
},
{
Name: "status",
Aliases: []string{"s"},
Usage: "Statistics about all running docker containers",
Action: func(c *cli.Context) {
cmd := exec.Command("/bin/sh", "-c", "docker stats")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
},
},
{
Name: "disk",
Aliases: []string{"d"},
Usage: "Statistics about disk usage by docker",
Action: func(c *cli.Context) {
cmd := exec.Command("/bin/sh", "-c", "docker system df")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
},
},
{
Name: "detstat",
Aliases: []string{"ds"},
Usage: "Detail statistics about all docker containers, images, volumes on host machine",
Action: func(c *cli.Context) {
cmd := exec.Command("/bin/sh", "-c", "docker system df -v")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
},
},
{
Name: "resetall",
Usage: "Resets all docker files to start state. If you want to delete mysql database files, you have run this script with SUDO rights!!!",
Action: func(c *cli.Context) {
// load .env
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
fmt.Println("Stop all containers")
cmd := exec.Command("/bin/sh", "-c", "docker stop $(docker ps -aq)")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
ap := os.Getenv("DEPLOY_LOCAL_DOCKER_PATH")
cmd = exec.Command("/bin/sh", "-c", fmt.Sprintf("rm -vrf %s/database %s/nginx_php %s/docker-compose.yml", ap, ap, ap))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
},
},
}
}
func main() {
// good PHP Dockerfile (alpine based) for Symfony https://github.com/eko/docker-symfony
// todo: сделать проверку отступов в конфигах контейнеров: (split на строки и проверка первых символов)
// override --help message
cli.AppHelpTemplate = `
USAGE:
mainscript (Rename filename to short name for fast usage!) <command>
{{if .Commands}}
COMMANDS:
{{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}
`
commands()
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func getfile(text string) string {
ap := os.Getenv("DEPLOY_LOCAL_DOCKER_PATH")
if text == "" {
file, err := ioutil.ReadFile(fmt.Sprintf("%s/internal/config/modules/nginx/site.conf", ap))
if err != nil {
log.Fatal(fmt.Sprintf("Error loading file with entered name: %s", text))
}
return string(file)
}
path := fmt.Sprintf("%s/internal/config/modules/nginx/sites_conf/%s", ap, text)
file, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(fmt.Sprintf("Error loading file with entered name: %s", text))
}
return string(file)
}