mirror of https://github.com/containers/podman.git
pkg/machine: make only one AddConnection() call
This function has to read/write the connections file as such it should only ever be called once otherwise we read/write the same file twice which makes no sense. Also cleanup the fucntion a bit and make it private as there are no external callers. Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
parent
d60757cca6
commit
30a18fc02d
|
@ -2,7 +2,6 @@ package connection
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/containers/podman/v5/pkg/machine/define"
|
"github.com/containers/podman/v5/pkg/machine/define"
|
||||||
|
@ -18,19 +17,22 @@ func AddSSHConnectionsToPodmanSocket(uid, port int, identityPath, name, remoteUs
|
||||||
uri := makeSSHURL(LocalhostIP, fmt.Sprintf("/run/user/%d/podman/podman.sock", uid), strconv.Itoa(port), remoteUsername)
|
uri := makeSSHURL(LocalhostIP, fmt.Sprintf("/run/user/%d/podman/podman.sock", uid), strconv.Itoa(port), remoteUsername)
|
||||||
uriRoot := makeSSHURL(LocalhostIP, "/run/podman/podman.sock", strconv.Itoa(port), "root")
|
uriRoot := makeSSHURL(LocalhostIP, "/run/podman/podman.sock", strconv.Itoa(port), "root")
|
||||||
|
|
||||||
uris := []*url.URL{uri, uriRoot}
|
cons := []connection{
|
||||||
names := []string{name, name + "-root"}
|
{
|
||||||
|
name: name,
|
||||||
|
uri: uri,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: name + "-root",
|
||||||
|
uri: uriRoot,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// The first connection defined when connections is empty will become the default
|
// The first connection defined when connections is empty will become the default
|
||||||
// regardless of IsDefault, so order according to rootful
|
// regardless of IsDefault, so order according to rootful
|
||||||
if opts.Rootful {
|
if opts.Rootful {
|
||||||
uris[0], names[0], uris[1], names[1] = uris[1], names[1], uris[0], names[0]
|
cons[0], cons[1] = cons[1], cons[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < 2; i++ {
|
return addConnection(cons, identityPath, opts.IsDefault)
|
||||||
if err := AddConnection(uris[i], names[i], identityPath, opts.IsDefault && i == 0); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,33 +15,40 @@ import (
|
||||||
|
|
||||||
const LocalhostIP = "127.0.0.1"
|
const LocalhostIP = "127.0.0.1"
|
||||||
|
|
||||||
func AddConnection(uri *url.URL, name, identity string, isDefault bool) error {
|
type connection struct {
|
||||||
|
name string
|
||||||
|
uri *url.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
func addConnection(cons []connection, identity string, isDefault bool) error {
|
||||||
if len(identity) < 1 {
|
if len(identity) < 1 {
|
||||||
return errors.New("identity must be defined")
|
return errors.New("identity must be defined")
|
||||||
}
|
}
|
||||||
|
|
||||||
return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
|
return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
|
||||||
if _, ok := cfg.Connection.Connections[name]; ok {
|
for i, con := range cons {
|
||||||
return errors.New("cannot overwrite connection")
|
if _, ok := cfg.Connection.Connections[con.name]; ok {
|
||||||
}
|
return fmt.Errorf("cannot overwrite connection %q", con.name)
|
||||||
|
}
|
||||||
dst := config.Destination{
|
|
||||||
URI: uri.String(),
|
dst := config.Destination{
|
||||||
IsMachine: true,
|
URI: con.uri.String(),
|
||||||
Identity: identity,
|
IsMachine: true,
|
||||||
}
|
Identity: identity,
|
||||||
|
}
|
||||||
if isDefault {
|
|
||||||
cfg.Connection.Default = name
|
if isDefault && i == 0 {
|
||||||
}
|
cfg.Connection.Default = con.name
|
||||||
|
}
|
||||||
if cfg.Connection.Connections == nil {
|
|
||||||
cfg.Connection.Connections = map[string]config.Destination{
|
if cfg.Connection.Connections == nil {
|
||||||
name: dst,
|
cfg.Connection.Connections = map[string]config.Destination{
|
||||||
|
con.name: dst,
|
||||||
|
}
|
||||||
|
cfg.Connection.Default = con.name
|
||||||
|
} else {
|
||||||
|
cfg.Connection.Connections[con.name] = dst
|
||||||
}
|
}
|
||||||
cfg.Connection.Default = name
|
|
||||||
} else {
|
|
||||||
cfg.Connection.Connections[name] = dst
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue