diff --git a/internal/provider-config/config.go b/internal/provider-config/config.go index 9fa5ab14..c37b7608 100644 --- a/internal/provider-config/config.go +++ b/internal/provider-config/config.go @@ -4,7 +4,6 @@ import ( "encoding/pem" "fmt" "os" - "path/filepath" "sync" "time" @@ -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) @@ -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. diff --git a/internal/utils/fs_unix.go b/internal/utils/fs_unix.go deleted file mode 100644 index a30d92ea..00000000 --- a/internal/utils/fs_unix.go +++ /dev/null @@ -1,20 +0,0 @@ -//go:build !windows - -package utils - -import ( - "log" - - "golang.org/x/sys/unix" -) - -// IsSocketWritable returns true if user has write permissions for socket on the given path. -func IsSocketWritable(socketPath string) bool { - err := unix.Access(socketPath, unix.W_OK) - if err != nil { - log.Printf("[DEBUG] Unix socket %q: %v", socketPath, err) - return false - } - - return true -} diff --git a/internal/utils/fs_windows.go b/internal/utils/fs_windows.go deleted file mode 100644 index 5cb5cdf5..00000000 --- a/internal/utils/fs_windows.go +++ /dev/null @@ -1,8 +0,0 @@ -//go:build windows - -package utils - -// IsSocketWritable always returns true when os is windows. -func IsSocketWritable(socketPath string) bool { - return true -}