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 <jakecorrenti+github@proton.me>
This commit is contained in:
Jake Correnti 2023-08-01 10:52:46 -04:00
parent 850482b314
commit 55c7b5ceca
4 changed files with 57 additions and 91 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}