Skip to content

Commit

Permalink
Add optional install path
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
crosbymichael committed Sep 11, 2018
1 parent ed2bf6d commit 60d13d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
7 changes: 7 additions & 0 deletions cmd/ctr/commands/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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...)
},
}
41 changes: 26 additions & 15 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions install_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}

0 comments on commit 60d13d6

Please sign in to comment.