-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy.go
61 lines (54 loc) · 1.26 KB
/
destroy.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
package fleet
import (
"fmt"
"strings"
"sync"
"time"
)
// Destroy units for a given target
func (c *FleetClient) Destroy(targets []string, wg *sync.WaitGroup, outchan chan string, errchan chan error) {
// expand @* targets
expandedTargets, err := expandTargets(c, targets)
if err != nil {
errchan <- err
return
}
for _, target := range expandedTargets {
wg.Add(1)
go doDestroy(c, target, wg, outchan, errchan)
}
return
}
func doDestroy(c *FleetClient, target string, wg *sync.WaitGroup, outchan chan string, errchan chan error) {
defer wg.Done()
// prepare string representation
component, num, err := splitTarget(target)
if err != nil {
errchan <- err
return
}
name, err := formatUnitName(component, num)
if err != nil {
errchan <- err
return
}
destroyed := fmt.Sprintf("\033[0;33m%v:\033[0m destroyed \r", name)
// tell fleet to destroy the unit
c.Fleet.DestroyUnit(name)
// loop until the unit is actually gone from unit states
outerLoop:
for {
time.Sleep(250 * time.Millisecond)
unitStates, err := cAPI.UnitStates()
if err != nil {
errchan <- err
}
for _, us := range unitStates {
if strings.HasPrefix(us.Name, name) {
continue outerLoop
}
}
outchan <- destroyed
return
}
}