2015-05-08 23:25:24 +02:00
|
|
|
package chef
|
2015-05-08 13:45:31 +02:00
|
|
|
|
|
|
|
import (
|
2015-05-08 23:25:24 +02:00
|
|
|
"fmt"
|
2015-06-25 15:48:54 +02:00
|
|
|
"path"
|
2015-05-08 13:45:31 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/communicator"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-05-08 23:25:24 +02:00
|
|
|
const (
|
2016-01-09 15:56:49 +01:00
|
|
|
chmod = "find %s -maxdepth 1 -type f -exec /bin/chmod %d {} +"
|
2018-03-20 11:51:14 +01:00
|
|
|
installURL = "https://omnitruck.chef.io/install.sh"
|
2015-05-08 23:25:24 +02:00
|
|
|
)
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
func (p *provisioner) linuxInstallChefClient(o terraform.UIOutput, comm communicator.Communicator) error {
|
2015-05-08 23:25:24 +02:00
|
|
|
// Build up the command prefix
|
|
|
|
prefix := ""
|
2015-05-08 13:45:31 +02:00
|
|
|
if p.HTTPProxy != "" {
|
2015-07-03 15:04:58 +02:00
|
|
|
prefix += fmt.Sprintf("http_proxy='%s' ", p.HTTPProxy)
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
2015-07-03 16:27:52 +02:00
|
|
|
if p.HTTPSProxy != "" {
|
|
|
|
prefix += fmt.Sprintf("https_proxy='%s' ", p.HTTPSProxy)
|
|
|
|
}
|
2017-05-19 20:42:14 +02:00
|
|
|
if len(p.NOProxy) > 0 {
|
2015-05-08 23:25:24 +02:00
|
|
|
prefix += fmt.Sprintf("no_proxy='%s' ", strings.Join(p.NOProxy, ","))
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
2015-05-08 23:25:24 +02:00
|
|
|
|
|
|
|
// First download the install.sh script from Chef
|
|
|
|
err := p.runCommand(o, comm, fmt.Sprintf("%scurl -LO %s", prefix, installURL))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
2015-05-08 23:25:24 +02:00
|
|
|
|
|
|
|
// Then execute the install.sh scrip to download and install Chef Client
|
2018-03-20 11:51:14 +01:00
|
|
|
err = p.runCommand(o, comm, fmt.Sprintf("%sbash ./install.sh -v %q -c %s", prefix, p.Version, p.Channel))
|
2015-05-08 23:25:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-05-08 18:17:57 +02:00
|
|
|
}
|
2015-05-08 13:45:31 +02:00
|
|
|
|
2015-05-08 23:25:24 +02:00
|
|
|
// And finally cleanup the install.sh script again
|
|
|
|
return p.runCommand(o, comm, fmt.Sprintf("%srm -f install.sh", prefix))
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
func (p *provisioner) linuxCreateConfigFiles(o terraform.UIOutput, comm communicator.Communicator) error {
|
2015-05-08 13:45:31 +02:00
|
|
|
// Make sure the config directory exists
|
2015-05-08 18:17:57 +02:00
|
|
|
if err := p.runCommand(o, comm, "mkdir -p "+linuxConfDir); err != nil {
|
2015-05-08 13:45:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we have enough rights to upload the files if using sudo
|
2015-05-12 10:37:38 +02:00
|
|
|
if p.useSudo {
|
2015-05-08 13:45:31 +02:00
|
|
|
if err := p.runCommand(o, comm, "chmod 777 "+linuxConfDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-09 15:56:49 +01:00
|
|
|
if err := p.runCommand(o, comm, fmt.Sprintf(chmod, linuxConfDir, 666)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.deployConfigFiles(o, comm, linuxConfDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-25 15:48:54 +02:00
|
|
|
if len(p.OhaiHints) > 0 {
|
|
|
|
// Make sure the hits directory exists
|
|
|
|
hintsDir := path.Join(linuxConfDir, "ohai/hints")
|
|
|
|
if err := p.runCommand(o, comm, "mkdir -p "+hintsDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-20 18:31:32 +02:00
|
|
|
// Make sure we have enough rights to upload the hints if using sudo
|
|
|
|
if p.useSudo {
|
|
|
|
if err := p.runCommand(o, comm, "chmod 777 "+hintsDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-09 15:56:49 +01:00
|
|
|
if err := p.runCommand(o, comm, fmt.Sprintf(chmod, hintsDir, 666)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-20 18:31:32 +02:00
|
|
|
}
|
|
|
|
|
2015-06-25 15:48:54 +02:00
|
|
|
if err := p.deployOhaiHints(o, comm, hintsDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-20 18:31:32 +02:00
|
|
|
|
|
|
|
// When done copying the hints restore the rights and make sure root is owner
|
|
|
|
if p.useSudo {
|
|
|
|
if err := p.runCommand(o, comm, "chmod 755 "+hintsDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-09 15:56:49 +01:00
|
|
|
if err := p.runCommand(o, comm, fmt.Sprintf(chmod, hintsDir, 600)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-20 18:31:32 +02:00
|
|
|
if err := p.runCommand(o, comm, "chown -R root.root "+hintsDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-06-25 15:48:54 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 18:31:32 +02:00
|
|
|
// When done copying all files restore the rights and make sure root is owner
|
2015-05-12 10:37:38 +02:00
|
|
|
if p.useSudo {
|
2015-05-08 13:45:31 +02:00
|
|
|
if err := p.runCommand(o, comm, "chmod 755 "+linuxConfDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-09 15:56:49 +01:00
|
|
|
if err := p.runCommand(o, comm, fmt.Sprintf(chmod, linuxConfDir, 600)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-08 13:45:31 +02:00
|
|
|
if err := p.runCommand(o, comm, "chown -R root.root "+linuxConfDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|