forked from canonical/lxd
/
containers_get.go
78 lines (66 loc) · 1.69 KB
/
containers_get.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
package main
import (
"fmt"
"net/http"
"time"
"github.com/lxc/lxd/shared"
)
func containersGet(d *Daemon, r *http.Request) Response {
for i := 0; i < 100; i++ {
result, err := doContainersGet(d, d.isRecursionRequest(r))
if err == nil {
return SyncResponse(true, result)
}
if !isDbLockedError(err) {
shared.LogDebugf("DBERR: containersGet: error %q", err)
return InternalError(err)
}
// 1 s may seem drastic, but we really don't want to thrash
// perhaps we should use a random amount
time.Sleep(100 * time.Millisecond)
}
shared.LogDebugf("DBERR: containersGet, db is locked")
shared.PrintStack()
return InternalError(fmt.Errorf("DB is locked"))
}
func doContainersGet(d *Daemon, recursion bool) (interface{}, error) {
result, err := dbContainersList(d.db, cTypeRegular)
if err != nil {
return nil, err
}
resultString := []string{}
resultList := []*shared.ContainerInfo{}
if err != nil {
return []string{}, err
}
for _, container := range result {
if !recursion {
url := fmt.Sprintf("/%s/containers/%s", shared.APIVersion, container)
resultString = append(resultString, url)
} else {
c, err := doContainerGet(d, container)
if err != nil {
c = &shared.ContainerInfo{
Name: container,
Status: shared.Error.String(),
StatusCode: shared.Error}
}
resultList = append(resultList, c)
}
}
if !recursion {
return resultString, nil
}
return resultList, nil
}
func doContainerGet(d *Daemon, cname string) (*shared.ContainerInfo, error) {
c, err := containerLoadByName(d, cname)
if err != nil {
return nil, err
}
cts, _, err := c.Render()
if err != nil {
return nil, err
}
return cts.(*shared.ContainerInfo), nil
}