forked from deis/deis
/
start.go
88 lines (74 loc) · 1.92 KB
/
start.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
package fleet
import (
"fmt"
"sync"
"time"
"github.com/coreos/fleet/schema"
)
// Start units and wait for their desiredState
func (c *FleetClient) Start(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 doStart(c, target, wg, outchan, errchan)
}
return
}
func doStart(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
}
requestState := "launched"
desiredState := "running"
if err := c.Fleet.SetUnitTargetState(name, requestState); err != nil {
errchan <- err
return
}
// start with the likely subState to avoid sending it across the channel
lastSubState := "dead"
for {
// poll for unit states
states, err := c.Fleet.UnitStates()
if err != nil {
errchan <- err
return
}
// FIXME: fleet UnitStates API forces us to iterate for now
var currentState *schema.UnitState
for _, s := range states {
if name == s.Name {
currentState = s
break
}
}
if currentState == nil {
errchan <- fmt.Errorf("could not find unit: %v", name)
return
}
// if subState changed, send it across the output channel
if lastSubState != currentState.SystemdSubState {
outchan <- fmt.Sprintf("\033[0;33m%v:\033[0m %v/%v \r",
name, currentState.SystemdActiveState, currentState.SystemdSubState)
}
// break when desired state is reached
if currentState.SystemdSubState == desiredState {
return
}
lastSubState = currentState.SystemdSubState
time.Sleep(250 * time.Millisecond)
}
}