Merge lp:~kalikiana/ubuntu-sdk-tools/lxd-snap into lp:ubuntu-sdk-tools

Proposed by Christian Dywan on 2016-10-20
Status: Needs review
Proposed branch: lp:~kalikiana/ubuntu-sdk-tools/lxd-snap
Merge into: lp:ubuntu-sdk-tools
Diff against target: 369 lines (+93/-69)
8 files modified
fixables/containerAccess.go (+1/-1)
fixables/dri.go (+8/-1)
helpers.go (+20/-37)
snapcraft.yaml (+23/-0)
usdk-target/create.go (+7/-1)
usdk-target/exec.go (+15/-7)
usdk-target/main.go (+3/-0)
usdk-target/register.go (+16/-22)
To merge this branch: bzr merge lp:~kalikiana/ubuntu-sdk-tools/lxd-snap
Reviewer Review Type Date Requested Status
Ubuntu SDK team 2016-10-20 Pending
Review via email: mp+308901@code.launchpad.net

Commit Message

Detect and use lxd from the snap

To post a comment you must log in.
40. By Christian Dywan on 2017-01-26

No need to reload config to add a remote

41. By Christian Dywan on 2017-01-26

Set the password of the user in the container if not snappy

42. By Christian Dywan on 2017-01-26

Only attempt container chmod if not snappy

Unmerged revisions

42. By Christian Dywan on 2017-01-26

Only attempt container chmod if not snappy

41. By Christian Dywan on 2017-01-26

Set the password of the user in the container if not snappy

40. By Christian Dywan on 2017-01-26

No need to reload config to add a remote

39. By Christian Dywan on 2017-01-24

Give snapcraft.yaml a proper description and drop comments

38. By Christian Dywan on 2017-01-24

Undo automatic 'go fmt' changes

37. By Christian Dywan on 2016-11-30

Don't fail on wrong directory permissions

36. By Christian Dywan on 2016-11-30

Turn errors into warnings and make sudo optional

35. By Christian Dywan on 2016-11-08

Replace all lxc command invokationds with lxd API calls

34. By Christian Dywan on 2016-10-20

Detect and use lxd from the snap

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'fixables/containerAccess.go'
2--- fixables/containerAccess.go 2016-06-24 12:12:46 +0000
3+++ fixables/containerAccess.go 2017-01-26 17:58:02 +0000
4@@ -46,7 +46,7 @@
5 }
6 }
7
8- if fi.Mode() != os.ModeDir | ubuntu_sdk_tools.LxdContainerPerm {
9+ if os.Getenv("SNAP") == "" && fi.Mode() != os.ModeDir | ubuntu_sdk_tools.LxdContainerPerm {
10 if !doFix {
11 return fmt.Errorf("Wrong directory permissions. Container rootfs of %s is not accessible.", container)
12 } else {
13
14=== modified file 'fixables/dri.go'
15--- fixables/dri.go 2016-06-24 12:12:46 +0000
16+++ fixables/dri.go 2017-01-26 17:58:02 +0000
17@@ -23,11 +23,18 @@
18 "launchpad.net/ubuntu-sdk-tools"
19 "github.com/lxc/lxd/shared"
20 "fmt"
21+ "os"
22 )
23
24 type DRIFixable struct { }
25
26 func (*DRIFixable) run(client *lxd.Client, container *shared.ContainerInfo, doFix bool) error {
27+ // FIXME: dri device isn't accessible under confinement
28+ if os.Getenv("SNAP") != "" {
29+ fmt.Fprintf(os.Stderr, "Skipping adding /dev/dri/card*\n")
30+ return nil
31+ }
32+
33 files, err := filepath.Glob("/dev/dri/card*")
34 if err != nil {
35 return err
36@@ -105,4 +112,4 @@
37
38 func (*DRIFixable) NeedsRoot () bool {
39 return false
40-}
41\ No newline at end of file
42+}
43
44=== modified file 'helpers.go'
45--- helpers.go 2016-07-06 09:22:33 +0000
46+++ helpers.go 2017-01-26 17:58:02 +0000
47@@ -30,7 +30,6 @@
48 "os"
49 "fmt"
50 "log"
51- "os/exec"
52 "strings"
53 )
54
55@@ -56,31 +55,13 @@
56 }
57
58 defaultRemoteName := "ubuntu-sdk-images"
59- remotes := config.Remotes
60- sdkRem, ok := remotes[defaultRemoteName]
61- if ok {
62- if sdkRem.Addr == defaultImageRemote {
63- return
64- } else {
65- cmd := exec.Command("lxc", "remote", "remove", defaultRemoteName)
66- err := cmd.Run()
67- if (err != nil) {
68- fmt.Fprintf(os.Stderr, "Could not remove the remote "+defaultRemoteName+". error: %v\n", err)
69- fmt.Fprintf(os.Stderr, "Please remove it manually.\n", err)
70- os.Exit(1)
71- }
72- }
73- }
74-
75- cmd := exec.Command("lxc", "remote", "add", "ubuntu-sdk-images", defaultImageRemote, "--accept-certificate", "--protocol=simplestreams")
76- err := cmd.Run()
77- if (err != nil) {
78- fmt.Fprintf(os.Stderr, "Could not register remote. error: %v\n", err)
79- os.Exit(1)
80- }
81-
82- //make sure config is loaded again
83- globConfig = nil
84+
85+ config.Remotes[defaultRemoteName] = lxd.RemoteConfig{
86+ Addr: defaultImageRemote,
87+ Static: true,
88+ Public: true,
89+ Protocol: "simplestreams",
90+ }
91 }
92
93 func GetConfigOrDie () (*lxd.Config) {
94@@ -89,10 +70,7 @@
95 return globConfig
96 }
97
98- configDir := "$HOME/.config/lxc"
99- if os.Getenv("LXD_CONF") != "" {
100- configDir = os.Getenv("LXD_CONF")
101- }
102+ configDir := "$XDG_CONFIG_HOME/ubuntu-sdk-target"
103 configPath := os.ExpandEnv(path.Join(configDir, "config.yml"))
104
105 globConfig, err := lxd.LoadConfig(configPath)
106@@ -117,6 +95,15 @@
107 }
108 }
109
110+ _, err = lxd.NewClient(globConfig, globConfig.DefaultRemote)
111+ if err != nil {
112+ os.Setenv("LXD_DIR", "/var/snap/lxd/common/lxd")
113+ _, err = lxd.NewClient(globConfig, globConfig.DefaultRemote)
114+ if err != nil {
115+ log.Fatal("Can't establish a working socket connection: %s", err)
116+ }
117+ }
118+
119 return globConfig
120 }
121
122@@ -193,15 +180,11 @@
123 }
124
125 command := []string {
126- "exec", container, "--",
127 "bash", "-c", "rm /etc/ld.so.cache; ldconfig",
128 }
129
130- cmd := exec.Command("lxc", command...)
131- cmd.Stdout = os.Stdout
132- cmd.Stderr = os.Stderr
133-
134- return cmd.Run()
135+ _, err = client.Exec(container, command, nil, os.Stdin, os.Stdout, os.Stderr, nil, 0, 0)
136+ return err
137 }
138
139 func AddDeviceSync (client *lxd.Client, container, devname, devtype string, props []string) error{
140@@ -316,4 +299,4 @@
141 }
142
143 return clickTargets, nil
144-}
145\ No newline at end of file
146+}
147
148=== added file 'snapcraft.yaml'
149--- snapcraft.yaml 1970-01-01 00:00:00 +0000
150+++ snapcraft.yaml 2017-01-26 17:58:02 +0000
151@@ -0,0 +1,23 @@
152+name: ubuntu-sdk-target
153+version: '0.6'
154+summary: Command line interface for creating and building with SDK containers
155+description: |
156+ This command line tool allows creating, building with, updating and
157+ removing SDK containers based on LXD.
158+
159+grade: devel
160+confinement: strict
161+
162+apps:
163+ ubuntu-sdk-target:
164+ command: usdk-target
165+ plugs: [network, lxd]
166+
167+parts:
168+ ubuntu-sdk-tools:
169+ plugin: go
170+ go-importpath: launchpad.net/ubuntu-sdk-tools
171+ go-packages:
172+ - launchpad.net/ubuntu-sdk-tools/usdk-target
173+ source: .
174+ build-packages: [bzr, gcc]
175
176=== modified file 'usdk-target/create.go'
177--- usdk-target/create.go 2016-10-05 12:48:19 +0000
178+++ usdk-target/create.go 2017-01-26 17:58:02 +0000
179@@ -73,7 +73,9 @@
180 }
181
182 if os.Getuid() != 0 {
183- return fmt.Errorf("This command needs to run as root")
184+ if os.Getenv("SNAP") == "" {
185+ return fmt.Errorf("This command needs to run as root")
186+ }
187 }
188
189 config := ubuntu_sdk_tools.GetConfigOrDie()
190@@ -148,6 +150,7 @@
191 return err
192 }
193
194+ fmt.Fprintf(os.Stderr, "Initializing progress tracker\n")
195 c.initProgressTracker(client, resp.Operation)
196 err = client.WaitForSuccess(resp.Operation)
197
198@@ -179,18 +182,21 @@
199 }
200
201 //add the required devices
202+ fmt.Fprintf(os.Stderr, "Adding devices to the container\n")
203 err = ubuntu_sdk_tools.AddDeviceSync(client, c.name, "tmp", "disk", []string{"source=/tmp", "path=/tmp", "recursive=true"})
204 if err != nil {
205 ubuntu_sdk_tools.RemoveContainerSync(client, c.name)
206 return err
207 }
208
209+ fmt.Fprintf(os.Stderr, "Adding the user\n")
210 err = RegisterUserInContainer(client, c.name, nil, c.createSupGroups)
211 if err != nil {
212 ubuntu_sdk_tools.RemoveContainerSync(client, c.name)
213 return err
214 }
215
216+ fmt.Fprintf(os.Stderr, "Updating the configuration\n")
217 ubuntu_sdk_tools.UpdateConfigSync(client, c.name)
218 if err != nil {
219 ubuntu_sdk_tools.RemoveContainerSync(client, c.name)
220
221=== modified file 'usdk-target/exec.go'
222--- usdk-target/exec.go 2016-07-06 09:59:58 +0000
223+++ usdk-target/exec.go 2017-01-26 17:58:02 +0000
224@@ -23,10 +23,10 @@
225 "fmt"
226 "os"
227 "os/user"
228+ "github.com/gorilla/websocket"
229+ "github.com/lxc/lxd"
230 "github.com/lxc/lxd/shared/gnuflag"
231 "launchpad.net/ubuntu-sdk-tools"
232- "syscall"
233- "os/exec"
234 )
235
236 type execCmd struct {
237@@ -64,14 +64,13 @@
238 c.container = args[0]
239 args = args[1:]
240
241- lxc_command, err := exec.LookPath("lxc")
242+ config := ubuntu_sdk_tools.GetConfigOrDie()
243+ d, err := lxd.NewClient(config, config.DefaultRemote)
244 if err != nil {
245 return err
246 }
247
248 lxc_args := []string {
249- lxc_command, "exec",
250- c.container, "--",
251 "su",
252 }
253
254@@ -111,7 +110,16 @@
255
256 os.Stdout.Sync()
257 os.Stderr.Sync()
258- err = syscall.Exec(lxc_command, lxc_args, os.Environ())
259- fmt.Printf("Error: %v\n", err)
260+ // Ensure the container's running first
261+ err = ubuntu_sdk_tools.BootContainerSync(d, c.container)
262+ if err != nil {
263+ return err
264+ }
265+ controlHandler := func(*lxd.Client, *websocket.Conn) {
266+ }
267+ _, err = d.Exec(c.container, lxc_args, nil, os.Stdin, os.Stdout, os.Stderr, controlHandler, 0, 0)
268+ if err != nil {
269+ return err
270+ }
271 return nil
272 }
273
274=== modified file 'usdk-target/main.go'
275--- usdk-target/main.go 2016-07-06 09:22:33 +0000
276+++ usdk-target/main.go 2017-01-26 17:58:02 +0000
277@@ -66,6 +66,9 @@
278 }
279
280 func main() {
281+ // FIXME: lp#1583259 Set environment variables in snapcraft.yaml
282+ os.Setenv("XDG_CONFIG_HOME", os.ExpandEnv("$HOME"))
283+
284 if err := run(); err != nil {
285 // The action we take depends on the error we get.
286 msg := fmt.Sprintf("error: %v", err)
287
288=== modified file 'usdk-target/register.go'
289--- usdk-target/register.go 2016-10-07 17:39:28 +0000
290+++ usdk-target/register.go 2017-01-26 17:58:02 +0000
291@@ -85,13 +85,11 @@
292 key = "PKEXEC_UID"
293 env = os.Getenv(key)
294 if len(env) == 0 {
295- return nil, nil
296+ fmt.Fprintf(os.Stderr, "Neither SUDO_UID nor PKEXEC_UID set\n")
297+ env = strconv.Itoa(os.Getuid())
298 }
299- fmt.Printf("%s\n", env)
300 }
301
302- fmt.Printf("%s\n", env)
303-
304 user, err := user.LookupId(env)
305 if err != nil {
306 return nil, fmt.Errorf("Os environment var :%s contains a invalid USER ID. error: %v", key, err)
307@@ -128,11 +126,6 @@
308 return fmt.Errorf("Registering root is not possible")
309 }
310
311- shadow,err := ubuntu_sdk_tools.Getspnam(*userName)
312- if (err != nil) {
313- return fmt.Errorf("Querying the password entry failed. error: %v", err)
314- }
315-
316 groups,err := ubuntu_sdk_tools.GetGroups()
317 if (err != nil) {
318 return fmt.Errorf("Querying the group entry failed. error: %v", err)
319@@ -173,11 +166,9 @@
320
321 fmt.Printf("Creating group %s\n", group.Name)
322
323- cmd := exec.Command("lxc", "exec", containerName, "--", "groupadd", "-g", strconv.FormatUint(uint64(group.Gid),10), group.Name)
324- cmd.Stdout = os.Stdout
325- cmd.Stderr = os.Stderr
326- err = cmd.Start()
327- if err := cmd.Wait(); err != nil {
328+ args := []string{"groupadd", "-g", strconv.FormatUint(uint64(group.Gid), 10), group.Name}
329+ _, err := client.Exec(containerName, args, nil, os.Stdin, os.Stdout, os.Stderr, nil, 0, 0)
330+ if err != nil {
331 print ("GroupAdd returned error\n")
332 if exiterr, ok := err.(*exec.ExitError); ok {
333 if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
334@@ -203,13 +194,20 @@
335 fmt.Printf("Creating user %s\n", pw.LoginName)
336
337 command := []string {
338- "exec", containerName, "--",
339 "useradd", "--no-create-home",
340 "-u", strconv.FormatUint(uint64(pw.Uid), 10),
341 "--gid", strconv.FormatUint(uint64(pw.Gid), 10),
342 "--home-dir", pw.Dir,
343 "-s", "/bin/bash",
344- "-p", shadow.Sp_pwdp,
345+ }
346+
347+ if os.Getenv("SNAP") == "" {
348+ shadow, err := ubuntu_sdk_tools.Getspnam(*userName)
349+ if (err != nil) {
350+ return fmt.Errorf("Querying the password entry failed. error: %v", err)
351+ }
352+ command = append(command, "-p")
353+ command = append(command, shadow.Sp_pwdp)
354 }
355
356 containsVideoGroup := false
357@@ -229,10 +227,6 @@
358 }
359
360 command = append(command,pw.LoginName)
361-
362- cmd := exec.Command("lxc", command...)
363- cmd.Stdout = os.Stdout
364- cmd.Stderr = os.Stderr
365-
366- return cmd.Run()
367+ _, err = client.Exec(containerName, command, nil, os.Stdin, os.Stdout, os.Stderr, nil, 0, 0)
368+ return err
369 }

Subscribers

People subscribed via source and target branches