-
Notifications
You must be signed in to change notification settings - Fork 39
/
cmd_stop.go
84 lines (68 loc) · 1.32 KB
/
cmd_stop.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
package main
import (
"fmt"
"os"
"sync"
"github.com/lxc/lxd/client"
"github.com/lxc/lxd/shared/api"
)
func cmdStop(c lxd.ContainerServer, args []string) error {
var wgBatch sync.WaitGroup
if os.Getuid() != 0 {
return fmt.Errorf("Container stop 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)
}
// Helper function
stopContainer := func(name string) {
defer wgBatch.Done()
req := api.ContainerStatePut{
Action: "stop",
Timeout: -1,
Force: true,
}
op, err := c.UpdateContainerState(name, req, "")
if err != nil {
return
}
err = op.Wait()
if err != nil {
return
}
}
// Stop the containers
batch := 8
batches := len(routers) / batch
remainder := len(routers) % batch
current := 0
for i := 0; i < batches; i++ {
for j := 0; j < batch; j++ {
wgBatch.Add(1)
go stopContainer(routers[current].Name)
current += 1
}
wgBatch.Wait()
}
for k := 0; k < remainder; k++ {
wgBatch.Add(1)
go stopContainer(routers[current].Name)
current += 1
}
wgBatch.Wait()
// Destroy all the interfaces
err = networkDestroy(routersMap)
if err != nil {
return err
}
return nil
}