Skip to content

Commit 75eca6e

Browse files
deevusclaude
andcommitted
refactor: extract clearAndRefreshHostKey and clearKnownHosts helpers
DRY up the repeated remove-known-hosts-then-wait-ssh pattern across Create, Start, RestoreSnapshot, and Delete in backend.go. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ca54624 commit 75eca6e

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

sandbox/truenas/backend.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ import (
1616
"github.com/deevus/pixels/sandbox"
1717
)
1818

19+
// clearAndRefreshHostKey removes stale known_hosts entries for both IP and hostname,
20+
// then waits for SSH readiness.
21+
func (t *TrueNAS) clearAndRefreshHostKey(ctx context.Context, name, ip, hostname string, timeout time.Duration) {
22+
t.clearKnownHosts(ip, hostname)
23+
if err := t.ssh.WaitReady(ctx, hostname, timeout, nil); err != nil {
24+
t.warnf("ssh wait %s: %v", name, err)
25+
}
26+
}
27+
28+
// clearKnownHosts removes known_hosts entries for both IP and hostname.
29+
func (t *TrueNAS) clearKnownHosts(ip, hostname string) {
30+
if ip != "" {
31+
ssh.RemoveKnownHost(t.cfg.knownHosts, ip)
32+
}
33+
ssh.RemoveKnownHost(t.cfg.knownHosts, hostname)
34+
}
35+
1936
// Create creates a new container instance with the full provisioning flow:
2037
// NIC resolution, instance creation, provisioning, restart, IP poll, SSH wait.
2138
// When opts.Bare is true, only the instance is created (no provisioning or SSH wait).
@@ -121,12 +138,7 @@ func (t *TrueNAS) Create(ctx context.Context, opts sandbox.CreateOpts) (*sandbox
121138

122139
// Wait for SSH readiness.
123140
if ip != "" {
124-
// Remove stale known_hosts entries — the container was just created.
125-
ssh.RemoveKnownHost(t.cfg.knownHosts, ip)
126-
ssh.RemoveKnownHost(t.cfg.knownHosts, full)
127-
if err := t.ssh.WaitReady(ctx, full, 90*time.Second, nil); err != nil {
128-
t.warnf("ssh wait %s: %v", name, err)
129-
}
141+
t.clearAndRefreshHostKey(ctx, name, ip, full, 90*time.Second)
130142
}
131143

132144
return &sandbox.Instance{
@@ -180,12 +192,7 @@ func (t *TrueNAS) Start(ctx context.Context, name string) error {
180192

181193
ip := ipFromAliases(inst.Aliases)
182194
if ip != "" {
183-
// Remove stale known_hosts entries — host key may differ after restart.
184-
ssh.RemoveKnownHost(t.cfg.knownHosts, ip)
185-
ssh.RemoveKnownHost(t.cfg.knownHosts, full)
186-
if err := t.ssh.WaitReady(ctx, full, 30*time.Second, nil); err != nil {
187-
t.warnf("ssh wait %s: %v", name, err)
188-
}
195+
t.clearAndRefreshHostKey(ctx, name, ip, full, 30*time.Second)
189196
}
190197
return nil
191198
}
@@ -222,10 +229,7 @@ func (t *TrueNAS) Delete(ctx context.Context, name string) error {
222229
}
223230

224231
// Clean up known_hosts entries for the now-dead container.
225-
if ip != "" {
226-
ssh.RemoveKnownHost(t.cfg.knownHosts, ip)
227-
}
228-
ssh.RemoveKnownHost(t.cfg.knownHosts, full)
232+
t.clearKnownHosts(ip, full)
229233
return nil
230234
}
231235

@@ -300,12 +304,7 @@ func (t *TrueNAS) RestoreSnapshot(ctx context.Context, name, label string) error
300304

301305
ip := ipFromAliases(inst.Aliases)
302306
if ip != "" {
303-
// Remove stale known_hosts entries — snapshot restore changes the host key.
304-
ssh.RemoveKnownHost(t.cfg.knownHosts, ip)
305-
ssh.RemoveKnownHost(t.cfg.knownHosts, full)
306-
if err := t.ssh.WaitReady(ctx, full, 30*time.Second, nil); err != nil {
307-
t.warnf("ssh wait %s: %v", name, err)
308-
}
307+
t.clearAndRefreshHostKey(ctx, name, ip, full, 30*time.Second)
309308
}
310309
return nil
311310
}

0 commit comments

Comments
 (0)