feat: deploy all machine keys (mesh)
When deploying keys to a machine, upload ALL private keys from ALL other machines (not just sync pair peers). Also populate known_hosts with all other machine hosts. Creates a full mesh where any machine can SSH to any other. - sshmanager/deploy.go: change knownHostsHost string parameter to knownHostsHosts []string for multi-host ssh-keyscan - handlers_machines.go: replace sync-pair-based key detection with loop over all machines, deduplicating by local key path
This commit is contained in:
@@ -26,7 +26,7 @@ type DeployResult struct {
|
||||
Errors []string
|
||||
}
|
||||
|
||||
func DeployKeysToMachine(ctx context.Context, serverKeyPath, serverPubKey string, host string, port int, user string, keys []DeployKey, knownHostsHost string, addServerPubKey bool) (*DeployResult, error) {
|
||||
func DeployKeysToMachine(ctx context.Context, serverKeyPath, serverPubKey string, host string, port int, user string, keys []DeployKey, knownHostsHosts []string, addServerPubKey bool) (*DeployResult, error) {
|
||||
result := &DeployResult{Success: true, Messages: []string{}, Errors: []string{}}
|
||||
|
||||
addr := fmt.Sprintf("%s:%d", host, port)
|
||||
@@ -141,22 +141,24 @@ func DeployKeysToMachine(ctx context.Context, serverKeyPath, serverPubKey string
|
||||
result.Messages = append(result.Messages, fmt.Sprintf("Uploaded %s to %s:%s", filepath.Base(k.LocalPath), host, k.RemotePath))
|
||||
}
|
||||
|
||||
if knownHostsHost != "" {
|
||||
session2, err := conn.NewSession()
|
||||
if err != nil {
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("session for ssh-keyscan: %v", err))
|
||||
result.Success = false
|
||||
} else {
|
||||
if len(knownHostsHosts) > 0 {
|
||||
for _, khHost := range knownHostsHosts {
|
||||
session2, err := conn.NewSession()
|
||||
if err != nil {
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("session for ssh-keyscan %s: %v", khHost, err))
|
||||
result.Success = false
|
||||
continue
|
||||
}
|
||||
session2.Stdout = &stdout
|
||||
session2.Stderr = &stderr
|
||||
err := session2.Run(fmt.Sprintf("ssh-keyscan -H -p %d %s 2>/dev/null >> %s/known_hosts", port, knownHostsHost, remoteSSHDir))
|
||||
err = session2.Run(fmt.Sprintf("ssh-keyscan -H -p %s 2>/dev/null >> %s/known_hosts", khHost, remoteSSHDir))
|
||||
session2.Close()
|
||||
if err != nil {
|
||||
slog.Warn("deploy: ssh-keyscan failed", "host", knownHostsHost, "port", port, "error", err)
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("ssh-keyscan %s: %v (stderr: %s)", knownHostsHost, err, stderr.String()))
|
||||
slog.Warn("deploy: ssh-keyscan failed", "host", khHost, "error", err)
|
||||
result.Errors = append(result.Errors, fmt.Sprintf("ssh-keyscan %s: %v (stderr: %s)", khHost, err, stderr.String()))
|
||||
result.Success = false
|
||||
} else {
|
||||
result.Messages = append(result.Messages, fmt.Sprintf("Populated known_hosts with %s:%d", knownHostsHost, port))
|
||||
result.Messages = append(result.Messages, fmt.Sprintf("Populated known_hosts with %s", khHost))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user