forked from nsec/the-internet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_start.go
82 lines (67 loc) · 1.46 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
package main
import (
"fmt"
"os"
"sync"
"github.com/lxc/lxd"
)
func cmdStart(c *lxd.Client, 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()
resp, err := c.Action(name, "start", -1, false, false)
if err != nil {
logf("Failed to start container '%s': %s", name, err)
return
}
err = c.WaitForSuccess(resp.Operation)
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
}