|
1 | 1 | package machine
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bufio" |
5 |
| - "fmt" |
6 | 4 | "os"
|
7 |
| - "path/filepath" |
8 |
| - "strings" |
9 | 5 |
|
10 |
| - "github.com/crc-org/crc/pkg/crc/constants" |
11 | 6 | "github.com/crc-org/crc/pkg/crc/logging"
|
12 | 7 | "github.com/crc-org/crc/pkg/crc/podman"
|
| 8 | + "github.com/crc-org/crc/pkg/crc/ssh" |
13 | 9 | "github.com/pkg/errors"
|
14 | 10 | )
|
15 | 11 |
|
@@ -44,58 +40,5 @@ func (client *client) Delete() error {
|
44 | 40 | logging.Warnf("Failed to remove crc contexts from kubeconfig: %v", err)
|
45 | 41 | }
|
46 | 42 | }
|
47 |
| - return removeCRCHostEntriesFromKnownHosts() |
48 |
| -} |
49 |
| - |
50 |
| -func removeCRCHostEntriesFromKnownHosts() error { |
51 |
| - knownHostsPath := filepath.Join(constants.GetHomeDir(), ".ssh", "known_hosts") |
52 |
| - if _, err := os.Stat(knownHostsPath); err != nil { |
53 |
| - return nil |
54 |
| - } |
55 |
| - f, err := os.Open(knownHostsPath) |
56 |
| - if err != nil { |
57 |
| - return fmt.Errorf("Unable to open user's 'known_hosts' file: %w", err) |
58 |
| - } |
59 |
| - defer f.Close() |
60 |
| - |
61 |
| - tempHostsFile, err := os.CreateTemp(filepath.Join(constants.GetHomeDir(), ".ssh"), "crc") |
62 |
| - if err != nil { |
63 |
| - return fmt.Errorf("Unable to create temp file: %w", err) |
64 |
| - } |
65 |
| - defer func() { |
66 |
| - tempHostsFile.Close() |
67 |
| - os.Remove(tempHostsFile.Name()) |
68 |
| - }() |
69 |
| - |
70 |
| - if err := tempHostsFile.Chmod(0600); err != nil { |
71 |
| - return fmt.Errorf("Error trying to change permissions for temp file: %w", err) |
72 |
| - } |
73 |
| - |
74 |
| - var foundCRCEntries bool |
75 |
| - scanner := bufio.NewScanner(f) |
76 |
| - writer := bufio.NewWriter(tempHostsFile) |
77 |
| - for scanner.Scan() { |
78 |
| - if strings.Contains(scanner.Text(), "[127.0.0.1]:2222") || strings.Contains(scanner.Text(), "192.168.130.11") { |
79 |
| - foundCRCEntries = true |
80 |
| - continue |
81 |
| - } |
82 |
| - if _, err := writer.WriteString(fmt.Sprintf("%s\n", scanner.Text())); err != nil { |
83 |
| - return fmt.Errorf("Error while writing hostsfile content to temp file: %w", err) |
84 |
| - } |
85 |
| - } |
86 |
| - |
87 |
| - if err := writer.Flush(); err != nil { |
88 |
| - return fmt.Errorf("Error while flushing buffered content to temp file: %w", err) |
89 |
| - } |
90 |
| - |
91 |
| - if foundCRCEntries { |
92 |
| - if err := f.Close(); err != nil { |
93 |
| - return fmt.Errorf("Error closing known_hosts file: %w", err) |
94 |
| - } |
95 |
| - if err := tempHostsFile.Close(); err != nil { |
96 |
| - return fmt.Errorf("Error closing temp file: %w", err) |
97 |
| - } |
98 |
| - return os.Rename(tempHostsFile.Name(), knownHostsPath) |
99 |
| - } |
100 |
| - return nil |
| 43 | + return ssh.RemoveCRCHostEntriesFromKnownHosts() |
101 | 44 | }
|
0 commit comments