fix: do not try to reset machine when removing unreachable machine

This commit is contained in:
Pasha Sviderski
2025-08-06 15:11:27 +10:00
parent fc0bf4a91b
commit 6c244bb8f9
3 changed files with 13 additions and 16 deletions
+11 -11
View File
@@ -90,11 +90,13 @@ func remove(ctx context.Context, uncli *cli.CLI, nameOrID string, opts removeOpt
reset := !opts.noReset reset := !opts.noReset
var containers []api.ServiceContainer var containers []api.ServiceContainer
reachable := false
if reset { if reset {
// Check if the machine is up and has service containers. // Check if the machine is up and has service containers.
listOpts := container.ListOptions{All: true} listOpts := container.ListOptions{All: true}
machineContainers, err := client.Docker.ListServiceContainers(mctx, "", listOpts) machineContainers, err := client.Docker.ListServiceContainers(mctx, "", listOpts)
if err == nil { if err == nil {
reachable = true
containers = machineContainers[0].Containers containers = machineContainers[0].Containers
if len(containers) > 0 { if len(containers) > 0 {
plural := "" plural := ""
@@ -104,7 +106,7 @@ func remove(ctx context.Context, uncli *cli.CLI, nameOrID string, opts removeOpt
fmt.Printf("Found %d service container%s on machine '%s':\n", len(containers), plural, m.Name) fmt.Printf("Found %d service container%s on machine '%s':\n", len(containers), plural, m.Name)
fmt.Println(formatContainerTree(containers)) fmt.Println(formatContainerTree(containers))
fmt.Println() fmt.Println()
fmt.Println("This will remove all service containers on the machine, remove it from the cluster, " + fmt.Println("This will remove all service containers from the machine, remove it from the cluster, " +
"and reset it to the uninitialised state.") "and reset it to the uninitialised state.")
} else { } else {
fmt.Printf("No service containers found on machine '%s'.\n", m.Name) fmt.Printf("No service containers found on machine '%s'.\n", m.Name)
@@ -129,16 +131,14 @@ func remove(ctx context.Context, uncli *cli.CLI, nameOrID string, opts removeOpt
} }
} }
if reset { if reset && len(containers) > 0 {
if len(containers) > 0 { err = progress.RunWithTitle(ctx, func(ctx context.Context) error {
err = progress.RunWithTitle(ctx, func(ctx context.Context) error { return removeContainers(ctx, client, containers)
return removeContainers(ctx, client, containers) }, uncli.ProgressOut(), "Removing containers")
}, uncli.ProgressOut(), "Removing containers") if err != nil {
if err != nil { return fmt.Errorf("remove containers: %w", err)
return fmt.Errorf("remove containers: %w", err)
}
fmt.Println()
} }
fmt.Println()
} }
if _, err = client.RemoveMachine(ctx, &pb.RemoveMachineRequest{Id: m.Id}); err != nil { if _, err = client.RemoveMachine(ctx, &pb.RemoveMachineRequest{Id: m.Id}); err != nil {
@@ -146,7 +146,7 @@ func remove(ctx context.Context, uncli *cli.CLI, nameOrID string, opts removeOpt
} }
fmt.Printf("Machine '%s' removed from the cluster.\n", m.Name) fmt.Printf("Machine '%s' removed from the cluster.\n", m.Name)
if reset { if reset && reachable {
_, err = client.MachineClient.Reset(mctx, &pb.ResetRequest{}) _, err = client.MachineClient.Reset(mctx, &pb.ResetRequest{})
if err != nil { if err != nil {
fmt.Printf("WARNING: Failed to reset machine: %v\n", err) fmt.Printf("WARNING: Failed to reset machine: %v\n", err)
+1 -4
View File
@@ -446,10 +446,7 @@ func (m *Machine) Run(ctx context.Context) error {
slog.Info("Local API proxy server stopped.") slog.Info("Local API proxy server stopped.")
// Clean up the machine data and resources if the machine shutdown was initiated by a reset. // Clean up the machine data and resources if the machine shutdown was initiated by a reset.
m.mu.RLock() if m.resetting {
resetting := m.resetting
m.mu.RUnlock()
if resetting {
slog.Info("Cleaning up machine data and resources.") slog.Info("Cleaning up machine data and resources.")
if err = m.cleanup(); err != nil { if err = m.cleanup(); err != nil {
slog.Error("Failed to clean up machine data and resources.", "err", err) slog.Error("Failed to clean up machine data and resources.", "err", err)
+1 -1
View File
@@ -63,7 +63,7 @@ func (c *SSHConnector) Connect(ctx context.Context) (*grpc.ClientConn, error) {
conn, dErr := c.client.DialContext(ctx, "unix", addr) conn, dErr := c.client.DialContext(ctx, "unix", addr)
if dErr != nil { if dErr != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"connect to machine API socket '%s' through SSH tunnel (is the Uncloud daemon running "+ "connect to machine API socket '%s' through SSH tunnel (is uncloud.service running "+
"on the remote machine and does the SSH user '%s' have permissions to access the socket?):"+ "on the remote machine and does the SSH user '%s' have permissions to access the socket?):"+
" %w", " %w",
addr, c.client.User(), dErr, addr, c.client.User(), dErr,