Remove redundant code and clean up some string concatenation.
Clean up Say statements.
This commit is contained in:
parent
a6f3bb3bb2
commit
ba5d7a9d72
|
@ -19,10 +19,12 @@ import (
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
const RemoteStagingPath = "/tmp/provision/chef-solo"
|
const (
|
||||||
const RemoteFileCachePath = "/tmp/provision/chef-solo"
|
RemoteStagingPath = "/tmp/provision/chef-solo"
|
||||||
const RemoteCookbookPath = "/tmp/provision/chef-solo/cookbooks"
|
RemoteFileCachePath = "/tmp/provision/chef-solo"
|
||||||
const DefaultCookbooksPath = "cookbooks"
|
RemoteCookbookPath = "/tmp/provision/chef-solo/cookbooks"
|
||||||
|
DefaultCookbooksPath = "cookbooks"
|
||||||
|
)
|
||||||
|
|
||||||
var Ui packer.Ui
|
var Ui packer.Ui
|
||||||
|
|
||||||
|
@ -68,7 +70,6 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
|
||||||
|
|
||||||
if p.config.CookbooksPaths == nil {
|
if p.config.CookbooksPaths == nil {
|
||||||
p.config.CookbooksPaths = []string{DefaultCookbooksPath}
|
p.config.CookbooksPaths = []string{DefaultCookbooksPath}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.config.Recipes == nil {
|
if p.config.Recipes == nil {
|
||||||
|
@ -99,9 +100,6 @@ 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 {
|
||||||
cookbooksPaths := make([]string, len(p.config.CookbooksPaths))
|
|
||||||
copy(cookbooksPaths, p.config.CookbooksPaths)
|
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
Ui = ui
|
Ui = ui
|
||||||
|
|
||||||
|
@ -128,7 +126,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upload all cookbooks
|
// Upload all cookbooks
|
||||||
for _, path := range cookbooksPaths {
|
for _, path := range p.config.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 {
|
||||||
|
@ -149,6 +147,8 @@ 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("SUCESS")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,7 +208,7 @@ func CreateRemoteDirectory(path string, comm packer.Communicator) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateSoloRb(cookbooksPaths []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("Creating Chef configuration file...")
|
||||||
|
|
||||||
remotePath := RemoteStagingPath + "/solo.rb"
|
remotePath := RemoteStagingPath + "/solo.rb"
|
||||||
|
|
||||||
|
@ -220,11 +220,24 @@ func CreateSoloRb(cookbooksPaths []string, comm packer.Communicator) (str string
|
||||||
// Write our contents to it
|
// Write our contents to it
|
||||||
writer := bufio.NewWriter(tf)
|
writer := bufio.NewWriter(tf)
|
||||||
|
|
||||||
// Messy, messy...
|
var cookbooksPathsFull = make([]string, len(cookbooksPaths))
|
||||||
cbPathsCat := "\"" + RemoteCookbookPath + "/" + strings.Join(cookbooksPaths, "\",\""+RemoteCookbookPath+"/") + "\""
|
for i, path := range cookbooksPaths {
|
||||||
contents := "file_cache_path \"" + RemoteFileCachePath + "\"\ncookbook_path [" + cbPathsCat + "]\n"
|
cookbooksPathsFull[i] = "\"" + RemoteCookbookPath + "/" + path + "\""
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := writer.WriteString(contents); err != nil {
|
var contents bytes.Buffer
|
||||||
|
var soloRbText = `
|
||||||
|
file_cache_path "{{.FileCachePath}}"
|
||||||
|
cookbook_path [{{.CookbookPath}}]
|
||||||
|
`
|
||||||
|
|
||||||
|
t := template.Must(template.New("soloRb").Parse(soloRbText))
|
||||||
|
t.Execute(&contents, map[string]string{
|
||||||
|
"FileCachePath": RemoteFileCachePath,
|
||||||
|
"CookbookPath": strings.Join(cookbooksPathsFull, ","),
|
||||||
|
})
|
||||||
|
|
||||||
|
if _, err := writer.WriteString(contents.String()); err != nil {
|
||||||
return "", fmt.Errorf("Error preparing solo.rb: %s", err)
|
return "", fmt.Errorf("Error preparing solo.rb: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,12 +258,12 @@ func CreateSoloRb(cookbooksPaths []string, comm packer.Communicator) (str string
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Error uploading Chef Solo configuration file: %s", err)
|
return "", fmt.Errorf("Error uploading Chef Solo configuration file: %s", err)
|
||||||
}
|
}
|
||||||
//executeCommand("sudo cat " + remotePath, comm)
|
|
||||||
return remotePath, nil
|
return remotePath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateAttributesJson(jsonAttrs map[string]interface{}, recipes []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("Creating and uploading Chef attributes file")
|
||||||
remotePath := RemoteStagingPath + "/node.json"
|
remotePath := RemoteStagingPath + "/node.json"
|
||||||
|
|
||||||
var formattedRecipes []string
|
var formattedRecipes []string
|
||||||
|
@ -305,7 +318,7 @@ func CreateAttributesJson(jsonAttrs map[string]interface{}, recipes []string, co
|
||||||
}
|
}
|
||||||
|
|
||||||
func InstallChefSolo(preventSudo bool, comm packer.Communicator) (err error) {
|
func InstallChefSolo(preventSudo bool, comm packer.Communicator) (err error) {
|
||||||
Ui.Say(fmt.Sprintf("Installing Chef Solo"))
|
Ui.Say("Installing Chef Solo")
|
||||||
|
|
||||||
var command bytes.Buffer
|
var command bytes.Buffer
|
||||||
t := template.Must(template.New("install-chef").Parse("curl -L https://www.opscode.com/chef/install.sh | {{if .sudo}}sudo {{end}}bash"))
|
t := template.Must(template.New("install-chef").Parse("curl -L https://www.opscode.com/chef/install.sh | {{if .sudo}}sudo {{end}}bash"))
|
||||||
|
@ -329,7 +342,6 @@ func executeCommand(command string, comm packer.Communicator) (err error) {
|
||||||
cmd.Stdout = stdout_w
|
cmd.Stdout = stdout_w
|
||||||
cmd.Stderr = stderr_w
|
cmd.Stderr = stderr_w
|
||||||
|
|
||||||
//Ui.Say(fmt.Sprintf("Executing command: %s", cmd.Command))
|
|
||||||
log.Printf("Executing command: %s", cmd.Command)
|
log.Printf("Executing command: %s", cmd.Command)
|
||||||
err = comm.Start(&cmd)
|
err = comm.Start(&cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue