From 20340b615cf0c5f3e9ed7fd0d508106aa08640ff Mon Sep 17 00:00:00 2001 From: Frederic Pinaud Date: Fri, 21 Feb 2020 13:38:38 +0100 Subject: [PATCH 01/10] Add interpreter options with an example Commented ShellMutex --- examples/test.tf | 47 ++++--------------------------- shell/data_source_shell_script.go | 15 ++++++++-- shell/resource_shell_script.go | 46 +++++++++++++++++++++--------- shell/utility.go | 19 ++++++++++++- 4 files changed, 68 insertions(+), 59 deletions(-) diff --git a/examples/test.tf b/examples/test.tf index ec5aa81..e9aad6d 100644 --- a/examples/test.tf +++ b/examples/test.tf @@ -40,13 +40,17 @@ resource "shell_script" "test3" { echo $out >> test3.json cat test3.json >&3 EOF - read = "cat test3.json >&3" + read = "cat test3.json >&3" delete = "rm -rf test3.json" } environment = { yolo = "yolo2" + } + interpreter = { + flag = "-c" + shell = "/bin/bash" } } @@ -72,45 +76,4 @@ resource "shell_script" "test4" { environment = { yolo = "yolo" } -} - -//test complete resource -resource "shell_script" "test5" { - lifecycle_commands { - create = file("${path.module}/scripts/create.sh") - read = file("${path.module}/scripts/read.sh") - update = file("${path.module}/scripts/update.sh") - delete = file("${path.module}/scripts/delete.sh") - } - - working_directory = "${path.module}" - - environment = { - yolo = "yolo" - ball = "room" - } -} - -output "commit_id2" { - value = shell_script.test5.output["commit_id"] -} - -//resource with triggers -resource "shell_script" "test6" { - lifecycle_commands { - create = file("${path.module}/scripts/create.sh") - read = file("${path.module}/scripts/read.sh") - delete = file("${path.module}/scripts/delete.sh") - } - - working_directory = "${path.module}" - - environment = { - yolo = "yolo" - ball = "room" - } - - triggers = { - abc = 123 - } } \ No newline at end of file diff --git a/shell/data_source_shell_script.go b/shell/data_source_shell_script.go index 9e7a82c..3a2c90b 100644 --- a/shell/data_source_shell_script.go +++ b/shell/data_source_shell_script.go @@ -44,6 +44,11 @@ func dataSourceShellScript() *schema.Resource { Computed: true, Elem: schema.TypeString, }, + "interpreter": { + Type: schema.TypeMap, + Optional: true, + Elem: schema.TypeString, + }, }, } } @@ -57,15 +62,19 @@ func dataSourceShellScriptRead(d *schema.ResourceData, meta interface{}) error { command := value.(string) vars := d.Get("environment").(map[string]interface{}) environment := readEnvironmentVariables(vars) + + inter := d.Get("interpreter").(map[string]interface{}) + interpreter := readInterpreterVariables(inter) + workingDirectory := d.Get("working_directory").(string) output := make(map[string]string) //obtain exclusive lock - shellMutexKV.Lock(shellScriptMutexKey) - defer shellMutexKV.Unlock(shellScriptMutexKey) + //shellMutexKV.Lock(shellScriptMutexKey) + //defer shellMutexKV.Unlock(shellScriptMutexKey) state := NewState(environment, output) - newState, err := runCommand(command, state, environment, workingDirectory) + newState, err := runCommand(command, state, environment, workingDirectory, interpreter) if err != nil { return err } diff --git a/shell/resource_shell_script.go b/shell/resource_shell_script.go index bdf7035..6e7bcd5 100644 --- a/shell/resource_shell_script.go +++ b/shell/resource_shell_script.go @@ -71,6 +71,12 @@ func resourceShellScript() *schema.Resource { Optional: true, Default: false, }, + "interpreter": { + Type: schema.TypeMap, + Optional: true, + Computed: true, + Elem: schema.TypeString, + }, }, } } @@ -100,18 +106,22 @@ func create(d *schema.ResourceData, meta interface{}, stack []string) error { command := c["create"].(string) vars := d.Get("environment").(map[string]interface{}) environment := readEnvironmentVariables(vars) + + inter := d.Get("interpreter").(map[string]interface{}) + interpreter := readInterpreterVariables(inter) + workingDirectory := d.Get("working_directory").(string) d.MarkNewResource() //obtain exclusive lock - shellMutexKV.Lock(shellScriptMutexKey) + //shellMutexKV.Lock(shellScriptMutexKey) output := make(map[string]string) state := NewState(environment, output) - newState, err := runCommand(command, state, environment, workingDirectory) + newState, err := runCommand(command, state, environment, workingDirectory, interpreter) if err != nil { return err } - shellMutexKV.Unlock(shellScriptMutexKey) + //shellMutexKV.Unlock(shellScriptMutexKey) //if create doesn't return a new state then must call the read operation if newState == nil { @@ -143,6 +153,10 @@ func read(d *schema.ResourceData, meta interface{}, stack []string) error { vars := d.Get("environment").(map[string]interface{}) environment := readEnvironmentVariables(vars) + + inter := d.Get("interpreter").(map[string]interface{}) + interpreter := readInterpreterVariables(inter) + workingDirectory := d.Get("working_directory").(string) o := d.Get("output").(map[string]interface{}) output := make(map[string]string) @@ -151,21 +165,20 @@ func read(d *schema.ResourceData, meta interface{}, stack []string) error { } //obtain exclusive lock - shellMutexKV.Lock(shellScriptMutexKey) + //shellMutexKV.Lock(shellScriptMutexKey) state := NewState(environment, output) - newState, err := runCommand(command, state, environment, workingDirectory) + newState, err := runCommand(command, state, environment, workingDirectory, interpreter) if err != nil { return err } - shellMutexKV.Unlock(shellScriptMutexKey) + //shellMutexKV.Unlock(shellScriptMutexKey) if newState == nil { log.Printf("[DEBUG] State from read operation was nil. Marking resource for deletion.") d.SetId("") return nil } - log.Printf("[DEBUG] output:|%v|", output) log.Printf("[DEBUG] new output:|%v|", newState.Output) isStateEqual := reflect.DeepEqual(output, newState.Output) isNewResource := d.IsNewResource() @@ -207,6 +220,9 @@ func update(d *schema.ResourceData, meta interface{}, stack []string) error { vars := d.Get("environment").(map[string]interface{}) environment := readEnvironmentVariables(vars) + inter := d.Get("interpreter").(map[string]interface{}) + interpreter := readInterpreterVariables(inter) + workingDirectory := d.Get("working_directory").(string) o := d.Get("output").(map[string]interface{}) output := make(map[string]string) @@ -215,15 +231,15 @@ func update(d *schema.ResourceData, meta interface{}, stack []string) error { } //obtain exclusive lock - shellMutexKV.Lock(shellScriptMutexKey) + //shellMutexKV.Lock(shellScriptMutexKey) state := NewState(oldEnvironment, output) - newState, err := runCommand(command, state, environment, workingDirectory) + newState, err := runCommand(command, state, environment, workingDirectory, interpreter) if err != nil { return err } - shellMutexKV.Unlock(shellScriptMutexKey) + //shellMutexKV.Unlock(shellScriptMutexKey) //if update doesn't return a new state then must call the read operation if newState == nil { @@ -245,6 +261,10 @@ func delete(d *schema.ResourceData, meta interface{}, stack []string) error { command := c["delete"].(string) vars := d.Get("environment").(map[string]interface{}) environment := readEnvironmentVariables(vars) + + inter := d.Get("interpreter").(map[string]interface{}) + interpreter := readInterpreterVariables(inter) + workingDirectory := d.Get("working_directory").(string) o := d.Get("output").(map[string]interface{}) output := make(map[string]string) @@ -253,11 +273,11 @@ func delete(d *schema.ResourceData, meta interface{}, stack []string) error { } //obtain exclusive lock - shellMutexKV.Lock(shellScriptMutexKey) - defer shellMutexKV.Unlock(shellScriptMutexKey) + //shellMutexKV.Lock(shellScriptMutexKey) + //defer shellMutexKV.Unlock(shellScriptMutexKey) state := NewState(environment, output) - _, err := runCommand(command, state, environment, workingDirectory) + _, err := runCommand(command, state, environment, workingDirectory, interpreter) if err != nil { return err } diff --git a/shell/utility.go b/shell/utility.go index 0b6d507..2852003 100644 --- a/shell/utility.go +++ b/shell/utility.go @@ -34,6 +34,20 @@ func readEnvironmentVariables(ev map[string]interface{}) []string { return variables } +func readInterpreterVariables(ev map[string]interface{}) [2]string { + var variables [2]string + if ev != nil { + for k := range ev { + if k == "flag" { + variables[0] = ev[k].(string) + } else if k == "shell" { + variables[1] = ev[k].(string) + } + } + } + return variables +} + func printStackTrace(stack []string) { log.Printf("-------------------------") log.Printf("[DEBUG] Current stack:") @@ -55,13 +69,16 @@ func parseJSON(b []byte) (map[string]string, error) { return output, err } -func runCommand(command string, state *State, environment []string, workingDirectory string) (*State, error) { +func runCommand(command string, state *State, environment []string, workingDirectory string, interpreter [2]string) (*State, error) { const maxBufSize = 8 * 1024 // Execute the command using a shell var shell, flag string if runtime.GOOS == "windows" { shell = "cmd" flag = "/C" + } else if interpreter[0] != "" { + shell = interpreter[1] + flag = interpreter[0] } else { shell = "/bin/sh" flag = "-c" From 1eb85f06bcd74215662befbb46c720f55859ecb4 Mon Sep 17 00:00:00 2001 From: Frederic Pinaud Date: Fri, 21 Feb 2020 14:02:12 +0100 Subject: [PATCH 02/10] Uncomment shellMutexKV --- shell/data_source_shell_script.go | 4 ++-- shell/resource_shell_script.go | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/shell/data_source_shell_script.go b/shell/data_source_shell_script.go index 3a2c90b..c8de1fe 100644 --- a/shell/data_source_shell_script.go +++ b/shell/data_source_shell_script.go @@ -70,8 +70,8 @@ func dataSourceShellScriptRead(d *schema.ResourceData, meta interface{}) error { output := make(map[string]string) //obtain exclusive lock - //shellMutexKV.Lock(shellScriptMutexKey) - //defer shellMutexKV.Unlock(shellScriptMutexKey) + shellMutexKV.Lock(shellScriptMutexKey) + defer shellMutexKV.Unlock(shellScriptMutexKey) state := NewState(environment, output) newState, err := runCommand(command, state, environment, workingDirectory, interpreter) diff --git a/shell/resource_shell_script.go b/shell/resource_shell_script.go index 6e7bcd5..a03d4d3 100644 --- a/shell/resource_shell_script.go +++ b/shell/resource_shell_script.go @@ -113,7 +113,7 @@ func create(d *schema.ResourceData, meta interface{}, stack []string) error { workingDirectory := d.Get("working_directory").(string) d.MarkNewResource() //obtain exclusive lock - //shellMutexKV.Lock(shellScriptMutexKey) + shellMutexKV.Lock(shellScriptMutexKey) output := make(map[string]string) state := NewState(environment, output) @@ -121,7 +121,7 @@ func create(d *schema.ResourceData, meta interface{}, stack []string) error { if err != nil { return err } - //shellMutexKV.Unlock(shellScriptMutexKey) + shellMutexKV.Unlock(shellScriptMutexKey) //if create doesn't return a new state then must call the read operation if newState == nil { @@ -165,7 +165,7 @@ func read(d *schema.ResourceData, meta interface{}, stack []string) error { } //obtain exclusive lock - //shellMutexKV.Lock(shellScriptMutexKey) + shellMutexKV.Lock(shellScriptMutexKey) state := NewState(environment, output) newState, err := runCommand(command, state, environment, workingDirectory, interpreter) @@ -173,7 +173,7 @@ func read(d *schema.ResourceData, meta interface{}, stack []string) error { return err } - //shellMutexKV.Unlock(shellScriptMutexKey) + shellMutexKV.Unlock(shellScriptMutexKey) if newState == nil { log.Printf("[DEBUG] State from read operation was nil. Marking resource for deletion.") d.SetId("") @@ -231,7 +231,7 @@ func update(d *schema.ResourceData, meta interface{}, stack []string) error { } //obtain exclusive lock - //shellMutexKV.Lock(shellScriptMutexKey) + shellMutexKV.Lock(shellScriptMutexKey) state := NewState(oldEnvironment, output) newState, err := runCommand(command, state, environment, workingDirectory, interpreter) @@ -239,7 +239,7 @@ func update(d *schema.ResourceData, meta interface{}, stack []string) error { return err } - //shellMutexKV.Unlock(shellScriptMutexKey) + shellMutexKV.Unlock(shellScriptMutexKey) //if update doesn't return a new state then must call the read operation if newState == nil { @@ -273,8 +273,8 @@ func delete(d *schema.ResourceData, meta interface{}, stack []string) error { } //obtain exclusive lock - //shellMutexKV.Lock(shellScriptMutexKey) - //defer shellMutexKV.Unlock(shellScriptMutexKey) + shellMutexKV.Lock(shellScriptMutexKey) + defer shellMutexKV.Unlock(shellScriptMutexKey) state := NewState(environment, output) _, err := runCommand(command, state, environment, workingDirectory, interpreter) From 2251e0e91f7e72a2b1f213adc35a0222bb246d38 Mon Sep 17 00:00:00 2001 From: Frederic Pinaud Date: Fri, 21 Feb 2020 14:15:29 +0100 Subject: [PATCH 03/10] Changed interpreter options --- shell/utility.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/shell/utility.go b/shell/utility.go index 2852003..b54aaeb 100644 --- a/shell/utility.go +++ b/shell/utility.go @@ -44,6 +44,7 @@ func readInterpreterVariables(ev map[string]interface{}) [2]string { variables[1] = ev[k].(string) } } + } return variables } @@ -73,15 +74,17 @@ func runCommand(command string, state *State, environment []string, workingDirec const maxBufSize = 8 * 1024 // Execute the command using a shell var shell, flag string - if runtime.GOOS == "windows" { - shell = "cmd" - flag = "/C" - } else if interpreter[0] != "" { + if interpreter[0] != "" { shell = interpreter[1] flag = interpreter[0] } else { - shell = "/bin/sh" - flag = "-c" + if runtime.GOOS == "windows" { + shell = "cmd" + flag = "/C" + } else { + shell = "/bin/sh" + flag = "-c" + } } // Setup the command From 4396e9bda6c9466ece767e6fd0a745b1884b8f37 Mon Sep 17 00:00:00 2001 From: Frederic Pinaud Date: Fri, 21 Feb 2020 14:55:15 +0100 Subject: [PATCH 04/10] Refactor runCommand --- shell/data_source_shell_script.go | 3 +-- shell/resource_shell_script.go | 12 ++++-------- shell/utility.go | 31 ++++++++++++------------------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/shell/data_source_shell_script.go b/shell/data_source_shell_script.go index c8de1fe..c148192 100644 --- a/shell/data_source_shell_script.go +++ b/shell/data_source_shell_script.go @@ -64,7 +64,6 @@ func dataSourceShellScriptRead(d *schema.ResourceData, meta interface{}) error { environment := readEnvironmentVariables(vars) inter := d.Get("interpreter").(map[string]interface{}) - interpreter := readInterpreterVariables(inter) workingDirectory := d.Get("working_directory").(string) output := make(map[string]string) @@ -74,7 +73,7 @@ func dataSourceShellScriptRead(d *schema.ResourceData, meta interface{}) error { defer shellMutexKV.Unlock(shellScriptMutexKey) state := NewState(environment, output) - newState, err := runCommand(command, state, environment, workingDirectory, interpreter) + newState, err := runCommand(command, state, environment, workingDirectory, inter) if err != nil { return err } diff --git a/shell/resource_shell_script.go b/shell/resource_shell_script.go index a03d4d3..21ec7e8 100644 --- a/shell/resource_shell_script.go +++ b/shell/resource_shell_script.go @@ -108,7 +108,6 @@ func create(d *schema.ResourceData, meta interface{}, stack []string) error { environment := readEnvironmentVariables(vars) inter := d.Get("interpreter").(map[string]interface{}) - interpreter := readInterpreterVariables(inter) workingDirectory := d.Get("working_directory").(string) d.MarkNewResource() @@ -117,7 +116,7 @@ func create(d *schema.ResourceData, meta interface{}, stack []string) error { output := make(map[string]string) state := NewState(environment, output) - newState, err := runCommand(command, state, environment, workingDirectory, interpreter) + newState, err := runCommand(command, state, environment, workingDirectory, inter) if err != nil { return err } @@ -155,7 +154,6 @@ func read(d *schema.ResourceData, meta interface{}, stack []string) error { environment := readEnvironmentVariables(vars) inter := d.Get("interpreter").(map[string]interface{}) - interpreter := readInterpreterVariables(inter) workingDirectory := d.Get("working_directory").(string) o := d.Get("output").(map[string]interface{}) @@ -168,7 +166,7 @@ func read(d *schema.ResourceData, meta interface{}, stack []string) error { shellMutexKV.Lock(shellScriptMutexKey) state := NewState(environment, output) - newState, err := runCommand(command, state, environment, workingDirectory, interpreter) + newState, err := runCommand(command, state, environment, workingDirectory, inter) if err != nil { return err } @@ -221,7 +219,6 @@ func update(d *schema.ResourceData, meta interface{}, stack []string) error { environment := readEnvironmentVariables(vars) inter := d.Get("interpreter").(map[string]interface{}) - interpreter := readInterpreterVariables(inter) workingDirectory := d.Get("working_directory").(string) o := d.Get("output").(map[string]interface{}) @@ -234,7 +231,7 @@ func update(d *schema.ResourceData, meta interface{}, stack []string) error { shellMutexKV.Lock(shellScriptMutexKey) state := NewState(oldEnvironment, output) - newState, err := runCommand(command, state, environment, workingDirectory, interpreter) + newState, err := runCommand(command, state, environment, workingDirectory, inter) if err != nil { return err } @@ -263,7 +260,6 @@ func delete(d *schema.ResourceData, meta interface{}, stack []string) error { environment := readEnvironmentVariables(vars) inter := d.Get("interpreter").(map[string]interface{}) - interpreter := readInterpreterVariables(inter) workingDirectory := d.Get("working_directory").(string) o := d.Get("output").(map[string]interface{}) @@ -277,7 +273,7 @@ func delete(d *schema.ResourceData, meta interface{}, stack []string) error { defer shellMutexKV.Unlock(shellScriptMutexKey) state := NewState(environment, output) - _, err := runCommand(command, state, environment, workingDirectory, interpreter) + _, err := runCommand(command, state, environment, workingDirectory, inter) if err != nil { return err } diff --git a/shell/utility.go b/shell/utility.go index b54aaeb..c7680b1 100644 --- a/shell/utility.go +++ b/shell/utility.go @@ -34,21 +34,6 @@ func readEnvironmentVariables(ev map[string]interface{}) []string { return variables } -func readInterpreterVariables(ev map[string]interface{}) [2]string { - var variables [2]string - if ev != nil { - for k := range ev { - if k == "flag" { - variables[0] = ev[k].(string) - } else if k == "shell" { - variables[1] = ev[k].(string) - } - } - - } - return variables -} - func printStackTrace(stack []string) { log.Printf("-------------------------") log.Printf("[DEBUG] Current stack:") @@ -70,13 +55,21 @@ func parseJSON(b []byte) (map[string]string, error) { return output, err } -func runCommand(command string, state *State, environment []string, workingDirectory string, interpreter [2]string) (*State, error) { +func runCommand(command string, state *State, environment []string, workingDirectory string, interpreter map[string]interface{}) (*State, error) { const maxBufSize = 8 * 1024 // Execute the command using a shell var shell, flag string - if interpreter[0] != "" { - shell = interpreter[1] - flag = interpreter[0] + if _, ok := interpreter["shell"]; ok { + if value, ok := interpreter["shell"]; ok { + shell = value.(string) + } else { + shell = "" + } + if value, ok := interpreter["flag"]; ok { + flag = value.(string) + } else { + flag = "" + } } else { if runtime.GOOS == "windows" { shell = "cmd" From edc0ab74442880048f49dffc99a5fb36f754d7d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric?= Date: Mon, 2 Mar 2020 11:26:07 +0100 Subject: [PATCH 05/10] Added example of use for interpreter and reset examples from master. --- examples/test.tf | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/examples/test.tf b/examples/test.tf index e9aad6d..278fd64 100644 --- a/examples/test.tf +++ b/examples/test.tf @@ -32,6 +32,7 @@ resource "shell_script" "test2" { } //test resource with no update +//test with interpreter example resource "shell_script" "test3" { lifecycle_commands { create = < Date: Wed, 1 Apr 2020 20:17:49 +0200 Subject: [PATCH 06/10] Changed interpreter logic --- shell/utility.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/shell/utility.go b/shell/utility.go index c7680b1..26d69e4 100644 --- a/shell/utility.go +++ b/shell/utility.go @@ -59,23 +59,21 @@ func runCommand(command string, state *State, environment []string, workingDirec const maxBufSize = 8 * 1024 // Execute the command using a shell var shell, flag string - if _, ok := interpreter["shell"]; ok { - if value, ok := interpreter["shell"]; ok { - shell = value.(string) - } else { - shell = "" - } - if value, ok := interpreter["flag"]; ok { - flag = value.(string) + if value, ok := interpreter["shell"]; ok && value != "" { + shell = value.(string) + } else { + if runtime.GOOS == "windows" { + shell = "cmd" } else { - flag = "" + shell = "/bin/sh" } + } + if value, ok := interpreter["flag"]; ok { + flag = value.(string) } else { if runtime.GOOS == "windows" { - shell = "cmd" flag = "/C" } else { - shell = "/bin/sh" flag = "-c" } } From c828db2a4cf0531c9ae3db6d0cbd7957bac5c063 Mon Sep 17 00:00:00 2001 From: Pierre Schefler Date: Wed, 1 Apr 2020 20:18:02 +0200 Subject: [PATCH 07/10] Added doc for interpreter --- README.md | 21 +++++++++++++++++++++ examples/test.tf | 8 ++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4277a84..06e4df6 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,27 @@ In the example above I am changing my working_directory, setting some environmen Stdout and stderr are available in the debug log files. +### Interpreter + +By default, the scripts will be executed by `cmd /C` on windows and `/bin/sh -c` on linux. You can overwrite this setting using `interpreter`: + + resource "shell_script" "test" { + lifecycle_commands { + create = file("create.sh") + read = file("read.sh") + update = file("update.sh") + delete = file("delete.sh") + } + + interpreter = { + shell = "/bin/bash" + flag = "-c" + } + } + +* `shell` (optional) contains the path to the shell binary. If empty or non present, it will be set to `cmd` on windows and `/bin/sh` otherwise. +* `flag` (optional) contains the options passed to the shell (this example will execute `/bin/bash -c create.sh`). Can be empty. + ## Python Support There is now an example for how to use the shell provider to invoke python files. Please check in the test/python-example folder for more information on this. Essentially it is an adapter around the shell resource that invokes methods on an interface that you implement. diff --git a/examples/test.tf b/examples/test.tf index 278fd64..9ed7c9f 100644 --- a/examples/test.tf +++ b/examples/test.tf @@ -36,7 +36,7 @@ resource "shell_script" "test2" { resource "shell_script" "test3" { lifecycle_commands { create = <> test3.json cat test3.json >&3 @@ -45,13 +45,9 @@ resource "shell_script" "test3" { delete = "rm -rf test3.json" } - environment = { - yolo = "yolo2" - } - interpreter = { - flag = "-c" shell = "/bin/bash" + flag = "-c" } } From 828f66ed3679d5d537c94b196fefa20084c8fd53 Mon Sep 17 00:00:00 2001 From: Pierre Schefler Date: Wed, 1 Apr 2020 20:24:28 +0200 Subject: [PATCH 08/10] Reset tests --- examples/test.tf | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/examples/test.tf b/examples/test.tf index 9ed7c9f..2f13482 100644 --- a/examples/test.tf +++ b/examples/test.tf @@ -5,7 +5,7 @@ provider "shell" {} data "shell_script" "test" { lifecycle_commands { read = <&3 + echo '{"commit_id": "b8f2b8b"}' EOF } } @@ -21,7 +21,7 @@ resource "shell_script" "test2" { out='{"commit_id": "b8f2b8b", "environment": "$yolo", "tags_at_commit": "sometags", "project": "someproject", "current_date": "09/10/2014", "version": "someversion"}' touch test2.json echo $out >> test2.json - cat test2.json >&3 + cat test2.json EOF delete = "rm -rf test2.json" } @@ -32,22 +32,21 @@ resource "shell_script" "test2" { } //test resource with no update -//test with interpreter example resource "shell_script" "test3" { lifecycle_commands { create = <> test3.json - cat test3.json >&3 + cat test3.json EOF - read = "cat test3.json >&3" + read = "cat test3.json" delete = "rm -rf test3.json" } - interpreter = { - shell = "/bin/bash" - flag = "-c" + environment = { + yolo = "yolo2" + } } @@ -58,14 +57,14 @@ resource "shell_script" "test4" { out='{"commit_id": "b8f2b8b", "environment": "$yolo", "tags_at_commit": "sometags", "project": "someproject", "current_date": "09/10/2014", "version": "someversion"}' touch test4.json echo $out >> test4.json - cat test4.json >&3 + cat test4.json EOF update = <> test4.json - cat test4.json >&3 + cat test4.json EOF delete = "rm -rf test4.json" } @@ -79,7 +78,7 @@ resource "shell_script" "test4" { resource "shell_script" "test5" { lifecycle_commands { create = file("${path.module}/scripts/create.sh") - read = file("${path.module}/scripts/read.sh") + read = file("${path.module}/scripts/read.sh") update = file("${path.module}/scripts/update.sh") delete = file("${path.module}/scripts/delete.sh") } @@ -100,7 +99,7 @@ output "commit_id2" { resource "shell_script" "test6" { lifecycle_commands { create = file("${path.module}/scripts/create.sh") - read = file("${path.module}/scripts/read.sh") + read = file("${path.module}/scripts/read.sh") delete = file("${path.module}/scripts/delete.sh") } @@ -115,3 +114,22 @@ resource "shell_script" "test6" { abc = 123 } } + +//test interpreter +resource "shell_script" "test7" { + lifecycle_commands { + create = <> test7.json + cat test7.json + EOF + read = "cat test7.json" + delete = "rm -rf test7.json" + } + + interpreter = { + shell = "/bin/bash" # will probably crash on windows! + flag = "-c" + } +} \ No newline at end of file From e2a1c66f53fac7b6a26dc8df521ba8d9742c409d Mon Sep 17 00:00:00 2001 From: Pierre Schefler Date: Wed, 1 Apr 2020 20:28:10 +0200 Subject: [PATCH 09/10] Fixed tests --- examples/test.tf | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/test.tf b/examples/test.tf index 2f13482..6eee6d7 100644 --- a/examples/test.tf +++ b/examples/test.tf @@ -5,7 +5,7 @@ provider "shell" {} data "shell_script" "test" { lifecycle_commands { read = <&3 EOF } } @@ -21,7 +21,7 @@ resource "shell_script" "test2" { out='{"commit_id": "b8f2b8b", "environment": "$yolo", "tags_at_commit": "sometags", "project": "someproject", "current_date": "09/10/2014", "version": "someversion"}' touch test2.json echo $out >> test2.json - cat test2.json + cat test2.json >&3 EOF delete = "rm -rf test2.json" } @@ -38,9 +38,9 @@ resource "shell_script" "test3" { out='{"commit_id": "b8f2b8b", "environment": "$yolo", "tags_at_commit": "sometags", "project": "someproject", "current_date": "09/10/2014", "version": "someversion"}' touch test3.json echo $out >> test3.json - cat test3.json + cat test3.json >&3 EOF - read = "cat test3.json" + read = "cat test3.json >&3" delete = "rm -rf test3.json" } @@ -57,14 +57,14 @@ resource "shell_script" "test4" { out='{"commit_id": "b8f2b8b", "environment": "$yolo", "tags_at_commit": "sometags", "project": "someproject", "current_date": "09/10/2014", "version": "someversion"}' touch test4.json echo $out >> test4.json - cat test4.json + cat test4.json >&3 EOF update = <> test4.json - cat test4.json + cat test4.json >&3 EOF delete = "rm -rf test4.json" } @@ -122,7 +122,7 @@ resource "shell_script" "test7" { out='{"commit_id": "b8f2b8b"}' touch test7.json echo $out >> test7.json - cat test7.json + cat test7.json >&3 EOF read = "cat test7.json" delete = "rm -rf test7.json" From dd11b2c0e00026ee2e1bd7d3f8ed402e926459ad Mon Sep 17 00:00:00 2001 From: Pierre Schefler Date: Wed, 1 Apr 2020 23:24:59 +0200 Subject: [PATCH 10/10] Removed unexpected Computed property to interpreter in resource schema --- shell/resource_shell_script.go | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/resource_shell_script.go b/shell/resource_shell_script.go index c86e3e2..065d7e7 100644 --- a/shell/resource_shell_script.go +++ b/shell/resource_shell_script.go @@ -75,7 +75,6 @@ func resourceShellScript() *schema.Resource { "interpreter": { Type: schema.TypeMap, Optional: true, - Computed: true, Elem: schema.TypeString, }, },