1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
package config
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/containers/storage/pkg/ioutils"
"github.com/containers/storage/pkg/lockfile"
)
const connectionsFile = "podman-connections.json"
// connectionsConfigFile returns the path to the rw connections config file
func connectionsConfigFile() (string, error) {
if path, found := os.LookupEnv("PODMAN_CONNECTIONS_CONF"); found {
return path, nil
}
path, err := userConfigPath()
if err != nil {
return "", err
}
// file is stored next to containers.conf
return filepath.Join(filepath.Dir(path), connectionsFile), nil
}
type ConnectionConfig struct {
Default string `json:",omitempty"`
Connections map[string]Destination `json:",omitempty"`
}
type ConnectionsFile struct {
Connection ConnectionConfig `json:",omitempty"`
Farm FarmConfig `json:",omitempty"`
}
type Connection struct {
// Name of the connection
Name string
// Destination for this connection
Destination
// Default if this connection is the default
Default bool
// ReadWrite if true the connection is stored in the connections file
ReadWrite bool
}
type Farm struct {
// Name of the farm
Name string
// Connections
Connections []string
// Default if this is the default farm
Default bool
// ReadWrite if true the farm is stored in the connections file
ReadWrite bool
}
func readConnectionConf(path string) (*ConnectionsFile, error) {
conf := new(ConnectionsFile)
f, err := os.Open(path)
if err != nil {
// return empty config if file does not exists
if errors.Is(err, fs.ErrNotExist) {
return conf, nil
}
return nil, err
}
defer f.Close()
err = json.NewDecoder(f).Decode(conf)
if err != nil {
return nil, fmt.Errorf("parse %q: %w", path, err)
}
return conf, nil
}
func writeConnectionConf(path string, conf *ConnectionsFile) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
opts := &ioutils.AtomicFileWriterOptions{ExplicitCommit: true}
configFile, err := ioutils.NewAtomicFileWriterWithOpts(path, 0o644, opts)
if err != nil {
return err
}
defer configFile.Close()
err = json.NewEncoder(configFile).Encode(conf)
if err != nil {
return err
}
// If no errors commit the changes to the config file
return configFile.Commit()
}
// EditConnectionConfig must be used to edit the connections config.
// The function will read and write the file automatically and the
// callback function just needs to modify the cfg as needed.
func EditConnectionConfig(callback func(cfg *ConnectionsFile) error) error {
path, err := connectionsConfigFile()
if err != nil {
return err
}
lockPath := path + ".lock"
lock, err := lockfile.GetLockFile(lockPath)
if err != nil {
return fmt.Errorf("obtain lock file: %w", err)
}
lock.Lock()
defer lock.Unlock()
conf, err := readConnectionConf(path)
if err != nil {
return fmt.Errorf("read connections file: %w", err)
}
if conf.Farm.List == nil {
conf.Farm.List = make(map[string][]string)
}
if err := callback(conf); err != nil {
return err
}
return writeConnectionConf(path, conf)
}
func makeConnection(name string, dst Destination, def, readWrite bool) *Connection {
return &Connection{
Name: name,
Destination: dst,
Default: def,
ReadWrite: readWrite,
}
}
// GetConnection return the connection for the given name or if def is set to true then return the default connection.
func (c *Config) GetConnection(name string, def bool) (*Connection, error) {
path, err := connectionsConfigFile()
if err != nil {
return nil, err
}
conConf, err := readConnectionConf(path)
if err != nil {
return nil, err
}
defaultCon := conConf.Connection.Default
if defaultCon == "" {
defaultCon = c.Engine.ActiveService
}
if def {
if defaultCon == "" {
return nil, errors.New("no default connection found")
}
name = defaultCon
} else {
def = defaultCon == name
}
if dst, ok := conConf.Connection.Connections[name]; ok {
return makeConnection(name, dst, def, true), nil
}
if dst, ok := c.Engine.ServiceDestinations[name]; ok {
return makeConnection(name, dst, def, false), nil
}
return nil, fmt.Errorf("connection %q not found", name)
}
// GetAllConnections return all configured connections
func (c *Config) GetAllConnections() ([]Connection, error) {
path, err := connectionsConfigFile()
if err != nil {
return nil, err
}
conConf, err := readConnectionConf(path)
if err != nil {
return nil, err
}
defaultCon := conConf.Connection.Default
if defaultCon == "" {
defaultCon = c.Engine.ActiveService
}
connections := make([]Connection, 0, len(conConf.Connection.Connections))
for name, dst := range conConf.Connection.Connections {
def := defaultCon == name
connections = append(connections, *makeConnection(name, dst, def, true))
}
for name, dst := range c.Engine.ServiceDestinations {
if _, ok := conConf.Connection.Connections[name]; ok {
// connection name is overwritten by connections file
continue
}
def := defaultCon == name
connections = append(connections, *makeConnection(name, dst, def, false))
}
return connections, nil
}
func getConnections(cons []string, dests map[string]Destination) ([]Connection, error) {
connections := make([]Connection, 0, len(cons))
for _, name := range cons {
if dst, ok := dests[name]; ok {
connections = append(connections, *makeConnection(name, dst, false, false))
} else {
return nil, fmt.Errorf("connection %q not found", name)
}
}
return connections, nil
}
// GetFarmConnections return all the connections for the given farm.
func (c *Config) GetFarmConnections(name string) ([]Connection, error) {
_, cons, err := c.getFarmConnections(name, false)
return cons, err
}
// GetDefaultFarmConnections returns the name of the default farm
// and the connections.
func (c *Config) GetDefaultFarmConnections() (string, []Connection, error) {
return c.getFarmConnections("", true)
}
// getFarmConnections returns all connections for the given farm,
// if def is true it will use the default farm instead of the name.
// Returns the name of the farm and the connections for it.
func (c *Config) getFarmConnections(name string, def bool) (string, []Connection, error) {
path, err := connectionsConfigFile()
if err != nil {
return "", nil, err
}
conConf, err := readConnectionConf(path)
if err != nil {
return "", nil, err
}
defaultFarm := conConf.Farm.Default
if defaultFarm == "" {
defaultFarm = c.Farms.Default
}
if def {
if defaultFarm == "" {
return "", nil, errors.New("no default farm found")
}
name = defaultFarm
}
if cons, ok := conConf.Farm.List[name]; ok {
cons, err := getConnections(cons, conConf.Connection.Connections)
return name, cons, err
}
if cons, ok := c.Farms.List[name]; ok {
cons, err := getConnections(cons, c.Engine.ServiceDestinations)
return name, cons, err
}
return "", nil, fmt.Errorf("farm %q not found", name)
}
func makeFarm(name string, cons []string, def, readWrite bool) Farm {
return Farm{
Name: name,
Connections: cons,
Default: def,
ReadWrite: readWrite,
}
}
// GetAllFarms returns all configured farms
func (c *Config) GetAllFarms() ([]Farm, error) {
path, err := connectionsConfigFile()
if err != nil {
return nil, err
}
conConf, err := readConnectionConf(path)
if err != nil {
return nil, err
}
defaultFarm := conConf.Farm.Default
if defaultFarm == "" {
defaultFarm = c.Farms.Default
}
farms := make([]Farm, 0, len(conConf.Farm.List))
for name, cons := range conConf.Farm.List {
def := defaultFarm == name
farms = append(farms, makeFarm(name, cons, def, true))
}
for name, cons := range c.Farms.List {
if _, ok := conConf.Farm.List[name]; ok {
// farm name is overwritten by connections file
continue
}
def := defaultFarm == name
farms = append(farms, makeFarm(name, cons, def, false))
}
return farms, nil
}
|