-
Notifications
You must be signed in to change notification settings - Fork 0
/
containers.go
139 lines (129 loc) · 5.4 KB
/
containers.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package rocked
import (
"encoding/json"
"io/ioutil"
"os"
"strings"
"time"
)
const (
DockerContainersDir = "/containers"
DockerContainerConfigFileName = "config.v2.json"
)
// Container represents a container config JSON
type Container struct {
State struct {
Running bool `json:"Running"`
Paused bool `json:"Paused"`
Restarting bool `json:"Restarting"`
OOMKilled bool `json:"OOMKilled"`
RemovalInProgress bool `json:"RemovalInProgress"`
Dead bool `json:"Dead"`
Pid int `json:"Pid"`
StartedAt time.Time `json:"StartedAt"`
FinishedAt time.Time `json:"FinishedAt"`
Health interface{} `json:"Health"`
} `json:"State"`
ID string `json:"ID"`
Created time.Time `json:"Created"`
Managed bool `json:"Managed"`
Path string `json:"Path"`
Args []string `json:"Args"`
Config struct {
Hostname string `json:"Hostname"`
Domainname string `json:"Domainname"`
User string `json:"User"`
AttachStdin bool `json:"AttachStdin"`
AttachStdout bool `json:"AttachStdout"`
AttachStderr bool `json:"AttachStderr"`
ExposedPorts map[string]struct{} `json:"ExposedPorts"`
Tty bool `json:"Tty"`
OpenStdin bool `json:"OpenStdin"`
StdinOnce bool `json:"StdinOnce"`
Env []string `json:"Env"`
Cmd []string `json:"Cmd"`
Image string `json:"Image"`
Volumes interface{} `json:"Volumes"`
WorkingDir string `json:"WorkingDir"`
Entrypoint interface{} `json:"Entrypoint"`
OnBuild interface{} `json:"OnBuild"`
Labels struct {
} `json:"Labels"`
} `json:"Config"`
Image string `json:"Image"`
NetworkSettings struct {
Bridge string `json:"Bridge"`
SandboxID string `json:"SandboxID"`
HairpinMode bool `json:"HairpinMode"`
LinkLocalIPv6Address string `json:"LinkLocalIPv6Address"`
LinkLocalIPv6PrefixLen int `json:"LinkLocalIPv6PrefixLen"`
Networks struct {
Bridge struct {
IPAMConfig interface{} `json:"IPAMConfig"`
Links interface{} `json:"Links"`
Aliases interface{} `json:"Aliases"`
NetworkID string `json:"NetworkID"`
EndpointID string `json:"EndpointID"`
Gateway string `json:"Gateway"`
IPAddress string `json:"IPAddress"`
IPPrefixLen int `json:"IPPrefixLen"`
IPv6Gateway string `json:"IPv6Gateway"`
GlobalIPv6Address string `json:"GlobalIPv6Address"`
GlobalIPv6PrefixLen int `json:"GlobalIPv6PrefixLen"`
MacAddress string `json:"MacAddress"`
} `json:"bridge"`
} `json:"Networks"`
Service interface{} `json:"Service"`
Ports map[string]struct{} `json:"Ports"`
SandboxKey string `json:"SandboxKey"`
SecondaryIPAddresses interface{} `json:"SecondaryIPAddresses"`
SecondaryIPv6Addresses interface{} `json:"SecondaryIPv6Addresses"`
IsAnonymousEndpoint bool `json:"IsAnonymousEndpoint"`
} `json:"NetworkSettings"`
LogPath string `json:"LogPath"`
Name string `json:"Name"`
Driver string `json:"Driver"`
MountLabel string `json:"MountLabel"`
ProcessLabel string `json:"ProcessLabel"`
RestartCount int `json:"RestartCount"`
HasBeenStartedBefore bool `json:"HasBeenStartedBefore"`
HasBeenManuallyStopped bool `json:"HasBeenManuallyStopped"`
MountPoints map[string]struct{} `json:"MountPoints"`
AppArmorProfile string `json:"AppArmorProfile"`
HostnamePath string `json:"HostnamePath"`
HostsPath string `json:"HostsPath"`
ShmPath string `json:"ShmPath"`
ResolvConfPath string `json:"ResolvConfPath"`
SeccompProfile string `json:"SeccompProfile"`
NoNewPrivileges bool `json:"NoNewPrivileges"`
}
// ListContainers returns the list of existing containers ID
func (r *Rocked) ListContainers() ([]string, error) {
fi, err := ioutil.ReadDir(r.DockerHome + DockerContainersDir)
if err != nil {
return []string{}, err
}
var containers []string
for _, container := range fi {
containers = append(containers, container.Name())
}
return containers, nil
}
// InspectContainer inspects a container by reading its config JSON file
func (r *Rocked) InspectContainer(id string) (Container, error) {
// Check if container exists
containerPath := r.DockerHome + DockerContainersDir + "/" + id
_, err := os.Stat(containerPath)
if err != nil {
return Container{}, err
}
// Read config file
data, err := ioutil.ReadFile(strings.Join([]string{containerPath, DockerContainerConfigFileName}, "/"))
if err != nil {
return Container{}, err
}
// Unmarshal
var containerInfo Container
err = json.Unmarshal(data, &containerInfo)
return containerInfo, err
}