podman

Форк
0
/
connection.go 
136 строк · 3.3 Кб
1
//go:build amd64 || arm64
2

3
package connection
4

5
import (
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

16
const LocalhostIP = "127.0.0.1"
17

18
type connection struct {
19
	name string
20
	uri  *url.URL
21
}
22

23
func addConnection(cons []connection, identity string, isDefault bool) error {
24
	if len(identity) < 1 {
25
		return errors.New("identity must be defined")
26
	}
27

28
	return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
29
		for i, con := range cons {
30
			if _, ok := cfg.Connection.Connections[con.name]; ok {
31
				return fmt.Errorf("cannot overwrite connection %q", con.name)
32
			}
33

34
			dst := config.Destination{
35
				URI:       con.uri.String(),
36
				IsMachine: true,
37
				Identity:  identity,
38
			}
39

40
			if isDefault && i == 0 {
41
				cfg.Connection.Default = con.name
42
			}
43

44
			if cfg.Connection.Connections == nil {
45
				cfg.Connection.Connections = map[string]config.Destination{
46
					con.name: dst,
47
				}
48
				cfg.Connection.Default = con.name
49
			} else {
50
				cfg.Connection.Connections[con.name] = dst
51
			}
52
		}
53

54
		return nil
55
	})
56
}
57

58
func UpdateConnectionPairPort(name string, port, uid int, remoteUsername string, identityPath string) error {
59
	cons := createConnections(name, uid, port, remoteUsername)
60
	return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
61
		for _, con := range cons {
62
			dst := config.Destination{
63
				IsMachine: true,
64
				URI:       con.uri.String(),
65
				Identity:  identityPath,
66
			}
67
			cfg.Connection.Connections[name] = dst
68
		}
69

70
		return 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
77
func UpdateConnectionIfDefault(rootful bool, name, rootfulName string) error {
78
	return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
79
		if name == cfg.Connection.Default && rootful {
80
			cfg.Connection.Default = rootfulName
81
		} else if rootfulName == cfg.Connection.Default && !rootful {
82
			cfg.Connection.Default = name
83
		}
84
		return nil
85
	})
86
}
87

88
func RemoveConnections(names ...string) error {
89
	return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
90
		for _, name := range names {
91
			if _, ok := cfg.Connection.Connections[name]; ok {
92
				delete(cfg.Connection.Connections, name)
93
			} else {
94
				return fmt.Errorf("unable to find connection named %q", name)
95
			}
96

97
			if cfg.Connection.Default == name {
98
				cfg.Connection.Default = ""
99
			}
100
		}
101
		for service := range cfg.Connection.Connections {
102
			cfg.Connection.Default = service
103
			break
104
		}
105
		return nil
106
	})
107
}
108

109
// removeFilesAndConnections removes any files and connections with the given names
110
func RemoveFilesAndConnections(files []string, names ...string) {
111
	for _, f := range files {
112
		if err := os.Remove(f); err != nil && !errors.Is(err, os.ErrNotExist) {
113
			logrus.Error(err)
114
		}
115
	}
116
	if err := RemoveConnections(names...); err != nil {
117
		logrus.Error(err)
118
	}
119
}
120

121
// makeSSHURL creates a URL from the given input
122
func makeSSHURL(host, path, port, userName string) *url.URL {
123
	var hostname string
124
	if len(port) > 0 {
125
		hostname = net.JoinHostPort(host, port)
126
	} else {
127
		hostname = host
128
	}
129
	userInfo := url.User(userName)
130
	return &url.URL{
131
		Scheme: "ssh",
132
		User:   userInfo,
133
		Host:   hostname,
134
		Path:   path,
135
	}
136
}
137

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.