2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
2019-12-17 05:25:56 -05:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
2019-05-31 08:27:41 -04:00
|
|
|
|
2015-12-22 09:56:33 -05:00
|
|
|
package lxc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-22 20:21:10 -05:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/common"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/config"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2015-12-22 09:56:33 -05:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The path to the lxc configuration file.
|
2019-06-06 10:29:25 -04:00
|
|
|
ConfigFile string `mapstructure:"config_file" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The directory in which to save the exported
|
2020-04-01 23:30:51 -04:00
|
|
|
// tar.gz. Defaults to `output-<BuildName>` in the current directory.
|
2019-06-06 10:29:25 -04:00
|
|
|
OutputDir string `mapstructure:"output_directory" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name of the LXC container. Usually stored
|
2020-04-01 23:30:51 -04:00
|
|
|
// in `/var/lib/lxc/containers/<container_name>`. Defaults to
|
|
|
|
// `packer-<BuildName>`.
|
2019-06-06 10:29:25 -04:00
|
|
|
ContainerName string `mapstructure:"container_name" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Allows you to specify a wrapper command, such
|
2019-06-06 10:29:25 -04:00
|
|
|
// as ssh so you can execute packer builds on a remote host. Defaults to
|
2020-02-20 05:05:27 -05:00
|
|
|
// `{{.Command}}`; i.e. no 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 timeout in seconds to wait for the the
|
2019-06-06 10:29:25 -04:00
|
|
|
// container to start. Defaults to 20 seconds.
|
2019-10-31 10:49:34 -04:00
|
|
|
InitTimeout time.Duration `mapstructure:"init_timeout" required:"false"`
|
|
|
|
|
2019-05-28 11:50:58 -04:00
|
|
|
// Options to pass to lxc-create. For
|
2019-06-06 10:29:25 -04:00
|
|
|
// instance, you can specify a custom LXC container configuration file with
|
|
|
|
// ["-f", "/path/to/lxc.conf"]. Defaults to []. See man 1 lxc-create for
|
|
|
|
// available options.
|
|
|
|
CreateOptions []string `mapstructure:"create_options" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Options to pass to lxc-start. For
|
2019-06-06 10:29:25 -04:00
|
|
|
// instance, you can override parameters from the LXC container configuration
|
|
|
|
// file via ["--define", "KEY=VALUE"]. Defaults to []. See
|
|
|
|
// man 1 lxc-start for available options.
|
|
|
|
StartOptions []string `mapstructure:"start_options" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Options to pass to lxc-attach. For
|
2019-06-06 10:29:25 -04:00
|
|
|
// instance, you can prevent the container from inheriting the host machine's
|
|
|
|
// environment by specifying ["--clear-env"]. Defaults to []. See
|
|
|
|
// man 1 lxc-attach for available options.
|
|
|
|
AttachOptions []string `mapstructure:"attach_options" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The LXC template name to use.
|
2019-06-06 10:29:25 -04:00
|
|
|
Name string `mapstructure:"template_name" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Options to pass to the given
|
2019-06-06 10:29:25 -04:00
|
|
|
// lxc-template command, usually located in
|
2020-04-01 23:30:51 -04:00
|
|
|
// `/usr/share/lxc/templates/lxc-<template_name>`. Note: This gets passed as
|
2019-06-06 10:29:25 -04:00
|
|
|
// ARGV to the template command. Ensure you have an array of strings, as a
|
|
|
|
// single string with spaces probably won't work. Defaults to [].
|
|
|
|
Parameters []string `mapstructure:"template_parameters" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Environmental variables to
|
2019-06-06 10:29:25 -04:00
|
|
|
// use to build the template with.
|
|
|
|
EnvVars []string `mapstructure:"template_environment_vars" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The minimum run level to wait for the
|
2019-06-06 10:29:25 -04:00
|
|
|
// container to reach. Note some distributions (Ubuntu) simulate run levels
|
|
|
|
// and may report 5 rather than 3.
|
|
|
|
TargetRunlevel int `mapstructure:"target_runlevel" required:"false"`
|
2015-12-22 09:56:33 -05:00
|
|
|
|
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (c *Config) Prepare(raws ...interface{}) error {
|
2015-12-22 09:56:33 -05:00
|
|
|
|
|
|
|
var md mapstructure.Metadata
|
2019-12-17 05:25:56 -05:00
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
2015-12-22 09:56:33 -05:00
|
|
|
Metadata: &md,
|
|
|
|
Interpolate: true,
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
return err
|
2015-12-22 09:56:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate any errors
|
2020-11-19 15:07:02 -05:00
|
|
|
var errs *packersdk.MultiError
|
2015-12-22 09:56:33 -05:00
|
|
|
|
|
|
|
if c.OutputDir == "" {
|
|
|
|
c.OutputDir = fmt.Sprintf("output-%s", c.PackerBuildName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ContainerName == "" {
|
|
|
|
c.ContainerName = fmt.Sprintf("packer-%s", c.PackerBuildName)
|
|
|
|
}
|
|
|
|
|
2016-05-16 01:59:39 -04:00
|
|
|
if c.TargetRunlevel == 0 {
|
|
|
|
c.TargetRunlevel = 3
|
|
|
|
}
|
|
|
|
|
2015-12-22 09:56:33 -05:00
|
|
|
if c.CommandWrapper == "" {
|
|
|
|
c.CommandWrapper = "{{.Command}}"
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:49:34 -04:00
|
|
|
if c.InitTimeout == 0 {
|
|
|
|
c.InitTimeout = 20 * time.Second
|
2015-12-22 09:56:33 -05:00
|
|
|
}
|
|
|
|
|
2016-05-10 20:37:01 -04:00
|
|
|
if _, err := os.Stat(c.ConfigFile); os.IsNotExist(err) {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("LXC Config file appears to be missing: %s", c.ConfigFile))
|
2016-05-10 20:37:01 -04:00
|
|
|
}
|
|
|
|
|
2015-12-22 09:56:33 -05:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 05:25:56 -05:00
|
|
|
return errs
|
2015-12-22 09:56:33 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil
|
2015-12-22 09:56:33 -05:00
|
|
|
}
|