|
1 | 1 | package fleet |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "strings" |
| 6 | + "sync" |
| 7 | + "time" |
5 | 8 |
|
6 | | - "github.com/coreos/fleet/job" |
7 | 9 | "github.com/coreos/fleet/schema" |
8 | 10 | ) |
9 | 11 |
|
10 | | -// Start launches target units and blocks until active |
11 | | -func (c *FleetClient) Start(targets []string) error { |
12 | | - units := make([][]string, len(targets)) |
13 | | - for i, target := range targets { |
14 | | - u, err := c.Units(target) |
| 12 | +// Start units and wait for their desiredState |
| 13 | +func (c *FleetClient) Start(targets []string, wg *sync.WaitGroup, outchan chan string, errchan chan error) { |
| 14 | + for _, target := range targets { |
| 15 | + wg.Add(1) |
| 16 | + go doStart(c, target, wg, outchan, errchan) |
| 17 | + } |
| 18 | + return |
| 19 | +} |
| 20 | + |
| 21 | +func doStart(c *FleetClient, target string, wg *sync.WaitGroup, outchan chan string, errchan chan error) { |
| 22 | + defer wg.Done() |
| 23 | + |
| 24 | + // prepare string representation |
| 25 | + component, num, err := splitTarget(target) |
| 26 | + if err != nil { |
| 27 | + errchan <- err |
| 28 | + return |
| 29 | + } |
| 30 | + name, err := formatUnitName(component, num) |
| 31 | + if err != nil { |
| 32 | + errchan <- err |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + requestState := "launched" |
| 37 | + var desiredState string |
| 38 | + if strings.Contains(name, "-data.service") { |
| 39 | + desiredState = "exited" |
| 40 | + } else { |
| 41 | + desiredState = "running" |
| 42 | + } |
| 43 | + |
| 44 | + if err := c.Fleet.SetUnitTargetState(name, requestState); err != nil { |
| 45 | + errchan <- err |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + // start with the likely subState to avoid sending it across the channel |
| 50 | + lastSubState := "dead" |
| 51 | + |
| 52 | + for { |
| 53 | + // poll for unit states |
| 54 | + states, err := c.Fleet.UnitStates() |
15 | 55 | if err != nil { |
16 | | - return err |
| 56 | + errchan <- err |
| 57 | + return |
17 | 58 | } |
18 | | - units[i] = u |
19 | | - } |
20 | | - desiredState := string(job.JobStateLaunched) |
21 | | - for _, names := range units { |
22 | | - for _, name := range names { |
23 | | - if err := c.Fleet.SetUnitTargetState(name, desiredState); err != nil { |
24 | | - return err |
| 59 | + |
| 60 | + // FIXME: fleet UnitStates API forces us to iterate for now |
| 61 | + var currentState *schema.UnitState |
| 62 | + for _, s := range states { |
| 63 | + if name == s.Name { |
| 64 | + currentState = s |
| 65 | + break |
25 | 66 | } |
26 | 67 | } |
27 | | - } |
28 | | - var errchan chan error |
29 | | - var outchan chan *schema.Unit |
30 | | - for _, names := range units { |
31 | | - for _, name := range names { |
32 | | - // wait for systemd to tell us that it's running, not fleet |
33 | | - // data containers are special snowflakes who just exit |
34 | | - if strings.Contains(name, "-data.service") { |
35 | | - outchan, errchan = waitForUnitSubStates(names, "exited") |
36 | | - } else { |
37 | | - outchan, errchan = waitForUnitSubStates(names, "running") |
38 | | - } |
39 | | - if err := printUnitSubState(name, outchan, errchan); err != nil { |
40 | | - return err |
41 | | - } |
| 68 | + if currentState == nil { |
| 69 | + errchan <- fmt.Errorf("could not find unit: %v", name) |
| 70 | + return |
42 | 71 | } |
| 72 | + |
| 73 | + // if subState changed, send it across the output channel |
| 74 | + if lastSubState != currentState.SystemdSubState { |
| 75 | + outchan <- fmt.Sprintf("\033[0;33m%v:\033[0m %v/%v \r", |
| 76 | + name, currentState.SystemdActiveState, currentState.SystemdSubState) |
| 77 | + } |
| 78 | + |
| 79 | + // break when desired state is reached |
| 80 | + if currentState.SystemdSubState == desiredState { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + lastSubState = currentState.SystemdSubState |
| 85 | + time.Sleep(250 * time.Millisecond) |
43 | 86 | } |
44 | | - return nil |
45 | 87 | } |
0 commit comments