2015-05-08 23:25:24 +02:00
|
|
|
package chef
|
2015-05-08 13:45:31 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-05-19 20:42:14 +02:00
|
|
|
"context"
|
2015-05-08 13:45:31 +02:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
2015-11-24 14:40:46 +01:00
|
|
|
"path"
|
2015-05-08 14:52:25 +02:00
|
|
|
"regexp"
|
2018-01-19 16:49:04 +01:00
|
|
|
"strconv"
|
2015-05-08 13:45:31 +02:00
|
|
|
"strings"
|
2016-10-01 00:35:27 +02:00
|
|
|
"sync"
|
2015-05-08 13:45:31 +02:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/communicator"
|
|
|
|
"github.com/hashicorp/terraform/communicator/remote"
|
2016-11-29 21:00:26 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-05-08 13:45:31 +02:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-02-04 15:31:24 +01:00
|
|
|
clienrb = "client.rb"
|
|
|
|
defaultEnv = "_default"
|
|
|
|
firstBoot = "first-boot.json"
|
|
|
|
logfileDir = "logfiles"
|
|
|
|
linuxChefCmd = "chef-client"
|
|
|
|
linuxConfDir = "/etc/chef"
|
2016-09-15 14:20:18 +02:00
|
|
|
linuxNoOutput = "> /dev/null 2>&1"
|
|
|
|
linuxGemCmd = "/opt/chef/embedded/bin/gem"
|
|
|
|
linuxKnifeCmd = "knife"
|
2016-02-04 15:31:24 +01:00
|
|
|
secretKey = "encrypted_data_bag_secret"
|
|
|
|
windowsChefCmd = "cmd /c chef-client"
|
|
|
|
windowsConfDir = "C:/chef"
|
2016-09-15 14:20:18 +02:00
|
|
|
windowsNoOutput = "> nul 2>&1"
|
|
|
|
windowsGemCmd = "C:/opscode/chef/embedded/bin/gem"
|
|
|
|
windowsKnifeCmd = "cmd /c knife"
|
2015-05-08 13:45:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const clientConf = `
|
|
|
|
log_location STDOUT
|
|
|
|
chef_server_url "{{ .ServerURL }}"
|
|
|
|
node_name "{{ .NodeName }}"
|
2015-09-26 01:56:24 +02:00
|
|
|
{{ if .UsePolicyfile }}
|
|
|
|
use_policyfile true
|
|
|
|
policy_group "{{ .PolicyGroup }}"
|
|
|
|
policy_name "{{ .PolicyName }}"
|
2016-07-22 16:01:48 +02:00
|
|
|
{{ end -}}
|
2015-09-26 01:56:24 +02:00
|
|
|
|
2015-05-08 13:45:31 +02:00
|
|
|
{{ if .HTTPProxy }}
|
|
|
|
http_proxy "{{ .HTTPProxy }}"
|
|
|
|
ENV['http_proxy'] = "{{ .HTTPProxy }}"
|
|
|
|
ENV['HTTP_PROXY'] = "{{ .HTTPProxy }}"
|
2016-07-22 16:01:48 +02:00
|
|
|
{{ end -}}
|
2015-05-08 13:45:31 +02:00
|
|
|
|
|
|
|
{{ if .HTTPSProxy }}
|
|
|
|
https_proxy "{{ .HTTPSProxy }}"
|
|
|
|
ENV['https_proxy'] = "{{ .HTTPSProxy }}"
|
|
|
|
ENV['HTTPS_PROXY'] = "{{ .HTTPSProxy }}"
|
2016-07-22 16:01:48 +02:00
|
|
|
{{ end -}}
|
2015-05-08 13:45:31 +02:00
|
|
|
|
2016-01-13 15:51:08 +01:00
|
|
|
{{ if .NOProxy }}
|
|
|
|
no_proxy "{{ join .NOProxy "," }}"
|
|
|
|
ENV['no_proxy'] = "{{ join .NOProxy "," }}"
|
2016-07-22 16:01:48 +02:00
|
|
|
{{ end -}}
|
2016-01-13 15:51:08 +01:00
|
|
|
|
2016-07-22 16:01:48 +02:00
|
|
|
{{ if .SSLVerifyMode }}
|
|
|
|
ssl_verify_mode {{ .SSLVerifyMode }}
|
|
|
|
{{- end -}}
|
2016-01-07 18:13:30 +01:00
|
|
|
|
2016-07-22 16:01:48 +02:00
|
|
|
{{ if .DisableReporting }}
|
|
|
|
enable_reporting false
|
|
|
|
{{ end -}}
|
2016-01-09 00:42:02 +01:00
|
|
|
|
2016-07-22 16:01:48 +02:00
|
|
|
{{ if .ClientOptions }}
|
|
|
|
{{ join .ClientOptions "\n" }}
|
|
|
|
{{ end }}
|
2015-05-08 13:45:31 +02:00
|
|
|
`
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
type provisionFn func(terraform.UIOutput, communicator.Communicator) error
|
|
|
|
|
|
|
|
type provisioner struct {
|
|
|
|
Attributes map[string]interface{}
|
2018-03-20 11:51:14 +01:00
|
|
|
Channel string
|
2016-11-29 21:00:26 +01:00
|
|
|
ClientOptions []string
|
|
|
|
DisableReporting bool
|
|
|
|
Environment string
|
|
|
|
FetchChefCertificates bool
|
|
|
|
LogToFile bool
|
|
|
|
UsePolicyfile bool
|
|
|
|
PolicyGroup string
|
|
|
|
PolicyName string
|
|
|
|
HTTPProxy string
|
|
|
|
HTTPSProxy string
|
|
|
|
NamedRunList string
|
|
|
|
NOProxy []string
|
|
|
|
NodeName string
|
|
|
|
OhaiHints []string
|
|
|
|
OSType string
|
|
|
|
RecreateClient bool
|
|
|
|
PreventSudo bool
|
|
|
|
RunList []string
|
|
|
|
SecretKey string
|
|
|
|
ServerURL string
|
|
|
|
SkipInstall bool
|
|
|
|
SkipRegister bool
|
|
|
|
SSLVerifyMode string
|
|
|
|
UserName string
|
|
|
|
UserKey string
|
2017-05-19 20:42:14 +02:00
|
|
|
Vaults map[string][]string
|
2016-11-29 21:00:26 +01:00
|
|
|
Version string
|
2016-09-15 14:20:18 +02:00
|
|
|
|
|
|
|
cleanupUserKeyCmd string
|
2017-05-19 20:42:14 +02:00
|
|
|
createConfigFiles provisionFn
|
|
|
|
installChefClient provisionFn
|
|
|
|
fetchChefCertificates provisionFn
|
|
|
|
generateClientKey provisionFn
|
|
|
|
configureVaults provisionFn
|
|
|
|
runChefClient provisionFn
|
2016-02-04 15:31:24 +01:00
|
|
|
useSudo bool
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
// Provisioner returns a Chef provisioner
|
2016-11-29 21:00:26 +01:00
|
|
|
func Provisioner() terraform.ResourceProvisioner {
|
|
|
|
return &schema.Provisioner{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-05-19 20:42:14 +02:00
|
|
|
"node_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"server_url": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"user_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"user_key": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"attributes_json": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2018-03-20 11:51:14 +01:00
|
|
|
"channel": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "stable",
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"client_options": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-11-29 21:00:26 +01:00
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"disable_reporting": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"environment": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2017-05-19 20:42:14 +02:00
|
|
|
Default: defaultEnv,
|
2016-11-29 21:00:26 +01:00
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"fetch_chef_certificates": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"log_to_file": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"use_policyfile": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"policy_group": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"policy_name": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"http_proxy": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"https_proxy": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"no_proxy": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-11-29 21:00:26 +01:00
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"named_run_list": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"ohai_hints": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-11-29 21:00:26 +01:00
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"os_type": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"recreate_client": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"prevent_sudo": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"run_list": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-11-29 21:00:26 +01:00
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"secret_key": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"skip_install": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"skip_register": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"ssl_verify_mode": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"vault_json": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
"version": &schema.Schema{
|
2016-11-29 21:00:26 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
2017-05-19 20:42:14 +02:00
|
|
|
|
|
|
|
ApplyFunc: applyFn,
|
|
|
|
ValidateFunc: validateFn,
|
2016-11-29 21:00:26 +01:00
|
|
|
}
|
2016-12-23 02:11:05 +01:00
|
|
|
}
|
|
|
|
|
2016-11-29 21:00:26 +01:00
|
|
|
// TODO: Support context cancelling (Provisioner Stop)
|
2017-05-19 20:42:14 +02:00
|
|
|
func applyFn(ctx context.Context) error {
|
2016-11-29 21:00:26 +01:00
|
|
|
o := ctx.Value(schema.ProvOutputKey).(terraform.UIOutput)
|
2017-06-14 21:40:31 +02:00
|
|
|
s := ctx.Value(schema.ProvRawStateKey).(*terraform.InstanceState)
|
2016-11-29 21:00:26 +01:00
|
|
|
d := ctx.Value(schema.ProvConfigDataKey).(*schema.ResourceData)
|
2017-05-19 20:42:14 +02:00
|
|
|
|
2017-06-15 19:57:04 +02:00
|
|
|
// Decode the provisioner config
|
2016-11-29 21:00:26 +01:00
|
|
|
p, err := decodeConfig(d)
|
2015-05-08 13:45:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-25 14:29:48 +02:00
|
|
|
if p.OSType == "" {
|
2017-06-14 21:40:31 +02:00
|
|
|
switch t := s.Ephemeral.ConnInfo["type"]; t {
|
2015-06-25 14:29:48 +02:00
|
|
|
case "ssh", "": // The default connection type is ssh, so if the type is empty assume ssh
|
|
|
|
p.OSType = "linux"
|
|
|
|
case "winrm":
|
|
|
|
p.OSType = "windows"
|
|
|
|
default:
|
2016-11-29 21:00:26 +01:00
|
|
|
return fmt.Errorf("Unsupported connection type: %s", t)
|
2015-06-25 14:29:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 13:45:31 +02:00
|
|
|
// Set some values based on the targeted OS
|
2015-06-25 14:29:48 +02:00
|
|
|
switch p.OSType {
|
|
|
|
case "linux":
|
2016-09-15 14:20:18 +02:00
|
|
|
p.cleanupUserKeyCmd = fmt.Sprintf("rm -f %s", path.Join(linuxConfDir, p.UserName+".pem"))
|
2015-06-25 14:29:48 +02:00
|
|
|
p.createConfigFiles = p.linuxCreateConfigFiles
|
2016-09-15 14:20:18 +02:00
|
|
|
p.installChefClient = p.linuxInstallChefClient
|
2016-02-25 20:10:50 +01:00
|
|
|
p.fetchChefCertificates = p.fetchChefCertificatesFunc(linuxKnifeCmd, linuxConfDir)
|
2016-09-15 14:20:18 +02:00
|
|
|
p.generateClientKey = p.generateClientKeyFunc(linuxKnifeCmd, linuxConfDir, linuxNoOutput)
|
|
|
|
p.configureVaults = p.configureVaultsFunc(linuxGemCmd, linuxKnifeCmd, linuxConfDir)
|
2015-07-10 12:56:27 +02:00
|
|
|
p.runChefClient = p.runChefClientFunc(linuxChefCmd, linuxConfDir)
|
2017-06-14 21:40:31 +02:00
|
|
|
p.useSudo = !p.PreventSudo && s.Ephemeral.ConnInfo["user"] != "root"
|
2015-06-25 14:29:48 +02:00
|
|
|
case "windows":
|
2016-09-15 14:20:18 +02:00
|
|
|
p.cleanupUserKeyCmd = fmt.Sprintf("cd %s && del /F /Q %s", windowsConfDir, p.UserName+".pem")
|
2015-06-25 14:29:48 +02:00
|
|
|
p.createConfigFiles = p.windowsCreateConfigFiles
|
2016-09-15 14:20:18 +02:00
|
|
|
p.installChefClient = p.windowsInstallChefClient
|
2016-02-25 20:10:50 +01:00
|
|
|
p.fetchChefCertificates = p.fetchChefCertificatesFunc(windowsKnifeCmd, windowsConfDir)
|
2016-09-15 14:20:18 +02:00
|
|
|
p.generateClientKey = p.generateClientKeyFunc(windowsKnifeCmd, windowsConfDir, windowsNoOutput)
|
|
|
|
p.configureVaults = p.configureVaultsFunc(windowsGemCmd, windowsKnifeCmd, windowsConfDir)
|
2015-07-10 12:56:27 +02:00
|
|
|
p.runChefClient = p.runChefClientFunc(windowsChefCmd, windowsConfDir)
|
2015-05-12 10:37:38 +02:00
|
|
|
p.useSudo = false
|
2015-05-08 13:45:31 +02:00
|
|
|
default:
|
2015-06-25 14:29:48 +02:00
|
|
|
return fmt.Errorf("Unsupported os type: %s", p.OSType)
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get a new communicator
|
2017-06-14 21:40:31 +02:00
|
|
|
comm, err := communicator.New(s)
|
2015-05-08 13:45:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-15 00:25:05 +01:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, comm.Timeout())
|
|
|
|
defer cancel()
|
|
|
|
|
2015-05-08 13:45:31 +02:00
|
|
|
// Wait and retry until we establish the connection
|
2018-02-15 00:25:05 +01:00
|
|
|
err = communicator.Retry(ctx, func() error {
|
2017-05-19 20:42:14 +02:00
|
|
|
return comm.Connect(o)
|
2015-05-08 13:45:31 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer comm.Disconnect()
|
|
|
|
|
2016-09-15 14:20:18 +02:00
|
|
|
// Make sure we always delete the user key from the new node!
|
2016-10-01 00:35:27 +02:00
|
|
|
var once sync.Once
|
|
|
|
cleanupUserKey := func() {
|
2016-09-15 14:20:18 +02:00
|
|
|
o.Output("Cleanup user key...")
|
|
|
|
if err := p.runCommand(o, comm, p.cleanupUserKeyCmd); err != nil {
|
|
|
|
o.Output("WARNING: Failed to cleanup user key on new node: " + err.Error())
|
|
|
|
}
|
2016-10-01 00:35:27 +02:00
|
|
|
}
|
|
|
|
defer once.Do(cleanupUserKey)
|
2016-09-15 14:20:18 +02:00
|
|
|
|
2015-05-08 13:45:31 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-29 15:37:48 +02:00
|
|
|
if !p.SkipRegister {
|
|
|
|
if p.FetchChefCertificates {
|
|
|
|
o.Output("Fetch Chef certificates...")
|
|
|
|
if err := p.fetchChefCertificates(o, comm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-04 15:31:24 +01:00
|
|
|
}
|
|
|
|
|
2016-09-29 15:37:48 +02:00
|
|
|
o.Output("Generate the private key...")
|
|
|
|
if err := p.generateClientKey(o, comm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-15 14:20:18 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
if p.Vaults != nil {
|
2016-09-15 14:20:18 +02:00
|
|
|
o.Output("Configure Chef vaults...")
|
|
|
|
if err := p.configureVaults(o, comm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-01 00:35:27 +02:00
|
|
|
// Cleanup the user key before we run Chef-Client to prevent issues
|
|
|
|
// with rights caused by changing settings during the run.
|
|
|
|
once.Do(cleanupUserKey)
|
|
|
|
|
2015-05-08 13:45:31 +02:00
|
|
|
o.Output("Starting initial Chef-Client run...")
|
|
|
|
if err := p.runChefClient(o, comm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 19:57:04 +02:00
|
|
|
func validateFn(c *terraform.ResourceConfig) (ws []string, es []error) {
|
2018-01-19 16:49:04 +01:00
|
|
|
usePolicyFile := false
|
|
|
|
if usePolicyFileRaw, ok := c.Get("use_policyfile"); ok {
|
|
|
|
switch usePolicyFileRaw := usePolicyFileRaw.(type) {
|
|
|
|
case bool:
|
|
|
|
usePolicyFile = usePolicyFileRaw
|
|
|
|
case string:
|
|
|
|
usePolicyFileBool, err := strconv.ParseBool(usePolicyFileRaw)
|
|
|
|
if err != nil {
|
|
|
|
return ws, append(es, errors.New("\"use_policyfile\" must be a boolean"))
|
|
|
|
}
|
|
|
|
usePolicyFile = usePolicyFileBool
|
|
|
|
default:
|
|
|
|
return ws, append(es, errors.New("\"use_policyfile\" must be a boolean"))
|
|
|
|
}
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 16:49:04 +01:00
|
|
|
if !usePolicyFile && !c.IsSet("run_list") {
|
2017-06-15 19:57:04 +02:00
|
|
|
es = append(es, errors.New("\"run_list\": required field is not set"))
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
2018-01-19 16:49:04 +01:00
|
|
|
if usePolicyFile && !c.IsSet("policy_name") {
|
2017-06-15 19:57:04 +02:00
|
|
|
es = append(es, errors.New("using policyfile, but \"policy_name\" not set"))
|
2015-09-26 01:56:24 +02:00
|
|
|
}
|
2018-01-19 16:49:04 +01:00
|
|
|
if usePolicyFile && !c.IsSet("policy_group") {
|
2017-06-15 19:57:04 +02:00
|
|
|
es = append(es, errors.New("using policyfile, but \"policy_group\" not set"))
|
2016-09-15 14:20:18 +02:00
|
|
|
}
|
2015-05-08 13:45:31 +02:00
|
|
|
|
|
|
|
return ws, es
|
|
|
|
}
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
func (p *provisioner) deployConfigFiles(o terraform.UIOutput, comm communicator.Communicator, confDir string) error {
|
2016-09-15 14:20:18 +02:00
|
|
|
// Copy the user key to the new instance
|
|
|
|
pk := strings.NewReader(p.UserKey)
|
|
|
|
if err := comm.Upload(path.Join(confDir, p.UserName+".pem"), pk); err != nil {
|
|
|
|
return fmt.Errorf("Uploading user key failed: %v", err)
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
2015-11-12 22:17:51 +01:00
|
|
|
if p.SecretKey != "" {
|
2015-07-09 15:05:12 +02:00
|
|
|
// Copy the secret key to the new instance
|
2016-09-15 14:20:18 +02:00
|
|
|
s := strings.NewReader(p.SecretKey)
|
2015-11-24 14:40:46 +01:00
|
|
|
if err := comm.Upload(path.Join(confDir, secretKey), s); err != nil {
|
2015-07-09 15:05:12 +02:00
|
|
|
return fmt.Errorf("Uploading %s failed: %v", secretKey, err)
|
|
|
|
}
|
2015-07-08 20:43:37 +02:00
|
|
|
}
|
|
|
|
|
2016-07-22 16:01:48 +02:00
|
|
|
// Make sure the SSLVerifyMode value is written as a symbol
|
|
|
|
if p.SSLVerifyMode != "" && !strings.HasPrefix(p.SSLVerifyMode, ":") {
|
|
|
|
p.SSLVerifyMode = fmt.Sprintf(":%s", p.SSLVerifyMode)
|
|
|
|
}
|
|
|
|
|
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
|
2016-09-15 14:20:18 +02:00
|
|
|
err := t.Execute(&buf, p)
|
2015-05-08 13:45:31 +02:00
|
|
|
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
|
2017-05-19 20:42:14 +02:00
|
|
|
if err = comm.Upload(path.Join(confDir, clienrb), &buf); err != nil {
|
2015-05-08 23:25:24 +02:00
|
|
|
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{})
|
2017-05-19 20:42:14 +02:00
|
|
|
if p.Attributes != nil {
|
|
|
|
fb = p.Attributes
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-01-17 03:05:26 +01:00
|
|
|
log.Printf("[WARN] Found a 'run_list' specified in the configured attributes! " +
|
2015-05-08 23:25:24 +02:00
|
|
|
"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
|
2015-09-26 01:56:24 +02:00
|
|
|
if !p.UsePolicyfile {
|
|
|
|
fb["run_list"] = p.RunList
|
|
|
|
}
|
2015-05-08 13:45:31 +02:00
|
|
|
|
|
|
|
// 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
|
2015-11-24 14:40:46 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
func (p *provisioner) deployOhaiHints(o terraform.UIOutput, comm communicator.Communicator, hintDir string) error {
|
2015-06-25 15:48:54 +02:00
|
|
|
for _, hint := range p.OhaiHints {
|
|
|
|
// Open the hint file
|
|
|
|
f, err := os.Open(hint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Copy the hint to the new instance
|
2015-11-24 14:40:46 +01:00
|
|
|
if err := comm.Upload(path.Join(hintDir, path.Base(hint)), f); err != nil {
|
|
|
|
return fmt.Errorf("Uploading %s failed: %v", path.Base(hint), err)
|
2015-06-25 15:48:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
func (p *provisioner) fetchChefCertificatesFunc(
|
2016-09-15 14:20:18 +02:00
|
|
|
knifeCmd string,
|
|
|
|
confDir string) func(terraform.UIOutput, communicator.Communicator) error {
|
|
|
|
return func(o terraform.UIOutput, comm communicator.Communicator) error {
|
|
|
|
clientrb := path.Join(confDir, clienrb)
|
|
|
|
cmd := fmt.Sprintf("%s ssl fetch -c %s", knifeCmd, clientrb)
|
|
|
|
|
|
|
|
return p.runCommand(o, comm, cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
func (p *provisioner) generateClientKeyFunc(knifeCmd string, confDir string, noOutput string) provisionFn {
|
2016-09-15 14:20:18 +02:00
|
|
|
return func(o terraform.UIOutput, comm communicator.Communicator) error {
|
|
|
|
options := fmt.Sprintf("-c %s -u %s --key %s",
|
|
|
|
path.Join(confDir, clienrb),
|
|
|
|
p.UserName,
|
|
|
|
path.Join(confDir, p.UserName+".pem"),
|
|
|
|
)
|
|
|
|
|
|
|
|
// See if we already have a node object
|
|
|
|
getNodeCmd := fmt.Sprintf("%s node show %s %s %s", knifeCmd, p.NodeName, options, noOutput)
|
|
|
|
node := p.runCommand(o, comm, getNodeCmd) == nil
|
|
|
|
|
|
|
|
// See if we already have a client object
|
|
|
|
getClientCmd := fmt.Sprintf("%s client show %s %s %s", knifeCmd, p.NodeName, options, noOutput)
|
|
|
|
client := p.runCommand(o, comm, getClientCmd) == nil
|
|
|
|
|
|
|
|
// If we have a client, we can only continue if we are to recreate the client
|
|
|
|
if client && !p.RecreateClient {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Chef client %q already exists, set recreate_client=true to automatically recreate the client", p.NodeName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the node exists, try to delete it
|
|
|
|
if node {
|
|
|
|
deleteNodeCmd := fmt.Sprintf("%s node delete %s -y %s",
|
|
|
|
knifeCmd,
|
|
|
|
p.NodeName,
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
if err := p.runCommand(o, comm, deleteNodeCmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the client exists, try to delete it
|
|
|
|
if client {
|
|
|
|
deleteClientCmd := fmt.Sprintf("%s client delete %s -y %s",
|
|
|
|
knifeCmd,
|
|
|
|
p.NodeName,
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
if err := p.runCommand(o, comm, deleteClientCmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new client object
|
|
|
|
createClientCmd := fmt.Sprintf("%s client create %s -d -f %s %s",
|
|
|
|
knifeCmd,
|
|
|
|
p.NodeName,
|
|
|
|
path.Join(confDir, "client.pem"),
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
|
|
|
|
return p.runCommand(o, comm, createClientCmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
func (p *provisioner) configureVaultsFunc(gemCmd string, knifeCmd string, confDir string) provisionFn {
|
2016-09-15 14:20:18 +02:00
|
|
|
return func(o terraform.UIOutput, comm communicator.Communicator) error {
|
|
|
|
if err := p.runCommand(o, comm, fmt.Sprintf("%s install chef-vault", gemCmd)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
options := fmt.Sprintf("-c %s -u %s --key %s",
|
|
|
|
path.Join(confDir, clienrb),
|
|
|
|
p.UserName,
|
|
|
|
path.Join(confDir, p.UserName+".pem"),
|
|
|
|
)
|
|
|
|
|
2017-10-20 17:46:29 +02:00
|
|
|
// if client gets recreated, remove (old) client (with old keys) from vaults/items
|
|
|
|
// otherwise, the (new) client (with new keys) will not be able to decrypt the vault
|
|
|
|
if p.RecreateClient {
|
|
|
|
for vault, items := range p.Vaults {
|
|
|
|
for _, item := range items {
|
|
|
|
deleteCmd := fmt.Sprintf("%s vault remove %s %s -C \"%s\" -M client %s",
|
|
|
|
knifeCmd,
|
|
|
|
vault,
|
|
|
|
item,
|
|
|
|
p.NodeName,
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
if err := p.runCommand(o, comm, deleteCmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
for vault, items := range p.Vaults {
|
2016-10-01 00:35:27 +02:00
|
|
|
for _, item := range items {
|
2017-04-11 10:36:05 +02:00
|
|
|
updateCmd := fmt.Sprintf("%s vault update %s %s -C %s -M client %s",
|
2016-10-01 00:35:27 +02:00
|
|
|
knifeCmd,
|
|
|
|
vault,
|
|
|
|
item,
|
|
|
|
p.NodeName,
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
if err := p.runCommand(o, comm, updateCmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-15 14:20:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
func (p *provisioner) runChefClientFunc(chefCmd string, confDir string) provisionFn {
|
2016-09-15 14:20:18 +02:00
|
|
|
return func(o terraform.UIOutput, comm communicator.Communicator) error {
|
|
|
|
fb := path.Join(confDir, firstBoot)
|
|
|
|
var cmd string
|
|
|
|
|
|
|
|
// Policyfiles do not support chef environments, so don't pass the `-E` flag.
|
2017-01-19 09:03:45 +01:00
|
|
|
switch {
|
|
|
|
case p.UsePolicyfile && p.NamedRunList == "":
|
2016-09-15 14:20:18 +02:00
|
|
|
cmd = fmt.Sprintf("%s -j %q", chefCmd, fb)
|
2017-01-19 09:03:45 +01:00
|
|
|
case p.UsePolicyfile && p.NamedRunList != "":
|
|
|
|
cmd = fmt.Sprintf("%s -j %q -n %q", chefCmd, fb, p.NamedRunList)
|
|
|
|
default:
|
2016-09-15 14:20:18 +02:00
|
|
|
cmd = fmt.Sprintf("%s -j %q -E %q", chefCmd, fb, p.Environment)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.LogToFile {
|
|
|
|
if err := os.MkdirAll(logfileDir, 0755); err != nil {
|
|
|
|
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
|
2017-05-19 20:42:14 +02:00
|
|
|
func (p *provisioner) Output(output string) {
|
2016-09-15 14:20:18 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
// 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, "")
|
|
|
|
output = strings.Replace(output, "\r", "\n", -1)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 13:45:31 +02:00
|
|
|
// runCommand is used to run already prepared commands
|
2017-05-19 20:42:14 +02:00
|
|
|
func (p *provisioner) runCommand(o terraform.UIOutput, comm communicator.Communicator, command string) error {
|
2015-05-08 13:45:31 +02:00
|
|
|
// 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)
|
2018-03-15 17:55:57 +01:00
|
|
|
go func() {
|
|
|
|
// Wait for output to clean up
|
|
|
|
outW.Close()
|
|
|
|
errW.Close()
|
|
|
|
<-outDoneCh
|
|
|
|
<-errDoneCh
|
|
|
|
}()
|
2015-05-08 13:45:31 +02:00
|
|
|
|
|
|
|
cmd := &remote.Cmd{
|
|
|
|
Command: command,
|
|
|
|
Stdout: outW,
|
|
|
|
Stderr: errW,
|
|
|
|
}
|
|
|
|
|
2016-09-15 14:20:18 +02:00
|
|
|
err := comm.Start(cmd)
|
|
|
|
if err != nil {
|
2015-05-08 13:45:31 +02:00
|
|
|
return fmt.Errorf("Error executing command %q: %v", cmd.Command, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Wait()
|
2018-03-15 17:55:57 +01:00
|
|
|
if cmd.Err() != nil {
|
|
|
|
return cmd.Err()
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 17:55:57 +01:00
|
|
|
if cmd.ExitStatus() != 0 {
|
|
|
|
return fmt.Errorf("Command %q exited with non-zero exit status: %d", cmd.Command, cmd.ExitStatus())
|
|
|
|
}
|
2015-05-08 13:45:31 +02:00
|
|
|
|
2018-03-15 17:55:57 +01:00
|
|
|
return nil
|
2015-05-08 13:45:31 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
func (p *provisioner) copyOutput(o terraform.UIOutput, r io.Reader, doneCh chan<- struct{}) {
|
2015-05-08 13:45:31 +02:00
|
|
|
defer close(doneCh)
|
|
|
|
lr := linereader.New(r)
|
|
|
|
for line := range lr.Ch {
|
|
|
|
o.Output(line)
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 14:20:18 +02:00
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
func decodeConfig(d *schema.ResourceData) (*provisioner, error) {
|
|
|
|
p := &provisioner{
|
2018-03-20 11:51:14 +01:00
|
|
|
Channel: d.Get("channel").(string),
|
2016-11-29 21:00:26 +01:00
|
|
|
ClientOptions: getStringList(d.Get("client_options")),
|
|
|
|
DisableReporting: d.Get("disable_reporting").(bool),
|
|
|
|
Environment: d.Get("environment").(string),
|
|
|
|
FetchChefCertificates: d.Get("fetch_chef_certificates").(bool),
|
|
|
|
LogToFile: d.Get("log_to_file").(bool),
|
|
|
|
UsePolicyfile: d.Get("use_policyfile").(bool),
|
|
|
|
PolicyGroup: d.Get("policy_group").(string),
|
|
|
|
PolicyName: d.Get("policy_name").(string),
|
|
|
|
HTTPProxy: d.Get("http_proxy").(string),
|
|
|
|
HTTPSProxy: d.Get("https_proxy").(string),
|
|
|
|
NOProxy: getStringList(d.Get("no_proxy")),
|
|
|
|
NamedRunList: d.Get("named_run_list").(string),
|
|
|
|
NodeName: d.Get("node_name").(string),
|
|
|
|
OhaiHints: getStringList(d.Get("ohai_hints")),
|
|
|
|
OSType: d.Get("os_type").(string),
|
|
|
|
RecreateClient: d.Get("recreate_client").(bool),
|
|
|
|
PreventSudo: d.Get("prevent_sudo").(bool),
|
|
|
|
RunList: getStringList(d.Get("run_list")),
|
|
|
|
SecretKey: d.Get("secret_key").(string),
|
|
|
|
ServerURL: d.Get("server_url").(string),
|
|
|
|
SkipInstall: d.Get("skip_install").(bool),
|
|
|
|
SkipRegister: d.Get("skip_register").(bool),
|
|
|
|
SSLVerifyMode: d.Get("ssl_verify_mode").(string),
|
|
|
|
UserName: d.Get("user_name").(string),
|
|
|
|
UserKey: d.Get("user_key").(string),
|
|
|
|
Version: d.Get("version").(string),
|
2017-05-19 20:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the supplied URL has a trailing slash
|
|
|
|
p.ServerURL = strings.TrimSuffix(p.ServerURL, "/") + "/"
|
|
|
|
|
|
|
|
for i, hint := range p.OhaiHints {
|
|
|
|
hintPath, err := homedir.Expand(hint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error expanding the path %s: %v", hint, err)
|
|
|
|
}
|
|
|
|
p.OhaiHints[i] = hintPath
|
|
|
|
}
|
2016-11-29 21:00:26 +01:00
|
|
|
|
2017-05-19 20:42:14 +02:00
|
|
|
if attrs, ok := d.GetOk("attributes_json"); ok {
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := json.Unmarshal([]byte(attrs.(string)), &m); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error parsing attributes_json: %v", err)
|
|
|
|
}
|
|
|
|
p.Attributes = m
|
2016-11-29 21:00:26 +01:00
|
|
|
}
|
2017-05-19 20:42:14 +02:00
|
|
|
|
|
|
|
if vaults, ok := d.GetOk("vault_json"); ok {
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := json.Unmarshal([]byte(vaults.(string)), &m); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error parsing vault_json: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
v := make(map[string][]string)
|
|
|
|
for vault, items := range m {
|
|
|
|
switch items := items.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
for _, item := range items {
|
|
|
|
if item, ok := item.(string); ok {
|
|
|
|
v[vault] = append(v[vault], item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case interface{}:
|
|
|
|
if item, ok := items.(string); ok {
|
|
|
|
v[vault] = append(v[vault], item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Vaults = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
2016-11-29 21:00:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getStringList(v interface{}) []string {
|
2017-07-22 09:58:58 +02:00
|
|
|
var result []string
|
|
|
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
case nil:
|
|
|
|
return result
|
2016-11-29 21:00:26 +01:00
|
|
|
case []interface{}:
|
2017-07-22 09:58:58 +02:00
|
|
|
for _, vv := range v {
|
|
|
|
if vv, ok := vv.(string); ok {
|
|
|
|
result = append(result, vv)
|
|
|
|
}
|
2016-11-29 21:00:26 +01:00
|
|
|
}
|
2017-07-22 09:58:58 +02:00
|
|
|
return result
|
2016-11-29 21:00:26 +01:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unsupported type: %T", v))
|
|
|
|
}
|
|
|
|
}
|