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"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
|
2016-12-27 13:13:51 -05:00
|
|
|
"encoding/json"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/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"`
|
|
|
|
Params map[string]string `mapstucture:"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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare provisioner somehow. TODO: actual docs
|
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
|
|
|
err := config.Decode(
|
|
|
|
&p.config,
|
|
|
|
&config.DecodeOpts{
|
|
|
|
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
|
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) maybeBootstrap(ui packer.Ui, comm packer.Communicator) error {
|
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
|
|
|
}
|
|
|
|
|
2016-12-27 11:50:33 -05:00
|
|
|
var out bytes.Buffer
|
|
|
|
cmd := &packer.RemoteCmd{
|
2016-12-27 13:22:17 -05:00
|
|
|
Command: command,
|
2016-12-27 11:50:33 -05:00
|
|
|
Stdin: nil,
|
|
|
|
Stdout: &out,
|
|
|
|
Stderr: &out,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = comm.Start(cmd); err != nil {
|
|
|
|
return fmt.Errorf("Error bootstrapping converge: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Wait()
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
ui.Error(out.String())
|
|
|
|
return errors.New("Error bootstrapping converge")
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(strings.TrimSpace(out.String()))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-27 12:29:36 -05:00
|
|
|
func (p *Provisioner) sendModuleDirectories(ui packer.Ui, comm packer.Communicator) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-27 13:13:51 -05:00
|
|
|
func (p *Provisioner) applyModules(ui packer.Ui, comm packer.Communicator) error {
|
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
|
|
|
|
var runOut bytes.Buffer
|
|
|
|
cmd := &packer.RemoteCmd{
|
2016-12-27 17:48:11 -05:00
|
|
|
Command: command,
|
|
|
|
Stdin: nil,
|
|
|
|
Stdout: &runOut,
|
|
|
|
Stderr: &runOut,
|
2016-12-27 17:32:19 -05:00
|
|
|
}
|
|
|
|
if err := comm.Start(cmd); err != nil {
|
|
|
|
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()
|
|
|
|
if cmd.ExitStatus == 127 {
|
|
|
|
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")
|
|
|
|
|
|
|
|
} else if cmd.ExitStatus != 0 {
|
|
|
|
ui.Error(strings.TrimSpace(runOut.String()))
|
|
|
|
ui.Error(fmt.Sprintf("exited with error code %d", cmd.ExitStatus))
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-27 11:50:33 -05:00
|
|
|
// Cancel the provisioning process
|
|
|
|
func (p *Provisioner) Cancel() {
|
2016-12-27 17:31:12 -05:00
|
|
|
// there's not an awful lot we can do to cancel Converge at the moment.
|
|
|
|
// The default semantics are fine.
|
2016-12-27 11:50:33 -05:00
|
|
|
}
|