Skip to content

Commit

Permalink
microcloud/service/lxd: Check if LXD is initialized before restarting
Browse files Browse the repository at this point in the history
Signed-off-by: Max Asnaashari <[email protected]>
  • Loading branch information
masnax committed Jul 24, 2023
1 parent d0089b4 commit b58bf50
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions microcloud/service/lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,13 +687,33 @@ func (s *LXDService) SetConfig(target string, secret string, config map[string]s
return c.UpdateServer(newServer, "")
}

// isInitialized checks if LXD is initialized by fetching the storage pools.
// If none exist, that means LXD has not yet been set up.
func (s *LXDService) isInitialized(c lxd.InstanceServer) (bool, error) {
pools, err := c.GetStoragePools()
if err != nil {
return false, err
}

return len(pools) != 0, nil
}

// Restart requests LXD to shutdown, then waits until it is ready.
func (s *LXDService) Restart(timeoutSeconds int) error {
c, err := s.client("")
if err != nil {
return err
}

isInit, err := s.isInitialized(c)
if err != nil {
return fmt.Errorf("Failed to check LXD initialization: %w", err)
}

if isInit {
return fmt.Errorf("LXD has already been set up")
}

_, _, err = c.RawQuery("PUT", "/internal/shutdown", nil, "")
if err != nil {
return fmt.Errorf("Failed to send shutdown request to LXD: %w", err)
Expand Down

0 comments on commit b58bf50

Please sign in to comment.