Skip to content

Commit

Permalink
Improve Incus integration and INCUS_CONF variable support
Browse files Browse the repository at this point in the history
  • Loading branch information
geaaru committed Aug 18, 2024
1 parent 610673a commit 31245ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/executor/executor_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (e *LxdCExecutor) RunHostCommandWithOutput(command string, envs map[string]

if e.ConfigDir != "" {
elist = append(elist, fmt.Sprintf("LXD_CONF=%s", e.ConfigDir))
elist = append(elist, fmt.Sprintf("INCUS_CONF=%s", e.ConfigDir))
}

hostCommand.Stdout = outBuffer
Expand Down
26 changes: 21 additions & 5 deletions pkg/executor/lxd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path"
"strings"

helpers "github.com/MottainaiCI/lxd-compose/pkg/helpers"
lxd "github.com/canonical/lxd/client"
lxd_config "github.com/canonical/lxd/lxc/config"
)
Expand All @@ -21,15 +22,30 @@ func getLxcDefaultConfDir() (string, error) {

if os.Getenv("LXD_CONF") != "" {
configDir = os.Getenv("LXD_CONF")
} else if os.Getenv("INCUS_CONF") != "" {
configDir = os.Getenv("INCUS_CONF")
} else if os.Getenv("HOME") != "" {
configDir = path.Join(os.Getenv("HOME"), ".config", "lxc")
incusConfigDir := path.Join(
os.Getenv("HOME"), ".config", "incus")

if helpers.Exists(incusConfigDir) {
configDir = incusConfigDir
} else {
configDir = path.Join(os.Getenv("HOME"), ".config", "lxc")
}
} else {
user, err := user.Current()
if err != nil {
return "", err
}

configDir = path.Join(user.HomeDir, ".config", "lxc")
incusConfigDir := path.Join(user.HomeDir, ".config", "incus")

if helpers.Exists(incusConfigDir) {
configDir = incusConfigDir
} else {
configDir = path.Join(user.HomeDir, ".config", "lxc")
}
}

return configDir, nil
Expand All @@ -40,18 +56,18 @@ func (e *LxdCExecutor) Setup() error {

configDir, err := getLxcDefaultConfDir()
if err != nil {
return errors.New("Error on retrieve default LXD config directory: " + err.Error())
return errors.New("Error on retrieve default LXD/Incus config directory: " + err.Error())
}

if e.ConfigDir == "" {
e.ConfigDir = configDir
}
configPath := path.Join(e.ConfigDir, "/config.yml")
e.Emitter.DebugLog(false, "Using LXD config file", configPath)
e.Emitter.DebugLog(false, "Using LXD/Incus config file", configPath)

e.LxdConfig, err = lxd_config.LoadConfig(configPath)
if err != nil {
return errors.New("Error on load LXD config: " + err.Error())
return errors.New("Error on load LXD/Incus config: " + err.Error())
}

if len(e.Endpoint) > 0 {
Expand Down

0 comments on commit 31245ca

Please sign in to comment.