podman
136 строк · 3.3 Кб
1//go:build amd64 || arm64
2
3package connection
4
5import (
6"errors"
7"fmt"
8"net"
9"net/url"
10"os"
11
12"github.com/containers/common/pkg/config"
13"github.com/sirupsen/logrus"
14)
15
16const LocalhostIP = "127.0.0.1"
17
18type connection struct {
19name string
20uri *url.URL
21}
22
23func addConnection(cons []connection, identity string, isDefault bool) error {
24if len(identity) < 1 {
25return errors.New("identity must be defined")
26}
27
28return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
29for i, con := range cons {
30if _, ok := cfg.Connection.Connections[con.name]; ok {
31return fmt.Errorf("cannot overwrite connection %q", con.name)
32}
33
34dst := config.Destination{
35URI: con.uri.String(),
36IsMachine: true,
37Identity: identity,
38}
39
40if isDefault && i == 0 {
41cfg.Connection.Default = con.name
42}
43
44if cfg.Connection.Connections == nil {
45cfg.Connection.Connections = map[string]config.Destination{
46con.name: dst,
47}
48cfg.Connection.Default = con.name
49} else {
50cfg.Connection.Connections[con.name] = dst
51}
52}
53
54return nil
55})
56}
57
58func UpdateConnectionPairPort(name string, port, uid int, remoteUsername string, identityPath string) error {
59cons := createConnections(name, uid, port, remoteUsername)
60return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
61for _, con := range cons {
62dst := config.Destination{
63IsMachine: true,
64URI: con.uri.String(),
65Identity: identityPath,
66}
67cfg.Connection.Connections[name] = dst
68}
69
70return nil
71})
72}
73
74// UpdateConnectionIfDefault updates the default connection to the rootful/rootless when depending
75// on the bool but only if other rootful/less connection was already the default.
76// Returns true if it modified the default
77func UpdateConnectionIfDefault(rootful bool, name, rootfulName string) error {
78return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
79if name == cfg.Connection.Default && rootful {
80cfg.Connection.Default = rootfulName
81} else if rootfulName == cfg.Connection.Default && !rootful {
82cfg.Connection.Default = name
83}
84return nil
85})
86}
87
88func RemoveConnections(names ...string) error {
89return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
90for _, name := range names {
91if _, ok := cfg.Connection.Connections[name]; ok {
92delete(cfg.Connection.Connections, name)
93} else {
94return fmt.Errorf("unable to find connection named %q", name)
95}
96
97if cfg.Connection.Default == name {
98cfg.Connection.Default = ""
99}
100}
101for service := range cfg.Connection.Connections {
102cfg.Connection.Default = service
103break
104}
105return nil
106})
107}
108
109// removeFilesAndConnections removes any files and connections with the given names
110func RemoveFilesAndConnections(files []string, names ...string) {
111for _, f := range files {
112if err := os.Remove(f); err != nil && !errors.Is(err, os.ErrNotExist) {
113logrus.Error(err)
114}
115}
116if err := RemoveConnections(names...); err != nil {
117logrus.Error(err)
118}
119}
120
121// makeSSHURL creates a URL from the given input
122func makeSSHURL(host, path, port, userName string) *url.URL {
123var hostname string
124if len(port) > 0 {
125hostname = net.JoinHostPort(host, port)
126} else {
127hostname = host
128}
129userInfo := url.User(userName)
130return &url.URL{
131Scheme: "ssh",
132User: userInfo,
133Host: hostname,
134Path: path,
135}
136}
137