-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
lxd.go
198 lines (162 loc) · 3.7 KB
/
lxd.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"bytes"
"errors"
"fmt"
"os"
"strconv"
"strings"
lxd "github.com/lxc/lxd/client"
"github.com/lxc/lxd/shared/api"
)
var (
timeout = 120
killTimeout = 5
)
type BytesBuffer struct {
*bytes.Buffer
}
func (b *BytesBuffer) Close() error {
return nil
}
type LXDClient struct {
Client lxd.InstanceServer
}
// Connect to the LXD socket.
func (c *LXDClient) Connect() error {
client, err := lxd.ConnectLXDUnix("", nil)
if err != nil {
client, err = lxd.ConnectLXDUnix("/var/snap/lxd/common/lxd/unix.socket", nil)
if err != nil {
return err
}
}
c.Client = client
return nil
}
// Create a container using a specific remote and alias.
func (c *LXDClient) Create(name string, remote string, alias string) error {
fmt.Printf("creating %s with %s:%s\n", name, remote, alias)
if c.Client == nil {
err := c.Connect()
if err != nil {
return err
}
}
server := ""
switch remote {
case "images":
server = "https://images.linuxcontainers.org"
case "ubuntu":
server = "https://cloud-images.ubuntu.com/releases"
case "ubuntu-daily":
server = "https://cloud-images.ubuntu.com/daily"
default:
return fmt.Errorf("unknown remote: %s", remote)
}
req := api.ContainersPost{
Name: name,
Source: api.ContainerSource{
Type: "image",
Mode: "pull",
Protocol: "simplestreams",
Server: server,
Alias: alias,
},
}
// Get LXD to create the container (background operation)
op, err := c.Client.CreateContainer(req)
if err != nil {
return err
}
// Wait for the operation to complete
err = op.Wait()
if err != nil {
return err
}
return nil
}
// Delete the given container.
func (c *LXDClient) Delete(name string) error {
fmt.Println("deleting", name)
op, err := c.Client.DeleteContainer(name)
if err != nil {
return err
}
return op.Wait()
}
// Run a command returning result struct. Will kill commands that take longer
// than 120 seconds.
func (c *LXDClient) Exec(name string, command ...string) error {
fmt.Printf("$ %s\n", strings.Join(command, " "))
cmd := []string{"/usr/bin/timeout", "-k", strconv.Itoa(killTimeout), strconv.Itoa(timeout)}
cmd = append(cmd, command...)
req := api.ContainerExecPost{
Command: cmd,
WaitForWS: true,
}
output := &BytesBuffer{bytes.NewBuffer(nil)}
args := lxd.ContainerExecArgs{
Stdout: output,
Stderr: output,
DataDone: make(chan bool),
}
op, err := c.Client.ExecContainer(name, req, &args)
if err != nil {
return err
}
err = op.Wait()
if err != nil {
return err
}
// Wait for any remaining I/O to be flushed
<-args.DataDone
// get the return code
opAPI := op.Get()
rc := int(opAPI.Metadata["return"].(float64))
if rc != 0 {
return errors.New(output.String())
}
fmt.Println(output.String())
return nil
}
// Push file to container.
func (c *LXDClient) Push(name string, src string, dst string) error {
fmt.Printf("cp %s %s%s\n", src, name, dst)
f, err := os.Open(src)
if err != nil {
return fmt.Errorf("error reading %s: %w", src, err)
}
defer f.Close()
return c.Client.CreateContainerFile(name, dst, lxd.ContainerFileArgs{
Content: f,
Mode: 0644,
})
}
// Start the given container.
func (c *LXDClient) Start(name string) error {
fmt.Println("starting", name)
reqState := api.ContainerStatePut{
Action: "start",
Timeout: -1,
}
op, err := c.Client.UpdateContainerState(name, reqState, "")
if err != nil {
return err
}
return op.Wait()
}
// Stop the given container.
func (c *LXDClient) Stop(name string) error {
fmt.Println("stopping", name)
reqState := api.ContainerStatePut{
Action: "stop",
Force: true,
Timeout: 10,
}
op, err := c.Client.UpdateContainerState(name, reqState, "")
if err != nil {
return err
}
return op.Wait()
}