provisioners/remote-exec: switch to helper/schema
This commit is contained in:
parent
27c19af9ff
commit
3c0c81957a
|
@ -2,6 +2,7 @@ package remoteexec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -11,26 +12,53 @@ import (
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/communicator"
|
"github.com/hashicorp/terraform/communicator"
|
||||||
"github.com/hashicorp/terraform/communicator/remote"
|
"github.com/hashicorp/terraform/communicator/remote"
|
||||||
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
"github.com/mitchellh/go-linereader"
|
"github.com/mitchellh/go-linereader"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ResourceProvisioner represents a remote exec provisioner
|
func Provisioner() terraform.ResourceProvisioner {
|
||||||
type ResourceProvisioner struct{}
|
return &schema.Provisioner{
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
|
"inline": &schema.Schema{
|
||||||
|
Type: schema.TypeList,
|
||||||
|
Elem: &schema.Schema{Type: schema.TypeString},
|
||||||
|
Optional: true,
|
||||||
|
ConflictsWith: []string{"script", "scripts"},
|
||||||
|
},
|
||||||
|
|
||||||
|
"script": &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
ConflictsWith: []string{"inline", "scripts"},
|
||||||
|
},
|
||||||
|
|
||||||
|
"scripts": &schema.Schema{
|
||||||
|
Type: schema.TypeList,
|
||||||
|
Elem: &schema.Schema{Type: schema.TypeString},
|
||||||
|
Optional: true,
|
||||||
|
ConflictsWith: []string{"script", "inline"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
ApplyFunc: applyFn,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Apply executes the remote exec provisioner
|
// Apply executes the remote exec provisioner
|
||||||
func (p *ResourceProvisioner) Apply(
|
func applyFn(ctx context.Context) error {
|
||||||
o terraform.UIOutput,
|
connState := ctx.Value(schema.ProvRawStateKey).(*terraform.InstanceState)
|
||||||
s *terraform.InstanceState,
|
data := ctx.Value(schema.ProvConfigDataKey).(*schema.ResourceData)
|
||||||
c *terraform.ResourceConfig) error {
|
o := ctx.Value(schema.ProvOutputKey).(terraform.UIOutput)
|
||||||
|
|
||||||
// Get a new communicator
|
// Get a new communicator
|
||||||
comm, err := communicator.New(s)
|
comm, err := communicator.New(connState)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collect the scripts
|
// Collect the scripts
|
||||||
scripts, err := p.collectScripts(c)
|
scripts, err := collectScripts(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -39,67 +67,33 @@ func (p *ResourceProvisioner) Apply(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy and execute each script
|
// Copy and execute each script
|
||||||
if err := p.runScripts(o, comm, scripts); err != nil {
|
if err := runScripts(o, comm, scripts); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate checks if the required arguments are configured
|
|
||||||
func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) (ws []string, es []error) {
|
|
||||||
num := 0
|
|
||||||
for name := range c.Raw {
|
|
||||||
switch name {
|
|
||||||
case "scripts", "script", "inline":
|
|
||||||
num++
|
|
||||||
default:
|
|
||||||
es = append(es, fmt.Errorf("Unknown configuration '%s'", name))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if num != 1 {
|
|
||||||
es = append(es, fmt.Errorf("Must provide one of 'scripts', 'script' or 'inline' to remote-exec"))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// generateScripts takes the configuration and creates a script from each inline config
|
// generateScripts takes the configuration and creates a script from each inline config
|
||||||
func (p *ResourceProvisioner) generateScripts(c *terraform.ResourceConfig) ([]string, error) {
|
func generateScripts(d *schema.ResourceData) ([]string, error) {
|
||||||
var scripts []string
|
var scripts []string
|
||||||
command, ok := c.Config["inline"]
|
for _, l := range d.Get("inline").([]interface{}) {
|
||||||
if ok {
|
scripts = append(scripts, l.(string))
|
||||||
switch cmd := command.(type) {
|
|
||||||
case string:
|
|
||||||
scripts = append(scripts, cmd)
|
|
||||||
case []string:
|
|
||||||
scripts = append(scripts, cmd...)
|
|
||||||
case []interface{}:
|
|
||||||
for _, l := range cmd {
|
|
||||||
lStr, ok := l.(string)
|
|
||||||
if ok {
|
|
||||||
scripts = append(scripts, lStr)
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("Unsupported 'inline' type! Must be string, or list of strings.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("Unsupported 'inline' type! Must be string, or list of strings.")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return scripts, nil
|
return scripts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// collectScripts is used to collect all the scripts we need
|
// collectScripts is used to collect all the scripts we need
|
||||||
// to execute in preparation for copying them.
|
// to execute in preparation for copying them.
|
||||||
func (p *ResourceProvisioner) collectScripts(c *terraform.ResourceConfig) ([]io.ReadCloser, error) {
|
func collectScripts(d *schema.ResourceData) ([]io.ReadCloser, error) {
|
||||||
// Check if inline
|
// Check if inline
|
||||||
_, ok := c.Config["inline"]
|
if _, ok := d.GetOk("inline"); ok {
|
||||||
if ok {
|
scripts, err := generateScripts(d)
|
||||||
scripts, err := p.generateScripts(c)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r := []io.ReadCloser{}
|
var r []io.ReadCloser
|
||||||
for _, script := range scripts {
|
for _, script := range scripts {
|
||||||
r = append(r, ioutil.NopCloser(bytes.NewReader([]byte(script))))
|
r = append(r, ioutil.NopCloser(bytes.NewReader([]byte(script))))
|
||||||
}
|
}
|
||||||
|
@ -109,31 +103,13 @@ func (p *ResourceProvisioner) collectScripts(c *terraform.ResourceConfig) ([]io.
|
||||||
|
|
||||||
// Collect scripts
|
// Collect scripts
|
||||||
var scripts []string
|
var scripts []string
|
||||||
s, ok := c.Config["script"]
|
if script, ok := d.GetOk("script"); ok {
|
||||||
if ok {
|
scripts = append(scripts, script.(string))
|
||||||
sStr, ok := s.(string)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("Unsupported 'script' type! Must be a string.")
|
|
||||||
}
|
|
||||||
scripts = append(scripts, sStr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sl, ok := c.Config["scripts"]
|
if scriptList, ok := d.GetOk("scripts"); ok {
|
||||||
if ok {
|
for _, script := range scriptList.([]interface{}) {
|
||||||
switch slt := sl.(type) {
|
scripts = append(scripts, script.(string))
|
||||||
case []string:
|
|
||||||
scripts = append(scripts, slt...)
|
|
||||||
case []interface{}:
|
|
||||||
for _, l := range slt {
|
|
||||||
lStr, ok := l.(string)
|
|
||||||
if ok {
|
|
||||||
scripts = append(scripts, lStr)
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("Unsupported 'scripts' type! Must be list of strings.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("Unsupported 'scripts' type! Must be list of strings.")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,7 +131,7 @@ func (p *ResourceProvisioner) collectScripts(c *terraform.ResourceConfig) ([]io.
|
||||||
}
|
}
|
||||||
|
|
||||||
// runScripts is used to copy and execute a set of scripts
|
// runScripts is used to copy and execute a set of scripts
|
||||||
func (p *ResourceProvisioner) runScripts(
|
func runScripts(
|
||||||
o terraform.UIOutput,
|
o terraform.UIOutput,
|
||||||
comm communicator.Communicator,
|
comm communicator.Communicator,
|
||||||
scripts []io.ReadCloser) error {
|
scripts []io.ReadCloser) error {
|
||||||
|
@ -175,8 +151,8 @@ func (p *ResourceProvisioner) runScripts(
|
||||||
errR, errW := io.Pipe()
|
errR, errW := io.Pipe()
|
||||||
outDoneCh := make(chan struct{})
|
outDoneCh := make(chan struct{})
|
||||||
errDoneCh := make(chan struct{})
|
errDoneCh := make(chan struct{})
|
||||||
go p.copyOutput(o, outR, outDoneCh)
|
go copyOutput(o, outR, outDoneCh)
|
||||||
go p.copyOutput(o, errR, errDoneCh)
|
go copyOutput(o, errR, errDoneCh)
|
||||||
|
|
||||||
remotePath := comm.ScriptPath()
|
remotePath := comm.ScriptPath()
|
||||||
err = retryFunc(comm.Timeout(), func() error {
|
err = retryFunc(comm.Timeout(), func() error {
|
||||||
|
@ -225,7 +201,7 @@ func (p *ResourceProvisioner) runScripts(
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ResourceProvisioner) copyOutput(
|
func copyOutput(
|
||||||
o terraform.UIOutput, r io.Reader, doneCh chan<- struct{}) {
|
o terraform.UIOutput, r io.Reader, doneCh chan<- struct{}) {
|
||||||
defer close(doneCh)
|
defer close(doneCh)
|
||||||
lr := linereader.New(r)
|
lr := linereader.New(r)
|
||||||
|
|
|
@ -9,18 +9,15 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/config"
|
"github.com/hashicorp/terraform/config"
|
||||||
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestResourceProvisioner_impl(t *testing.T) {
|
|
||||||
var _ terraform.ResourceProvisioner = new(ResourceProvisioner)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestResourceProvider_Validate_good(t *testing.T) {
|
func TestResourceProvider_Validate_good(t *testing.T) {
|
||||||
c := testConfig(t, map[string]interface{}{
|
c := testConfig(t, map[string]interface{}{
|
||||||
"inline": "echo foo",
|
"inline": "echo foo",
|
||||||
})
|
})
|
||||||
p := new(ResourceProvisioner)
|
p := Provisioner()
|
||||||
warn, errs := p.Validate(c)
|
warn, errs := p.Validate(c)
|
||||||
if len(warn) > 0 {
|
if len(warn) > 0 {
|
||||||
t.Fatalf("Warnings: %v", warn)
|
t.Fatalf("Warnings: %v", warn)
|
||||||
|
@ -34,7 +31,7 @@ func TestResourceProvider_Validate_bad(t *testing.T) {
|
||||||
c := testConfig(t, map[string]interface{}{
|
c := testConfig(t, map[string]interface{}{
|
||||||
"invalid": "nope",
|
"invalid": "nope",
|
||||||
})
|
})
|
||||||
p := new(ResourceProvisioner)
|
p := Provisioner()
|
||||||
warn, errs := p.Validate(c)
|
warn, errs := p.Validate(c)
|
||||||
if len(warn) > 0 {
|
if len(warn) > 0 {
|
||||||
t.Fatalf("Warnings: %v", warn)
|
t.Fatalf("Warnings: %v", warn)
|
||||||
|
@ -51,16 +48,17 @@ exit 0
|
||||||
|
|
||||||
var expectedInlineScriptsOut = strings.Split(expectedScriptOut, "\n")
|
var expectedInlineScriptsOut = strings.Split(expectedScriptOut, "\n")
|
||||||
|
|
||||||
func TestResourceProvider_generateScripts(t *testing.T) {
|
func TestResourceProvider_generateScript(t *testing.T) {
|
||||||
p := new(ResourceProvisioner)
|
p := Provisioner().(*schema.Provisioner)
|
||||||
conf := testConfig(t, map[string]interface{}{
|
conf := map[string]interface{}{
|
||||||
"inline": []interface{}{
|
"inline": []interface{}{
|
||||||
"cd /tmp",
|
"cd /tmp",
|
||||||
"wget http://foobar",
|
"wget http://foobar",
|
||||||
"exit 0",
|
"exit 0",
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
out, err := p.generateScripts(conf)
|
out, err := generateScripts(schema.TestResourceDataRaw(
|
||||||
|
t, p.Schema, conf))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -71,16 +69,17 @@ func TestResourceProvider_generateScripts(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResourceProvider_CollectScripts_inline(t *testing.T) {
|
func TestResourceProvider_CollectScripts_inline(t *testing.T) {
|
||||||
p := new(ResourceProvisioner)
|
p := Provisioner().(*schema.Provisioner)
|
||||||
conf := testConfig(t, map[string]interface{}{
|
conf := map[string]interface{}{
|
||||||
"inline": []interface{}{
|
"inline": []interface{}{
|
||||||
"cd /tmp",
|
"cd /tmp",
|
||||||
"wget http://foobar",
|
"wget http://foobar",
|
||||||
"exit 0",
|
"exit 0",
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
|
||||||
scripts, err := p.collectScripts(conf)
|
scripts, err := collectScripts(schema.TestResourceDataRaw(
|
||||||
|
t, p.Schema, conf))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -103,12 +102,13 @@ func TestResourceProvider_CollectScripts_inline(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResourceProvider_CollectScripts_script(t *testing.T) {
|
func TestResourceProvider_CollectScripts_script(t *testing.T) {
|
||||||
p := new(ResourceProvisioner)
|
p := Provisioner().(*schema.Provisioner)
|
||||||
conf := testConfig(t, map[string]interface{}{
|
conf := map[string]interface{}{
|
||||||
"script": "test-fixtures/script1.sh",
|
"script": "test-fixtures/script1.sh",
|
||||||
})
|
}
|
||||||
|
|
||||||
scripts, err := p.collectScripts(conf)
|
scripts, err := collectScripts(schema.TestResourceDataRaw(
|
||||||
|
t, p.Schema, conf))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -129,16 +129,17 @@ func TestResourceProvider_CollectScripts_script(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResourceProvider_CollectScripts_scripts(t *testing.T) {
|
func TestResourceProvider_CollectScripts_scripts(t *testing.T) {
|
||||||
p := new(ResourceProvisioner)
|
p := Provisioner().(*schema.Provisioner)
|
||||||
conf := testConfig(t, map[string]interface{}{
|
conf := map[string]interface{}{
|
||||||
"scripts": []interface{}{
|
"scripts": []interface{}{
|
||||||
"test-fixtures/script1.sh",
|
"test-fixtures/script1.sh",
|
||||||
"test-fixtures/script1.sh",
|
"test-fixtures/script1.sh",
|
||||||
"test-fixtures/script1.sh",
|
"test-fixtures/script1.sh",
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
|
||||||
scripts, err := p.collectScripts(conf)
|
scripts, err := collectScripts(schema.TestResourceDataRaw(
|
||||||
|
t, p.Schema, conf))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
package schema
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/config"
|
||||||
|
"github.com/hashicorp/terraform/terraform"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestResourceDataRaw creates a ResourceData from a raw configuration map.
|
||||||
|
func TestResourceDataRaw(
|
||||||
|
t *testing.T, schema map[string]*Schema, raw map[string]interface{}) *ResourceData {
|
||||||
|
c, err := config.NewRawConfig(raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sm := schemaMap(schema)
|
||||||
|
diff, err := sm.Diff(nil, terraform.NewResourceConfig(c))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := sm.Data(nil, diff)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
Loading…
Reference in New Issue