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-08-26 00:07:37 -04:00
|
|
|
"github.com/mitchellh/packer/common"
|
2013-07-05 00:26:48 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
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"
|
|
|
|
)
|
|
|
|
|
2013-08-26 00:07:37 -04:00
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-09-16 02:28:41 -04:00
|
|
|
ConfigTemplate string `mapstructure:"config_template"`
|
2013-08-27 15:20:35 -04:00
|
|
|
CookbookPaths []string `mapstructure:"cookbook_paths"`
|
2013-09-18 17:14:18 -04:00
|
|
|
RolesPath string `mapstructure:"roles_path"`
|
|
|
|
DataBagsPath string `mapstructure:"data_bags_path"`
|
2013-08-27 15:20:35 -04:00
|
|
|
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"`
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-08-26 00:07:37 -04:00
|
|
|
tpl *packer.ConfigTemplate
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
2013-08-26 00:07:37 -04:00
|
|
|
config Config
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
type ConfigTemplate struct {
|
2013-09-18 17:14:18 -04:00
|
|
|
CookbookPaths string
|
|
|
|
RolesPath string
|
|
|
|
DataBagsPath string
|
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 {
|
2013-08-26 00:07:37 -04:00
|
|
|
md, err := common.DecodeConfig(&p.config, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-08-26 00:07:37 -04:00
|
|
|
p.config.tpl, err = packer.NewConfigTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.config.tpl.UserVars = p.config.PackerUserVars
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
if p.config.ExecuteCommand == "" {
|
|
|
|
p.config.ExecuteCommand = "{{if .Sudo}}sudo {{end}}chef-solo --no-color -c {{.ConfigPath}} -j {{.JsonPath}}"
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-26 00:07:37 -04:00
|
|
|
if p.config.InstallCommand == "" {
|
|
|
|
p.config.InstallCommand = "curl -L https://www.opscode.com/chef/install.sh | {{if .Sudo}}sudo {{end}}bash"
|
|
|
|
}
|
|
|
|
|
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 == "" {
|
|
|
|
p.config.StagingDir = "/tmp/packer-chef-solo"
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
// Accumulate any errors
|
|
|
|
errs := common.CheckUnusedConfig(md)
|
|
|
|
|
2013-08-27 20:41:49 -04:00
|
|
|
templates := map[string]*string{
|
2013-09-16 02:28:41 -04:00
|
|
|
"config_template": &p.config.ConfigTemplate,
|
|
|
|
"staging_dir": &p.config.StagingDir,
|
2013-08-27 20:41:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for n, ptr := range templates {
|
|
|
|
var err error
|
|
|
|
*ptr, err = p.config.tpl.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s: %s", n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceTemplates := map[string][]string{
|
|
|
|
"cookbook_paths": p.config.CookbookPaths,
|
|
|
|
"remote_cookbook_paths": p.config.RemoteCookbookPaths,
|
2013-08-30 19:05:04 -04:00
|
|
|
"run_list": p.config.RunList,
|
2013-08-27 20:41:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for n, slice := range sliceTemplates {
|
|
|
|
for i, elem := range slice {
|
|
|
|
var err error
|
|
|
|
slice[i], err = p.config.tpl.Process(elem, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error processing %s[%d]: %s", n, i, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
validates := map[string]*string{
|
|
|
|
"execute_command": &p.config.ExecuteCommand,
|
|
|
|
"install_command": &p.config.InstallCommand,
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, ptr := range validates {
|
|
|
|
if err := p.config.tpl.Validate(*ptr); err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Error parsing %s: %s", n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-30 19:05:04 -04:00
|
|
|
// 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-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-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 != "" {
|
|
|
|
rolesPath := fmt.Sprintf("%s/roles", p.config.StagingDir)
|
|
|
|
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 != "" {
|
|
|
|
dataBagsPath := fmt.Sprintf("%s/data_bags", p.config.StagingDir)
|
|
|
|
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-09-10 23:00:29 -04:00
|
|
|
configPath, err := p.createConfig(ui, comm, cookbookPaths, rolesPath, dataBagsPath)
|
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-09-10 23:00:29 -04:00
|
|
|
func (p *Provisioner) createConfig(ui packer.Ui, comm packer.Communicator, localCookbooks []string, rolesPath string, dataBagsPath 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-10 23:00:29 -04:00
|
|
|
roles_path := ""
|
|
|
|
if rolesPath != "" {
|
|
|
|
roles_path = fmt.Sprintf(`"%s"`, rolesPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
data_bags_path := ""
|
|
|
|
if dataBagsPath != "" {
|
|
|
|
data_bags_path = fmt.Sprintf(`"%s"`, dataBagsPath)
|
|
|
|
}
|
2013-08-27 16:27:06 -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)
|
|
|
|
}
|
|
|
|
|
|
|
|
configString, err := p.config.tpl.Process(tpl, &ConfigTemplate{
|
2013-08-27 15:20:35 -04:00
|
|
|
CookbookPaths: strings.Join(cookbook_paths, ","),
|
2013-09-18 17:14:18 -04:00
|
|
|
RolesPath: roles_path,
|
|
|
|
DataBagsPath: data_bags_path,
|
2013-08-27 15:20:35 -04:00
|
|
|
})
|
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
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
remotePath := filepath.Join(p.config.StagingDir, "solo.rb")
|
|
|
|
if err := comm.Upload(remotePath, bytes.NewReader([]byte(configString))); err != nil {
|
|
|
|
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
|
|
|
|
remotePath := filepath.Join(p.config.StagingDir, "node.json")
|
2013-08-30 19:05:04 -04:00
|
|
|
if err := comm.Upload(remotePath, bytes.NewReader(jsonBytes)); 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-08-27 15:20:35 -04:00
|
|
|
cmd := &packer.RemoteCmd{
|
2013-08-27 16:27:06 -04:00
|
|
|
Command: fmt.Sprintf("mkdir -p '%s'", dir),
|
2013-07-05 21:57:56 -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-11 12:22:15 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-08-27 15:20:35 -04:00
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
return fmt.Errorf("Non-zero exit status.")
|
2013-07-05 21:57:56 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
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 {
|
2013-08-27 17:35:11 -04:00
|
|
|
command, err := p.config.tpl.Process(p.config.ExecuteCommand, &ExecuteTemplate{
|
2013-08-27 15:20:35 -04:00
|
|
|
ConfigPath: config,
|
|
|
|
JsonPath: json,
|
|
|
|
Sudo: !p.config.PreventSudo,
|
|
|
|
})
|
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
|
|
|
|
2013-08-26 00:07:37 -04:00
|
|
|
command, err := p.config.tpl.Process(p.config.InstallCommand, &InstallChefTemplate{
|
|
|
|
Sudo: !p.config.PreventSudo,
|
|
|
|
})
|
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
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
for k, v := range p.config.tpl.UserVars {
|
|
|
|
originalUserVars[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we reset them no matter what
|
|
|
|
defer func() {
|
|
|
|
p.config.tpl.UserVars = originalUserVars
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Make the current user variables JSON string safe.
|
|
|
|
for k, v := range p.config.tpl.UserVars {
|
|
|
|
v = strings.Replace(v, `\`, `\\`, -1)
|
|
|
|
v = strings.Replace(v, `"`, `\"`, -1)
|
|
|
|
p.config.tpl.UserVars[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the bytes with the template processor
|
|
|
|
jsonBytesProcessed, err := p.config.tpl.Process(string(jsonBytes), nil)
|
|
|
|
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}}]
|
|
|
|
{{if .RolesPath != ""}}
|
|
|
|
role_path {{.RolesPath}}
|
|
|
|
{{end}}
|
|
|
|
{{if .DataBagsPath != ""}}
|
|
|
|
data_bag_path {{.DataBagsPath}}
|
|
|
|
{{end}}
|
2013-08-27 15:20:35 -04:00
|
|
|
`
|