2013-08-26 00:07:37 -04:00
|
|
|
// This package implements a provisioner for Packer that uses
|
|
|
|
// Chef to provision the remote machine, specifically with chef-solo (that is,
|
|
|
|
// without a Chef server).
|
|
|
|
package chefsolo
|
2013-07-05 00:26:48 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2013-07-11 10:47:51 -04:00
|
|
|
"encoding/json"
|
2013-07-05 00:26:48 -04:00
|
|
|
"fmt"
|
2013-09-16 02:28:41 -04:00
|
|
|
"io/ioutil"
|
2013-07-05 00:26:48 -04:00
|
|
|
"os"
|
2013-07-11 10:47:51 -04:00
|
|
|
"path/filepath"
|
2013-07-05 00:26:48 -04:00
|
|
|
"strings"
|
2015-05-27 17:50:20 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/helper/config"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2015-08-20 16:27:51 -04:00
|
|
|
"github.com/mitchellh/packer/provisioner"
|
2015-05-27 17:50:20 -04:00
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2013-07-05 00:26:48 -04:00
|
|
|
)
|
|
|
|
|
2015-08-20 16:27:51 -04:00
|
|
|
type guestOSTypeConfig struct {
|
|
|
|
executeCommand string
|
|
|
|
installCommand string
|
|
|
|
stagingDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
var guestOSTypeConfigs = map[string]guestOSTypeConfig{
|
|
|
|
provisioner.UnixOSType: guestOSTypeConfig{
|
|
|
|
executeCommand: "{{if .Sudo}}sudo {{end}}chef-solo --no-color -c {{.ConfigPath}} -j {{.JsonPath}}",
|
|
|
|
installCommand: "curl -L https://www.chef.io/chef/install.sh | {{if .Sudo}}sudo {{end}}bash",
|
|
|
|
stagingDir: "/tmp/packer-chef-client",
|
|
|
|
},
|
|
|
|
provisioner.WindowsOSType: guestOSTypeConfig{
|
|
|
|
executeCommand: "c:/opscode/chef/bin/chef-solo.bat --no-color -c {{.ConfigPath}} -j {{.JsonPath}}",
|
|
|
|
installCommand: "powershell.exe -Command \"(New-Object System.Net.WebClient).DownloadFile('http://chef.io/chef/install.msi', 'C:\\Windows\\Temp\\chef.msi');Start-Process 'msiexec' -ArgumentList '/qb /i C:\\Windows\\Temp\\chef.msi' -NoNewWindow -Wait\"",
|
|
|
|
stagingDir: "C:/Windows/Temp/packer-chef-client",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-08-26 00:07:37 -04:00
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-11-18 18:46:37 -05:00
|
|
|
ChefEnvironment string `mapstructure:"chef_environment"`
|
|
|
|
ConfigTemplate string `mapstructure:"config_template"`
|
|
|
|
CookbookPaths []string `mapstructure:"cookbook_paths"`
|
|
|
|
RolesPath string `mapstructure:"roles_path"`
|
|
|
|
DataBagsPath string `mapstructure:"data_bags_path"`
|
|
|
|
EncryptedDataBagSecretPath string `mapstructure:"encrypted_data_bag_secret_path"`
|
|
|
|
EnvironmentsPath string `mapstructure:"environments_path"`
|
|
|
|
ExecuteCommand string `mapstructure:"execute_command"`
|
|
|
|
InstallCommand string `mapstructure:"install_command"`
|
|
|
|
RemoteCookbookPaths []string `mapstructure:"remote_cookbook_paths"`
|
|
|
|
Json map[string]interface{}
|
|
|
|
PreventSudo bool `mapstructure:"prevent_sudo"`
|
|
|
|
RunList []string `mapstructure:"run_list"`
|
|
|
|
SkipInstall bool `mapstructure:"skip_install"`
|
|
|
|
StagingDir string `mapstructure:"staging_directory"`
|
2015-08-20 16:27:51 -04:00
|
|
|
GuestOSType string `mapstructure:"guest_os_type"`
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
ctx interpolate.Context
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
2015-08-20 16:27:51 -04:00
|
|
|
config Config
|
|
|
|
guestOSTypeConfig guestOSTypeConfig
|
|
|
|
guestCommands *provisioner.GuestCommands
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
type ConfigTemplate struct {
|
2013-11-18 18:46:37 -05:00
|
|
|
CookbookPaths string
|
|
|
|
DataBagsPath string
|
|
|
|
EncryptedDataBagSecretPath string
|
|
|
|
RolesPath string
|
|
|
|
EnvironmentsPath string
|
|
|
|
ChefEnvironment string
|
2013-09-19 14:54:24 -04:00
|
|
|
|
|
|
|
// Templates don't support boolean statements until Go 1.2. In the
|
|
|
|
// mean time, we do this.
|
|
|
|
// TODO(mitchellh): Remove when Go 1.2 is released
|
2013-11-18 18:46:37 -05:00
|
|
|
HasDataBagsPath bool
|
|
|
|
HasEncryptedDataBagSecretPath bool
|
|
|
|
HasRolesPath bool
|
|
|
|
HasEnvironmentsPath bool
|
2013-08-27 15:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExecuteTemplate struct {
|
|
|
|
ConfigPath string
|
2013-07-11 10:47:51 -04:00
|
|
|
JsonPath string
|
|
|
|
Sudo bool
|
2013-07-05 23:56:51 -04:00
|
|
|
}
|
|
|
|
|
2013-08-26 00:07:37 -04:00
|
|
|
type InstallChefTemplate struct {
|
|
|
|
Sudo bool
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
2015-05-27 17:50:20 -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:50:20 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"execute_command",
|
|
|
|
"install_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-08-26 00:07:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:27:51 -04:00
|
|
|
if p.config.GuestOSType == "" {
|
|
|
|
p.config.GuestOSType = provisioner.DefaultOSType
|
|
|
|
}
|
|
|
|
p.config.GuestOSType = strings.ToLower(p.config.GuestOSType)
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
p.guestOSTypeConfig, ok = guestOSTypeConfigs[p.config.GuestOSType]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.guestCommands, err = provisioner.NewGuestCommands(p.config.GuestOSType, !p.config.PreventSudo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Invalid guest_os_type: \"%s\"", p.config.GuestOSType)
|
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
if p.config.ExecuteCommand == "" {
|
2015-08-20 16:27:51 -04:00
|
|
|
p.config.ExecuteCommand = p.guestOSTypeConfig.executeCommand
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-26 00:07:37 -04:00
|
|
|
if p.config.InstallCommand == "" {
|
2015-08-20 16:27:51 -04:00
|
|
|
p.config.InstallCommand = p.guestOSTypeConfig.installCommand
|
2013-08-26 00:07:37 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
if p.config.RunList == nil {
|
|
|
|
p.config.RunList = make([]string, 0)
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
if p.config.StagingDir == "" {
|
2015-08-20 16:27:51 -04:00
|
|
|
p.config.StagingDir = p.guestOSTypeConfig.stagingDir
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
var errs *packer.MultiError
|
2013-09-16 02:28:41 -04:00
|
|
|
if p.config.ConfigTemplate != "" {
|
|
|
|
fi, err := os.Stat(p.config.ConfigTemplate)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Bad config template path: %s", err))
|
|
|
|
} else if fi.IsDir() {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Config template path must be a file: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
for _, path := range p.config.CookbookPaths {
|
2013-07-05 00:26:48 -04:00
|
|
|
pFileInfo, err := os.Stat(path)
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
if err != nil || !pFileInfo.IsDir() {
|
2013-08-26 00:07:37 -04:00
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Bad cookbook path '%s': %s", path, err))
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
}
|
2013-09-18 17:14:18 -04:00
|
|
|
|
2013-09-10 23:00:29 -04:00
|
|
|
if p.config.RolesPath != "" {
|
|
|
|
pFileInfo, err := os.Stat(p.config.RolesPath)
|
2013-09-18 17:14:18 -04:00
|
|
|
|
2013-09-10 23:00:29 -04:00
|
|
|
if err != nil || !pFileInfo.IsDir() {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Bad roles path '%s': %s", p.config.RolesPath, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.DataBagsPath != "" {
|
|
|
|
pFileInfo, err := os.Stat(p.config.DataBagsPath)
|
2013-09-18 17:14:18 -04:00
|
|
|
|
2013-09-10 23:00:29 -04:00
|
|
|
if err != nil || !pFileInfo.IsDir() {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Bad data bags path '%s': %s", p.config.DataBagsPath, err))
|
|
|
|
}
|
|
|
|
}
|
2013-07-05 00:26:48 -04:00
|
|
|
|
2013-11-18 18:46:37 -05:00
|
|
|
if p.config.EncryptedDataBagSecretPath != "" {
|
|
|
|
pFileInfo, err := os.Stat(p.config.EncryptedDataBagSecretPath)
|
2013-11-13 13:39:29 -05:00
|
|
|
|
|
|
|
if err != nil || pFileInfo.IsDir() {
|
|
|
|
errs = packer.MultiErrorAppend(
|
2013-11-18 18:46:37 -05:00
|
|
|
errs, fmt.Errorf("Bad encrypted data bag secret '%s': %s", p.config.EncryptedDataBagSecretPath, err))
|
2013-11-13 13:39:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-01 11:20:36 -04:00
|
|
|
if p.config.EnvironmentsPath != "" {
|
|
|
|
pFileInfo, err := os.Stat(p.config.EnvironmentsPath)
|
|
|
|
|
|
|
|
if err != nil || !pFileInfo.IsDir() {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Bad environments path '%s': %s", p.config.EnvironmentsPath, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 00:56:32 -04:00
|
|
|
jsonValid := true
|
|
|
|
for k, v := range p.config.Json {
|
|
|
|
p.config.Json[k], err = p.deepJsonFix(k, v)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing JSON: %s", err))
|
|
|
|
jsonValid = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if jsonValid {
|
|
|
|
// Process the user variables within the JSON and set the JSON.
|
|
|
|
// Do this early so that we can validate and show errors.
|
|
|
|
p.config.Json, err = p.processJsonUserVars()
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing user variables in JSON: %s", err))
|
|
|
|
}
|
2013-08-30 19:05:04 -04:00
|
|
|
}
|
|
|
|
|
2013-08-26 00:07:37 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
2013-11-09 21:44:53 -05:00
|
|
|
ui.Say("Provisioning with chef-solo")
|
|
|
|
|
2013-07-06 00:15:21 -04:00
|
|
|
if !p.config.SkipInstall {
|
2013-08-26 00:07:37 -04:00
|
|
|
if err := p.installChef(ui, comm); err != nil {
|
|
|
|
return fmt.Errorf("Error installing Chef: %s", err)
|
2013-07-06 00:15:21 -04:00
|
|
|
}
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-08-27 16:27:06 -04:00
|
|
|
if err := p.createDir(ui, comm, p.config.StagingDir); err != nil {
|
2013-08-27 15:20:35 -04:00
|
|
|
return fmt.Errorf("Error creating staging directory: %s", err)
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-08-27 16:27:06 -04:00
|
|
|
cookbookPaths := make([]string, 0, len(p.config.CookbookPaths))
|
|
|
|
for i, path := range p.config.CookbookPaths {
|
|
|
|
targetPath := fmt.Sprintf("%s/cookbooks-%d", p.config.StagingDir, i)
|
|
|
|
if err := p.uploadDirectory(ui, comm, targetPath, path); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading cookbooks: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cookbookPaths = append(cookbookPaths, targetPath)
|
|
|
|
}
|
2013-09-18 17:14:18 -04:00
|
|
|
|
2013-09-10 23:00:29 -04:00
|
|
|
rolesPath := ""
|
|
|
|
if p.config.RolesPath != "" {
|
2013-09-27 11:13:55 -04:00
|
|
|
rolesPath = fmt.Sprintf("%s/roles", p.config.StagingDir)
|
2013-09-10 23:00:29 -04:00
|
|
|
if err := p.uploadDirectory(ui, comm, rolesPath, p.config.RolesPath); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading roles: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dataBagsPath := ""
|
|
|
|
if p.config.DataBagsPath != "" {
|
2013-09-27 11:13:55 -04:00
|
|
|
dataBagsPath = fmt.Sprintf("%s/data_bags", p.config.StagingDir)
|
2013-09-10 23:00:29 -04:00
|
|
|
if err := p.uploadDirectory(ui, comm, dataBagsPath, p.config.DataBagsPath); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading data bags: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-11-18 18:48:13 -05:00
|
|
|
encryptedDataBagSecretPath := ""
|
2013-11-18 18:46:37 -05:00
|
|
|
if p.config.EncryptedDataBagSecretPath != "" {
|
|
|
|
encryptedDataBagSecretPath = fmt.Sprintf("%s/encrypted_data_bag_secret", p.config.StagingDir)
|
|
|
|
if err := p.uploadFile(ui, comm, encryptedDataBagSecretPath, p.config.EncryptedDataBagSecretPath); err != nil {
|
2013-11-13 13:39:29 -05:00
|
|
|
return fmt.Errorf("Error uploading encrypted data bag secret: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-01 11:20:36 -04:00
|
|
|
environmentsPath := ""
|
|
|
|
if p.config.EnvironmentsPath != "" {
|
|
|
|
environmentsPath = fmt.Sprintf("%s/environments", p.config.StagingDir)
|
|
|
|
if err := p.uploadDirectory(ui, comm, environmentsPath, p.config.EnvironmentsPath); err != nil {
|
|
|
|
return fmt.Errorf("Error uploading environments: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 18:46:37 -05:00
|
|
|
configPath, err := p.createConfig(ui, comm, cookbookPaths, rolesPath, dataBagsPath, encryptedDataBagSecretPath, environmentsPath, p.config.ChefEnvironment)
|
2013-07-05 00:26:48 -04:00
|
|
|
if err != nil {
|
2013-08-27 15:20:35 -04:00
|
|
|
return fmt.Errorf("Error creating Chef config file: %s", err)
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
jsonPath, err := p.createJson(ui, comm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating JSON attributes: %s", err)
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
if err := p.executeChef(ui, comm, configPath, jsonPath); err != nil {
|
|
|
|
return fmt.Errorf("Error executing Chef: %s", err)
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-31 02:23:36 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-08-27 16:27:06 -04:00
|
|
|
func (p *Provisioner) uploadDirectory(ui packer.Ui, comm packer.Communicator, dst string, 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)
|
|
|
|
}
|
|
|
|
|
2013-11-13 13:39:29 -05:00
|
|
|
func (p *Provisioner) uploadFile(ui packer.Ui, comm packer.Communicator, dst string, src string) error {
|
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2014-05-10 00:03:35 -04:00
|
|
|
return comm.Upload(dst, f, nil)
|
2013-11-13 13:39:29 -05:00
|
|
|
}
|
|
|
|
|
2013-11-18 18:46:37 -05:00
|
|
|
func (p *Provisioner) createConfig(ui packer.Ui, comm packer.Communicator, localCookbooks []string, rolesPath string, dataBagsPath string, encryptedDataBagSecretPath string, environmentsPath string, chefEnvironment string) (string, error) {
|
2013-08-27 15:20:35 -04:00
|
|
|
ui.Message("Creating configuration file 'solo.rb'")
|
|
|
|
|
2013-08-27 16:27:06 -04:00
|
|
|
cookbook_paths := make([]string, len(p.config.RemoteCookbookPaths)+len(localCookbooks))
|
2013-08-27 15:20:35 -04:00
|
|
|
for i, path := range p.config.RemoteCookbookPaths {
|
|
|
|
cookbook_paths[i] = fmt.Sprintf(`"%s"`, path)
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-08-27 16:27:06 -04:00
|
|
|
for i, path := range localCookbooks {
|
|
|
|
i = len(p.config.RemoteCookbookPaths) + i
|
|
|
|
cookbook_paths[i] = fmt.Sprintf(`"%s"`, path)
|
|
|
|
}
|
2013-09-18 17:14:18 -04:00
|
|
|
|
2013-09-16 02:28:41 -04:00
|
|
|
// Read the template
|
|
|
|
tpl := DefaultConfigTemplate
|
|
|
|
if p.config.ConfigTemplate != "" {
|
|
|
|
f, err := os.Open(p.config.ConfigTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
tplBytes, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl = string(tplBytes)
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
p.config.ctx.Data = &ConfigTemplate{
|
2013-11-18 18:46:37 -05:00
|
|
|
CookbookPaths: strings.Join(cookbook_paths, ","),
|
|
|
|
RolesPath: rolesPath,
|
|
|
|
DataBagsPath: dataBagsPath,
|
|
|
|
EncryptedDataBagSecretPath: encryptedDataBagSecretPath,
|
|
|
|
EnvironmentsPath: environmentsPath,
|
|
|
|
HasRolesPath: rolesPath != "",
|
|
|
|
HasDataBagsPath: dataBagsPath != "",
|
|
|
|
HasEncryptedDataBagSecretPath: encryptedDataBagSecretPath != "",
|
|
|
|
HasEnvironmentsPath: environmentsPath != "",
|
|
|
|
ChefEnvironment: chefEnvironment,
|
2015-05-27 17:50:20 -04:00
|
|
|
}
|
|
|
|
configString, err := interpolate.Render(tpl, &p.config.ctx)
|
2013-07-05 00:26:48 -04:00
|
|
|
if err != nil {
|
2013-08-27 15:20:35 -04:00
|
|
|
return "", err
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2014-07-11 06:12:34 -04:00
|
|
|
remotePath := filepath.ToSlash(filepath.Join(p.config.StagingDir, "solo.rb"))
|
2014-05-10 00:03:35 -04:00
|
|
|
if err := comm.Upload(remotePath, bytes.NewReader([]byte(configString)), nil); err != nil {
|
2013-08-27 15:20:35 -04:00
|
|
|
return "", err
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
return remotePath, nil
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
func (p *Provisioner) createJson(ui packer.Ui, comm packer.Communicator) (string, error) {
|
|
|
|
ui.Message("Creating JSON attribute file")
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
jsonData := make(map[string]interface{})
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
// Copy the configured JSON
|
|
|
|
for k, v := range p.config.Json {
|
|
|
|
jsonData[k] = v
|
2013-07-12 11:09:52 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
// Set the run list if it was specified
|
|
|
|
if len(p.config.RunList) > 0 {
|
|
|
|
jsonData["run_list"] = p.config.RunList
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
jsonBytes, err := json.MarshalIndent(jsonData, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-08-28 20:27:14 -04:00
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
// Upload the bytes
|
2014-07-11 06:12:34 -04:00
|
|
|
remotePath := filepath.ToSlash(filepath.Join(p.config.StagingDir, "node.json"))
|
2014-05-10 00:03:35 -04:00
|
|
|
if err := comm.Upload(remotePath, bytes.NewReader(jsonBytes), nil); err != nil {
|
2013-08-27 15:20:35 -04:00
|
|
|
return "", err
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-12 11:09:52 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
return remotePath, nil
|
|
|
|
}
|
|
|
|
|
2013-08-27 16:27:06 -04:00
|
|
|
func (p *Provisioner) createDir(ui packer.Ui, comm packer.Communicator, dir string) error {
|
|
|
|
ui.Message(fmt.Sprintf("Creating directory: %s", dir))
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2015-08-20 16:27:51 -04:00
|
|
|
cmd := &packer.RemoteCmd{Command: p.guestCommands.CreateDir(dir)}
|
2013-08-27 15:20:35 -04:00
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
2013-07-11 12:22:15 -04:00
|
|
|
}
|
2013-08-27 15:20:35 -04:00
|
|
|
if cmd.ExitStatus != 0 {
|
2015-08-20 16:27:51 -04:00
|
|
|
return fmt.Errorf("Non-zero exit status. See output above for more info.")
|
2013-07-05 21:57:56 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2016-02-09 06:03:05 -05:00
|
|
|
// Chmod the directory to 0777 just so that we can access it as our user
|
|
|
|
cmd = &packer.RemoteCmd{Command: p.guestCommands.Chmod(dir, "0777")}
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf("Non-zero exit status. See output above for more info.")
|
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) executeChef(ui packer.Ui, comm packer.Communicator, config string, json string) error {
|
2015-05-27 17:50:20 -04:00
|
|
|
p.config.ctx.Data = &ExecuteTemplate{
|
2013-08-27 15:20:35 -04:00
|
|
|
ConfigPath: config,
|
|
|
|
JsonPath: json,
|
|
|
|
Sudo: !p.config.PreventSudo,
|
2015-05-27 17:50:20 -04:00
|
|
|
}
|
|
|
|
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
2013-07-05 21:57:56 -04:00
|
|
|
if err != nil {
|
2013-08-27 15:20:35 -04:00
|
|
|
return err
|
2013-07-05 21:57:56 -04:00
|
|
|
}
|
2013-07-05 00:26:48 -04:00
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
ui.Message(fmt.Sprintf("Executing Chef: %s", command))
|
2013-07-05 00:26:48 -04:00
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
cmd := &packer.RemoteCmd{
|
|
|
|
Command: command,
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf("Non-zero exit status: %d", cmd.ExitStatus)
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
return nil
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-26 00:07:37 -04:00
|
|
|
func (p *Provisioner) installChef(ui packer.Ui, comm packer.Communicator) error {
|
|
|
|
ui.Message("Installing Chef...")
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2015-05-27 17:50:20 -04:00
|
|
|
p.config.ctx.Data = &InstallChefTemplate{
|
2013-08-26 00:07:37 -04:00
|
|
|
Sudo: !p.config.PreventSudo,
|
2015-05-27 17:50:20 -04:00
|
|
|
}
|
|
|
|
command, err := interpolate.Render(p.config.InstallCommand, &p.config.ctx)
|
2013-07-05 00:26:48 -04:00
|
|
|
if err != nil {
|
2013-08-26 00:07:37 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &packer.RemoteCmd{Command: command}
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Install script exited with non-zero exit status %d", cmd.ExitStatus)
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-29 00:56:32 -04:00
|
|
|
func (p *Provisioner) deepJsonFix(key string, current interface{}) (interface{}, error) {
|
|
|
|
if current == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c := current.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
val := make([]interface{}, len(c))
|
|
|
|
for i, v := range c {
|
|
|
|
var err error
|
|
|
|
val[i], err = p.deepJsonFix(fmt.Sprintf("%s[%d]", key, i), v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return val, nil
|
2014-04-29 03:33:35 -04:00
|
|
|
case []uint8:
|
|
|
|
return string(c), nil
|
2014-04-29 00:56:32 -04:00
|
|
|
case map[interface{}]interface{}:
|
|
|
|
val := make(map[string]interface{})
|
|
|
|
for k, v := range c {
|
|
|
|
ks, ok := k.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%s: key is not string", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
val[ks], err = p.deepJsonFix(
|
|
|
|
fmt.Sprintf("%s.%s", key, ks), v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return val, nil
|
|
|
|
default:
|
2014-04-29 01:20:57 -04:00
|
|
|
return current, nil
|
2014-04-29 00:56:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-30 19:35:57 -04:00
|
|
|
func (p *Provisioner) processJsonUserVars() (map[string]interface{}, error) {
|
|
|
|
jsonBytes, err := json.Marshal(p.config.Json)
|
|
|
|
if err != nil {
|
|
|
|
// This really shouldn't happen since we literally just unmarshalled
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the user variables so that we can restore them later, and
|
|
|
|
// make sure we make the quotes JSON-friendly in the user variables.
|
|
|
|
originalUserVars := make(map[string]string)
|
2015-05-27 17:50:20 -04:00
|
|
|
for k, v := range p.config.ctx.UserVariables {
|
2013-08-30 19:35:57 -04:00
|
|
|
originalUserVars[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we reset them no matter what
|
|
|
|
defer func() {
|
2015-05-27 17:50:20 -04:00
|
|
|
p.config.ctx.UserVariables = originalUserVars
|
2013-08-30 19:35:57 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Make the current user variables JSON string safe.
|
2015-05-27 17:50:20 -04:00
|
|
|
for k, v := range p.config.ctx.UserVariables {
|
2013-08-30 19:35:57 -04:00
|
|
|
v = strings.Replace(v, `\`, `\\`, -1)
|
|
|
|
v = strings.Replace(v, `"`, `\"`, -1)
|
2015-05-27 17:50:20 -04:00
|
|
|
p.config.ctx.UserVariables[k] = v
|
2013-08-30 19:35:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process the bytes with the template processor
|
2015-05-27 17:50:20 -04:00
|
|
|
p.config.ctx.Data = nil
|
|
|
|
jsonBytesProcessed, err := interpolate.Render(string(jsonBytes), &p.config.ctx)
|
2013-08-30 19:35:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result map[string]interface{}
|
|
|
|
if err := json.Unmarshal([]byte(jsonBytesProcessed), &result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
var DefaultConfigTemplate = `
|
2013-09-10 23:00:29 -04:00
|
|
|
cookbook_path [{{.CookbookPaths}}]
|
2013-09-19 14:54:24 -04:00
|
|
|
{{if .HasRolesPath}}
|
2013-09-18 17:17:07 -04:00
|
|
|
role_path "{{.RolesPath}}"
|
2013-09-10 23:00:29 -04:00
|
|
|
{{end}}
|
2013-09-19 14:54:24 -04:00
|
|
|
{{if .HasDataBagsPath}}
|
2013-09-18 17:17:07 -04:00
|
|
|
data_bag_path "{{.DataBagsPath}}"
|
2013-09-10 23:00:29 -04:00
|
|
|
{{end}}
|
2013-11-18 18:46:37 -05:00
|
|
|
{{if .HasEncryptedDataBagSecretPath}}
|
|
|
|
encrypted_data_bag_secret "{{.EncryptedDataBagSecretPath}}"
|
2013-11-13 13:39:29 -05:00
|
|
|
{{end}}
|
2013-10-01 11:20:36 -04:00
|
|
|
{{if .HasEnvironmentsPath}}
|
2013-12-17 20:15:37 -05:00
|
|
|
environment_path "{{.EnvironmentsPath}}"
|
|
|
|
environment "{{.ChefEnvironment}}"
|
2013-10-01 11:20:36 -04:00
|
|
|
{{end}}
|
2013-08-27 15:20:35 -04:00
|
|
|
`
|