forked from benjaminbollen/mintnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine.go
191 lines (165 loc) · 4.5 KB
/
machine.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
package main
import (
"errors"
"fmt"
"strings"
"sync"
. "github.com/tendermint/go-common"
"github.com/codegangsta/cli"
)
//--------------------------------------------------------------------------------
func cmdDocker(c *cli.Context) {
args := c.Args()
machines := ParseMachines(c.String("machines"))
var wg sync.WaitGroup
for _, mach := range machines {
wg.Add(1)
go func(mach string) {
defer wg.Done()
dockerCmd(mach, args)
}(mach)
}
wg.Wait()
}
func dockerCmd(mach string, args []string) error {
args = []string{"ssh", mach, "docker " + strings.Join(args, " ")}
if !runProcess("docker-cmd-"+mach, "docker-machine", args, true) {
return errors.New("Failed to exec docker command on machine " + mach)
}
return nil
}
//--------------------------------------------------------------------------------
func cmdCreate(c *cli.Context) {
args := c.Args()
machines := ParseMachines(c.String("machines"))
errs := createMachines(machines, args)
if len(errs) > 0 {
Exit(Fmt("There were %v errors", len(errs)))
} else {
fmt.Println(Fmt("Successfully created %v machines", len(machines)))
}
}
func createMachines(machines []string, args []string) (errs []error) {
var wg sync.WaitGroup
for _, mach := range machines {
wg.Add(1)
go func(mach string) {
defer wg.Done()
err := createMachine(args, mach)
if err != nil {
errs = append(errs, err)
}
}(mach)
}
wg.Wait()
return errs
}
func createMachine(args []string, mach string) error {
args = append([]string{"create"}, args...)
args = append(args, mach)
if !runProcess("create-"+mach, "docker-machine", args, true) {
return errors.New("Failed to create machine " + mach)
}
return nil
}
//--------------------------------------------------------------------------------
func cmdDestroy(c *cli.Context) {
machines := ParseMachines(c.String("machines"))
// Destroy each machine.
var wg sync.WaitGroup
for _, mach := range machines {
wg.Add(1)
go func(mach string) {
defer wg.Done()
err := removeMachine(mach)
if err != nil {
fmt.Println(Red(err.Error()))
}
}(mach)
}
wg.Wait()
fmt.Println("Success!")
}
//--------------------------------------------------------------------------------
func cmdProvision(c *cli.Context) {
args := c.Args()
machines := ParseMachines(c.String("machines"))
errs := provisionMachines(machines, args)
if len(errs) > 0 {
Exit(Fmt("There were %v errors", len(errs)))
} else {
fmt.Println(Fmt("Successfully created %v machines", len(machines)))
}
}
func provisionMachines(machines []string, args []string) (errs []error) {
var wg sync.WaitGroup
for _, mach := range machines {
wg.Add(1)
go func(mach string) {
defer wg.Done()
err := provisionMachine(args, mach)
if err != nil {
errs = append(errs, err)
}
}(mach)
}
wg.Wait()
return errs
}
func provisionMachine(args []string, mach string) error {
args = append([]string{"provision"}, args...)
args = append(args, mach)
if !runProcess("provision-"+mach, "docker-machine", args, true) {
return errors.New("Failed to provision machine " + mach)
}
return nil
}
//--------------------------------------------------------------------------------
// Stop a machine
// mach: name of machine
func stopMachine(mach string) error {
args := []string{"stop", mach}
if !runProcess("stop-"+mach, "docker-machine", args, true) {
return errors.New("Failed to stop machine " + mach)
}
return nil
}
// Remove a machine
// mach: name of machine
func removeMachine(mach string) error {
args := []string{"rm", "-f", mach}
if !runProcess("remove-"+mach, "docker-machine", args, true) {
return errors.New("Failed to remove machine " + mach)
}
return nil
}
// List machine names that match prefix
func listMachines(prefix string) ([]string, error) {
args := []string{"ls", "--quiet"}
output, ok := runProcessGetResult("list-machines", "docker-machine", args, true)
if !ok {
return nil, errors.New("Failed to list machines")
}
output = strings.TrimSpace(output)
if len(output) == 0 {
return nil, nil
}
machines := strings.Split(output, "\n")
matched := []string{}
for _, mach := range machines {
if strings.HasPrefix(mach, prefix+"-") {
matched = append(matched, mach)
}
}
return matched, nil
}
// Get ip of a machine
// mach: name of machine
func getMachineIP(mach string) (string, error) {
args := []string{"ip", mach}
output, ok := runProcessGetResult("get-ip-"+mach, "docker-machine", args, true)
if !ok {
return "", errors.New("Failed to get ip of machine" + mach)
}
return strings.TrimSpace(output), nil
}