[lxc-devel] [lxd/master] client: Introduce LXD_SOCKET

stgraber on Github lxc-bot at linuxcontainers.org
Mon Apr 9 00:50:56 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 439 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180409/97aabcfc/attachment.bin>
-------------- next part --------------
From f9f2cafa489983ca858275085715ac3cf06da38d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 9 Apr 2018 02:49:34 +0200
Subject: [PATCH] client: Introduce LXD_SOCKET
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

LXD_SOCKET overrides whatever socket path would normally be used.

Closes #4422

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 client/connection.go       | 18 +++++++++++-------
 lxd/daemon.go              |  6 ++++++
 lxd/endpoints/endpoints.go |  8 +++++++-
 lxd/endpoints/local.go     |  5 +----
 4 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/client/connection.go b/client/connection.go
index 27d64b7bf8..a2ebbdc5f6 100644
--- a/client/connection.go
+++ b/client/connection.go
@@ -65,8 +65,9 @@ func ConnectLXD(url string, args *ConnectionArgs) (ContainerServer, error) {
 
 // ConnectLXDUnix lets you connect to a remote LXD daemon over a local unix socket.
 //
-// If the path argument is empty, then $LXD_DIR/unix.socket will be used.
-// If that one isn't set either, then the path will default to /var/lib/lxd/unix.socket.
+// If the path argument is empty, then $LXD_SOCKET will be used, if
+// unset $LXD_DIR/unix.socket will be used and if that one isn't set
+// either, then the path will default to /var/lib/lxd/unix.socket.
 func ConnectLXDUnix(path string, args *ConnectionArgs) (ContainerServer, error) {
 	logger.Debugf("Connecting to a local LXD over a Unix socket")
 
@@ -84,12 +85,15 @@ func ConnectLXDUnix(path string, args *ConnectionArgs) (ContainerServer, error)
 
 	// Determine the socket path
 	if path == "" {
-		lxdDir := os.Getenv("LXD_DIR")
-		if lxdDir == "" {
-			lxdDir = "/var/lib/lxd"
+		path = os.Getenv("LXD_SOCKET")
+		if path == "" {
+			lxdDir := os.Getenv("LXD_DIR")
+			if lxdDir == "" {
+				lxdDir = "/var/lib/lxd"
+			}
+
+			path = filepath.Join(lxdDir, "unix.socket")
 		}
-
-		path = filepath.Join(lxdDir, "unix.socket")
 	}
 
 	// Setup the HTTP client
diff --git a/lxd/daemon.go b/lxd/daemon.go
index 4328953dc8..a045bb3396 100644
--- a/lxd/daemon.go
+++ b/lxd/daemon.go
@@ -216,6 +216,11 @@ func (d *Daemon) State() *state.State {
 // UnixSocket returns the full path to the unix.socket file that this daemon is
 // listening on. Used by tests.
 func (d *Daemon) UnixSocket() string {
+	path := os.Getenv("LXD_SOCKET")
+	if path != "" {
+		return path
+	}
+
 	return filepath.Join(d.os.VarDir, "unix.socket")
 }
 
@@ -443,6 +448,7 @@ func (d *Daemon) init() error {
 	/* Setup the web server */
 	config := &endpoints.Config{
 		Dir:                  d.os.VarDir,
+		UnixSocket:           d.UnixSocket(),
 		Cert:                 certInfo,
 		RestServer:           RestServer(d),
 		DevLxdServer:         DevLxdServer(d),
diff --git a/lxd/endpoints/endpoints.go b/lxd/endpoints/endpoints.go
index 75894398a4..4b4192670f 100644
--- a/lxd/endpoints/endpoints.go
+++ b/lxd/endpoints/endpoints.go
@@ -19,6 +19,9 @@ type Config struct {
 	// The LXD var directory to create Unix sockets in.
 	Dir string
 
+	// UnixSocket is the path to the Unix socket to bind
+	UnixSocket string
+
 	// HTTP server handling requests for the LXD RESTful API.
 	RestServer *http.Server
 
@@ -87,6 +90,9 @@ func Up(config *Config) (*Endpoints, error) {
 	if config.Dir == "" {
 		return nil, fmt.Errorf("no directory configured")
 	}
+	if config.UnixSocket == "" {
+		return nil, fmt.Errorf("no unix socket configured")
+	}
 	if config.RestServer == nil {
 		return nil, fmt.Errorf("no REST server configured")
 	}
@@ -148,7 +154,7 @@ func (e *Endpoints) up(config *Config) error {
 		logger.Infof("LXD isn't socket activated")
 		e.listeners = map[kind]net.Listener{}
 
-		e.listeners[local], err = localCreateListener(config.Dir, config.LocalUnixSocketGroup)
+		e.listeners[local], err = localCreateListener(config.UnixSocket, config.LocalUnixSocketGroup)
 		if err != nil {
 			return fmt.Errorf("local endpoint: %v", err)
 		}
diff --git a/lxd/endpoints/local.go b/lxd/endpoints/local.go
index c91db701e4..59ef1edf7e 100644
--- a/lxd/endpoints/local.go
+++ b/lxd/endpoints/local.go
@@ -2,13 +2,10 @@ package endpoints
 
 import (
 	"net"
-	"path/filepath"
 )
 
 // Create a new net.Listener bound to the unix socket of the local endpoint.
-func localCreateListener(dir string, group string) (net.Listener, error) {
-	path := filepath.Join(dir, "unix.socket")
-
+func localCreateListener(path string, group string) (net.Listener, error) {
 	err := CheckAlreadyRunning(path)
 	if err != nil {
 		return nil, err


More information about the lxc-devel mailing list