From 60d13d63c435f1159b309128a9cac1639cc9e46f Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 11 Sep 2018 10:36:00 -0400 Subject: [PATCH] Add optional install path This allows users to consume the install functionality but also install to other areas instead of the managed `/opt` dir. ```bash > ctr install --path /usr/local ``` Signed-off-by: Michael Crosby --- cmd/ctr/commands/install/install.go | 7 +++++ install.go | 41 ++++++++++++++++++----------- install_opts.go | 9 +++++++ 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/cmd/ctr/commands/install/install.go b/cmd/ctr/commands/install/install.go index 4113418fbf8b..e04fa926a2f0 100644 --- a/cmd/ctr/commands/install/install.go +++ b/cmd/ctr/commands/install/install.go @@ -37,6 +37,10 @@ var Command = cli.Command{ Name: "replace,r", Usage: "replace any binaries or libs in the opt directory", }, + cli.StringFlag{ + Name: "path", + Usage: "set an optional install path other than the managed opt directory", + }, }, Action: func(context *cli.Context) error { client, ctx, cancel, err := commands.NewClient(context) @@ -56,6 +60,9 @@ var Command = cli.Command{ if context.Bool("replace") { opts = append(opts, containerd.WithInstallReplace) } + if path := context.String("path"); path != "" { + opts = append(opts, containerd.WithInstallPath(path)) + } return client.Install(ctx, image, opts...) }, } diff --git a/install.go b/install.go index 2aa8b039469b..5e4c6a2c8d40 100644 --- a/install.go +++ b/install.go @@ -33,25 +33,14 @@ import ( // Install a binary image into the opt service func (c *Client) Install(ctx context.Context, image Image, opts ...InstallOpts) error { - resp, err := c.IntrospectionService().Plugins(ctx, &introspectionapi.PluginsRequest{ - Filters: []string{ - "id==opt", - }, - }) - if err != nil { - return err - } - if len(resp.Plugins) != 1 { - return errors.New("opt service not enabled") - } - path := resp.Plugins[0].Exports["path"] - if path == "" { - return errors.New("opt path not exported") - } var config InstallConfig for _, o := range opts { o(&config) } + path, err := c.getInstallPath(ctx, config) + if err != nil { + return err + } var ( cs = image.ContentStore() platform = platforms.Default() @@ -89,3 +78,25 @@ func (c *Client) Install(ctx context.Context, image Image, opts ...InstallOpts) } return nil } + +func (c *Client) getInstallPath(ctx context.Context, config InstallConfig) (string, error) { + if config.Path != "" { + return config.Path, nil + } + resp, err := c.IntrospectionService().Plugins(ctx, &introspectionapi.PluginsRequest{ + Filters: []string{ + "id==opt", + }, + }) + if err != nil { + return "", err + } + if len(resp.Plugins) != 1 { + return "", errors.New("opt service not enabled") + } + path := resp.Plugins[0].Exports["path"] + if path == "" { + return "", errors.New("opt path not exported") + } + return path, nil +} diff --git a/install_opts.go b/install_opts.go index b11e7f3d6d0f..b0c9213cb277 100644 --- a/install_opts.go +++ b/install_opts.go @@ -25,6 +25,8 @@ type InstallConfig struct { Libs bool // Replace will overwrite existing binaries or libs in the opt directory Replace bool + // Path to install libs and binaries to + Path string } // WithInstallLibs installs libs from the image @@ -36,3 +38,10 @@ func WithInstallLibs(c *InstallConfig) { func WithInstallReplace(c *InstallConfig) { c.Replace = true } + +// WithInstallPath sets the optional install path +func WithInstallPath(path string) InstallOpts { + return func(c *InstallConfig) { + c.Path = path + } +}