Skip to content

Commit

Permalink
Merge branch 'release/v0.1.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
rucciva committed Nov 6, 2020
2 parents 6826b37 + f73b517 commit 7d3990f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
10 changes: 5 additions & 5 deletions docs/resources/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ The following arguments are supported:

- `lifecycle_commands` - (Required) Block that contains commands to be remotely executed respectively in Create|Read|Update|Delete phase. For complex commands, use [the file function](https://www.terraform.io/docs/configuration/functions/file.html).
- `triggers` - (Optional, string map) Attribute that will trigger resource recreation on changes just like the one in [null_resource](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource#triggers). Default empty map.
- `environment` - (Optional, string map ) A list of linux environment that will be available in each `lifecycle_commands`. Default empty map.
- `sensitive_environment` - (Optional, string map) Just like `environment` except they don't show up in log files. Default empty map.
- `interpreter` - (Optional, string list) Interpreter for running each `lifecycle_commands`. Default empty list which is equal to `[ "sh" , "-c" ]`.
- `environment` - (Optional, string map) A list of linux environment that will be available in each `lifecycle_commands`. Default empty map.
- `sensitive_environment` - (Optional, string map) Just like `environment` except they don't show up in log files. In case of duplication, environment variables defined here will take precedence over the ones in `environment`. Default empty map.
- `interpreter` - (Optional, string list) Interpreter for running each `lifecycle_commands`. Default empty list.
- `working_directory` - (Optional, string) The working directory where each `lifecycle_commands` is executed. Default empty string.

### lifecycle_commands

The following arguments are supported:

- `create` - (Required, string) Commands that will be execued in Create phase.
- `read` - (Required, string) Commands that will be execued in Read phase and after execution of `create` or `update` commands. Terraform will record the output of these commands and trigger update/recreation when the output changes. If the result of running these commands is empty string, the resource is considered as destroyed.
- `update` - (Optional, string) Commands that will be execued in Update phase. Omiting this will disable Update phase and trigger resource recreation (Delete -> Create) each time terraform detect changes.
- `read` - (Required, string) Commands that will be execued in Read phase and after execution of `create` or `update` commands. Terraform will record the output of these commands inside `output` attributes and trigger update/recreation when it changes. If the result of running these commands is empty string, the resource is considered as destroyed.
- `update` - (Optional, string) Commands that will be execued in Update phase. Previous `output` are available from stdin. Omiting this will disable Update phase and trigger resource recreation (Delete -> Create) each time terraform detect changes.
- `delete` - (Required, string) Commands that will be execued in Delete phase.

## Attribute Reference
Expand Down
4 changes: 3 additions & 1 deletion linux/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func (l *linux) createFile(ctx context.Context, f *file) (err error) {
err = l.upload(ctx, f.path, strings.NewReader(f.content))
case true:
pathSafe := shellescape.Quote(f.path)
err = l.exec(ctx, &remote.Cmd{Command: fmt.Sprintf(`sh -c "touch %s && [ -f %s ]"`, pathSafe, pathSafe)})
err = l.exec(ctx, &remote.Cmd{
Command: fmt.Sprintf(`{ touch %s && [ -f %s ] ;}`, pathSafe, pathSafe),
})
}
if err != nil {
return
Expand Down
31 changes: 17 additions & 14 deletions linux/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,15 @@ type permission struct {

func (l *linux) setPermission(ctx context.Context, path string, p permission) (err error) {
pathSafe := shellescape.Quote(path)
cmd := fmt.Sprintf(`sh -c "chown %d:%d %s && chmod %s %s"`,
cmd := fmt.Sprintf(`{ chown %d:%d %s && chmod %s %s ;}`,
p.owner, p.group, pathSafe, p.mode, pathSafe)
return l.exec(ctx, &remote.Cmd{Command: cmd})
}

func (l *linux) getPermission(ctx context.Context, path string) (p permission, err error) {
stdout := new(bytes.Buffer)
cmd := remote.Cmd{
Command: fmt.Sprintf(`stat -c '%%u %%g %%a' %s`, shellescape.Quote(path)),
Stdout: stdout,
}
err = l.exec(ctx, &cmd)
cmd := shellescape.QuoteCommand([]string{"stat", "-c", "%u %g %a", path})
err = l.exec(ctx, &remote.Cmd{Command: cmd, Stdout: stdout})
var exitError *remote.ExitError
if errors.As(err, &exitError) {
err = errPathNotExist
Expand All @@ -148,7 +145,7 @@ func (l *linux) getPermission(ctx context.Context, path string) (p permission, e
}
parts := strings.Split(strings.TrimSpace(out), " ")
if len(parts) != 3 {
err = fmt.Errorf("malformed output of %q: %q", cmd.Command, out)
err = fmt.Errorf("malformed output of %q: %q", cmd, out)
return
}
owner, err := strconv.ParseUint(parts[0], 10, 16)
Expand Down Expand Up @@ -179,21 +176,21 @@ func (l *linux) reservePath(ctx context.Context, path string) (err error) {
}

func (l *linux) mkdirp(ctx context.Context, path string) (err error) {
cmd := fmt.Sprintf(`mkdir -p %s`, shellescape.Quote(path))
cmd := shellescape.QuoteCommand([]string{"mkdir", "-p", path})
return l.exec(ctx, &remote.Cmd{Command: cmd})
}

func (l *linux) cat(ctx context.Context, path string) (s string, err error) {
stdout := new(bytes.Buffer)
cmd := fmt.Sprintf("cat %s", shellescape.Quote(path))
cmd := shellescape.QuoteCommand([]string{"cat", path})
if err = l.exec(ctx, &remote.Cmd{Command: cmd, Stdout: stdout}); err != nil {
return
}
return stdout.String(), nil
}

func (l *linux) mv(ctx context.Context, old, new string) (err error) {
cmd := fmt.Sprintf(`mv %s %s`, shellescape.Quote(old), shellescape.Quote(new))
cmd := shellescape.QuoteCommand([]string{"mv", old, new})
return l.exec(ctx, &remote.Cmd{Command: cmd})
}

Expand All @@ -203,12 +200,18 @@ func (l *linux) remove(ctx context.Context, path, recyclePath string) (err error
}

var cmd string
path = shellescape.Quote(path)
if recyclePath != "" {
recycleFolder := shellescape.Quote(fmt.Sprintf("%s/%d", recyclePath, time.Now().Unix()))
cmd = fmt.Sprintf(`sh -c "[ ! -e %s ] || { mkdir -p %s && mv %s %s; }"`, path, recycleFolder, path, recycleFolder)
recycleFolder := fmt.Sprintf("%s/%d", recyclePath, time.Now().Unix())
cmd = fmt.Sprintf(`{ [ ! -e %s ] || { %s && %s ;} ;}`,
shellescape.Quote(path),
shellescape.QuoteCommand([]string{"mkdir", "-p", recycleFolder}),
shellescape.QuoteCommand([]string{"mv", path, recycleFolder}),
)
} else {
cmd = fmt.Sprintf(`sh -c "[ ! -e %s ] || rm -rf %s"`, path, path)
cmd = fmt.Sprintf(`{ [ ! -e %s ] || %s ;}`,
shellescape.Quote(path),
shellescape.QuoteCommand([]string{"rm", "-rf", path}),
)
}
return l.exec(ctx, &remote.Cmd{Command: cmd})
}
10 changes: 8 additions & 2 deletions linux/script-resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ func TestAccLinuxScriptBasic(t *testing.T) {
conf1 := tfConf{
Provider: testAccProvider,
Script: tfScript{
Interpreter: tfList{`"sh"`, `"-c"`},
Environment: tfmap{
"FILE": fmt.Sprintf(`"/tmp/linux/%s"`, acctest.RandString(16)),
"CONTENT": `"test"`,
"FILE": fmt.Sprintf(`"/tmp/linux/%s.yml"`, acctest.RandString(16)),
"CONTENT": heredoc.Doc(`
key:
- key1: "val1"
key2: 'val'
- key1: "val2"
`),
},
},
}
Expand Down
11 changes: 7 additions & 4 deletions linux/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (

type env map[string]string

func (e env) serialize(s string) string {
func (e env) serialize(sep string) string {
b := strings.Builder{}
first := true
for k, v := range e {
if first {
first = false
} else {
b.WriteString(s)
b.WriteString(sep)
}

b.WriteString(k)
Expand Down Expand Up @@ -57,8 +57,11 @@ func (sc *script) exec(ctx context.Context) (res string, err error) {
}
defer func() { _ = sc.l.remove(ctx, path, "") }()

cmd := sc.env.inline() + " " + shellescape.QuoteCommand(sc.interpreter) + " " + path
cmd = fmt.Sprintf(`sh -c 'cd %s && %s'`, shellescape.Quote(sc.workdir), cmd)
cmd := fmt.Sprintf(`{ %s && %s && %s %s ;}`,
shellescape.QuoteCommand([]string{"mkdir", "-p", sc.workdir}),
shellescape.QuoteCommand([]string{"cd", sc.workdir}),
sc.env.inline(), shellescape.QuoteCommand(append(sc.interpreter, path)),
)
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
err = sc.l.exec(ctx,
&remote.Cmd{
Expand Down

0 comments on commit 7d3990f

Please sign in to comment.