Skip to content

Commit 6c244bb

Browse files
committed
fix: do not try to reset machine when removing unreachable machine
1 parent fc0bf4a commit 6c244bb

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

cmd/uncloud/machine/rm.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ func remove(ctx context.Context, uncli *cli.CLI, nameOrID string, opts removeOpt
9090

9191
reset := !opts.noReset
9292
var containers []api.ServiceContainer
93+
reachable := false
9394
if reset {
9495
// Check if the machine is up and has service containers.
9596
listOpts := container.ListOptions{All: true}
9697
machineContainers, err := client.Docker.ListServiceContainers(mctx, "", listOpts)
9798
if err == nil {
99+
reachable = true
98100
containers = machineContainers[0].Containers
99101
if len(containers) > 0 {
100102
plural := ""
@@ -104,7 +106,7 @@ func remove(ctx context.Context, uncli *cli.CLI, nameOrID string, opts removeOpt
104106
fmt.Printf("Found %d service container%s on machine '%s':\n", len(containers), plural, m.Name)
105107
fmt.Println(formatContainerTree(containers))
106108
fmt.Println()
107-
fmt.Println("This will remove all service containers on the machine, remove it from the cluster, " +
109+
fmt.Println("This will remove all service containers from the machine, remove it from the cluster, " +
108110
"and reset it to the uninitialised state.")
109111
} else {
110112
fmt.Printf("No service containers found on machine '%s'.\n", m.Name)
@@ -129,24 +131,22 @@ func remove(ctx context.Context, uncli *cli.CLI, nameOrID string, opts removeOpt
129131
}
130132
}
131133

132-
if reset {
133-
if len(containers) > 0 {
134-
err = progress.RunWithTitle(ctx, func(ctx context.Context) error {
135-
return removeContainers(ctx, client, containers)
136-
}, uncli.ProgressOut(), "Removing containers")
137-
if err != nil {
138-
return fmt.Errorf("remove containers: %w", err)
139-
}
140-
fmt.Println()
134+
if reset && len(containers) > 0 {
135+
err = progress.RunWithTitle(ctx, func(ctx context.Context) error {
136+
return removeContainers(ctx, client, containers)
137+
}, uncli.ProgressOut(), "Removing containers")
138+
if err != nil {
139+
return fmt.Errorf("remove containers: %w", err)
141140
}
141+
fmt.Println()
142142
}
143143

144144
if _, err = client.RemoveMachine(ctx, &pb.RemoveMachineRequest{Id: m.Id}); err != nil {
145145
return fmt.Errorf("remove machine from cluster: %w", err)
146146
}
147147
fmt.Printf("Machine '%s' removed from the cluster.\n", m.Name)
148148

149-
if reset {
149+
if reset && reachable {
150150
_, err = client.MachineClient.Reset(mctx, &pb.ResetRequest{})
151151
if err != nil {
152152
fmt.Printf("WARNING: Failed to reset machine: %v\n", err)

internal/machine/machine.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,7 @@ func (m *Machine) Run(ctx context.Context) error {
446446
slog.Info("Local API proxy server stopped.")
447447

448448
// Clean up the machine data and resources if the machine shutdown was initiated by a reset.
449-
m.mu.RLock()
450-
resetting := m.resetting
451-
m.mu.RUnlock()
452-
if resetting {
449+
if m.resetting {
453450
slog.Info("Cleaning up machine data and resources.")
454451
if err = m.cleanup(); err != nil {
455452
slog.Error("Failed to clean up machine data and resources.", "err", err)

pkg/client/connector/ssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (c *SSHConnector) Connect(ctx context.Context) (*grpc.ClientConn, error) {
6363
conn, dErr := c.client.DialContext(ctx, "unix", addr)
6464
if dErr != nil {
6565
return nil, fmt.Errorf(
66-
"connect to machine API socket '%s' through SSH tunnel (is the Uncloud daemon running "+
66+
"connect to machine API socket '%s' through SSH tunnel (is uncloud.service running "+
6767
"on the remote machine and does the SSH user '%s' have permissions to access the socket?):"+
6868
" %w",
6969
addr, c.client.User(), dErr,

0 commit comments

Comments
 (0)