Skip to content

Commit

Permalink
fix: ensure that lxd is started again after refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsgruk committed Nov 20, 2024
1 parent bbd14b0 commit 158c3a7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
23 changes: 18 additions & 5 deletions internal/providers/lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ func (l *LXD) Restore() error {

// install ensures that LXD is installed.
func (l *LXD) install() error {
err := l.workaroundRefresh()
// Check if LXD is already installed, and stop the snap if it is.
restart, err := l.workaroundRefresh()
if err != nil {
return err
}
Expand All @@ -116,6 +117,17 @@ func (l *LXD) install() error {
return err
}

// If we stopped the LXD snap, make sure we start it again now the refresh
// has happened.
if restart {
args := []string{"start", l.Name()}
cmd := system.NewCommand("snap", args)
_, err = l.system.RunExclusive(cmd)
if err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -151,20 +163,21 @@ func (l *LXD) deconflictFirewall() error {
// workaroundRefresh checks if LXD will be refreshed and stops it first.
// This is a workaround for an issue in the LXD snap sometimes failing
// on refresh because of a missing snap socket file.
func (l *LXD) workaroundRefresh() error {
func (l *LXD) workaroundRefresh() (bool, error) {
snapInfo, err := l.system.SnapInfo(l.Name(), l.Channel)
if err != nil {
return fmt.Errorf("failed to lookup snap details: %w", err)
return false, fmt.Errorf("failed to lookup snap details: %w", err)
}

if snapInfo.Installed {
args := []string{"stop", l.Name()}
cmd := system.NewCommand("snap", args)
_, err = l.system.RunExclusive(cmd)
if err != nil {
return fmt.Errorf("command failed: %w", err)
return false, fmt.Errorf("command failed: %w", err)
}
return true, nil
}

return nil
return false, nil
}
1 change: 1 addition & 0 deletions internal/providers/lxd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func TestLXDPrepareCommandsLXDAlreadyInstalled(t *testing.T) {
expected := []string{
"snap stop lxd",
"snap refresh lxd",
"snap start lxd",
"lxd waitready",
"lxd init --minimal",
"lxc network set lxdbr0 ipv6.address none",
Expand Down

0 comments on commit 158c3a7

Please sign in to comment.