2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config,ModuleDir
|
|
|
|
|
2016-12-27 11:50:33 -05:00
|
|
|
// This package implements a provisioner for Packer that executes
|
|
|
|
// Converge to provision a remote machine
|
|
|
|
|
|
|
|
package converge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-03-19 13:11:19 -04:00
|
|
|
"context"
|
2016-12-27 11:50:33 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
|
2016-12-27 13:13:51 -05:00
|
|
|
"encoding/json"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
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"
|
2016-12-27 11:50:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config for Converge provisioner
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2016-12-27 12:29:36 -05:00
|
|
|
// Bootstrapping
|
2016-12-28 13:53:22 -05:00
|
|
|
Bootstrap bool `mapstructure:"bootstrap"`
|
|
|
|
Version string `mapstructure:"version"`
|
|
|
|
BootstrapCommand string `mapstructure:"bootstrap_command"`
|
|
|
|
PreventBootstrapSudo bool `mapstructure:"prevent_bootstrap_sudo"`
|
2016-12-27 11:50:33 -05:00
|
|
|
|
2016-12-27 12:29:36 -05:00
|
|
|
// Modules
|
|
|
|
ModuleDirs []ModuleDir `mapstructure:"module_dirs"`
|
2016-12-27 17:32:19 -05:00
|
|
|
|
|
|
|
// Execution
|
|
|
|
Module string `mapstructure:"module"`
|
|
|
|
WorkingDirectory string `mapstructure:"working_directory"`
|
2017-12-18 15:21:26 -05:00
|
|
|
Params map[string]string `mapstructure:"params"`
|
2016-12-27 17:48:11 -05:00
|
|
|
ExecuteCommand string `mapstructure:"execute_command"`
|
2016-12-28 09:19:03 -05:00
|
|
|
PreventSudo bool `mapstructure:"prevent_sudo"`
|
2016-12-27 12:29:36 -05:00
|
|
|
|
2016-12-27 11:50:33 -05:00
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
2016-12-27 12:29:36 -05:00
|
|
|
// ModuleDir is a directory to transfer to the remote system
|
|
|
|
type ModuleDir struct {
|
|
|
|
Source string `mapstructure:"source"`
|
|
|
|
Destination string `mapstructure:"destination"`
|
|
|
|
Exclude []string `mapstructure:"exclude"`
|
|
|
|
}
|
|
|
|
|
2016-12-27 11:50:33 -05:00
|
|
|
// Provisioner for Converge
|
|
|
|
type Provisioner struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *Provisioner) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2016-12-27 11:50:33 -05:00
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
|
|
|
err := config.Decode(
|
|
|
|
&p.config,
|
|
|
|
&config.DecodeOpts{
|
2020-10-09 20:01:55 -04:00
|
|
|
PluginType: "converge",
|
2016-12-27 11:50:33 -05:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
2016-12-27 17:32:19 -05:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
2016-12-28 09:45:19 -05:00
|
|
|
Exclude: []string{
|
|
|
|
"execute_command",
|
|
|
|
"bootstrap_command",
|
|
|
|
},
|
2016-12-27 17:32:19 -05:00
|
|
|
},
|
2016-12-27 11:50:33 -05:00
|
|
|
},
|
|
|
|
raws...,
|
|
|
|
)
|
2016-12-27 12:41:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-27 17:48:11 -05:00
|
|
|
// require a single module
|
|
|
|
if p.config.Module == "" {
|
|
|
|
return errors.New("Converge requires a module to provision the system")
|
|
|
|
}
|
|
|
|
|
|
|
|
// set defaults
|
|
|
|
if p.config.WorkingDirectory == "" {
|
|
|
|
p.config.WorkingDirectory = "/tmp"
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.ExecuteCommand == "" {
|
2016-12-28 09:19:03 -05:00
|
|
|
p.config.ExecuteCommand = "cd {{.WorkingDirectory}} && {{if .Sudo}}sudo {{end}}converge apply --local --log-level=WARNING --paramsJSON '{{.ParamsJSON}}' {{.Module}}"
|
2016-12-27 17:48:11 -05:00
|
|
|
}
|
|
|
|
|
2016-12-28 09:45:19 -05:00
|
|
|
if p.config.BootstrapCommand == "" {
|
2016-12-28 13:53:22 -05:00
|
|
|
p.config.BootstrapCommand = "curl -s https://get.converge.sh | {{if .Sudo}}sudo {{end}}sh {{if ne .Version \"\"}}-s -- -v {{.Version}}{{end}}"
|
2016-12-28 09:45:19 -05:00
|
|
|
}
|
|
|
|
|
2016-12-27 12:41:52 -05:00
|
|
|
// validate sources and destinations
|
|
|
|
for i, dir := range p.config.ModuleDirs {
|
|
|
|
if dir.Source == "" {
|
|
|
|
return fmt.Errorf("Source (\"source\" key) is required in Converge module dir #%d", i)
|
|
|
|
}
|
|
|
|
if dir.Destination == "" {
|
|
|
|
return fmt.Errorf("Destination (\"destination\" key) is required in Converge module dir #%d", i)
|
|
|
|
}
|
|
|
|
}
|
2016-12-27 11:50:33 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provision node somehow. TODO: actual docs
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, _ map[string]interface{}) error {
|
2016-12-27 11:50:33 -05:00
|
|
|
ui.Say("Provisioning with Converge")
|
|
|
|
|
|
|
|
// bootstrapping
|
|
|
|
if err := p.maybeBootstrap(ui, comm); err != nil {
|
|
|
|
return err // error messages are already user-friendly
|
|
|
|
}
|
|
|
|
|
2016-12-27 12:29:36 -05:00
|
|
|
// send module directories to the remote host
|
|
|
|
if err := p.sendModuleDirectories(ui, comm); err != nil {
|
|
|
|
return err // error messages are already user-friendly
|
|
|
|
}
|
|
|
|
|
2016-12-27 13:13:51 -05:00
|
|
|
// apply all the modules
|
|
|
|
if err := p.applyModules(ui, comm); err != nil {
|
|
|
|
return err // error messages are already user-friendly
|
|
|
|
}
|
|
|
|
|
2016-12-27 11:50:33 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) maybeBootstrap(ui packersdk.Ui, comm packersdk.Communicator) error {
|
2019-04-03 11:14:55 -04:00
|
|
|
ctx := context.TODO()
|
2016-12-28 13:49:40 -05:00
|
|
|
if !p.config.Bootstrap {
|
2016-12-27 11:50:33 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ui.Message("bootstrapping converge")
|
|
|
|
|
2016-12-28 09:45:19 -05:00
|
|
|
p.config.ctx.Data = struct {
|
|
|
|
Version string
|
2016-12-28 13:53:22 -05:00
|
|
|
Sudo bool
|
2016-12-28 09:45:19 -05:00
|
|
|
}{
|
|
|
|
Version: p.config.Version,
|
2016-12-28 13:53:22 -05:00
|
|
|
Sudo: !p.config.PreventBootstrapSudo,
|
2016-12-27 11:50:33 -05:00
|
|
|
}
|
2016-12-28 09:45:19 -05:00
|
|
|
command, err := interpolate.Render(p.config.BootstrapCommand, &p.config.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not interpolate bootstrap command: %s", err)
|
2016-12-27 13:22:17 -05:00
|
|
|
}
|
|
|
|
|
2017-11-09 18:20:37 -05:00
|
|
|
var out, outErr bytes.Buffer
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{
|
2016-12-27 13:22:17 -05:00
|
|
|
Command: command,
|
2016-12-27 11:50:33 -05:00
|
|
|
Stdin: nil,
|
|
|
|
Stdout: &out,
|
2017-11-09 18:20:37 -05:00
|
|
|
Stderr: &outErr,
|
2016-12-27 11:50:33 -05:00
|
|
|
}
|
|
|
|
|
2019-04-03 11:14:55 -04:00
|
|
|
if err = comm.Start(ctx, cmd); err != nil {
|
2016-12-27 11:50:33 -05:00
|
|
|
return fmt.Errorf("Error bootstrapping converge: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Wait()
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() != 0 {
|
2016-12-27 11:50:33 -05:00
|
|
|
ui.Error(out.String())
|
2017-11-09 18:20:37 -05:00
|
|
|
ui.Error(outErr.String())
|
2016-12-27 11:50:33 -05:00
|
|
|
return errors.New("Error bootstrapping converge")
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(strings.TrimSpace(out.String()))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) sendModuleDirectories(ui packersdk.Ui, comm packersdk.Communicator) error {
|
2016-12-27 12:29:36 -05:00
|
|
|
for _, dir := range p.config.ModuleDirs {
|
|
|
|
if err := comm.UploadDir(dir.Destination, dir.Source, dir.Exclude); err != nil {
|
|
|
|
return fmt.Errorf("Could not upload %q: %s", dir.Source, err)
|
|
|
|
}
|
|
|
|
ui.Message(fmt.Sprintf("transferred %q to %q", dir.Source, dir.Destination))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *Provisioner) applyModules(ui packersdk.Ui, comm packersdk.Communicator) error {
|
2019-04-03 11:14:55 -04:00
|
|
|
ctx := context.TODO()
|
2016-12-27 17:32:19 -05:00
|
|
|
// create params JSON file
|
|
|
|
params, err := json.Marshal(p.config.Params)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not marshal parameters as JSON: %s", err)
|
|
|
|
}
|
2016-12-27 17:03:35 -05:00
|
|
|
|
2016-12-27 17:48:11 -05:00
|
|
|
p.config.ctx.Data = struct {
|
|
|
|
ParamsJSON, WorkingDirectory, Module string
|
2016-12-28 09:19:03 -05:00
|
|
|
Sudo bool
|
2016-12-27 17:48:11 -05:00
|
|
|
}{
|
|
|
|
ParamsJSON: string(params),
|
|
|
|
WorkingDirectory: p.config.WorkingDirectory,
|
|
|
|
Module: p.config.Module,
|
2016-12-28 09:19:03 -05:00
|
|
|
Sudo: !p.config.PreventSudo,
|
2016-12-27 17:48:11 -05:00
|
|
|
}
|
|
|
|
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not interpolate execute command: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-12-27 17:32:19 -05:00
|
|
|
// run Converge in the specified directory
|
2017-11-09 18:20:37 -05:00
|
|
|
var runOut, runErr bytes.Buffer
|
2020-11-19 18:10:00 -05:00
|
|
|
cmd := &packersdk.RemoteCmd{
|
2016-12-27 17:48:11 -05:00
|
|
|
Command: command,
|
|
|
|
Stdin: nil,
|
|
|
|
Stdout: &runOut,
|
2017-11-09 18:20:37 -05:00
|
|
|
Stderr: &runErr,
|
2016-12-27 17:32:19 -05:00
|
|
|
}
|
2019-04-03 11:14:55 -04:00
|
|
|
if err := comm.Start(ctx, cmd); err != nil {
|
2016-12-27 17:32:19 -05:00
|
|
|
return fmt.Errorf("Error applying %q: %s", p.config.Module, err)
|
|
|
|
}
|
2016-12-27 17:03:35 -05:00
|
|
|
|
2016-12-27 17:32:19 -05:00
|
|
|
cmd.Wait()
|
2019-04-03 11:14:55 -04:00
|
|
|
if cmd.ExitStatus() == 127 {
|
2016-12-27 17:32:19 -05:00
|
|
|
ui.Error("Could not find Converge. Is it installed and in PATH?")
|
2016-12-28 13:49:40 -05:00
|
|
|
if !p.config.Bootstrap {
|
2016-12-27 17:32:19 -05:00
|
|
|
ui.Error("Bootstrapping was disabled for this run. That might be why Converge isn't present.")
|
2016-12-27 13:13:51 -05:00
|
|
|
}
|
|
|
|
|
2016-12-27 17:32:19 -05:00
|
|
|
return errors.New("Could not find Converge")
|
|
|
|
|
2019-04-03 11:14:55 -04:00
|
|
|
} else if cmd.ExitStatus() != 0 {
|
2016-12-27 17:32:19 -05:00
|
|
|
ui.Error(strings.TrimSpace(runOut.String()))
|
2017-11-09 18:20:37 -05:00
|
|
|
ui.Error(strings.TrimSpace(runErr.String()))
|
2019-04-03 11:14:55 -04:00
|
|
|
ui.Error(fmt.Sprintf("Exited with error code %d.", cmd.ExitStatus()))
|
2016-12-27 17:32:19 -05:00
|
|
|
return fmt.Errorf("Error applying %q", p.config.Module)
|
2016-12-27 13:13:51 -05:00
|
|
|
}
|
|
|
|
|
2016-12-27 17:32:19 -05:00
|
|
|
ui.Message(strings.TrimSpace(runOut.String()))
|
|
|
|
|
2016-12-27 13:13:51 -05:00
|
|
|
return nil
|
|
|
|
}
|