summaryrefslogtreecommitdiff
path: root/pkg/adapter/client.go
blob: 69aa3220a6b296ac564ca368aadba11b92dcac91 (plain)
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
// +build remoteclient

package adapter

import (
	"fmt"
	"os"

	"github.com/containers/libpod/cmd/podman/remoteclientconfig"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"github.com/varlink/go/varlink"
)

var remoteEndpoint *Endpoint

func (r RemoteRuntime) RemoteEndpoint() (remoteEndpoint *Endpoint, err error) {
	remoteConfigConnections, _ := remoteclientconfig.ReadRemoteConfig(r.config)

	// If the user defines an env variable for podman_varlink_bridge
	// we use that as passed.
	if bridge := os.Getenv("PODMAN_VARLINK_BRIDGE"); bridge != "" {
		logrus.Debug("creating a varlink bridge based on env variable")
		remoteEndpoint, err = newBridgeConnection(bridge, nil, r.cmd.LogLevel)
		// if an environment variable for podman_varlink_address is defined,
		// we used that as passed
	} else if address := os.Getenv("PODMAN_VARLINK_ADDRESS"); address != "" {
		logrus.Debug("creating a varlink address based on env variable: %s", address)
		remoteEndpoint, err = newSocketConnection(address)
		//	if the user provides a remote host, we use it to configure a bridge connection
	} else if len(r.cmd.RemoteHost) > 0 {
		logrus.Debug("creating a varlink bridge based on user input")
		if len(r.cmd.RemoteUserName) < 1 {
			return nil, errors.New("you must provide a username when providing a remote host name")
		}
		rc := remoteclientconfig.RemoteConnection{r.cmd.RemoteHost, r.cmd.RemoteUserName, false}
		remoteEndpoint, err = newBridgeConnection("", &rc, r.cmd.LogLevel)
		//  if the user has a config file with connections in it
	} else if len(remoteConfigConnections.Connections) > 0 {
		logrus.Debug("creating a varlink bridge based configuration file")
		var rc *remoteclientconfig.RemoteConnection
		if len(r.cmd.ConnectionName) > 0 {
			rc, err = remoteConfigConnections.GetRemoteConnection(r.cmd.ConnectionName)
		} else {
			rc, err = remoteConfigConnections.GetDefault()
		}
		if err != nil {
			return nil, err
		}
		remoteEndpoint, err = newBridgeConnection("", rc, r.cmd.LogLevel)
		//	last resort is to make a socket connection with the default varlink address for root user
	} else {
		logrus.Debug("creating a varlink address based default root address")
		remoteEndpoint, err = newSocketConnection(DefaultAddress)
	}
	return
}

// Connect provides a varlink connection
func (r RemoteRuntime) Connect() (*varlink.Connection, error) {
	ep, err := r.RemoteEndpoint()
	if err != nil {
		return nil, err
	}

	switch ep.Type {
	case DirectConnection:
		return varlink.NewConnection(ep.Connection)
	case BridgeConnection:
		return varlink.NewBridge(ep.Connection)
	}
	return nil, errors.New(fmt.Sprintf("Unable to determine type of varlink connection: %s", ep.Connection))
}

// RefreshConnection is used to replace the current r.Conn after things like
// using an upgraded varlink connection
func (r RemoteRuntime) RefreshConnection() error {
	newConn, err := r.Connect()
	if err != nil {
		return err
	}
	r.Conn = newConn
	return nil
}

// newSocketConnection returns an endpoint for a uds based connection
func newSocketConnection(address string) (*Endpoint, error) {
	endpoint := Endpoint{
		Type:       DirectConnection,
		Connection: address,
	}
	return &endpoint, nil
}