-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
81 lines (66 loc) · 1.65 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
package main
import (
"errors"
"flag"
"fmt"
"os"
"github.com/gophergala/go_ne/core"
"github.com/mgutz/ansi"
)
var (
taskName = flag.String("task", "", "which task to run")
configFile = flag.String("config", ".kiss.yml", "path to config file")
group = flag.String("group", "", "defines the server group for which the task should run")
)
func main() {
flag.Parse()
config, err := core.NewConfig()
if err != nil {
fail(err)
}
err = config.Load(*configFile)
if err != nil {
fail(err)
}
checkFlags()
hosts, err := config.GetServerGroup(*group)
if err != nil {
fail(err)
}
fmt.Println(ansi.Color(fmt.Sprintf("Running tasks for group `%v`", *group), "green"))
for _, host := range hosts {
fmt.Println(ansi.Color(fmt.Sprintf("Selecting host `%v`", host.Host), "green"))
var runner core.Runner
if host.RunLocally {
runner, err = core.NewLocalRunner()
if err != nil {
fail(err)
}
go core.LogOutput(runner.(*core.Local))
} else {
runner, err = core.NewRemoteRunner(host)
if err != nil {
fail(err)
}
}
fmt.Println(ansi.Color(fmt.Sprintf("Executing `%v`", *taskName), "green"))
err = core.RunTask(runner, config, *taskName)
if err != nil {
fail(err)
} else {
fmt.Println(ansi.Color("Tasks completed successfully", "green"))
}
}
}
func checkFlags() {
if len(*group) == 0 {
fail(errors.New("Please select the target server group by passing the `-group=name` flag"))
}
if len(*taskName) == 0 {
fail(errors.New("Please select the task to execute by passing the `-task=name` flag"))
}
}
func fail(err error) {
fmt.Println(ansi.Color(fmt.Sprintf("Task failed: %v", err), "red"))
os.Exit(1)
}