-
Notifications
You must be signed in to change notification settings - Fork 13
/
init.go
165 lines (158 loc) · 3.49 KB
/
init.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/containerd/containerd/pkg/progress"
"github.com/crosbymichael/boss/config"
"github.com/crosbymichael/boss/system"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var initCommand = cli.Command{
Name: "init",
Usage: "init boss on a system",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "undo",
Usage: "remove all boss init steps from the system, :(",
},
cli.StringSliceFlag{
Name: "step",
Usage: "run a specific steps by name",
Value: &cli.StringSlice{},
},
},
Action: func(clix *cli.Context) error {
ctx := Context()
c, err := config.Load()
if err != nil {
return err
}
if err := c.Containerd.Run(ctx, nil, clix); err != nil {
return err
}
client, err := system.NewClient()
if err != nil {
return errors.Wrap(err, "unable to connect to containerd, it's the one thing you have to do...for now")
}
defer client.Close()
var (
undo = clix.Bool("undo")
r = bufio.NewScanner(os.Stdin)
steps = c.Steps()
)
steps = filter(steps, clix.StringSlice("step"))
action := "install"
if undo {
action = "remove"
}
for _, s := range steps {
if s.Name() == "dns" {
fmt.Print("boss and consul are going to manage your server's DNS, is this ok? (y/n): ")
r.Scan()
yn := r.Text()
if strings.Trim(yn, " \n") == "n" {
fmt.Println("ok, aborting... :(")
return nil
}
}
fmt.Printf("%s -> %s\n", action, s.Name())
}
fmt.Printf("ready to %s..., continue? (y/n): ", action)
r.Scan()
yn := r.Text()
if strings.Trim(yn, " \n") == "n" {
fmt.Println("ok, aborting... :(")
return nil
}
var (
cmu sync.Mutex
pwg sync.WaitGroup
start = time.Now()
fw = progress.NewWriter(os.Stderr)
total = float64(len(steps))
stepProgress = make(chan output, 10)
current = output{
i: 0,
name: "init",
}
)
pwg.Add(1)
go func() {
defer pwg.Done()
for s := range stepProgress {
bar := progress.Bar(float64(s.i+1) / total)
fmt.Fprintf(fw, "%s:\t%d/%d\t%40r\t\n", s.name, s.i+1, int(total), bar)
fmt.Fprintf(fw, "elapsed: %-4.1fs\t\n",
time.Since(start).Seconds(),
)
fw.Flush()
}
}()
ticker := time.NewTicker(100 * time.Millisecond)
go func() {
for range ticker.C {
cmu.Lock()
stepProgress <- current
cmu.Unlock()
}
}()
for i, s := range steps {
cmu.Lock()
current = output{
name: s.Name(),
i: i,
}
stepProgress <- current
cmu.Unlock()
fn := s.Run
if clix.Bool("undo") {
fn = s.Remove
}
if err := fn(ctx, client, clix); err != nil {
if clix.Bool("undo") {
logrus.WithError(err).Warnf("execute %s", s.Name())
} else {
return errors.Wrapf(err, "execute %s", s.Name())
}
}
}
ticker.Stop()
close(stepProgress)
pwg.Wait()
if clix.Bool("undo") {
fmt.Println("boss removed and out of your way")
return nil
}
fmt.Println("boss intitalized and ready for use, have fun!")
return nil
},
}
type output struct {
i int
name string
}
func filter(steps []config.Step, filter []string) (o []config.Step) {
if len(filter) == 0 {
return steps
}
for _, s := range steps {
if hasFilter(filter, s.Name()) {
o = append(o, s)
}
}
return o
}
func hasFilter(filters []string, name string) bool {
for _, filter := range filters {
if name == filter || name == config.RegisterName(filter) {
return true
}
}
return false
}