scottwinkler / terraform-provider-shell

Terraform provider for executing shell commands and saving output to state file
Mozilla Public License 2.0
279 stars 61 forks source link

Show stdout and stderr in Terraform output #27

Closed scottwinkler closed 4 years ago

scottwinkler commented 4 years ago

This adds pipes for both stdout and stderr so that it will print messages while the user is running Terraform. Fixes issue #17 . Does not show up by default, still requires setting TF_LOG=DEBUG because Terraform providers don't seem to respect [INFO] tags. For the following data source:

data "shell_script" "weather" {
  lifecycle_commands {
    read = <<-EOF
        echo "starting long process"
        sleep 5
        echo "some other output"
        sleep 10
        echo "some stderr" >&2
        echo "{\"SanFrancisco\": \"$(curl wttr.in/SanFrancisco?format="%l:+%c+%t")\"}" >&3
    EOF
  }
}

The following will show up in the output:

data.shell_script.weather: Refreshing state...
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04 [DEBUG] Reading shell script data resource...
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04 [DEBUG] Locking "shellScriptMutexKey"
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04 [DEBUG] Locked "shellScriptMutexKey"
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04 [DEBUG] shell script command old state: "&{[] map[]}"
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04 [DEBUG] shell script going to execute: /bin/sh -c
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04    echo "starting long process"
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04    sleep 5
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04    echo "some other output"
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04    sleep 10
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04    echo "some stderr" >&2
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04    echo "{\"SanFrancisco\": \"$(curl wttr.in/SanFrancisco?format="%l:+%c+%t")\"}" >&3
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04 -------------------------
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04 [DEBUG] Starting execution...
2020-03-02T16:28:04.661-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04 -------------------------
2020-03-02T16:28:04.667-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:04   starting long process
2020-03-02T16:28:09.674-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:09   some other output
2020-03-02T16:28:19.680-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:19   some stderr
2020-03-02T16:28:19.694-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:19     % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
2020-03-02T16:28:19.694-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:19                                    Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     002 16:28:19
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     002 16:28:20
100    28  100    28    0     0     21      0  0:00:01  0:00:01 --:--:--    2102 16:28:21
2020-03-02T16:28:21.004-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:21 -------------------------
2020-03-02T16:28:21.004-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:21 [DEBUG] Command execution completed. Reading from output pipe: >&3
2020-03-02T16:28:21.004-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:21 -------------------------
2020-03-02T16:28:21.004-0800 [DEBUG] plugin.terraform-provider-shell: 2020/03/02 16:28:21 [DEBUG] shell script command output:
2020-03-02T16:28:21.004-0800 [DEBUG] plugin.terraform-provider-shell: {"SanFrancisco": "SanFrancisco: ⛅️ +77°F"}