2019-05-31 08:27:41 -04:00
//go:generate struct-markdown
2019-10-14 10:43:59 -04:00
//go:generate mapstructure-to-hcl2 -type Config
2019-05-31 08:27:41 -04:00
2016-05-26 23:21:55 -04:00
package lxd
import (
"fmt"
2018-01-22 20:21:10 -05:00
2020-11-12 17:44:02 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/common"
2020-11-19 15:07:02 -05:00
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
2020-11-18 13:34:59 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/template/config"
2020-11-11 13:21:37 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
2016-05-26 23:21:55 -04:00
"github.com/mitchellh/mapstructure"
)
type Config struct {
common . PackerConfig ` mapstructure:",squash" `
2019-05-28 11:50:58 -04:00
// The name of the output artifact. Defaults to
2019-06-06 10:29:25 -04:00
// name.
OutputImage string ` mapstructure:"output_image" required:"false" `
ContainerName string ` mapstructure:"container_name" `
2019-05-28 11:50:58 -04:00
// Lets you prefix all builder commands, such as
2020-03-23 20:02:12 -04:00
// with ssh for a remote build host. Defaults to `{{.Command}}`; i.e. no
2020-02-20 05:05:27 -05:00
// wrapper.
2019-06-06 10:29:25 -04:00
CommandWrapper string ` mapstructure:"command_wrapper" required:"false" `
2019-05-28 11:50:58 -04:00
// The source image to use when creating the build
2019-06-06 10:29:25 -04:00
// container. This can be a (local or remote) image (name or fingerprint).
// E.G. my-base-image, ubuntu-daily:x, 08fababf6f27, ...
Image string ` mapstructure:"image" required:"true" `
Profile string ` mapstructure:"profile" `
2019-05-28 11:50:58 -04:00
// The number of seconds to sleep between launching
2019-06-06 10:29:25 -04:00
// the LXD instance and provisioning it; defaults to 3 seconds.
InitSleep string ` mapstructure:"init_sleep" required:"false" `
2019-05-28 11:50:58 -04:00
// Pass key values to the publish
2019-06-06 10:29:25 -04:00
// step to be set as properties on the output image. This is most helpful to
// set the description, but can be used to set anything needed. See
// https://stgraber.org/2016/03/30/lxd-2-0-image-management-512/
// for more properties.
PublishProperties map [ string ] string ` mapstructure:"publish_properties" required:"false" `
2019-05-28 11:50:58 -04:00
// List of key/value pairs you wish to
2019-06-06 10:29:25 -04:00
// pass to lxc launch via --config. Defaults to empty.
LaunchConfig map [ string ] string ` mapstructure:"launch_config" required:"false" `
2016-05-26 23:21:55 -04:00
ctx interpolate . Context
}
2019-12-17 05:25:56 -05:00
func ( c * Config ) Prepare ( raws ... interface { } ) error {
2016-05-26 23:21:55 -04:00
var md mapstructure . Metadata
2019-12-17 05:25:56 -05:00
err := config . Decode ( c , & config . DecodeOpts {
2016-05-26 23:21:55 -04:00
Metadata : & md ,
Interpolate : true ,
} , raws ... )
if err != nil {
2019-12-17 05:25:56 -05:00
return err
2016-05-26 23:21:55 -04:00
}
// Accumulate any errors
2020-11-19 15:07:02 -05:00
var errs * packersdk . MultiError
2016-05-26 23:21:55 -04:00
if c . ContainerName == "" {
c . ContainerName = fmt . Sprintf ( "packer-%s" , c . PackerBuildName )
}
2016-05-30 19:13:59 -04:00
if c . OutputImage == "" {
c . OutputImage = c . ContainerName
}
2016-05-26 23:21:55 -04:00
if c . CommandWrapper == "" {
c . CommandWrapper = "{{.Command}}"
}
if c . Image == "" {
2020-11-19 15:07:02 -05:00
errs = packersdk . MultiErrorAppend ( errs , fmt . Errorf ( "`image` is a required parameter for LXD. Please specify an image by alias or fingerprint. e.g. `ubuntu-daily:x`" ) )
2016-05-26 23:21:55 -04:00
}
2018-03-20 15:30:46 -04:00
if c . Profile == "" {
c . Profile = "default"
}
// Sadly we have to wait a few seconds for /tmp to be intialized and networking
// to finish starting. There isn't a great cross platform to check when things are ready.
if c . InitSleep == "" {
c . InitSleep = "3"
}
2016-05-26 23:21:55 -04:00
if errs != nil && len ( errs . Errors ) > 0 {
2019-12-17 05:25:56 -05:00
return errs
2016-05-26 23:21:55 -04:00
}
2019-12-17 05:25:56 -05:00
return nil
2016-05-26 23:21:55 -04:00
}