Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dev-ago opened this issue Jul 17, 2024 · 7 comments
Open

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

dev-ago opened this issue Jul 17, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@dev-ago
Copy link

dev-ago commented Jul 17, 2024

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 dev-ago added the bug Something isn't working label Jul 17, 2024
@dev-ago
Copy link
Author

dev-ago commented Jul 17, 2024

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

@kvendingoldo
Copy link
Collaborator

we'll try to fix it permanently

@dev-ago
Copy link
Author

dev-ago commented Jul 17, 2024

@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.

@kvendingoldo
Copy link
Collaborator

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

@dev-ago
Copy link
Author

dev-ago commented Jul 22, 2024

@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
Copy link
Author

dev-ago commented Jul 22, 2024

I cant create a PR.

@kvendingoldo
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants