Fix down(): only remove local private key if server deletion succeeded

Previously removed the local ephemeral key unconditionally, even when
the Hetzner API call to delete the server failed -- meaning a failed
teardown could both leave the server running AND destroy the one thing
that could get back into it to inspect or manually clean up. Confirmed
missing in exactly this scenario while investigating ci run #62
(2026-08-05): server still alive, local key already gone.
This commit is contained in:
Philip Peterson 2026-08-05 03:49:22 -08:00
parent 1462524931
commit 5f57db6c82

11
main.go
View file

@ -343,9 +343,11 @@ func up(ctx context.Context, client *hcloud.Client, token string, cfg config) (*
func down(ctx context.Context, client *hcloud.Client, st *state) error {
var errs []string
serverDeleted := true
if st.ServerID != 0 {
if _, _, err := client.Server.DeleteWithResult(ctx, &hcloud.Server{ID: st.ServerID}); err != nil {
errs = append(errs, fmt.Sprintf("delete server %d: %v", st.ServerID, err))
serverDeleted = false
}
}
if st.SSHKeyID != 0 {
@ -353,7 +355,14 @@ func down(ctx context.Context, client *hcloud.Client, st *state) error {
errs = append(errs, fmt.Sprintf("delete ssh key %d: %v", st.SSHKeyID, err))
}
}
if st.PrivKeyPath != "" {
// Only remove the local private key once the server itself is
// confirmed gone. Removing it unconditionally (as this used to do)
// meant a failed/errored server deletion still discarded the one
// thing that could get back into the now-orphaned box to inspect or
// manually clean it up -- confirmed missing in exactly this scenario
// investigating ci run #62 (2026-08-05): server still alive, local
// key already gone, no way back in.
if st.PrivKeyPath != "" && serverDeleted {
os.Remove(st.PrivKeyPath)
}
if len(errs) > 0 {