mirror of
https://github.com/podman-container-tools/podman.git
synced 2026-08-25 17:57:54 +00:00
We now no longer write containers.conf, instead system connections and farms are written to a new file called podman-connections.conf. This is a major rework and I had to change a lot of things to get this to compile again with my c/common changes. It is a breaking change for users as connections/farms added before this commit can now no longer be removed or modified directly. However because the logic keeps reading from containers.conf the old connections can still be used to connect to a remote host. Signed-off-by: Paul Holzinger <pholzing@redhat.com>
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
//go:build amd64 || arm64
|
|
|
|
package machine
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/containers/common/pkg/config"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const LocalhostIP = "127.0.0.1"
|
|
|
|
func AddConnection(uri fmt.Stringer, name, identity string, isDefault bool) error {
|
|
if len(identity) < 1 {
|
|
return errors.New("identity must be defined")
|
|
}
|
|
|
|
return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
|
|
if _, ok := cfg.Connection.Connections[name]; ok {
|
|
return errors.New("cannot overwrite connection")
|
|
}
|
|
|
|
dst := config.Destination{
|
|
URI: uri.String(),
|
|
IsMachine: true,
|
|
Identity: identity,
|
|
}
|
|
|
|
if isDefault {
|
|
cfg.Connection.Default = name
|
|
}
|
|
|
|
if cfg.Connection.Connections == nil {
|
|
cfg.Connection.Connections = map[string]config.Destination{
|
|
name: dst,
|
|
}
|
|
cfg.Connection.Default = name
|
|
} else {
|
|
cfg.Connection.Connections[name] = dst
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func ChangeConnectionURI(name string, uri fmt.Stringer) error {
|
|
return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
|
|
dst, ok := cfg.Connection.Connections[name]
|
|
if !ok {
|
|
return errors.New("connection not found")
|
|
}
|
|
dst.URI = uri.String()
|
|
cfg.Connection.Connections[name] = dst
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// UpdateConnectionIfDefault updates the default connection to the rootful/rootless when depending
|
|
// on the bool but only if other rootful/less connection was already the default.
|
|
// Returns true if it modified the default
|
|
func UpdateConnectionIfDefault(rootful bool, name, rootfulName string) error {
|
|
return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
|
|
if name == cfg.Connection.Default && rootful {
|
|
cfg.Connection.Default = rootfulName
|
|
} else if rootfulName == cfg.Connection.Default && !rootful {
|
|
cfg.Connection.Default = name
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func RemoveConnections(names ...string) error {
|
|
return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
|
|
for _, name := range names {
|
|
if _, ok := cfg.Connection.Connections[name]; ok {
|
|
delete(cfg.Connection.Connections, name)
|
|
} else {
|
|
return fmt.Errorf("unable to find connection named %q", name)
|
|
}
|
|
|
|
if cfg.Connection.Default == name {
|
|
cfg.Connection.Default = ""
|
|
}
|
|
}
|
|
for service := range cfg.Connection.Connections {
|
|
cfg.Connection.Default = service
|
|
break
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// removeFilesAndConnections removes any files and connections with the given names
|
|
func RemoveFilesAndConnections(files []string, names ...string) {
|
|
for _, f := range files {
|
|
if err := os.Remove(f); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
logrus.Error(err)
|
|
}
|
|
}
|
|
if err := RemoveConnections(names...); err != nil {
|
|
logrus.Error(err)
|
|
}
|
|
}
|