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 <crosbymichael@gmail.com>
This commit is contained in:
		| @@ -37,6 +37,10 @@ var Command = cli.Command{ | |||||||
| 			Name:  "replace,r", | 			Name:  "replace,r", | ||||||
| 			Usage: "replace any binaries or libs in the opt directory", | 			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 { | 	Action: func(context *cli.Context) error { | ||||||
| 		client, ctx, cancel, err := commands.NewClient(context) | 		client, ctx, cancel, err := commands.NewClient(context) | ||||||
| @@ -56,6 +60,9 @@ var Command = cli.Command{ | |||||||
| 		if context.Bool("replace") { | 		if context.Bool("replace") { | ||||||
| 			opts = append(opts, containerd.WithInstallReplace) | 			opts = append(opts, containerd.WithInstallReplace) | ||||||
| 		} | 		} | ||||||
|  | 		if path := context.String("path"); path != "" { | ||||||
|  | 			opts = append(opts, containerd.WithInstallPath(path)) | ||||||
|  | 		} | ||||||
| 		return client.Install(ctx, image, opts...) | 		return client.Install(ctx, image, opts...) | ||||||
| 	}, | 	}, | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										41
									
								
								install.go
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								install.go
									
									
									
									
									
								
							| @@ -33,25 +33,14 @@ import ( | |||||||
|  |  | ||||||
| // Install a binary image into the opt service | // Install a binary image into the opt service | ||||||
| func (c *Client) Install(ctx context.Context, image Image, opts ...InstallOpts) error { | 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 | 	var config InstallConfig | ||||||
| 	for _, o := range opts { | 	for _, o := range opts { | ||||||
| 		o(&config) | 		o(&config) | ||||||
| 	} | 	} | ||||||
|  | 	path, err := c.getInstallPath(ctx, config) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
| 	var ( | 	var ( | ||||||
| 		cs       = image.ContentStore() | 		cs       = image.ContentStore() | ||||||
| 		platform = platforms.Default() | 		platform = platforms.Default() | ||||||
| @@ -89,3 +78,25 @@ func (c *Client) Install(ctx context.Context, image Image, opts ...InstallOpts) | |||||||
| 	} | 	} | ||||||
| 	return nil | 	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 | ||||||
|  | } | ||||||
|   | |||||||
| @@ -25,6 +25,8 @@ type InstallConfig struct { | |||||||
| 	Libs bool | 	Libs bool | ||||||
| 	// Replace will overwrite existing binaries or libs in the opt directory | 	// Replace will overwrite existing binaries or libs in the opt directory | ||||||
| 	Replace bool | 	Replace bool | ||||||
|  | 	// Path to install libs and binaries to | ||||||
|  | 	Path string | ||||||
| } | } | ||||||
|  |  | ||||||
| // WithInstallLibs installs libs from the image | // WithInstallLibs installs libs from the image | ||||||
| @@ -36,3 +38,10 @@ func WithInstallLibs(c *InstallConfig) { | |||||||
| func WithInstallReplace(c *InstallConfig) { | func WithInstallReplace(c *InstallConfig) { | ||||||
| 	c.Replace = true | 	c.Replace = true | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // WithInstallPath sets the optional install path | ||||||
|  | func WithInstallPath(path string) InstallOpts { | ||||||
|  | 	return func(c *InstallConfig) { | ||||||
|  | 		c.Path = path | ||||||
|  | 	} | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Michael Crosby
					Michael Crosby