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

package adapter

import (
	"fmt"
	"os"

	"github.com/pkg/errors"
	"github.com/varlink/go/varlink"
)

type VarlinkConnectionInfo struct {
	RemoteUserName string
	RemoteHost     string
	VarlinkAddress string
}

// Connect provides a varlink connection
func (r RemoteRuntime) Connect() (*varlink.Connection, error) {
	var (
		err        error
		connection *varlink.Connection
	)

	logLevel := r.cmd.LogLevel

	// I'm leaving this here for now as a document of the birdge format.  It can be removed later once the bridge
	// function is more flushed out.
	//bridge := `ssh -T root@192.168.122.1 "/usr/bin/varlink -A '/usr/bin/podman varlink \$VARLINK_ADDRESS' bridge"`
	if len(r.cmd.RemoteHost) > 0 {
		// The user has provided a remote host endpoint
		if len(r.cmd.RemoteUserName) < 1 {
			return nil, errors.New("you must provide a username when providing a remote host name")
		}
		bridge := fmt.Sprintf(`ssh -T %s@%s /usr/bin/varlink -A \'/usr/bin/podman --log-level=%s varlink \\\$VARLINK_ADDRESS\' bridge`, r.cmd.RemoteUserName, r.cmd.RemoteHost, logLevel)
		connection, err = varlink.NewBridge(bridge)
	} else if bridge := os.Getenv("PODMAN_VARLINK_BRIDGE"); bridge != "" {
		connection, err = varlink.NewBridge(bridge)
	} else {
		address := os.Getenv("PODMAN_VARLINK_ADDRESS")
		if address == "" {
			address = DefaultAddress
		}
		connection, err = varlink.NewConnection(address)
	}
	if err != nil {
		return nil, err
	}
	return connection, nil
}

// 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
}