-
Notifications
You must be signed in to change notification settings - Fork 39
/
cmd_start.go
88 lines (72 loc) · 1.54 KB
/
cmd_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 main
import (
"fmt"
"os"
"sync"
"github.com/lxc/lxd/client"
"github.com/lxc/lxd/shared/api"
)
func cmdStart(c lxd.ContainerServer, args []string) error {
var wgBatch sync.WaitGroup
if os.Getuid() != 0 {
return fmt.Errorf("Container startup must be run as root.")
}
// Load the simulation
routersMap, err := importFromLXD(c)
if err != nil {
return err
}
routers := []*Router{}
for _, v := range routersMap {
if v.Tier < 1 || v.Tier > 3 {
continue
}
routers = append(routers, v)
}
// Create all the needed interfaces
logf("Creating the network interfaces")
err = networkCreate(routersMap)
if err != nil {
return err
}
// Helper function
startContainer := func(name string) {
defer wgBatch.Done()
req := api.ContainerStatePut{
Action: "start",
Timeout: -1,
}
op, err := c.UpdateContainerState(name, req, "")
if err != nil {
logf("Failed to start container: %s: %s", name, err)
return
}
err = op.Wait()
if err != nil {
logf("Failed to start container: %s: %s", name, err)
return
}
}
// Start the containers
batch := 8
batches := len(routers) / batch
remainder := len(routers) % batch
logf("Starting the containers")
current := 0
for i := 0; i < batches; i++ {
for j := 0; j < batch; j++ {
wgBatch.Add(1)
go startContainer(routers[current].Name)
current += 1
}
wgBatch.Wait()
}
for k := 0; k < remainder; k++ {
wgBatch.Add(1)
go startContainer(routers[current].Name)
current += 1
}
wgBatch.Wait()
logf("%d containers started", len(routers))
return nil
}