pre-commit-opentofu icon indicating copy to clipboard operation
pre-commit-opentofu copied to clipboard

Command not found Error in Docker Pre-Commit Hook for OpenTofu

Open dev-ago opened this issue 1 year ago • 7 comments

Describe the bug

I am encountering an issue while using the pre-commit hook for OpenTofu in Docker. The hook consistently fails with the following error message.

The same pre-commit config works fine on Mac OS X directly

OpenTofu fmt.............................................................Failed
- hook id: tofu_fmt
- exit code: 127

/root/.cache/pre-commit/repo4ovfh8vo/hooks/tofu_fmt.sh: line 47: tofu: command not found

Repository: pre-commit-opentofu

Docker Image Tags Tested: latest, nightly Docker Image: tofuutils/pre-commit-opentofu

How can we reproduce it?

1.	Pull the pre-commit-opentofu Docker image.
2.	Run the pre-commit hook.

Observed Behavior:

The hook fails with the tofu: command not found error.

Expected Behavior:

The hook should run the tofu fmt command successfully.

Environment information

Docker: tofuutils/pre-commit-opentofu:nightly Docker: tofuutils/pre-commit-opentofu:latest

  • Docker image tag/git commit:

  • latest & nightly

fb6ffcd8529e:/app# pre-commit run -a
[INFO] Initializing environment for https://github.com/tofuutils/pre-commit-opentofu.
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
The authenticity of host 'gitlab.com (172.65.251.78)' can't be established.
ED25519 key fingerprint is SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Passed
OpenTofu docs............................................................Passed
OpenTofu fmt.............................................................Failed
- hook id: tofu_fmt
- exit code: 127

/root/.cache/pre-commit/repo20_pojta/hooks/tofu_fmt.sh: line 47: tofu: command not found

OpenTofu validate with tflint............................................Passed
check for added large files..............................................Passed
detect private key.......................................................Passed
trim trailing whitespace.................................................Passed

  • .pre-commit-config.yaml:
exclude: '^examples/'

repos:
- repo: "https://github.com/tofuutils/pre-commit-opentofu"
  rev: "v1.0.2" # Get the latest from: https://github.com/tofuutils/pre-commit-opentofu/releases
  hooks:
    - id: tofu_trivy
    - id: tofu_docs
      args:
        - --hook-config=--path-to-file=./README.md
        - --hook-config=--add-to-existing-file=true
    - id: tofu_fmt
    - id: tofu_tflint
      args:
      - --args=--enable-rule=terraform_documented_variables
      - --args=--enable-rule=terraform_comment_syntax
      - --args=--enable-rule=terraform_deprecated_index
      - --args=--enable-rule=terraform_deprecated_interpolation
      - --args=--enable-rule=terraform_documented_outputs
      - --args=--enable-rule=terraform_module_pinned_source
      - --args=--enable-rule=terraform_module_version
      - --args=--enable-rule=terraform_naming_convention
      - --args=--enable-rule=terraform_required_providers
      - --args=--enable-rule=terraform_required_version
      - --args=--enable-rule=terraform_standard_module_structure
      - --args=--enable-rule=terraform_typed_variables
      - --args=--enable-rule=terraform_unused_declarations
      - --args=--enable-rule=terraform_unused_required_providers
      - --args=--enable-rule=terraform_workspace_remote
      - --args=--call-module-type=all
- repo: "https://github.com/pre-commit/pre-commit-hooks"
  rev: "v4.4.0"
  hooks:
    - id: "check-added-large-files"
    - id: "detect-private-key"
    - id: "trailing-whitespace"

dev-ago avatar Jul 17 '24 10:07 dev-ago

i found a quick fix for this issue, installing opentofu in the container:

echo '@community https://dl-cdn.alpinelinux.org/alpine/edge/community' >> /etc/apk/repositories
apk add opentofu@community

dev-ago avatar Jul 17 '24 10:07 dev-ago

we'll try to fix it permanently

kvendingoldo avatar Jul 17 '24 10:07 kvendingoldo

@kvendingoldo great i think the problem ist this code here:

# https://github.com/tofuutils/pre-commit-opentofu/blob/main/Dockerfile#L24C1-L28C26
RUN curl -LO https://github.com/opentofu/opentofu/releases/download/v${TOFU_VERSION}/tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip \
 && curl -LO https://github.com/opentofu/opentofu/releases/download/v${TOFU_VERSION}/tofu_${TOFU_VERSION}_SHA256SUMS \
 && [ $(sha256sum "tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip" | cut -f 1 -d ' ') = "$(grep "tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip" tofu_*_SHA256SUMS | cut -f 1 -d ' ')" ] \
 && unzip tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip \
 && mv tofu /usr/bin/tofu
Screenshot 2024-07-17 at 14 16 46

The OpenTofu binary seems not to have been extracted and what was moved to /usr/bin were the ZIPs.

I plan to create a PR by the end of the week, if you make it before then, perfect.

dev-ago avatar Jul 17 '24 12:07 dev-ago

@dev-ago sounds great! Will wait for the pr!

kvendingoldo avatar Jul 17 '24 14:07 kvendingoldo

@kvendingoldo this is the fix for the dockerfile:

RUN curl -LO https://github.com/opentofu/opentofu/releases/download/v${TOFU_VERSION}/tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip \
 && curl -LO https://github.com/opentofu/opentofu/releases/download/v${TOFU_VERSION}/tofu_${TOFU_VERSION}_SHA256SUMS \
 && [ $(sha256sum "tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip" | cut -f 1 -d ' ') = "$(grep "tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip" tofu_*_SHA256SUMS | cut -f 1 -d ' ')" ] \
 && unzip tofu_${TOFU_VERSION}_${TARGETOS}_${TARGETARCH}.zip \
 && mv tofu /usr/bin/

dev-ago avatar Jul 22 '24 07:07 dev-ago

I cant create a PR.

dev-ago avatar Jul 22 '24 07:07 dev-ago

Is it the issue with permissions or ..? I will do this PR, thank you for your code :)

kvendingoldo avatar Jul 22 '24 10:07 kvendingoldo

Duplicate of #11 (or at least the same root cause with the same resolution). Suggesting this issue be closed as resolved.

jimbocoder avatar Mar 29 '25 16:03 jimbocoder