-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
336 lines (253 loc) · 8.68 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
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
package main
import (
"fmt"
"os"
"regexp"
"sort"
"webup/pliz/actions"
"webup/pliz/config"
"webup/pliz/domain"
"webup/pliz/utils"
"github.com/Songmu/prompter"
"github.com/fatih/color"
cli "github.com/jawher/mow.cli"
)
func main() {
app := cli.App("pliz", "Manage projects building")
app.Version("v version", "Pliz 15")
// option to change the Pliz env
plizEnv := app.String(cli.StringOpt{
Name: "env",
Value: "",
Desc: "Change the environnment of Pliz (i.e. 'prod'). The environment var 'PLIZ_ENV' can be use too.",
})
prod := false
var executionContext domain.ExecutionContext
app.Before = func() {
// Parse and check config
parseAndCheckConfig()
// check for env
if *plizEnv == "prod" || os.Getenv("PLIZ_ENV") == "prod" {
prod = true
}
executionContext = domain.ExecutionContext{Env: *plizEnv}
}
app.Command("start", "Start (or restart) the project", func(cmd *cli.Cmd) {
cmd.Action = func() {
actions.StartActionHandler(prod, true)
if !prod {
// display access infos
containerID, _ := utils.GetContainerID(config.Get().StartupContainer, executionContext)
ports := utils.GetExposedPorts(containerID, executionContext)
if len(ports) > 0 {
// get ip from DOCKER_HOST env variable
rgexp := regexp.MustCompile("(\\d{1,3}(?:\\.\\d{1,3}){3})")
ip := rgexp.FindString(os.Getenv("DOCKER_HOST"))
if ip == "" {
ip = "localhost"
}
fmt.Printf("\nYour app is accessible using:\n")
for _, port := range ports {
color.Green(" http://%s:%s", ip, port)
}
} else {
fmt.Printf("\n%s: The proxy doesn't seem to be exposed. Check your ports settings.\n", color.YellowString("Warning"))
}
}
}
})
app.Command("stop", "Stop the project", func(cmd *cli.Cmd) {
cmd.Action = func() {
cmd := domain.NewComposeCommand([]string{"stop"}, prod)
cmd.Execute()
}
})
app.Command("install", "Install (or update) the project dependencies (docker containers, npm, composer...)", func(cmd *cli.Cmd) {
forced := cmd.BoolOpt("f force", false, "Force the installation process")
skipped := cmd.Strings(cli.StringsOpt{
Name: "s skip",
Value: []string{},
Desc: "Allows to skip some default tasks",
EnvVar: "PLIZ_INSTALL_SKIP",
})
// cmd.StringsOpt("skip", []string{}, "")
cmd.Action = func() {
config := config.Get()
if prod {
backup := prompter.YN("You're in production. Do you want to make a backup?", true)
if backup {
err := actions.BackupActionHandler(executionContext, nil, nil, nil, nil, false)
if err != nil {
fmt.Printf("\n%s: %v\n", color.RedString("Error during backup"), err)
}
fmt.Println("")
}
ok := prompter.YN("The installation is going to start. Are you sure you want to continue?", false)
if !ok {
return
}
}
/*
* 1. Duplicate and edit the config files (.env, docker_ports.yml...)
*/
fmt.Printf("\n %s ️ Prepare config files...\n\n", color.YellowString("▶"))
for _, configFile := range config.ConfigFiles {
created := false
// if the 'force' option is set, then we consider that the file has been created
if *forced {
created = true
}
// check if the file exists. If not, duplicate the sample
if _, err := os.Stat(configFile.Target); os.IsNotExist(err) {
utils.CopyFileContents(configFile.Sample, configFile.Target)
created = true
}
// edit the file
if created {
cmd := domain.NewCommand([]string{"vim", configFile.Target}, true)
cmd.Execute()
}
fmt.Println(configFile.Target + color.GreenString(" OK."))
}
fmt.Println("")
/*
* 2. Build the containers
*/
fmt.Printf("\n %s ️ Build the containers...\n", color.YellowString("▶"))
cmd := domain.NewComposeCommand([]string{"build"}, prod)
cmd.Execute()
fmt.Println("")
/*
* 3. Start the containers
*/
fmt.Printf("\n %s ️ Starting containers...\n", color.YellowString("▶"))
// and start the containers
actions.StartActionHandler(prod, false)
fmt.Println("")
/*
* 4. Run the enabled tasks
*/
fmt.Printf("\n %s ️ Run install tasks...\n", color.YellowString("▶"))
TaskLoop:
for _, id := range config.InstallTasks {
task := config.Tasks[id]
// check if the task is skipped
for _, taskName := range *skipped {
if taskName == string(task.Name) {
fmt.Printf("\n%s %s %s\n", color.YellowString("-->"), task.Name, color.YellowString("skipped"))
continue TaskLoop
}
}
fmt.Printf("\n%s %s %s\n", color.CyanString("***"), task.Name, color.CyanString("***"))
// disable the execution check if the installation is forced
if *forced {
task.ExecutionCheck = nil
}
if task.Execute(domain.TaskExecutionContext{Prod: prod}) {
fmt.Printf("Task '%s' %s.\n", task.Name, color.GreenString("executed"))
}
}
/*
* 5. The end
*/
fmt.Printf("\n\n%s You may now run '%s' to launch your project\n", color.GreenString("✓"), color.MagentaString("pliz start"))
if len(config.Checklist) > 0 {
for _, item := range config.Checklist {
fmt.Printf(" %s %s\n", color.RedString("→"), item)
}
}
fmt.Println("")
}
})
app.Command("bash", "Display a shell inside the builder service (or the specified service)", func(cmd *cli.Cmd) {
// parse and check config
parseAndCheckConfig()
defaultContainer := config.Get().Containers.Builder
cmd.Spec = "[-p...] [SERVICE]"
container := cmd.StringArg("SERVICE", defaultContainer, "The Compose service that will be used to display the shell")
ports := cmd.StringsOpt("p port", []string{}, "List of ports that will be published (e.g. 9000:3306)")
cmd.Action = func() {
options := []string{}
if len(*ports) > 0 {
for _, port := range *ports {
options = append(options, "-p", port)
}
}
cmd := domain.NewContainerCommand(*container, []string{"bash"}, options, prod)
cmd.Execute()
}
})
app.Command("logs", "Display logs of all services (or the specified service)", func(cmd *cli.Cmd) {
cmd.Spec = "[SERVICE]"
container := cmd.StringArg("SERVICE", "", "The Compose service to log")
cmd.Action = func() {
args := []string{"logs"}
if *container != "" {
args = append(args, *container)
}
cmd := domain.NewComposeCommand(args, prod)
cmd.Execute()
}
})
app.Command("run", "Execute a single task", func(cmd *cli.Cmd) {
// parse and check config
parseAndCheckConfig()
taskIDs := []string{}
for id := range config.Get().Tasks {
taskIDs = append(taskIDs, string(id))
}
sort.Strings(taskIDs)
for _, id := range taskIDs {
task := config.Get().Tasks[domain.TaskID(id)]
cmd.Command(id, task.Description, func(cmd *cli.Cmd) {
cmd.Action = actions.RunTaskActionHandler(task, prod)
})
}
})
app.Command("backup", "Perform a backup of the project", func(cmd *cli.Cmd) {
cmd.Spec = "[-q [--files] [--db]] [-o] [-k] [-v]"
quiet := cmd.BoolOpt("q quiet", false, "Avoid prompt")
backupFiles := cmd.BoolOpt("files", false, "Indicates if files will be backup")
backupDB := cmd.BoolOpt("db", false, "Indicates if DB will be backup")
outputFilename := cmd.StringOpt("o output", "", "Set the filename of the tar.gz")
key := cmd.StringOpt("k", "", "the encryption password")
verbose := cmd.BoolOpt("v", false, "Display more informations during the restore process")
cmd.Action = func() {
if *quiet == false {
backupFiles = nil
backupDB = nil
}
err := actions.BackupActionHandler(executionContext, backupFiles, backupDB, outputFilename, key, *verbose)
if err != nil {
fmt.Printf("\n%s: %v\n", color.RedString("Error during backup"), err)
cli.Exit(1)
}
}
})
app.Command("restore", "Restore a backup (Warning: files will be overrided)", func(cmd *cli.Cmd) {
cmd.Spec = "[-q [--config-files] [--files] [--db]] [-k] [-v] FILE"
quiet := cmd.BoolOpt("q quiet", false, "Avoid prompt")
restoreConfigFiles := cmd.BoolOpt("config-files", false, "Indicates if config files will be restored")
restoreFiles := cmd.BoolOpt("files", false, "Indicates if files will be restored")
restoreDB := cmd.BoolOpt("db", false, "Indicates if DB will be restored")
key := cmd.StringOpt("k", "", "the encryption password")
verbose := cmd.BoolOpt("v", false, "Display more informations during the restore process")
file := cmd.StringArg("FILE", "", "A pliz backup file (tar.gz)")
cmd.Action = func() {
if *quiet == false {
restoreConfigFiles = nil
restoreFiles = nil
restoreDB = nil
}
actions.RestoreActionHandler(executionContext, *file, restoreConfigFiles, restoreFiles, restoreDB, key, *verbose)
}
})
app.Run(os.Args)
}
func parseAndCheckConfig() {
err := config.Check()
if err != nil {
os.Exit(1)
return
}
}