← Back to team overview

ubuntu-sdk-team team mailing list archive

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

 

Christian Dywan has proposed merging lp:~kalikiana/ubuntu-sdk-tools/lxd-snap into lp:ubuntu-sdk-tools.

Commit message:
Detect and use lxd from the snap

Requested reviews:
  Ubuntu SDK team (ubuntu-sdk-team)

For more details, see:
https://code.launchpad.net/~kalikiana/ubuntu-sdk-tools/lxd-snap/+merge/308901
-- 
Your team Ubuntu SDK team is requested to review the proposed merge of lp:~kalikiana/ubuntu-sdk-tools/lxd-snap into lp:ubuntu-sdk-tools.
=== modified file 'fixables/containerAccess.go'
--- fixables/containerAccess.go	2016-06-24 12:12:46 +0000
+++ fixables/containerAccess.go	2017-01-24 11:01:51 +0000
@@ -48,11 +48,11 @@
 
 	if fi.Mode() != os.ModeDir | ubuntu_sdk_tools.LxdContainerPerm {
 		if !doFix {
-			return fmt.Errorf("Wrong directory permissions. Container rootfs of %s is not accessible.", container)
+			fmt.Printf("Wrong directory permissions. Container rootfs of %s is not accessible.", container)
 		} else {
 			err = os.Chmod(targetPath, ubuntu_sdk_tools.LxdContainerPerm)
 			if err != nil {
-				return fmt.Errorf("Failed to make container readable. error: %v.\n",err)
+				fmt.Printf("Failed to make container readable. error: %v.\n",err)
 			}
 		}
 

=== modified file 'fixables/dri.go'
--- fixables/dri.go	2016-06-24 12:12:46 +0000
+++ fixables/dri.go	2017-01-24 11:01:51 +0000
@@ -23,11 +23,18 @@
 	"launchpad.net/ubuntu-sdk-tools"
 	"github.com/lxc/lxd/shared"
 	"fmt"
+	"os"
 )
 
 type DRIFixable struct { }
 
 func (*DRIFixable) run(client *lxd.Client, container *shared.ContainerInfo, doFix bool) error {
+	// FIXME: dri device isn't accessible under confinement
+	if os.Getenv("SNAP") != "" {
+		fmt.Fprintf(os.Stderr, "Skipping adding /dev/dri/card*\n")
+		return nil
+	}
+
 	files, err := filepath.Glob("/dev/dri/card*")
 	if err != nil {
 		return err
@@ -105,4 +112,4 @@
 
 func (*DRIFixable) NeedsRoot () bool {
 	return false
-}
\ No newline at end of file
+}

=== modified file 'helpers.go'
--- helpers.go	2016-07-06 09:22:33 +0000
+++ helpers.go	2017-01-24 11:01:51 +0000
@@ -30,7 +30,6 @@
 	"os"
 	"fmt"
 	"log"
-	"os/exec"
 	"strings"
 )
 
@@ -56,31 +55,17 @@
 	}
 
 	defaultRemoteName  := "ubuntu-sdk-images"
-	remotes := config.Remotes
-	sdkRem, ok := remotes[defaultRemoteName]
-	if ok {
-		if sdkRem.Addr == defaultImageRemote {
-			return
-		} else {
-			cmd := exec.Command("lxc", "remote", "remove", defaultRemoteName)
-			err := cmd.Run()
-			if (err != nil) {
-				fmt.Fprintf(os.Stderr, "Could not remove the remote "+defaultRemoteName+". error: %v\n", err)
-				fmt.Fprintf(os.Stderr, "Please remove it manually.\n", err)
-				os.Exit(1)
-			}
-		}
-	}
-
-	cmd := exec.Command("lxc", "remote", "add", "ubuntu-sdk-images", defaultImageRemote, "--accept-certificate", "--protocol=simplestreams")
-	err := cmd.Run()
-	if (err != nil) {
-		fmt.Fprintf(os.Stderr, "Could not register remote. error: %v\n", err)
-		os.Exit(1)
-	}
 
 	//make sure config is loaded again
 	globConfig = nil
+	//XXX: globConfig needs reloading here?
+
+	config.Remotes[defaultRemoteName] = lxd.RemoteConfig{
+		Addr:     defaultImageRemote,
+		Static:   true,
+		Public:   true,
+		Protocol: "simplestreams",
+	}
 }
 
 func GetConfigOrDie ()  (*lxd.Config) {
@@ -89,10 +74,7 @@
 		return globConfig
 	}
 
-	configDir := "$HOME/.config/lxc"
-	if os.Getenv("LXD_CONF") != "" {
-		configDir = os.Getenv("LXD_CONF")
-	}
+	configDir := "$XDG_CONFIG_HOME/ubuntu-sdk-target"
 	configPath := os.ExpandEnv(path.Join(configDir, "config.yml"))
 
 	globConfig, err := lxd.LoadConfig(configPath)
@@ -117,6 +99,15 @@
 		}
 	}
 
+	_, err = lxd.NewClient(globConfig, globConfig.DefaultRemote)
+	if err != nil {
+		os.Setenv("LXD_DIR", "/var/snap/lxd/common/lxd")
+		_, err = lxd.NewClient(globConfig, globConfig.DefaultRemote)
+		if err != nil {
+			log.Fatal("Can't establish a working socket connection: %s", err)
+		}
+	}
+
 	return globConfig
 }
 
@@ -193,15 +184,11 @@
 	}
 
 	command := []string {
-		"exec", container, "--",
 		"bash", "-c", "rm /etc/ld.so.cache; ldconfig",
 	}
 
-	cmd := exec.Command("lxc", command...)
-	cmd.Stdout = os.Stdout
-	cmd.Stderr = os.Stderr
-
-	return cmd.Run()
+	_, err = client.Exec(container, command, nil, os.Stdin, os.Stdout, os.Stderr, nil, 0, 0)
+	return err
 }
 
 func AddDeviceSync (client *lxd.Client, container, devname, devtype string, props []string) error{
@@ -316,4 +303,4 @@
 	}
 
 	return clickTargets, nil
-}
\ No newline at end of file
+}

=== added file 'snapcraft.yaml'
--- snapcraft.yaml	1970-01-01 00:00:00 +0000
+++ snapcraft.yaml	2017-01-24 11:01:51 +0000
@@ -0,0 +1,23 @@
+name: ubuntu-sdk-target
+version: '0.6'
+summary: Command line interface for creating and building with SDK containers
+description: |
+  This command line tool allows creating, building with, updating and
+  removing SDK containers based on LXD.
+
+grade: devel
+confinement: strict
+
+apps:
+    ubuntu-sdk-target:
+        command: usdk-target
+        plugs: [network, lxd]
+
+parts:
+  ubuntu-sdk-tools:
+    plugin: go
+    go-importpath: launchpad.net/ubuntu-sdk-tools
+    go-packages:
+      - launchpad.net/ubuntu-sdk-tools/usdk-target
+    source: .
+    build-packages: [bzr, gcc]

=== modified file 'usdk-target/create.go'
--- usdk-target/create.go	2016-10-05 12:48:19 +0000
+++ usdk-target/create.go	2017-01-24 11:01:51 +0000
@@ -73,7 +73,9 @@
 	}
 
 	if os.Getuid() != 0 {
-		return fmt.Errorf("This command needs to run as root")
+		if os.Getenv("SNAP") == "" {
+			return fmt.Errorf("This command needs to run as root")
+		}
 	}
 
 	config := ubuntu_sdk_tools.GetConfigOrDie()
@@ -148,6 +150,7 @@
 		return err
 	}
 
+	fmt.Fprintf(os.Stderr, "Initializing progress tracker\n")
 	c.initProgressTracker(client, resp.Operation)
 	err = client.WaitForSuccess(resp.Operation)
 
@@ -179,18 +182,21 @@
 	}
 
 	//add the required devices
+	fmt.Fprintf(os.Stderr, "Adding devices to the container\n")
 	err = ubuntu_sdk_tools.AddDeviceSync(client, c.name, "tmp", "disk", []string{"source=/tmp", "path=/tmp", "recursive=true"})
 	if err != nil {
 		ubuntu_sdk_tools.RemoveContainerSync(client, c.name)
 		return err
 	}
 
+	fmt.Fprintf(os.Stderr, "Adding the user\n")
 	err = RegisterUserInContainer(client, c.name, nil, c.createSupGroups)
 	if err != nil {
 		ubuntu_sdk_tools.RemoveContainerSync(client, c.name)
 		return err
 	}
 
+	fmt.Fprintf(os.Stderr, "Updating the configuration\n")
 	ubuntu_sdk_tools.UpdateConfigSync(client, c.name)
 	if err != nil {
 		ubuntu_sdk_tools.RemoveContainerSync(client, c.name)

=== modified file 'usdk-target/exec.go'
--- usdk-target/exec.go	2016-07-06 09:59:58 +0000
+++ usdk-target/exec.go	2017-01-24 11:01:51 +0000
@@ -23,10 +23,10 @@
 	"fmt"
 	"os"
 	"os/user"
+	"github.com/gorilla/websocket"
+	"github.com/lxc/lxd"
 	"github.com/lxc/lxd/shared/gnuflag"
 	"launchpad.net/ubuntu-sdk-tools"
-	"syscall"
-	"os/exec"
 )
 
 type execCmd struct {
@@ -64,14 +64,13 @@
 	c.container = args[0]
 	args = args[1:]
 
-	lxc_command, err := exec.LookPath("lxc")
+	config := ubuntu_sdk_tools.GetConfigOrDie()
+	d, err := lxd.NewClient(config, config.DefaultRemote)
 	if err != nil {
 		return err
 	}
 
 	lxc_args := []string {
-		lxc_command, "exec",
-		c.container, "--",
 		"su",
 	}
 
@@ -111,7 +110,16 @@
 
 	os.Stdout.Sync()
 	os.Stderr.Sync()
-	err = syscall.Exec(lxc_command, lxc_args, os.Environ())
-	fmt.Printf("Error: %v\n", err)
+	// Ensure the container's running first
+	err = ubuntu_sdk_tools.BootContainerSync(d, c.container)
+	if err != nil {
+		return err
+	}
+	controlHandler := func(*lxd.Client, *websocket.Conn) {
+	}
+	_, err = d.Exec(c.container, lxc_args, nil, os.Stdin, os.Stdout, os.Stderr, controlHandler, 0, 0)
+	if err != nil {
+		return err
+	}
 	return nil
 }

=== modified file 'usdk-target/main.go'
--- usdk-target/main.go	2016-07-06 09:22:33 +0000
+++ usdk-target/main.go	2017-01-24 11:01:51 +0000
@@ -66,6 +66,9 @@
 }
 
 func main() {
+	// FIXME: lp#1583259 Set environment variables in snapcraft.yaml
+	os.Setenv("XDG_CONFIG_HOME", os.ExpandEnv("$HOME"))
+
 	if err := run(); err != nil {
 		// The action we take depends on the error we get.
 		msg := fmt.Sprintf("error: %v", err)

=== modified file 'usdk-target/register.go'
--- usdk-target/register.go	2016-10-07 17:39:28 +0000
+++ usdk-target/register.go	2017-01-24 11:01:51 +0000
@@ -85,13 +85,11 @@
 		key = "PKEXEC_UID"
 		env = os.Getenv(key)
 		if len(env) == 0 {
-			return nil, nil
+			fmt.Fprintf(os.Stderr, "Neither SUDO_UID nor PKEXEC_UID set\n")
+			env = strconv.Itoa(os.Getuid())
 		}
-		fmt.Printf("%s\n", env)
 	}
 
-	fmt.Printf("%s\n", env)
-
 	user, err := user.LookupId(env)
 	if err != nil {
 		return nil, fmt.Errorf("Os environment var :%s contains a invalid USER ID. error: %v", key, err)
@@ -128,10 +126,12 @@
 		return fmt.Errorf("Registering root is not possible")
 	}
 
+	/* FIXME: error while looking up passwd for $USER: permission denied
 	shadow,err := ubuntu_sdk_tools.Getspnam(*userName)
 	if (err != nil) {
 		return fmt.Errorf("Querying the password entry failed. error: %v", err)
 	}
+	*/
 
 	groups,err := ubuntu_sdk_tools.GetGroups()
 	if (err != nil) {
@@ -173,11 +173,9 @@
 
 		fmt.Printf("Creating group %s\n", group.Name)
 
-		cmd := exec.Command("lxc", "exec", containerName, "--", "groupadd", "-g",  strconv.FormatUint(uint64(group.Gid),10), group.Name)
-		cmd.Stdout = os.Stdout
-		cmd.Stderr = os.Stderr
-		err = cmd.Start()
-		if err := cmd.Wait(); err != nil {
+		args := []string{"groupadd", "-g", strconv.FormatUint(uint64(group.Gid), 10), group.Name}
+		_, err := client.Exec(containerName, args, nil, os.Stdin, os.Stdout, os.Stderr, nil, 0, 0)
+		if err != nil {
 			print ("GroupAdd returned error\n")
 			if exiterr, ok := err.(*exec.ExitError); ok {
 				if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
@@ -203,13 +201,12 @@
 	fmt.Printf("Creating user %s\n", pw.LoginName)
 
 	command := []string {
-		"exec", containerName, "--",
 		"useradd", "--no-create-home",
 		"-u", strconv.FormatUint(uint64(pw.Uid), 10),
 		"--gid", strconv.FormatUint(uint64(pw.Gid), 10),
 		"--home-dir", pw.Dir,
 		"-s", "/bin/bash",
-		"-p", shadow.Sp_pwdp,
+		// FIXME: "-p", shadow.Sp_pwdp,
 	}
 
 	containsVideoGroup := false
@@ -229,10 +226,6 @@
 	}
 
 	command = append(command,pw.LoginName)
-
-	cmd := exec.Command("lxc", command...)
-	cmd.Stdout = os.Stdout
-	cmd.Stderr = os.Stderr
-
-	return cmd.Run()
+	_, err = client.Exec(containerName, command, nil, os.Stdin, os.Stdout, os.Stderr, nil, 0, 0)
+	return err
 }


Follow ups