summaryrefslogtreecommitdiff
path: root/cmd/podman/varlink.go
blob: 38ce77415d0efcde4a3c0bfb7e49e51b341848a0 (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
// +build varlink

package main

import (
	"time"

	"github.com/containers/libpod/cmd/podman/libpodruntime"
	iopodman "github.com/containers/libpod/cmd/podman/varlink"
	"github.com/containers/libpod/pkg/varlinkapi"
	"github.com/containers/libpod/version"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"github.com/urfave/cli"
	"github.com/varlink/go/varlink"
)

var (
	varlinkDescription = `
	podman varlink

	run varlink interface
`
	varlinkFlags = []cli.Flag{
		cli.IntFlag{
			Name:  "timeout, t",
			Usage: "Time until the varlink session expires in milliseconds.  Use 0 to disable the timeout.",
			Value: 1000,
		},
	}
	varlinkCommand = &cli.Command{
		Name:         "varlink",
		Usage:        "Run varlink interface",
		Description:  varlinkDescription,
		Flags:        sortFlags(varlinkFlags),
		Action:       varlinkCmd,
		ArgsUsage:    "VARLINK_URI",
		OnUsageError: usageErrorHandler,
	}
)

func varlinkCmd(c *cli.Context) error {
	args := c.Args()
	if len(args) < 1 {
		return errors.Errorf("you must provide a varlink URI")
	}
	timeout := time.Duration(c.Int64("timeout")) * time.Millisecond

	// Create a single runtime for varlink
	runtime, err := libpodruntime.GetRuntime(c)
	if err != nil {
		return errors.Wrapf(err, "error creating libpod runtime")
	}
	defer runtime.Shutdown(false)

	var varlinkInterfaces = []*iopodman.VarlinkInterface{varlinkapi.New(c, runtime)}
	// Register varlink service. The metadata can be retrieved with:
	// $ varlink info [varlink address URI]
	service, err := varlink.NewService(
		"Atomic",
		"podman",
		version.Version,
		"https://github.com/containers/libpod",
	)
	if err != nil {
		return errors.Wrapf(err, "unable to create new varlink service")
	}

	for _, i := range varlinkInterfaces {
		if err := service.RegisterInterface(i); err != nil {
			return errors.Errorf("unable to register varlink interface %v", i)
		}
	}

	// Run the varlink server at the given address
	if err = service.Listen(args[0], timeout); err != nil {
		switch err.(type) {
		case varlink.ServiceTimeoutError:
			logrus.Infof("varlink service expired (use --timeout to increase session time beyond %d ms, 0 means never timeout)", c.Int64("timeout"))
			return nil
		default:
			return errors.Errorf("unable to start varlink service")
		}
	}

	return nil
}