2013-09-29 16:38:37 -04:00
|
|
|
package ansiblelocal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-10-06 01:33:09 -04:00
|
|
|
"io/ioutil"
|
2013-09-29 16:38:37 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-01-22 07:40:06 -05:00
|
|
|
"strings"
|
2014-10-06 01:33:09 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-09-29 16:38:37 -04:00
|
|
|
)
|
|
|
|
|
2013-09-29 18:42:42 -04:00
|
|
|
const DefaultStagingDir = "/tmp/packer-provisioner-ansible-local"
|
|
|
|
|
2013-09-29 16:38:37 -04:00
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-05-27 17:41:47 -04:00
|
|
|
ctx interpolate.Context
|
2013-09-29 16:38:37 -04:00
|
|
|
|
2014-02-21 18:27:39 -05:00
|
|
|
// The command to run ansible
|
|
|
|
Command string
|
|
|
|
|
|
|
|
// Extra options to pass to the ansible command
|
|
|
|
ExtraArguments []string `mapstructure:"extra_arguments"`
|
|
|
|
|
|
|
|
// Path to group_vars directory
|
|
|
|
GroupVars string `mapstructure:"group_vars"`
|
|
|
|
|
|
|
|
// Path to host_vars directory
|
|
|
|
HostVars string `mapstructure:"host_vars"`
|
|
|
|
|
2014-05-09 11:59:05 -04:00
|
|
|
// The playbook dir to upload.
|
|
|
|
PlaybookDir string `mapstructure:"playbook_dir"`
|
2014-07-17 15:33:09 -04:00
|
|
|
|
2013-09-29 16:38:37 -04:00
|
|
|
// The main playbook file to execute.
|
|
|
|
PlaybookFile string `mapstructure:"playbook_file"`
|
|
|
|
|
|
|
|
// An array of local paths of playbook files to upload.
|
|
|
|
PlaybookPaths []string `mapstructure:"playbook_paths"`
|
|
|
|
|
|
|
|
// An array of local paths of roles to upload.
|
|
|
|
RolePaths []string `mapstructure:"role_paths"`
|
|
|
|
|
|
|
|
// The directory where files will be uploaded. Packer requires write
|
|
|
|
// permissions in this directory.
|
|
|
|
StagingDir string `mapstructure:"staging_directory"`
|
2014-04-04 14:19:55 -04:00
|
|
|
|
|
|
|
// The optional inventory file
|
|
|
|
InventoryFile string `mapstructure:"inventory_file"`
|
2015-04-30 12:33:15 -04:00
|
|
|
|
|
|
|
// The optional inventory groups
|
|
|
|
InventoryGroups []string `mapstructure:"inventory_groups"`
|
2016-03-07 03:20:18 -05:00
|
|
|
|
|
|
|
// The optional ansible-galaxy requirements file
|
|
|
|
GalaxyFile string `mapstructure:"galaxy_file"`
|
|
|
|
|
|
|
|
// The command to run ansible-galaxy
|
|
|
|
GalaxyCommand string
|
2013-09-29 16:38:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
2015-05-27 17:41:47 -04:00
|
|
|
err := config.Decode(&p.config, &config.DecodeOpts{
|
2015-06-22 15:26:54 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &p.config.ctx,
|
2015-05-27 17:41:47 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-09-29 16:38:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-22 07:40:06 -05:00
|
|
|
// Defaults
|
|
|
|
if p.config.Command == "" {
|
2014-10-13 18:31:11 -04:00
|
|
|
p.config.Command = "ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 ansible-playbook"
|
2014-01-22 07:40:06 -05:00
|
|
|
}
|
2016-03-07 03:20:18 -05:00
|
|
|
if p.config.GalaxyCommand == "" {
|
|
|
|
p.config.GalaxyCommand = "ansible-galaxy"
|
|
|
|
}
|
2014-01-22 07:40:06 -05:00
|
|
|
|
2014-02-21 18:27:39 -05:00
|
|
|
if p.config.StagingDir == "" {
|
2017-04-12 20:02:55 -04:00
|
|
|
p.config.StagingDir = filepath.ToSlash(filepath.Join(DefaultStagingDir, uuid.TimeOrderedUUID()))
|
2014-02-21 18:27:39 -05:00
|
|
|
}
|
|
|
|
|
2013-09-29 16:38:37 -04:00
|
|
|
// Validation
|
2015-05-27 17:41:47 -04:00
|
|
|
var errs *packer.MultiError
|
2013-09-29 16:38:37 -04:00
|
|
|
err = validateFileConfig(p.config.PlaybookFile, "playbook_file", true)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
2014-01-18 21:48:12 -05:00
|
|
|
|
2014-04-04 14:19:55 -04:00
|
|
|
// Check that the inventory file exists, if configured
|
|
|
|
if len(p.config.InventoryFile) > 0 {
|
|
|
|
err = validateFileConfig(p.config.InventoryFile, "inventory_file", true)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 03:20:18 -05:00
|
|
|
// Check that the galaxy file exists, if configured
|
|
|
|
if len(p.config.GalaxyFile) > 0 {
|
|
|
|
err = validateFileConfig(p.config.GalaxyFile, "galaxy_file", true)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 11:59:05 -04:00
|
|
|
// Check that the playbook_dir directory exists, if configured
|
|
|
|
if len(p.config.PlaybookDir) > 0 {
|
|
|
|
if err := validateDirConfig(p.config.PlaybookDir, "playbook_dir"); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
}
|
2014-07-17 15:33:09 -04:00
|
|
|
|
2014-01-18 21:48:12 -05:00
|
|
|
// Check that the group_vars directory exists, if configured
|
|
|
|
if len(p.config.GroupVars) > 0 {
|
|
|
|
if err := validateDirConfig(p.config.GroupVars, "group_vars"); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the host_vars directory exists, if configured
|
|
|
|
if len(p.config.HostVars) > 0 {
|
|
|
|
if err := validateDirConfig(p.config.HostVars, "host_vars"); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 18:27:39 -05:00
|
|
|
|
|
|
|
for _, path := range p.config.PlaybookPaths {
|
|
|
|
err := validateDirConfig(path, "playbook_paths")
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, path := range p.config.RolePaths {
|
|
|
|
if err := validateDirConfig(path, "role_paths"); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-29 16:38:37 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
|
|
|
ui.Say("Provisioning with Ansible...")
|
|
|
|
|
2014-05-09 11:59:05 -04:00
|
|
|
if len(p.config.PlaybookDir) > 0 {
|
|
|
|
ui.Message("Uploading Playbook directory to Ansible staging directory...")
|
|
|
|
if err := p.uploadDir(ui, comm, p.config.StagingDir, p.config.PlaybookDir); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading playbook_dir directory: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ui.Message("Creating Ansible staging directory...")
|
|
|
|
if err := p.createDir(ui, comm, p.config.StagingDir); err != nil {
|
|
|
|
return fmt.Errorf("Error creating staging directory: %s", err)
|
|
|
|
}
|
2013-09-29 16:38:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message("Uploading main Playbook file...")
|
|
|
|
src := p.config.PlaybookFile
|
2014-03-18 14:11:34 -04:00
|
|
|
dst := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(src)))
|
2013-09-29 16:38:37 -04:00
|
|
|
if err := p.uploadFile(ui, comm, dst, src); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading main playbook: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-06 01:33:09 -04:00
|
|
|
if len(p.config.InventoryFile) == 0 {
|
|
|
|
tf, err := ioutil.TempFile("", "packer-provisioner-ansible-local")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error preparing inventory file: %s", err)
|
2014-04-04 14:19:55 -04:00
|
|
|
}
|
2014-10-06 01:33:09 -04:00
|
|
|
defer os.Remove(tf.Name())
|
2015-04-30 12:33:15 -04:00
|
|
|
if len(p.config.InventoryGroups) != 0 {
|
|
|
|
content := ""
|
|
|
|
for _, group := range p.config.InventoryGroups {
|
|
|
|
content += fmt.Sprintf("[%s]\n127.0.0.1\n", group)
|
|
|
|
}
|
|
|
|
_, err = tf.Write([]byte(content))
|
|
|
|
} else {
|
|
|
|
_, err = tf.Write([]byte("127.0.0.1"))
|
|
|
|
}
|
2014-10-06 01:33:09 -04:00
|
|
|
if err != nil {
|
|
|
|
tf.Close()
|
|
|
|
return fmt.Errorf("Error preparing inventory file: %s", err)
|
|
|
|
}
|
|
|
|
tf.Close()
|
|
|
|
p.config.InventoryFile = tf.Name()
|
|
|
|
defer func() {
|
|
|
|
p.config.InventoryFile = ""
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-03-07 03:20:18 -05:00
|
|
|
if len(p.config.GalaxyFile) > 0 {
|
|
|
|
ui.Message("Uploading galaxy file...")
|
|
|
|
src = p.config.GalaxyFile
|
|
|
|
dst = filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(src)))
|
|
|
|
if err := p.uploadFile(ui, comm, dst, src); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading galaxy file: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 01:33:09 -04:00
|
|
|
ui.Message("Uploading inventory file...")
|
|
|
|
src = p.config.InventoryFile
|
|
|
|
dst = filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(src)))
|
|
|
|
if err := p.uploadFile(ui, comm, dst, src); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading inventory file: %s", err)
|
2014-04-04 14:19:55 -04:00
|
|
|
}
|
|
|
|
|
2014-01-18 21:48:12 -05:00
|
|
|
if len(p.config.GroupVars) > 0 {
|
|
|
|
ui.Message("Uploading group_vars directory...")
|
|
|
|
src := p.config.GroupVars
|
2014-03-18 14:11:34 -04:00
|
|
|
dst := filepath.ToSlash(filepath.Join(p.config.StagingDir, "group_vars"))
|
2014-01-18 21:48:12 -05:00
|
|
|
if err := p.uploadDir(ui, comm, dst, src); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading group_vars directory: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.config.HostVars) > 0 {
|
|
|
|
ui.Message("Uploading host_vars directory...")
|
|
|
|
src := p.config.HostVars
|
2014-03-18 14:11:34 -04:00
|
|
|
dst := filepath.ToSlash(filepath.Join(p.config.StagingDir, "host_vars"))
|
2014-01-18 21:48:12 -05:00
|
|
|
if err := p.uploadDir(ui, comm, dst, src); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading host_vars directory: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-29 16:38:37 -04:00
|
|
|
if len(p.config.RolePaths) > 0 {
|
|
|
|
ui.Message("Uploading role directories...")
|
|
|
|
for _, src := range p.config.RolePaths {
|
2014-03-18 14:11:34 -04:00
|
|
|
dst := filepath.ToSlash(filepath.Join(p.config.StagingDir, "roles", filepath.Base(src)))
|
2013-09-29 16:38:37 -04:00
|
|
|
if err := p.uploadDir(ui, comm, dst, src); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading roles: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.config.PlaybookPaths) > 0 {
|
|
|
|
ui.Message("Uploading additional Playbooks...")
|
2014-03-18 14:11:34 -04:00
|
|
|
playbookDir := filepath.ToSlash(filepath.Join(p.config.StagingDir, "playbooks"))
|
|
|
|
if err := p.createDir(ui, comm, playbookDir); err != nil {
|
2013-09-29 16:38:37 -04:00
|
|
|
return fmt.Errorf("Error creating playbooks directory: %s", err)
|
|
|
|
}
|
|
|
|
for _, src := range p.config.PlaybookPaths {
|
2014-03-18 14:11:34 -04:00
|
|
|
dst := filepath.ToSlash(filepath.Join(playbookDir, filepath.Base(src)))
|
2014-01-17 19:49:17 -05:00
|
|
|
if err := p.uploadDir(ui, comm, dst, src); err != nil {
|
2013-09-29 16:38:37 -04:00
|
|
|
return fmt.Errorf("Error uploading playbooks: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.executeAnsible(ui, comm); err != nil {
|
|
|
|
return fmt.Errorf("Error executing Ansible: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Cancel() {
|
|
|
|
// Just hard quit. It isn't a big deal if what we're doing keeps
|
|
|
|
// running on the other side.
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2016-03-07 03:20:18 -05:00
|
|
|
func (p *Provisioner) executeGalaxy(ui packer.Ui, comm packer.Communicator) error {
|
|
|
|
rolesDir := filepath.ToSlash(filepath.Join(p.config.StagingDir, "roles"))
|
|
|
|
galaxyFile := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.GalaxyFile)))
|
|
|
|
|
|
|
|
// ansible-galaxy install -r requirements.yml -p roles/
|
|
|
|
command := fmt.Sprintf("cd %s && %s install -r %s -p %s",
|
|
|
|
p.config.StagingDir, p.config.GalaxyCommand, galaxyFile, rolesDir)
|
|
|
|
ui.Message(fmt.Sprintf("Executing Ansible Galaxy: %s", command))
|
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: command,
|
|
|
|
}
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
// ansible-galaxy version 2.0.0.2 doesn't return exit codes on error..
|
|
|
|
return fmt.Errorf("Non-zero exit status: %d", cmd.ExitStatus)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-29 16:38:37 -04:00
|
|
|
func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator) error {
|
2014-03-18 14:11:34 -04:00
|
|
|
playbook := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.PlaybookFile)))
|
2014-10-06 01:33:09 -04:00
|
|
|
inventory := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.InventoryFile)))
|
2014-04-04 14:19:55 -04:00
|
|
|
|
2017-04-22 11:24:37 -04:00
|
|
|
extraArgs := fmt.Sprintf(" --extra-vars \"packer_build_name=%s packer_builder_type=%s packer_http_addr=%s\" ",
|
|
|
|
p.config.PackerBuildName, p.config.PackerBuilderType, common.GetHTTPAddr())
|
2014-01-22 07:40:06 -05:00
|
|
|
if len(p.config.ExtraArguments) > 0 {
|
2017-04-22 11:24:37 -04:00
|
|
|
extraArgs = extraArgs + strings.Join(p.config.ExtraArguments, " ")
|
2014-01-22 07:40:06 -05:00
|
|
|
}
|
2013-09-29 16:38:37 -04:00
|
|
|
|
2016-03-07 03:20:18 -05:00
|
|
|
// Fetch external dependencies
|
|
|
|
if len(p.config.GalaxyFile) > 0 {
|
|
|
|
if err := p.executeGalaxy(ui, comm); err != nil {
|
|
|
|
return fmt.Errorf("Error executing Ansible Galaxy: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 10:57:24 -04:00
|
|
|
command := fmt.Sprintf("cd %s && %s %s%s -c local -i %s",
|
|
|
|
p.config.StagingDir, p.config.Command, playbook, extraArgs, inventory)
|
2013-09-29 16:38:37 -04:00
|
|
|
ui.Message(fmt.Sprintf("Executing Ansible: %s", command))
|
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: command,
|
|
|
|
}
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cmd.ExitStatus != 0 {
|
2014-02-21 23:29:56 -05:00
|
|
|
if cmd.ExitStatus == 127 {
|
2014-02-24 11:26:51 -05:00
|
|
|
return fmt.Errorf("%s could not be found. Verify that it is available on the\n"+
|
2014-02-21 23:29:56 -05:00
|
|
|
"PATH after connecting to the machine.",
|
|
|
|
p.config.Command)
|
|
|
|
}
|
|
|
|
|
2013-09-29 16:38:37 -04:00
|
|
|
return fmt.Errorf("Non-zero exit status: %d", cmd.ExitStatus)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateDirConfig(path string, config string) error {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s is invalid: %s", config, path, err)
|
|
|
|
} else if !info.IsDir() {
|
|
|
|
return fmt.Errorf("%s: %s must point to a directory", config, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateFileConfig(name string, config string, req bool) error {
|
|
|
|
if req {
|
|
|
|
if name == "" {
|
|
|
|
return fmt.Errorf("%s must be specified.", config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info, err := os.Stat(name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s is invalid: %s", config, name, err)
|
|
|
|
} else if info.IsDir() {
|
|
|
|
return fmt.Errorf("%s: %s must point to a file", config, name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) uploadFile(ui packer.Ui, comm packer.Communicator, dst, src string) error {
|
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error opening: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2014-05-10 00:03:35 -04:00
|
|
|
if err = comm.Upload(dst, f, nil); err != nil {
|
2013-09-29 16:38:37 -04:00
|
|
|
return fmt.Errorf("Error uploading %s: %s", src, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) createDir(ui packer.Ui, comm packer.Communicator, dir string) error {
|
|
|
|
ui.Message(fmt.Sprintf("Creating directory: %s", dir))
|
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: fmt.Sprintf("mkdir -p '%s'", dir),
|
|
|
|
}
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf("Non-zero exit status.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) uploadDir(ui packer.Ui, comm packer.Communicator, dst, src string) error {
|
|
|
|
if err := p.createDir(ui, comm, dst); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure there is a trailing "/" so that the directory isn't
|
|
|
|
// created on the other side.
|
|
|
|
if src[len(src)-1] != '/' {
|
|
|
|
src = src + "/"
|
|
|
|
}
|
|
|
|
return comm.UploadDir(dst, src, nil)
|
|
|
|
}
|