2015-05-08 23:25:24 +02:00
|
|
|
package chef
|
2015-05-08 13:45:31 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2015-05-08 14:52:25 +02:00
|
|
|
"regexp"
|
2015-05-08 13:45:31 +02:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/communicator"
|
|
|
|
"github.com/hashicorp/terraform/communicator/remote"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-06-03 14:45:30 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2015-05-08 13:45:31 +02:00
|
|
|
"github.com/mitchellh/go-linereader"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-05-08 23:25:24 +02:00
|
|
|
clienrb = "client.rb"
|
2015-05-08 23:27:12 +02:00
|
|
|
defaultEnv = "_default"
|
2015-05-08 13:45:31 +02:00
|
|
|
firstBoot = "first-boot.json"
|
|
|
|
logfileDir = "logfiles"
|
|
|
|
linuxConfDir = "/etc/chef"
|
2015-05-08 23:25:24 +02:00
|
|
|
validationKey = "validation.pem"
|
2015-05-08 13:45:31 +02:00
|
|
|
windowsConfDir = "C:/chef"
|
|
|
|
)
|
|
|
|
|
|
|
|
const clientConf = `
|
|
|
|
log_location STDOUT
|
|
|
|
chef_server_url "{{ .ServerURL }}"
|
|
|
|
validation_client_name "{{ .ValidationClientName }}"
|
|
|
|
node_name "{{ .NodeName }}"
|
|
|
|
|
|
|
|
{{ if .HTTPProxy }}
|
|
|
|
http_proxy "{{ .HTTPProxy }}"
|
|
|
|
ENV['http_proxy'] = "{{ .HTTPProxy }}"
|
|
|
|
ENV['HTTP_PROXY'] = "{{ .HTTPProxy }}"
|
|
|
|
{{ end }}
|
|
|
|
|
|
|
|
{{ if .HTTPSProxy }}
|
|
|
|
https_proxy "{{ .HTTPSProxy }}"
|
|
|
|
ENV['https_proxy'] = "{{ .HTTPSProxy }}"
|
|
|
|
ENV['HTTPS_PROXY'] = "{{ .HTTPSProxy }}"
|
|
|
|
{{ end }}
|
|
|
|
|
|
|
|
{{ if .NOProxy }}no_proxy "{{ join .NOProxy "," }}"{{ end }}
|
|
|
|
{{ if .SSLVerifyMode }}ssl_verify_mode {{ .SSLVerifyMode }}{{ end }}
|
|
|
|
`
|
|
|
|
|
|
|
|
// Provisioner represents a specificly configured chef provisioner
|
|
|
|
type Provisioner struct {
|
2015-05-08 23:25:24 +02:00
|
|
|
Attributes interface{} `mapstructure:"attributes"`
|
2015-05-08 13:45:31 +02:00
|
|
|
Environment string `mapstructure:"environment"`
|
|
|
|
LogToFile bool `mapstructure:"log_to_file"`
|
|
|
|
HTTPProxy string `mapstructure:"http_proxy"`
|
|
|
|
HTTPSProxy string `mapstructure:"https_proxy"`
|
|
|
|
NOProxy []string `mapstructure:"no_proxy"`
|
|
|
|
NodeName string `mapstructure:"node_name"`
|
|
|
|
PreventSudo bool `mapstructure:"prevent_sudo"`
|
|
|
|
RunList []string `mapstructure:"run_list"`
|
|
|
|
ServerURL string `mapstructure:"server_url"`
|
|
|
|
SkipInstall bool `mapstructure:"skip_install"`
|
|
|
|
SSLVerifyMode string `mapstructure:"ssl_verify_mode"`
|
|
|
|
ValidationClientName string `mapstructure:"validation_client_name"`
|
|
|
|
ValidationKeyPath string `mapstructure:"validation_key_path"`
|
|
|
|
Version string `mapstructure:"version"`
|
|
|
|
|
|
|
|
installChefClient func(terraform.UIOutput, communicator.Communicator) error
|
|
|
|
createConfigFiles func(terraform.UIOutput, communicator.Communicator) error
|
|
|
|
runChefClient func(terraform.UIOutput, communicator.Communicator) error
|
2015-05-12 10:37:38 +02:00
|
|
|
useSudo bool
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceProvisioner represents a generic chef provisioner
|
|
|
|
type ResourceProvisioner struct{}
|
|
|
|
|
|
|
|
// Apply executes the file provisioner
|
|
|
|
func (r *ResourceProvisioner) Apply(
|
|
|
|
o terraform.UIOutput,
|
|
|
|
s *terraform.InstanceState,
|
|
|
|
c *terraform.ResourceConfig) error {
|
|
|
|
// Decode the raw config for this provisioner
|
|
|
|
p, err := r.decodeConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set some values based on the targeted OS
|
|
|
|
switch s.Ephemeral.ConnInfo["type"] {
|
|
|
|
case "ssh", "": // The default connection type is ssh, so if the type is empty use ssh
|
|
|
|
p.installChefClient = p.sshInstallChefClient
|
|
|
|
p.createConfigFiles = p.sshCreateConfigFiles
|
|
|
|
p.runChefClient = p.runChefClientFunc(linuxConfDir)
|
2015-05-12 10:37:38 +02:00
|
|
|
p.useSudo = !p.PreventSudo && s.Ephemeral.ConnInfo["user"] != "root"
|
2015-05-08 13:45:31 +02:00
|
|
|
case "winrm":
|
|
|
|
p.installChefClient = p.winrmInstallChefClient
|
|
|
|
p.createConfigFiles = p.winrmCreateConfigFiles
|
|
|
|
p.runChefClient = p.runChefClientFunc(windowsConfDir)
|
2015-05-12 10:37:38 +02:00
|
|
|
p.useSudo = false
|
2015-05-08 13:45:31 +02:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unsupported connection type: %s", s.Ephemeral.ConnInfo["type"])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a new communicator
|
|
|
|
comm, err := communicator.New(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait and retry until we establish the connection
|
|
|
|
err = retryFunc(comm.Timeout(), func() error {
|
|
|
|
err := comm.Connect(o)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer comm.Disconnect()
|
|
|
|
|
|
|
|
if !p.SkipInstall {
|
|
|
|
if err := p.installChefClient(o, comm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
o.Output("Creating configuration files...")
|
|
|
|
if err := p.createConfigFiles(o, comm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
o.Output("Starting initial Chef-Client run...")
|
|
|
|
if err := p.runChefClient(o, comm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks if the required arguments are configured
|
|
|
|
func (r *ResourceProvisioner) Validate(c *terraform.ResourceConfig) (ws []string, es []error) {
|
|
|
|
p, err := r.decodeConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
es = append(es, err)
|
|
|
|
return ws, es
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.NodeName == "" {
|
|
|
|
es = append(es, fmt.Errorf("Key not found: node_name"))
|
|
|
|
}
|
|
|
|
if p.RunList == nil {
|
|
|
|
es = append(es, fmt.Errorf("Key not found: run_list"))
|
|
|
|
}
|
|
|
|
if p.ServerURL == "" {
|
|
|
|
es = append(es, fmt.Errorf("Key not found: server_url"))
|
|
|
|
}
|
|
|
|
if p.ValidationClientName == "" {
|
|
|
|
es = append(es, fmt.Errorf("Key not found: validation_client_name"))
|
|
|
|
}
|
|
|
|
if p.ValidationKeyPath == "" {
|
|
|
|
es = append(es, fmt.Errorf("Key not found: validation_key_path"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ws, es
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ResourceProvisioner) decodeConfig(c *terraform.ResourceConfig) (*Provisioner, error) {
|
|
|
|
p := new(Provisioner)
|
|
|
|
|
|
|
|
decConf := &mapstructure.DecoderConfig{
|
2015-05-08 23:25:24 +02:00
|
|
|
ErrorUnused: true,
|
2015-05-08 13:45:31 +02:00
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: p,
|
|
|
|
}
|
|
|
|
dec, err := mapstructure.NewDecoder(decConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-03 14:45:30 +02:00
|
|
|
// We need to decode this twice. Once for the Raw config and once
|
|
|
|
// for the parsed Config. This makes sure that all values are there
|
|
|
|
// even if some still need to be interpolated later on.
|
|
|
|
// Without this the validation will fail when using a variable for
|
|
|
|
// a required parameter (the node_name for example).
|
|
|
|
if err := dec.Decode(c.Raw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-01 15:29:43 +02:00
|
|
|
if err := dec.Decode(c.Config); err != nil {
|
2015-05-08 13:45:31 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Environment == "" {
|
2015-05-08 23:27:12 +02:00
|
|
|
p.Environment = defaultEnv
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
2015-06-03 14:45:30 +02:00
|
|
|
if p.ValidationKeyPath != "" {
|
|
|
|
keyPath, err := homedir.Expand(p.ValidationKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error expanding the validation key path: %v", err)
|
|
|
|
}
|
|
|
|
p.ValidationKeyPath = keyPath
|
|
|
|
}
|
|
|
|
|
2015-06-01 15:29:43 +02:00
|
|
|
if attrs, ok := c.Config["attributes"]; ok {
|
2015-05-08 13:45:31 +02:00
|
|
|
p.Attributes, err = rawToJSON(attrs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error parsing the attributes: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func rawToJSON(raw interface{}) (interface{}, error) {
|
|
|
|
switch s := raw.(type) {
|
|
|
|
case []map[string]interface{}:
|
|
|
|
if len(s) != 1 {
|
|
|
|
return nil, errors.New("unexpected input while parsing raw config to JSON")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for k, v := range s[0] {
|
|
|
|
s[0][k], err = rawToJSON(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s[0], nil
|
|
|
|
default:
|
|
|
|
return raw, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// retryFunc is used to retry a function for a given duration
|
|
|
|
func retryFunc(timeout time.Duration, f func() error) error {
|
|
|
|
finish := time.After(timeout)
|
|
|
|
for {
|
|
|
|
err := f()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Printf("Retryable error: %v", err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-finish:
|
|
|
|
return err
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) runChefClientFunc(
|
|
|
|
confDir string) func(terraform.UIOutput, communicator.Communicator) error {
|
|
|
|
return func(o terraform.UIOutput, comm communicator.Communicator) error {
|
|
|
|
fb := path.Join(confDir, firstBoot)
|
|
|
|
cmd := fmt.Sprintf("chef-client -j %q -E %q", fb, p.Environment)
|
|
|
|
|
|
|
|
if p.LogToFile {
|
2015-05-08 23:25:24 +02:00
|
|
|
if err := os.MkdirAll(logfileDir, 0755); err != nil {
|
2015-05-08 13:45:31 +02:00
|
|
|
return fmt.Errorf("Error creating logfile directory %s: %v", logfileDir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logFile := path.Join(logfileDir, p.NodeName)
|
|
|
|
f, err := os.Create(path.Join(logFile))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating logfile %s: %v", logFile, err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
o.Output("Writing Chef Client output to " + logFile)
|
|
|
|
o = p
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.runCommand(o, comm, cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output implementation of terraform.UIOutput interface
|
|
|
|
func (p *Provisioner) Output(output string) {
|
|
|
|
logFile := path.Join(logfileDir, p.NodeName)
|
|
|
|
f, err := os.OpenFile(logFile, os.O_APPEND|os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error creating logfile %s: %v", logFile, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2015-05-08 14:52:25 +02:00
|
|
|
// These steps are needed to remove any ANSI escape codes used to colorize
|
|
|
|
// the output and to make sure we have proper line endings before writing
|
|
|
|
// the string to the logfile.
|
|
|
|
re := regexp.MustCompile(`\x1b\[[0-9;]+m`)
|
|
|
|
output = re.ReplaceAllString(output, "")
|
2015-05-08 13:45:31 +02:00
|
|
|
output = strings.Replace(output, "\r", "\n", -1)
|
2015-05-08 14:52:25 +02:00
|
|
|
|
2015-05-08 13:45:31 +02:00
|
|
|
if _, err := f.WriteString(output); err != nil {
|
|
|
|
log.Printf("Error writing output to logfile %s: %v", logFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.Sync(); err != nil {
|
|
|
|
log.Printf("Error saving logfile %s to disk: %v", logFile, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) deployConfigFiles(
|
|
|
|
o terraform.UIOutput,
|
|
|
|
comm communicator.Communicator,
|
|
|
|
confDir string) error {
|
2015-05-08 23:25:24 +02:00
|
|
|
// Open the validation key file
|
2015-05-08 13:45:31 +02:00
|
|
|
f, err := os.Open(p.ValidationKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2015-05-08 23:25:24 +02:00
|
|
|
// Copy the validation key to the new instance
|
|
|
|
if err := comm.Upload(path.Join(confDir, validationKey), f); err != nil {
|
|
|
|
return fmt.Errorf("Uploading %s failed: %v", validationKey, err)
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make strings.Join available for use within the template
|
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"join": strings.Join,
|
|
|
|
}
|
|
|
|
|
2015-05-08 23:25:24 +02:00
|
|
|
// Create a new template and parse the client config into it
|
|
|
|
t := template.Must(template.New(clienrb).Funcs(funcMap).Parse(clientConf))
|
2015-05-08 13:45:31 +02:00
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = t.Execute(&buf, p)
|
|
|
|
if err != nil {
|
2015-05-08 23:25:24 +02:00
|
|
|
return fmt.Errorf("Error executing %s template: %s", clienrb, err)
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
2015-05-08 23:25:24 +02:00
|
|
|
// Copy the client config to the new instance
|
|
|
|
if err := comm.Upload(path.Join(confDir, clienrb), &buf); err != nil {
|
|
|
|
return fmt.Errorf("Uploading %s failed: %v", clienrb, err)
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a map with first boot settings
|
|
|
|
fb := make(map[string]interface{})
|
|
|
|
if p.Attributes != nil {
|
|
|
|
fb = p.Attributes.(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
2015-05-08 23:25:24 +02:00
|
|
|
// Check if the run_list was also in the attributes and if so log a warning
|
|
|
|
// that it will be overwritten with the value of the run_list argument.
|
|
|
|
if _, found := fb["run_list"]; found {
|
|
|
|
log.Printf("[WARNING] Found a 'run_list' specified in the configured attributes! " +
|
|
|
|
"This value will be overwritten by the value of the `run_list` argument!")
|
|
|
|
}
|
|
|
|
|
2015-05-08 13:45:31 +02:00
|
|
|
// Add the initial runlist to the first boot settings
|
|
|
|
fb["run_list"] = p.RunList
|
|
|
|
|
|
|
|
// Marshal the first boot settings to JSON
|
|
|
|
d, err := json.Marshal(fb)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to create %s data: %s", firstBoot, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the first-boot.json to the new instance
|
|
|
|
if err := comm.Upload(path.Join(confDir, firstBoot), bytes.NewReader(d)); err != nil {
|
2015-05-08 23:25:24 +02:00
|
|
|
return fmt.Errorf("Uploading %s failed: %v", firstBoot, err)
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// runCommand is used to run already prepared commands
|
|
|
|
func (p *Provisioner) runCommand(
|
|
|
|
o terraform.UIOutput,
|
|
|
|
comm communicator.Communicator,
|
|
|
|
command string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Unless prevented, prefix the command with sudo
|
2015-05-12 10:37:38 +02:00
|
|
|
if p.useSudo {
|
2015-05-08 13:45:31 +02:00
|
|
|
command = "sudo " + command
|
|
|
|
}
|
|
|
|
|
|
|
|
outR, outW := io.Pipe()
|
|
|
|
errR, errW := io.Pipe()
|
|
|
|
outDoneCh := make(chan struct{})
|
|
|
|
errDoneCh := make(chan struct{})
|
|
|
|
go p.copyOutput(o, outR, outDoneCh)
|
|
|
|
go p.copyOutput(o, errR, errDoneCh)
|
|
|
|
|
|
|
|
cmd := &remote.Cmd{
|
|
|
|
Command: command,
|
|
|
|
Stdout: outW,
|
|
|
|
Stderr: errW,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := comm.Start(cmd); err != nil {
|
|
|
|
return fmt.Errorf("Error executing command %q: %v", cmd.Command, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Wait()
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"Command %q exited with non-zero exit status: %d", cmd.Command, cmd.ExitStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for output to clean up
|
|
|
|
outW.Close()
|
|
|
|
errW.Close()
|
|
|
|
<-outDoneCh
|
|
|
|
<-errDoneCh
|
|
|
|
|
|
|
|
// If we have an error, return it out now that we've cleaned up
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provisioner) copyOutput(o terraform.UIOutput, r io.Reader, doneCh chan<- struct{}) {
|
|
|
|
defer close(doneCh)
|
|
|
|
lr := linereader.New(r)
|
|
|
|
for line := range lr.Ch {
|
|
|
|
o.Output(line)
|
|
|
|
}
|
|
|
|
}
|