2013-07-05 00:26:48 -04:00
|
|
|
// This package implements a provisioner for Packer that executes
|
|
|
|
// shell scripts within the remote machine.
|
|
|
|
package chefSolo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2013-07-11 10:47:51 -04:00
|
|
|
"encoding/json"
|
2013-07-05 00:26:48 -04:00
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/iochan"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2013-07-11 10:47:51 -04:00
|
|
|
"path/filepath"
|
2013-07-05 00:26:48 -04:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
const RemoteStagingPath = "/tmp/provision/chef-solo"
|
|
|
|
const RemoteFileCachePath = "/tmp/provision/chef-solo"
|
|
|
|
const RemoteCookbookPath = "/tmp/provision/chef-solo/cookbooks"
|
2013-07-11 10:47:51 -04:00
|
|
|
const DefaultCookbooksPath = "cookbooks"
|
2013-07-05 00:26:48 -04:00
|
|
|
|
|
|
|
var Ui packer.Ui
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
// An array of local paths of cookbooks to upload.
|
2013-07-05 22:33:24 -04:00
|
|
|
CookbooksPaths []string `mapstructure:"cookbooks_paths"`
|
2013-07-05 00:26:48 -04:00
|
|
|
|
|
|
|
// An array of recipes to run.
|
2013-07-05 22:33:24 -04:00
|
|
|
Recipes []string
|
2013-07-05 00:26:48 -04:00
|
|
|
|
2013-07-05 21:57:56 -04:00
|
|
|
// A string of JSON that will be used as the JSON attributes for the
|
|
|
|
// Chef run.
|
|
|
|
Json map[string]interface{}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-06 00:15:21 -04:00
|
|
|
// Option to avoid sudo use when executing commands. Defaults to false.
|
2013-07-06 00:37:59 -04:00
|
|
|
PreventSudo bool `mapstructure:"prevent_sudo"`
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-06 00:15:21 -04:00
|
|
|
// If true, skips installing Chef. Defaults to false.
|
2013-07-05 21:57:56 -04:00
|
|
|
SkipInstall bool `mapstructure:"skip_install"`
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provisioner struct {
|
|
|
|
config config
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecuteRecipeTemplate struct {
|
|
|
|
SoloRbPath string
|
2013-07-11 10:47:51 -04:00
|
|
|
JsonPath string
|
|
|
|
Sudo bool
|
2013-07-05 23:56:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExecuteInstallChefTemplate struct {
|
2013-07-06 00:37:59 -04:00
|
|
|
PreventSudo bool
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
|
|
|
errs := make([]error, 0)
|
|
|
|
for _, raw := range raws {
|
|
|
|
if err := mapstructure.Decode(raw, &p.config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 22:33:24 -04:00
|
|
|
if p.config.CookbooksPaths == nil {
|
2013-07-11 10:47:51 -04:00
|
|
|
p.config.CookbooksPaths = []string{DefaultCookbooksPath}
|
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-07-05 22:33:24 -04:00
|
|
|
if p.config.Recipes == nil {
|
|
|
|
p.config.Recipes = make([]string, 0)
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-07-05 21:57:56 -04:00
|
|
|
if p.config.Json != nil {
|
|
|
|
if _, err := json.Marshal(p.config.Json); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("Bad JSON: %s", err))
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-05 21:57:56 -04:00
|
|
|
} else {
|
|
|
|
p.config.Json = make(map[string]interface{})
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2013-07-05 22:33:24 -04:00
|
|
|
for _, path := range p.config.CookbooksPaths {
|
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() {
|
|
|
|
errs = append(errs, fmt.Errorf("Bad cookbook path '%s': %s", path, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return &packer.MultiError{errs}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
2013-07-05 22:33:24 -04:00
|
|
|
cookbooksPaths := make([]string, len(p.config.CookbooksPaths))
|
|
|
|
copy(cookbooksPaths, p.config.CookbooksPaths)
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-06 00:15:21 -04:00
|
|
|
var err error
|
2013-07-05 00:26:48 -04:00
|
|
|
Ui = ui
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-06 00:15:21 -04:00
|
|
|
if !p.config.SkipInstall {
|
2013-07-06 00:37:59 -04:00
|
|
|
err = InstallChefSolo(p.config.PreventSudo, comm)
|
2013-07-06 00:15:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error installing Chef Solo: %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
|
|
|
err = CreateRemoteDirectory(RemoteCookbookPath, comm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating remote staging directory: %s", err)
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 22:33:24 -04:00
|
|
|
soloRbPath, err := CreateSoloRb(p.config.CookbooksPaths, comm)
|
2013-07-05 00:26:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating Chef Solo configuration file: %s", err)
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 22:33:24 -04:00
|
|
|
jsonPath, err := CreateAttributesJson(p.config.Json, p.config.Recipes, comm)
|
2013-07-05 00:26:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading JSON attributes file: %s", err)
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
// Upload all cookbooks
|
2013-07-05 22:33:24 -04:00
|
|
|
for _, path := range cookbooksPaths {
|
2013-07-05 00:26:48 -04:00
|
|
|
ui.Say(fmt.Sprintf("Copying cookbook path: %s", path))
|
|
|
|
err = UploadLocalDirectory(path, comm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading cookbooks: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
// Execute requested recipes
|
2013-07-05 21:57:56 -04:00
|
|
|
ui.Say("Beginning Chef Solo run")
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 21:57:56 -04:00
|
|
|
// Compile the command
|
|
|
|
var command bytes.Buffer
|
2013-07-06 00:37:59 -04:00
|
|
|
t := template.Must(template.New("chef-run").Parse("{{if .Sudo}}sudo {{end}}chef-solo --no-color -c {{.SoloRbPath}} -j {{.JsonPath}}"))
|
|
|
|
t.Execute(&command, &ExecuteRecipeTemplate{soloRbPath, jsonPath, !p.config.PreventSudo})
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 21:57:56 -04:00
|
|
|
err = executeCommand(command.String(), comm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error running Chef Solo: %s", err)
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-06 00:17:28 -04:00
|
|
|
// return fmt.Errorf("Die")
|
2013-07-05 00:26:48 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UploadLocalDirectory(localDir string, comm packer.Communicator) (err error) {
|
2013-07-11 10:47:51 -04:00
|
|
|
visitPath := func(path string, f os.FileInfo, err error) (err2 error) {
|
2013-07-05 00:26:48 -04:00
|
|
|
var remotePath = RemoteCookbookPath + "/" + path
|
|
|
|
if f.IsDir() {
|
|
|
|
// Make remote directory
|
|
|
|
err = CreateRemoteDirectory(remotePath, comm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Upload file to existing directory
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error opening file: %s", err)
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
err = comm.Upload(remotePath, file)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading file: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
err = filepath.Walk(localDir, visitPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error uploading cookbook %s: %s", localDir, err)
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateRemoteDirectory(path string, comm packer.Communicator) (err error) {
|
|
|
|
//Ui.Say(fmt.Sprintf("Creating directory: %s", path))
|
|
|
|
var copyCommand = []string{"mkdir -p", path}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
var cmd packer.RemoteCmd
|
|
|
|
cmd.Command = strings.Join(copyCommand, " ")
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
// Start the command
|
|
|
|
if err := comm.Start(&cmd); err != nil {
|
2013-07-11 10:47:51 -04:00
|
|
|
return fmt.Errorf("Unable to create remote directory %s: %d", path, err)
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for it to complete
|
|
|
|
cmd.Wait()
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-07-05 22:33:24 -04:00
|
|
|
func CreateSoloRb(cookbooksPaths []string, comm packer.Communicator) (str string, err error) {
|
2013-07-05 00:26:48 -04:00
|
|
|
Ui.Say(fmt.Sprintf("Creating Chef configuration file..."))
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
remotePath := RemoteStagingPath + "/solo.rb"
|
|
|
|
tf, err := ioutil.TempFile("", "packer-chef-solo-rb")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error preparing Chef solo.rb: %s", err)
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
// Write our contents to it
|
|
|
|
writer := bufio.NewWriter(tf)
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
// Messy, messy...
|
2013-07-11 10:47:51 -04:00
|
|
|
cbPathsCat := "\"" + RemoteCookbookPath + "/" + strings.Join(cookbooksPaths, "\",\""+RemoteCookbookPath+"/") + "\""
|
2013-07-05 00:26:48 -04:00
|
|
|
contents := "file_cache_path \"" + RemoteFileCachePath + "\"\ncookbook_path [" + cbPathsCat + "]\n"
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
if _, err := writer.WriteString(contents); err != nil {
|
|
|
|
return "", fmt.Errorf("Error preparing solo.rb: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := writer.Flush(); err != nil {
|
|
|
|
return "", fmt.Errorf("Error preparing solo.rb: %s", err)
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
name := tf.Name()
|
|
|
|
tf.Close()
|
|
|
|
f, err := os.Open(name)
|
|
|
|
comm.Upload(remotePath, f)
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
defer os.Remove(name)
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
// Upload the Chef Solo configuration file to the cookbook directory.
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error uploading Chef Solo configuration file: %s", err)
|
|
|
|
}
|
|
|
|
//executeCommand("sudo cat " + remotePath, comm)
|
|
|
|
return remotePath, nil
|
|
|
|
}
|
|
|
|
|
2013-07-05 22:33:24 -04:00
|
|
|
func CreateAttributesJson(jsonAttrs map[string]interface{}, recipes []string, comm packer.Communicator) (str string, err error) {
|
2013-07-05 21:57:56 -04:00
|
|
|
Ui.Say(fmt.Sprintf("Creating and uploading Chef attributes file"))
|
2013-07-05 00:26:48 -04:00
|
|
|
remotePath := RemoteStagingPath + "/node.json"
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 22:33:24 -04:00
|
|
|
var formattedRecipes []string
|
|
|
|
for _, value := range recipes {
|
2013-07-11 10:47:51 -04:00
|
|
|
formattedRecipes = append(formattedRecipes, "recipe["+value+"]")
|
2013-07-05 21:57:56 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 22:33:24 -04:00
|
|
|
// Add Recipes to JSON
|
|
|
|
jsonAttrs["run_list"] = formattedRecipes
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 21:57:56 -04:00
|
|
|
// Convert to JSON string
|
|
|
|
jsonString, err := json.MarshalIndent(jsonAttrs, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error parsing JSON attributes: %s", err)
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 21:57:56 -04:00
|
|
|
tf, err := ioutil.TempFile("", "packer-chef-solo-json")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error preparing Chef attributes file: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
2013-07-05 00:26:48 -04:00
|
|
|
|
2013-07-05 21:57:56 -04:00
|
|
|
// Write our contents to it
|
|
|
|
writer := bufio.NewWriter(tf)
|
|
|
|
if _, err := writer.WriteString(string(jsonString)); err != nil {
|
|
|
|
return "", fmt.Errorf("Error preparing Chef attributes file: %s", err)
|
|
|
|
}
|
2013-07-05 00:26:48 -04:00
|
|
|
|
2013-07-05 21:57:56 -04:00
|
|
|
if err := writer.Flush(); err != nil {
|
|
|
|
return "", fmt.Errorf("Error preparing Chef attributes file: %s", err)
|
2013-07-05 00:26:48 -04:00
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 21:57:56 -04:00
|
|
|
jsonFile := tf.Name()
|
|
|
|
tf.Close()
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
log.Printf("Opening %s for reading", jsonFile)
|
|
|
|
f, err := os.Open(jsonFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error opening JSON attributes file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Uploading %s => %s", jsonFile, remotePath)
|
|
|
|
err = comm.Upload(remotePath, f)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error uploading JSON attributes file: %s", err)
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
return remotePath, nil
|
|
|
|
}
|
|
|
|
|
2013-07-06 00:37:59 -04:00
|
|
|
func InstallChefSolo(preventSudo bool, comm packer.Communicator) (err error) {
|
2013-07-05 00:26:48 -04:00
|
|
|
Ui.Say(fmt.Sprintf("Installing Chef Solo"))
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 23:56:51 -04:00
|
|
|
var command bytes.Buffer
|
2013-07-06 00:37:59 -04:00
|
|
|
t := template.Must(template.New("install-chef").Parse("curl -L https://www.opscode.com/chef/install.sh | {{if .sudo}}sudo {{end}}bash"))
|
|
|
|
t.Execute(&command, map[string]bool{"sudo": !preventSudo})
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 23:56:51 -04:00
|
|
|
err = executeCommand(command.String(), comm)
|
2013-07-05 00:26:48 -04:00
|
|
|
if err != nil {
|
2013-07-11 10:47:51 -04:00
|
|
|
return fmt.Errorf("Unable to install Chef Solo: %d", 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeCommand(command string, comm packer.Communicator) (err error) {
|
|
|
|
// Setup the remote command
|
|
|
|
stdout_r, stdout_w := io.Pipe()
|
|
|
|
stderr_r, stderr_w := io.Pipe()
|
|
|
|
|
|
|
|
var cmd packer.RemoteCmd
|
|
|
|
cmd.Command = command
|
|
|
|
cmd.Stdout = stdout_w
|
|
|
|
cmd.Stderr = stderr_w
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
//Ui.Say(fmt.Sprintf("Executing command: %s", cmd.Command))
|
|
|
|
log.Printf("Executing command: %s", cmd.Command)
|
|
|
|
err = comm.Start(&cmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed executing command: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exitChan := make(chan int, 1)
|
|
|
|
stdoutChan := iochan.DelimReader(stdout_r, '\n')
|
|
|
|
stderrChan := iochan.DelimReader(stderr_r, '\n')
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer stdout_w.Close()
|
|
|
|
defer stderr_w.Close()
|
|
|
|
|
|
|
|
cmd.Wait()
|
|
|
|
exitChan <- cmd.ExitStatus
|
|
|
|
}()
|
|
|
|
|
|
|
|
OutputLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case output := <-stderrChan:
|
|
|
|
Ui.Message(strings.TrimSpace(output))
|
|
|
|
case output := <-stdoutChan:
|
|
|
|
Ui.Message(strings.TrimSpace(output))
|
|
|
|
case exitStatus := <-exitChan:
|
|
|
|
log.Printf("Chef Solo provisioner exited with status %d", exitStatus)
|
|
|
|
|
|
|
|
if exitStatus != 0 {
|
|
|
|
return fmt.Errorf("Command exited with non-zero exit status: %d", exitStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
break OutputLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we finish off stdout/stderr because we may have gotten
|
|
|
|
// a message from the exit channel first.
|
|
|
|
for output := range stdoutChan {
|
|
|
|
Ui.Message(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
for output := range stderrChan {
|
|
|
|
Ui.Message(output)
|
|
|
|
}
|
2013-07-11 10:47:51 -04:00
|
|
|
|
2013-07-05 00:26:48 -04:00
|
|
|
return nil
|
|
|
|
}
|