From 55c7b5cecae2bc445a9af7c7c9a2ee8c8f8f6840 Mon Sep 17 00:00:00 2001 From: Jake Correnti Date: Tue, 1 Aug 2023 10:52:46 -0400 Subject: [PATCH] Move `addSSHConnectionsToPodmanSocket` code to shared file Moves the implementation of `addSSHConnectionsToPodmanSocket` into the common file `pkg/machine/machine_common.go`. The implementation was shared between the hypervisors and does not need to be implemented multiple times. Signed-off-by: Jake Correnti --- pkg/machine/applehv/machine.go | 39 ++++++++------------------------- pkg/machine/hyperv/machine.go | 39 ++++++++------------------------- pkg/machine/machine_common.go | 30 +++++++++++++++++++++++++ pkg/machine/qemu/machine.go | 40 ++++++++-------------------------- 4 files changed, 57 insertions(+), 91 deletions(-) diff --git a/pkg/machine/applehv/machine.go b/pkg/machine/applehv/machine.go index e018d71d78..6677712c9f 100644 --- a/pkg/machine/applehv/machine.go +++ b/pkg/machine/applehv/machine.go @@ -10,7 +10,6 @@ import ( "fmt" "io/fs" "net" - "net/url" "os" "os/exec" "path/filepath" @@ -169,34 +168,6 @@ func (m *MacMachine) addMountsToVM(opts machine.InitOptions, virtiofsMnts *[]mac return nil } -func (m *MacMachine) addSSHConnectionsToPodmanSocket(opts machine.InitOptions) error { - if len(opts.IgnitionPath) < 1 { - // TODO localhost needs to be restored here - uri := machine.SSHRemoteConnection.MakeSSHURL("localhost", fmt.Sprintf("/run/user/%d/podman/podman.sock", m.UID), strconv.Itoa(m.Port), m.RemoteUsername) - uriRoot := machine.SSHRemoteConnection.MakeSSHURL("localhost", "/run/podman/podman.sock", strconv.Itoa(m.Port), "root") - identity := m.IdentityPath - - uris := []url.URL{uri, uriRoot} - names := []string{m.Name, m.Name + "-root"} - - // The first connection defined when connections is empty will become the default - // regardless of IsDefault, so order according to rootful - if opts.Rootful { - uris[0], names[0], uris[1], names[1] = uris[1], names[1], uris[0], names[0] - } - - for i := 0; i < 2; i++ { - if err := machine.AddConnection(&uris[i], names[i], identity, opts.IsDefault && i == 0); err != nil { - return err - } - } - } else { - fmt.Println("An ignition path was provided. No SSH connection was added to Podman") - } - - return nil -} - // writeIgnitionConfigFile generates the ignition config and writes it to the filesystem func (m *MacMachine) writeIgnitionConfigFile(opts machine.InitOptions, key string, virtiofsMnts *[]machine.VirtIoFs) error { // Write the ignition file @@ -300,7 +271,15 @@ func (m *MacMachine) Init(opts machine.InitOptions) (bool, error) { return false, err } - if err := m.addSSHConnectionsToPodmanSocket(opts); err != nil { + err = machine.AddSSHConnectionsToPodmanSocket( + m.UID, + m.Port, + m.IdentityPath, + m.Name, + m.RemoteUsername, + opts, + ) + if err != nil { return false, err } diff --git a/pkg/machine/hyperv/machine.go b/pkg/machine/hyperv/machine.go index 5f18bb8a17..353b3a3502 100644 --- a/pkg/machine/hyperv/machine.go +++ b/pkg/machine/hyperv/machine.go @@ -9,10 +9,8 @@ import ( "errors" "fmt" "io/fs" - "net/url" "os" "path/filepath" - "strconv" "time" "github.com/containers/common/pkg/config" @@ -83,33 +81,6 @@ func (m *HyperVMachine) addNetworkAndReadySocketsToRegistry() error { return nil } -// addSSHConnectionsToPodmanSocket adds SSH connections to the podman socket if -// no ignition path was provided -func (m *HyperVMachine) addSSHConnectionsToPodmanSocket(opts machine.InitOptions) error { - if len(opts.IgnitionPath) < 1 { - uri := machine.SSHRemoteConnection.MakeSSHURL(machine.LocalhostIP, fmt.Sprintf("/run/user/%d/podman/podman.sock", m.UID), strconv.Itoa(m.Port), m.RemoteUsername) - uriRoot := machine.SSHRemoteConnection.MakeSSHURL(machine.LocalhostIP, "/run/podman/podman.sock", strconv.Itoa(m.Port), "root") - - uris := []url.URL{uri, uriRoot} - names := []string{m.Name, m.Name + "-root"} - - // The first connection defined when connections is empty will become the default - // regardless of IsDefault, so order according to rootful - if opts.Rootful { - uris[0], names[0], uris[1], names[1] = uris[1], names[1], uris[0], names[0] - } - - for i := 0; i < 2; i++ { - if err := machine.AddConnection(&uris[i], names[i], m.IdentityPath, opts.IsDefault && i == 0); err != nil { - return err - } - } - } else { - fmt.Println("An ignition path was provided. No SSH connection was added to Podman") - } - return nil -} - // writeIgnitionConfigFile generates the ignition config and writes it to the // filesystem func (m *HyperVMachine) writeIgnitionConfigFile(opts machine.InitOptions, user, key string) error { @@ -246,7 +217,15 @@ func (m *HyperVMachine) Init(opts machine.InitOptions) (bool, error) { } m.Port = sshPort - if err := m.addSSHConnectionsToPodmanSocket(opts); err != nil { + err = machine.AddSSHConnectionsToPodmanSocket( + m.UID, + m.Port, + m.IdentityPath, + m.Name, + m.RemoteUsername, + opts, + ) + if err != nil { return false, err } diff --git a/pkg/machine/machine_common.go b/pkg/machine/machine_common.go index c781dd9a74..ae34d771b6 100644 --- a/pkg/machine/machine_common.go +++ b/pkg/machine/machine_common.go @@ -4,7 +4,10 @@ package machine import ( + "fmt" + "net/url" "os" + "strconv" ) // getDevNullFiles returns pointers to Read-only and Write-only DevNull files @@ -24,3 +27,30 @@ func GetDevNullFiles() (*os.File, *os.File, error) { return dnr, dnw, nil } + +// AddSSHConnectionsToPodmanSocket adds SSH connections to the podman socket if +// no ignition path is provided +func AddSSHConnectionsToPodmanSocket(uid, port int, identityPath, name, remoteUsername string, opts InitOptions) error { + if len(opts.IgnitionPath) < 1 { + uri := SSHRemoteConnection.MakeSSHURL(LocalhostIP, fmt.Sprintf("/run/user/%d/podman/podman.sock", uid), strconv.Itoa(port), remoteUsername) + uriRoot := SSHRemoteConnection.MakeSSHURL(LocalhostIP, "/run/podman/podman.sock", strconv.Itoa(port), "root") + + uris := []url.URL{uri, uriRoot} + names := []string{name, name + "-root"} + + // The first connection defined when connections is empty will become the default + // regardless of IsDefault, so order according to rootful + if opts.Rootful { + uris[0], names[0], uris[1], names[1] = uris[1], names[1], uris[0], names[0] + } + + for i := 0; i < 2; i++ { + if err := AddConnection(&uris[i], names[i], identityPath, opts.IsDefault && i == 0); err != nil { + return err + } + } + } else { + fmt.Println("An ignition path was provided. No SSH connection was added to Podman") + } + return nil +} diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go index 699fccfca6..bc23298a3c 100644 --- a/pkg/machine/qemu/machine.go +++ b/pkg/machine/qemu/machine.go @@ -12,7 +12,6 @@ import ( "fmt" "io/fs" "net" - "net/url" "os" "os/exec" "os/signal" @@ -266,35 +265,6 @@ func (v *MachineVM) addMountsToVM(opts machine.InitOptions) error { return nil } -// addSSHConnectionsToPodmanSocket adds SSH connections to the podman socket if -// no ignition path is provided -func (v *MachineVM) addSSHConnectionsToPodmanSocket(opts machine.InitOptions) error { - // This kind of stinks but no other way around this r/n - if len(opts.IgnitionPath) > 0 { - fmt.Println("An ignition path was provided. No SSH connection was added to Podman") - return nil - } - - uri := machine.SSHRemoteConnection.MakeSSHURL(machine.LocalhostIP, fmt.Sprintf("/run/user/%d/podman/podman.sock", v.UID), strconv.Itoa(v.Port), v.RemoteUsername) - uriRoot := machine.SSHRemoteConnection.MakeSSHURL(machine.LocalhostIP, "/run/podman/podman.sock", strconv.Itoa(v.Port), "root") - - uris := []url.URL{uri, uriRoot} - names := []string{v.Name, v.Name + "-root"} - - // The first connection defined when connections is empty will become the default - // regardless of IsDefault, so order according to rootful - if opts.Rootful { - uris[0], names[0], uris[1], names[1] = uris[1], names[1], uris[0], names[0] - } - - for i := 0; i < 2; i++ { - if err := machine.AddConnection(&uris[i], names[i], v.IdentityPath, opts.IsDefault && i == 0); err != nil { - return err - } - } - return nil -} - // writeIgnitionConfigFile generates the ignition config and writes it to the // filesystem func (v *MachineVM) writeIgnitionConfigFile(opts machine.InitOptions, key string) error { @@ -364,7 +334,15 @@ func (v *MachineVM) Init(opts machine.InitOptions) (bool, error) { // Add location of bootable image v.CmdLine = append(v.CmdLine, "-drive", "if=virtio,file="+v.getImageFile()) - if err := v.addSSHConnectionsToPodmanSocket(opts); err != nil { + err := machine.AddSSHConnectionsToPodmanSocket( + v.UID, + v.Port, + v.IdentityPath, + v.Name, + v.RemoteUsername, + opts, + ) + if err != nil { return false, err }