-
Notifications
You must be signed in to change notification settings - Fork 11
/
scale.go
95 lines (87 loc) · 1.89 KB
/
scale.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
package main
import (
"fmt"
"os"
"strconv"
"sync"
"github.com/gliderlabs/hostctl/providers"
"github.com/spf13/cobra"
)
func init() {
Hostctl.AddCommand(scaleCmd)
}
var scaleCmd = &cobra.Command{
Use: "scale <name> <count>",
Short: "Resize host cluster",
Run: func(cmd *cobra.Command, args []string) {
if (len(args) < 2 && defaultName == "") ||
(len(args) < 1 && defaultName != "") {
cmd.Usage()
os.Exit(1)
}
var name, count string
if len(args) == 1 {
name = defaultName
count = args[0]
} else {
name = args[0]
count = args[1]
}
loadStdinUserdata()
provider, err := providers.Get(providerName, true)
fatal(err)
existing := existingHosts(provider, name)
desired := desiredHosts(name, count)
hosts := append(strSet(existing, desired), namespace+name)
finished := progressBar(".", 2)
parallelWait(hosts, func(_ int, host string, wg *sync.WaitGroup) {
defer wg.Done()
if !strIn(host, desired) {
fatal(provider.Destroy(host))
return
}
if strIn(host, desired) && !strIn(host, existing) {
fatal(provider.Create(newHost(host)))
return
}
})
finished()
},
}
func desiredHosts(name, count string) []string {
c, err := strconv.Atoi(count)
fatal(err)
var hosts []string
for i := 0; i < c; i++ {
hosts = append(hosts, fmt.Sprintf("%s%s.%v", namespace, name, i))
}
return hosts
}
func existingHosts(provider providers.HostProvider, name string) []string {
var hosts []string
for _, h := range provider.List(namespace + name + ".*") {
hosts = append(hosts, h.Name)
}
return hosts
}
func strIn(str string, list []string) bool {
for i := range list {
if str == list[i] {
return true
}
}
return false
}
func strSet(strs ...[]string) []string {
m := make(map[string]bool)
for i := range strs {
for _, str := range strs[i] {
m[str] = true
}
}
var set []string
for k := range m {
set = append(set, k)
}
return set
}