Skip to content

Commit

Permalink
Merge pull request #485 from MusicDin/fix/unix-socket
Browse files Browse the repository at this point in the history
Let LXD decide the unix socket
  • Loading branch information
simondeziel authored Jun 27, 2024
2 parents 460ee87 + 2986423 commit de57494
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 82 deletions.
56 changes: 2 additions & 54 deletions internal/provider-config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/pem"
"fmt"
"os"
"path/filepath"
"sync"
"time"

Expand Down Expand Up @@ -153,22 +152,10 @@ func (p *LxdProviderConfig) server(remoteName string) (lxd.Server, error) {
}
}

lxdRemoteConfig := p.getLxdConfigRemote(remoteName)

// If remote address is not provided or is only set to the prefix for
// Unix sockets (`unix://`) then determine which LXD directory
// contains a writable unix socket.
if lxdRemoteConfig.Addr == "" || lxdRemoteConfig.Addr == "unix://" {
lxdDir, err := determineLxdDir()
if err != nil {
return nil, err
}

_ = os.Setenv("LXD_DIR", lxdDir)
}

var err error

lxdRemoteConfig := p.getLxdConfigRemote(remoteName)

switch lxdRemoteConfig.Protocol {
case "simplestreams":
server, err = p.getLxdConfigImageServer(remoteName)
Expand Down Expand Up @@ -384,45 +371,6 @@ func determineLxdDaemonAddr(remote LxdProviderRemoteConfig) string {
return daemonAddr
}

// determineLxdDir determines which standard LXD directory contains a writable UNIX socket.
// If environment variable LXD_DIR or LXD_SOCKET is set, the function will return LXD directory
// based on the value from any of those variables.
func determineLxdDir() (string, error) {
lxdSocket, ok := os.LookupEnv("LXD_SOCKET")
if ok {
if utils.IsSocketWritable(lxdSocket) {
return filepath.Dir(lxdSocket), nil
}

return "", fmt.Errorf("Environment variable LXD_SOCKET points to either a non-existing or non-writable unix socket")
}

lxdDir, ok := os.LookupEnv("LXD_DIR")
if ok {
socketPath := filepath.Join(lxdDir, "unix.socket")
if utils.IsSocketWritable(socketPath) {
return lxdDir, nil
}

return "", fmt.Errorf("Environment variable LXD_DIR points to a LXD directory that does not contain a writable unix socket")
}

lxdDirs := []string{
"/var/lib/lxd",
"/var/snap/lxd/common/lxd",
}

// Iterate over LXD directories and find a writable unix socket.
for _, lxdDir := range lxdDirs {
socketPath := filepath.Join(lxdDir, "unix.socket")
if utils.IsSocketWritable(socketPath) {
return lxdDir, nil
}
}

return "", fmt.Errorf("LXD socket with write permissions not found. Searched LXD directories: %v", lxdDirs)
}

/* Getters & Setters */

// remote returns LXD remote with the given name or default otherwise.
Expand Down
20 changes: 0 additions & 20 deletions internal/utils/fs_unix.go

This file was deleted.

8 changes: 0 additions & 8 deletions internal/utils/fs_windows.go

This file was deleted.

0 comments on commit de57494

Please sign in to comment.