Rename configuration `cookbook_paths` to `cookbooks_paths` to
conform to Vagrant wording. Remove configuration `cookbook_path` to simplify some things.
This commit is contained in:
parent
608d874b45
commit
bb3ff62623
|
@ -5,7 +5,6 @@ package chefSolo
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/iochan"
|
"github.com/mitchellh/iochan"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
|
@ -29,13 +28,10 @@ var Ui packer.Ui
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
// An array of local paths of cookbooks to upload.
|
// An array of local paths of cookbooks to upload.
|
||||||
CookbookPaths []string `mapstructure:"cookbook_paths"`
|
CookbooksPaths []string `mapstructure:"cookbooks_paths"`
|
||||||
|
|
||||||
// The local path of the cookbooks to upload.
|
|
||||||
CookbookPath string `mapstructure:"cookbook_path"`
|
|
||||||
|
|
||||||
// An array of recipes to run.
|
// An array of recipes to run.
|
||||||
RunList []string `mapstructure:"run_list"`
|
Recipes []string
|
||||||
|
|
||||||
// A string of JSON that will be used as the JSON attributes for the
|
// A string of JSON that will be used as the JSON attributes for the
|
||||||
// Chef run.
|
// Chef run.
|
||||||
|
@ -64,24 +60,12 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.config.CookbookPaths == nil {
|
if p.config.CookbooksPaths == nil {
|
||||||
p.config.CookbookPaths = make([]string, 0)
|
p.config.CookbooksPaths = make([]string, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.config.CookbookPath == "" {
|
if p.config.Recipes == nil {
|
||||||
p.config.CookbookPath = DefaultCookbookPath
|
p.config.Recipes = make([]string, 0)
|
||||||
}
|
|
||||||
|
|
||||||
if len(p.config.CookbookPaths) > 0 && p.config.CookbookPath != "" {
|
|
||||||
errs = append(errs, errors.New("Only one of cookbooks or cookbook can be specified."))
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(p.config.CookbookPaths) == 0 {
|
|
||||||
p.config.CookbookPaths = append(p.config.CookbookPaths, p.config.CookbookPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.config.RunList == nil {
|
|
||||||
p.config.RunList = make([]string, 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.config.Json != nil {
|
if p.config.Json != nil {
|
||||||
|
@ -92,7 +76,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
|
||||||
p.config.Json = make(map[string]interface{})
|
p.config.Json = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, path := range p.config.CookbookPaths {
|
for _, path := range p.config.CookbooksPaths {
|
||||||
pFileInfo, err := os.Stat(path)
|
pFileInfo, err := os.Stat(path)
|
||||||
|
|
||||||
if err != nil || !pFileInfo.IsDir() {
|
if err != nil || !pFileInfo.IsDir() {
|
||||||
|
@ -108,8 +92,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
||||||
cookbookPaths := make([]string, len(p.config.CookbookPaths))
|
cookbooksPaths := make([]string, len(p.config.CookbooksPaths))
|
||||||
copy(cookbookPaths, p.config.CookbookPaths)
|
copy(cookbooksPaths, p.config.CookbooksPaths)
|
||||||
|
|
||||||
Ui = ui
|
Ui = ui
|
||||||
|
|
||||||
|
@ -124,18 +108,18 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
||||||
return fmt.Errorf("Error creating remote staging directory: %s", err)
|
return fmt.Errorf("Error creating remote staging directory: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
soloRbPath, err := CreateSoloRb(p.config.CookbookPaths, comm)
|
soloRbPath, err := CreateSoloRb(p.config.CookbooksPaths, comm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error creating Chef Solo configuration file: %s", err)
|
return fmt.Errorf("Error creating Chef Solo configuration file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonPath, err := CreateAttributesJson(p.config.Json, p.config.RunList, comm)
|
jsonPath, err := CreateAttributesJson(p.config.Json, p.config.Recipes, comm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error uploading JSON attributes file: %s", err)
|
return fmt.Errorf("Error uploading JSON attributes file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upload all cookbooks
|
// Upload all cookbooks
|
||||||
for _, path := range cookbookPaths {
|
for _, path := range cookbooksPaths {
|
||||||
ui.Say(fmt.Sprintf("Copying cookbook path: %s", path))
|
ui.Say(fmt.Sprintf("Copying cookbook path: %s", path))
|
||||||
err = UploadLocalDirectory(path, comm)
|
err = UploadLocalDirectory(path, comm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -156,7 +140,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
||||||
return fmt.Errorf("Error running Chef Solo: %s", err)
|
return fmt.Errorf("Error running Chef Solo: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf("Die")
|
// return fmt.Errorf("Die")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -214,7 +198,7 @@ func CreateRemoteDirectory(path string, comm packer.Communicator) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateSoloRb(cookbookPaths []string, comm packer.Communicator) (str string, err error) {
|
func CreateSoloRb(cookbooksPaths []string, comm packer.Communicator) (str string, err error) {
|
||||||
Ui.Say(fmt.Sprintf("Creating Chef configuration file..."))
|
Ui.Say(fmt.Sprintf("Creating Chef configuration file..."))
|
||||||
|
|
||||||
remotePath := RemoteStagingPath + "/solo.rb"
|
remotePath := RemoteStagingPath + "/solo.rb"
|
||||||
|
@ -227,7 +211,7 @@ func CreateSoloRb(cookbookPaths []string, comm packer.Communicator) (str string,
|
||||||
writer := bufio.NewWriter(tf)
|
writer := bufio.NewWriter(tf)
|
||||||
|
|
||||||
// Messy, messy...
|
// Messy, messy...
|
||||||
cbPathsCat := "\"" + RemoteCookbookPath + "/" + strings.Join(cookbookPaths, "\",\"" + RemoteCookbookPath + "/") + "\""
|
cbPathsCat := "\"" + RemoteCookbookPath + "/" + strings.Join(cookbooksPaths, "\",\"" + RemoteCookbookPath + "/") + "\""
|
||||||
contents := "file_cache_path \"" + RemoteFileCachePath + "\"\ncookbook_path [" + cbPathsCat + "]\n"
|
contents := "file_cache_path \"" + RemoteFileCachePath + "\"\ncookbook_path [" + cbPathsCat + "]\n"
|
||||||
|
|
||||||
if _, err := writer.WriteString(contents); err != nil {
|
if _, err := writer.WriteString(contents); err != nil {
|
||||||
|
@ -254,17 +238,17 @@ func CreateSoloRb(cookbookPaths []string, comm packer.Communicator) (str string,
|
||||||
return remotePath, nil
|
return remotePath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateAttributesJson(jsonAttrs map[string]interface{}, runList []string, comm packer.Communicator) (str string, err error) {
|
func CreateAttributesJson(jsonAttrs map[string]interface{}, recipes []string, comm packer.Communicator) (str string, err error) {
|
||||||
Ui.Say(fmt.Sprintf("Creating and uploading Chef attributes file"))
|
Ui.Say(fmt.Sprintf("Creating and uploading Chef attributes file"))
|
||||||
remotePath := RemoteStagingPath + "/node.json"
|
remotePath := RemoteStagingPath + "/node.json"
|
||||||
|
|
||||||
var formattedRunList []string
|
var formattedRecipes []string
|
||||||
for _, value := range runList {
|
for _, value := range recipes {
|
||||||
formattedRunList = append(formattedRunList, "recipe[" + value + "]")
|
formattedRecipes = append(formattedRecipes, "recipe[" + value + "]")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add RunList to JSON
|
// Add Recipes to JSON
|
||||||
jsonAttrs["run_list"] = formattedRunList
|
jsonAttrs["run_list"] = formattedRecipes
|
||||||
|
|
||||||
// Convert to JSON string
|
// Convert to JSON string
|
||||||
jsonString, err := json.MarshalIndent(jsonAttrs, "", " ")
|
jsonString, err := json.MarshalIndent(jsonAttrs, "", " ")
|
||||||
|
|
Loading…
Reference in New Issue