-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxc.go
220 lines (202 loc) · 5.46 KB
/
lxc.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"syscall"
lxd "github.com/lxc/lxd/client"
lxdApi "github.com/lxc/lxd/shared/api"
)
type writerBuffer struct {
bytes.Buffer
}
func (b *writerBuffer) Close() error {
return nil
}
type lxcExecError struct {
ExitCode int
Stdout string
Stderr string
}
func (err *lxcExecError) Error() string {
return fmt.Sprintf("failed to exec lxc: exitCode=%d stdout=%s stderr=%s", err.ExitCode, err.Stdout, err.Stderr)
}
func newLxdClient() (lxd.InstanceServer, error) {
return lxd.ConnectLXDUnix("", nil)
}
func lxcExists(name string) (bool, error) {
c, err := newLxdClient()
if err != nil {
return false, fmt.Errorf("failed to create the lxd client: %w", err)
}
instance, _, err := c.GetInstance(name)
if err != nil {
if lxdApi.StatusErrorCheck(err, 404) {
return false, nil
}
return false, err
}
return instance != nil, nil
}
func lxcDelete(name string) error {
c, err := newLxdClient()
if err != nil {
return fmt.Errorf("failed to create the lxd client: %w", err)
}
instance, _, err := c.GetInstance(name)
if err != nil {
if lxdApi.StatusErrorCheck(err, 404) {
return nil
}
return err
}
if instance.StatusCode != 0 && instance.StatusCode != lxdApi.Stopped {
stateRequest := lxdApi.InstanceStatePut{
Action: "stop",
Timeout: -1,
Force: true,
}
op, err := c.UpdateInstanceState(name, stateRequest, "")
if err != nil {
return err
}
err = op.Wait()
if err != nil {
return fmt.Errorf("failed to stop instance %s: %w", name, err)
}
if instance.Ephemeral {
return nil
}
}
op, err := c.DeleteInstance(name)
if err != nil {
return fmt.Errorf("failed to delete instance %s: %w", name, err)
}
return op.Wait()
}
func lxcExec(name string, user string, command string) error {
path, err := exec.LookPath("lxc")
if err != nil {
return fmt.Errorf("failed to find lxc: %w", err)
}
return syscall.Exec(path, []string{"lxc", "exec", name, "--", "su", "-l", "-s", command, user}, []string{})
}
func lxcExecWithInput(name string, input string, args ...string) (string, error) {
c, err := newLxdClient()
if err != nil {
return "", fmt.Errorf("failed to create the lxd client: %w", err)
}
execRequest := lxdApi.InstanceExecPost{
Command: args,
WaitForWS: true,
}
execStdout := &writerBuffer{}
execStderr := &writerBuffer{}
execArgs := lxd.InstanceExecArgs{
Stdin: io.NopCloser(strings.NewReader(input)),
Stdout: execStdout,
Stderr: execStderr,
DataDone: make(chan bool),
}
op, err := c.ExecInstance(name, execRequest, &execArgs)
if err != nil {
return "", fmt.Errorf("failed to start the exec for the instance to be fully running: %w", err)
}
err = op.Wait()
if err != nil {
return "", fmt.Errorf("failed to wait for the instance to be fully running: %w", err)
}
opAPI := op.Get()
<-execArgs.DataDone
exitCode := int(opAPI.Metadata["return"].(float64))
if exitCode != 0 {
return "", &lxcExecError{
ExitCode: exitCode,
Stdout: execStdout.String(),
Stderr: execStderr.String(),
}
}
return strings.TrimSpace(execStdout.String()), nil
}
func lxcCopy(from, to string) error {
// delete the "to" instance if it exists.
exists, err := lxcExists(to)
if err != nil {
return fmt.Errorf("failed to copy %s to %s because exists failed: %w", from, to, err)
}
if exists {
log.Printf("Deleting the existing %s instance", to)
err := lxcDelete(to)
if err != nil {
return fmt.Errorf("failed to copy %s to %s because delete failed: %w", from, to, err)
}
}
// clone the existing instance into a new one.
// NB we should not use the --ephemeral flag because we loose the
// possibility to troubleshoot. instead, we should explicitly
// delete the instance ourselfs.
log.Printf("Copying %s to the %s instance", from, to)
c, err := newLxdClient()
if err != nil {
return fmt.Errorf("failed to create the lxd client: %w", err)
}
instance, _, err := c.GetInstance(from)
if err != nil {
return fmt.Errorf("failed to get instance %s: %w", from, err)
}
op, err := c.CopyInstance(c, *instance, &lxd.InstanceCopyArgs{Name: to})
if err != nil {
return fmt.Errorf("failed to copy instance %s to %s: %w", from, to, err)
}
err = op.Wait()
if err != nil {
return fmt.Errorf("failed to wait for copy instance %s to %s: %w", from, to, err)
}
return nil
}
func lxcStart(name string) error {
c, err := newLxdClient()
if err != nil {
return fmt.Errorf("failed to create the lxd client: %w", err)
}
// start the instance.
log.Printf("Starting the %s instance", name)
op, err := c.UpdateInstanceState(name, lxdApi.InstanceStatePut{
Action: "start",
Timeout: -1,
}, "")
if err != nil {
return fmt.Errorf("failed to start instance: %w", err)
}
err = op.Wait()
if err != nil {
return fmt.Errorf("failed to wait for start instance: %w", err)
}
// wait for the instance to be fully running.
log.Printf("Waiting for the %s instance to be fully running", name)
execRequest := lxdApi.InstanceExecPost{
Command: []string{
"bash",
"-c",
"while [ \"$(systemctl is-system-running)\" != \"running\" ]; do sleep 1; done",
},
}
execArgs := lxd.InstanceExecArgs{
Stdin: nil,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
op, err = c.ExecInstance(name, execRequest, &execArgs)
if err != nil {
return fmt.Errorf("failed to start the exec for the instance to be fully running: %w", err)
}
err = op.Wait()
if err != nil {
return fmt.Errorf("failed to wait for the instance to be fully running: %w", err)
}
return nil
}