-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.go
160 lines (120 loc) · 3.19 KB
/
container.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)
// IsSSHRunning returns true if the container is running.
func IsSSHRunning(container string) (bool, error) {
cli, err := CreateConnection()
// Error handling
if err != nil {
return false, err
}
defer cli.Close()
res, err := cli.ContainerInspect(context.Background(), container)
if err != nil {
return false, err
}
return res.State.Health.Status == "healthy", nil
}
// GetContainerIP returns the port of a container.
func GetContainerIP(container string) (string, error) {
cli, err := CreateConnection()
// Error handling
if err != nil {
return "", err
}
defer cli.Close()
res, err := cli.ContainerInspect(context.Background(), container)
if err != nil {
return "", err
}
return res.NetworkSettings.Networks["no-internet"].IPAddress, nil
}
// StopContainer stops the container
func StopContainer(containerName string) error {
cli, err := CreateConnection()
// Error handling
if err != nil {
return err
}
defer cli.Close()
timeDuration, err := time.ParseDuration("5s")
if err != nil {
return err
}
cli.ContainerStop(context.Background(), containerName, &timeDuration)
return nil
}
// StartExistingContainer starts the container
func StartExistingContainer(containerName string) error {
cli, err := CreateConnection()
// Error handling
if err != nil {
return err
}
defer cli.Close()
// Inspect container attributes
res, err := cli.ContainerInspect(context.Background(), containerName)
if err != nil {
return err
}
// If the container is running, do nothing
if res.State.Running {
return nil
}
// Start the container with a specific ID
err = cli.ContainerStart(context.Background(), containerName, types.ContainerStartOptions{})
// Check for errors
if err != nil {
return err
}
cli.Close()
return nil
}
// CreateAndStartNewContainer creates a new container
func CreateAndStartNewContainer() (string, error) {
cli, err := CreateConnection()
// Error handling
if err != nil {
return "", err
}
defer cli.Close()
resp, err := cli.ContainerCreate(context.Background(), &container.Config{
Image: "sshh",
Hostname: "ecorp-finances",
ExposedPorts: nat.PortSet{
"22/tcp": struct{}{},
},
}, &container.HostConfig{
// Should we make the user create a docker network?
NetworkMode: "no-internet",
}, nil, "")
// Return an error, if any
if err != nil {
return resp.ID, err
}
// Start the container with the specific ID
err = cli.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{})
// Check for errors
if err != nil {
return resp.ID, err
}
return resp.ID, nil
}
// CreateConnection creates a new docker connection
func CreateConnection() (*client.Client, error) {
// A good place to get up and running: https://docs.docker.com/engine/api/sdk/
// Create new docker client
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
// If error, we do nothing
if err != nil {
debugPrint("Unable to create the docker connection")
return nil, err
}
return cli, nil
}