Skip to content

Commit

Permalink
incus: Create config paths when no local daemons
Browse files Browse the repository at this point in the history
Closes lxc#345

Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Dec 29, 2023
1 parent 1d1e3bb commit 2446a58
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions cmd/incus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,48 +361,51 @@ func (c *cmdGlobal) PreRun(cmd *cobra.Command, args []string) error {
// and this is the first time the client has been run by the user, then check to see
// if the server has been properly configured. Don't display the message if the var path
// does not exist (server missing), as the user may be targeting a remote daemon.
if !c.flagForceLocal && util.PathExists(internalUtil.VarPath("")) && !util.PathExists(c.confPath) {
if !c.flagForceLocal && !util.PathExists(c.confPath) {
// Create the config dir so that we don't get in here again for this user.
err = os.MkdirAll(c.conf.ConfigDir, 0750)
if err != nil {
return err
}

// Attempt to connect to the local server
runInit := true
d, err := incus.ConnectIncusUnix("", nil)
if err == nil {
// Check if server is initialized.
info, _, err := d.GetServer()
if err == nil && info.Environment.Storage != "" {
runInit = false
}

// Detect usable project.
names, err := d.GetProjectNames()
// Handle local servers.
if util.PathExists(internalUtil.VarPath("")) {
// Attempt to connect to the local server
runInit := true
d, err := incus.ConnectIncusUnix("", nil)
if err == nil {
if len(names) == 1 && names[0] != api.ProjectDefaultName {
remote := c.conf.Remotes["local"]
remote.Project = names[0]
c.conf.Remotes["local"] = remote
// Check if server is initialized.
info, _, err := d.GetServer()
if err == nil && info.Environment.Storage != "" {
runInit = false
}

// Detect usable project.
names, err := d.GetProjectNames()
if err == nil {
if len(names) == 1 && names[0] != api.ProjectDefaultName {
remote := c.conf.Remotes["local"]
remote.Project = names[0]
c.conf.Remotes["local"] = remote
}
}
}
}

flush := false
if runInit && (cmd.Name() != "init" || cmd.Parent() == nil || cmd.Parent().Name() != "admin") {
fmt.Fprintf(os.Stderr, i18n.G("If this is your first time running Incus on this machine, you should also run: incus admin init")+"\n")
flush = true
}
flush := false
if runInit && (cmd.Name() != "init" || cmd.Parent() == nil || cmd.Parent().Name() != "admin") {
fmt.Fprintf(os.Stderr, i18n.G("If this is your first time running Incus on this machine, you should also run: incus admin init")+"\n")
flush = true
}

if !util.ValueInSlice(cmd.Name(), []string{"admin", "create", "launch"}) && (cmd.Parent() == nil || cmd.Parent().Name() != "admin") {
fmt.Fprintf(os.Stderr, i18n.G(`To start your first container, try: incus launch images:ubuntu/22.04
if !util.ValueInSlice(cmd.Name(), []string{"admin", "create", "launch"}) && (cmd.Parent() == nil || cmd.Parent().Name() != "admin") {
fmt.Fprintf(os.Stderr, i18n.G(`To start your first container, try: incus launch images:ubuntu/22.04
Or for a virtual machine: incus launch images:ubuntu/22.04 --vm`)+"\n")
flush = true
}
flush = true
}

if flush {
fmt.Fprintf(os.Stderr, "\n")
if flush {
fmt.Fprintf(os.Stderr, "\n")
}
}

// And save the initial configuration
Expand Down

0 comments on commit 2446a58

Please sign in to comment.