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

Unable to get any output :-( #29

Closed bentterp closed 4 years ago

bentterp commented 4 years ago

I have problems getting anything working here, including the most basic of the examples in the docs:

2020-03-11T08:12:18.171+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: 2020/03/11 08:12:18 [DEBUG] Locking "shellScriptMutexKey" 2020-03-11T08:12:18.171+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: 2020/03/11 08:12:18 [DEBUG] Locked "shellScriptMutexKey" 2020-03-11T08:12:18.171+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: 2020/03/11 08:12:18 [DEBUG] shell script command old state: "&{[] map[]}" 2020-03-11T08:12:18.171+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: 2020/03/11 08:12:18 [DEBUG] shell script going to execute: /bin/sh -c "cd . && echo "{\"user\": \"$(whoami)\"}" >&3 2020-03-11T08:12:18.171+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: " 2020-03-11T08:12:18.214+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: 2020/03/11 08:12:18 [DEBUG] Command execution completed. Reading from output pipe: >&3 2020-03-11T08:12:18.214+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: 2020/03/11 08:12:18 [DEBUG] shell script command stdout: "" 2020-03-11T08:12:18.214+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: 2020/03/11 08:12:18 [DEBUG] shell script command stderr: "" 2020-03-11T08:12:18.214+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: 2020/03/11 08:12:18 [DEBUG] shell script command output: "" 2020-03-11T08:12:18.214+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: 2020/03/11 08:12:18 [DEBUG] Unable to unmarshall data to map[string]string: 'unexpected end of JSON input' 2020-03-11T08:12:18.214+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: 2020/03/11 08:12:18 [DEBUG] State from read operation was nil. Marking resource for deletion. 2020-03-11T08:12:18.214+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: 2020/03/11 08:12:18 [DEBUG] Unlocking "shellScriptMutexKey" 2020-03-11T08:12:18.214+0100 [DEBUG] plugin.terraform-provider-shell_v0.1.4: 2020/03/11 08:12:18 [DEBUG] Unlocked "shellScriptMutexKey"

I simply get no output whatsoever:

$ terraform state show data.shell_script.user

data.shell_script.user:

data "shell_script" "user" { } $

scottwinkler commented 4 years ago

Thats very curious, I'm not sure I can say why that is happening. Can you tell me about your environment? What operating system are you using? Do you have the whoami command available in your shell? And can you share the configuration code you are using?

I have a new build of the provider that I will publish tomorrow. It streams stdout and stderr back to terraform. This should allow some debugging to see what is happening.

bentterp commented 4 years ago

[fbte61@SEA37LT16653 ~]$ which whoami /usr/bin/whoami [fbte61@SEA37LT16653 ~]$

Running CentOSWSL (https://github.com/yuk7/CentWSL)

Conf:

data "shell_script" "user" {
    lifecycle_commands {
        read = <<-EOF
          echo "{\"user\": \"$(whoami)\"}" >&3
        EOF
    }
}

output "user" {
    value = data.shell_script.user

}
scottwinkler commented 4 years ago

I suspect the issue has to do with the fact that you're actually running linux on top of windows. I know there is a problem with reading from device 3 on windows. I wonder if that might be the reason you dont see any output.

I will publish a new build tomorrow that should hopefully fix your problem. It eliminates the need for device 3 entirely. If you still have a problem then i will need your help to perform further debugging.

bentterp commented 4 years ago

that was also my first thought, but file descriptors seem to work as intended:

[fbte61@SEA37LT16653 ~]$ exec 3</tmp/provs.json
[fbte61@SEA37LT16653 ~]$ read -u 3 line
[fbte61@SEA37LT16653 ~]$ echo $line
[
[fbte61@SEA37LT16653 ~]$ read -u 3 line
[fbte61@SEA37LT16653 ~]$ echo $line
{
[fbte61@SEA37LT16653 ~]$ exec 3>&-
[fbte61@SEA37LT16653 ~]$ read -u 3 line
-bash: read: 3: invalid file descriptor: Bad file descriptor
[fbte61@SEA37LT16653 ~]$

(found this test here, example 9 https://catonmat.net/bash-one-liners-explained-part-three)

scottwinkler commented 4 years ago

Right, but I'm wondering if it works in golang for the exec command which attaches a file for storing the results of device 3. I can write up a small test tomorrow for you to run if the new version doesnt work

scottwinkler commented 4 years ago

okay i released the latest version. can you please try again? note that this latest version does not use device 3 any longer.

bentterp commented 4 years ago

I've tested a bit and now it works as it should, as long as the JSON output isn't nested in any way. (which is fine as that limitation is clearly stated!)

I can do inline bash as the example above

I can run a command like this:

data "shell_script" "provider_registration" {
  lifecycle_commands {
    read = <<-EOF
      az provider list --subscription ${data.azurerm_subscription.current.subscription_id} | jq '{state : .[]|select(.namespace == "Microsoft.RedHatOpenShift").registrationState }'
    EOF
  }
}

Or I can have the same command in an external scriptfile and access it like this:

data "shell_script" "script" {
    lifecycle_commands {
        read = <<-EOF
          ./scripts/check_provider.sh
        EOF
    }
}

Very happy about this, we're going to use it a lot I think!