Commit 41f44255 authored by nimrod's avatar nimrod
Browse files

Packer hooks.

New hooks for packer fmt, fix and validate. Includes test files.
parent e750b791
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ pre-commit-try-repo:
      /etc/apt/sources.list.d/hashicorp.list
    # yamllint enable rule:line-length
    - apt-get update
    - apt-get install -y terraform
    - apt-get install -y terraform packer
  script:
    - pre-commit try-repo --all-files ./
  cache:
+21 −0
Original line number Diff line number Diff line
@@ -37,6 +37,27 @@
  types: [terraform]
  entry: terraform-validate

- id: packer-fix
  name: Fix Packer templates
  description: Fix known backwards incompatibilities in Packer templates
  language: python
  entry: packer-fix
  types: [json, hcl]

- id: packer-fmt
  name: Format Packer templates
  description: Rewrites all Packer configuration files to a canonical format
  language: python
  entry: packer-fmt
  types: [hcl]

- id: packer-validate
  name: Validate Packer templates
  description: Checks that the Packer template is valid
  language: python
  entry: packer-validate
  types: [json, hcl]

- id: poetry-check
  name: poetry check
  description: Validate pyproject.toml files using Poetry
+15 −0
Original line number Diff line number Diff line
@@ -22,6 +22,9 @@ A collection of [pre-commit](https://pre-commit.com/) hooks.
    - id: docker-compose
    - id: terraform-fmt
    - id: terraform-validate
    - id: packer-fmt
    - id: packer-fix
    - id: packer-validate
    - id: poetry-check
    - id: branch-merge-conflict
```
@@ -50,6 +53,18 @@ Requires an installed `terraform`.
Validate Terraform modules using `terraform validate`.
Requires an installed `terraform`.

### `packer-fix`

Fix known backwards incompatibilities in Packer templates.

### `packer-fmt`

Rewrites all Packer configuration files to a canonical format (just HCL files).

### `packer-validate`

Checks that the Packer template is valid.

### `poetry-check`

Validate `pyproject.toml` files using Poetry.

hooks/packer_fix.py

0 → 100644
+38 −0
Original line number Diff line number Diff line
"""Fix known backwards incompatibilities in Packer templates."""

import argparse
import locale
import pathlib
import sys
import hooks.utils


def packer_fix(file):
    """Runs packer fix.

    If the invocation succeeds, overwrite the file with the fixed output from
    packer. If it fails, fail.
    """
    proc = hooks.utils.run(["packer", "fix", file])
    if proc.returncode > 0:
        return 1
    # pylint: disable=invalid-name
    with open(file, "w", encoding=locale.getpreferredencoding()) as fh:
        fh.write(proc.stdout)
    return 0


def main():
    """Main entrypoint."""
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("file", nargs="+", type=pathlib.Path)
    args = parser.parse_args()
    hooks.utils.check_executable("packer")
    return hooks.utils.bulk_check(
        packer_fix,
        args.file,
    )


if __name__ == "__main__":
    sys.exit(main())

hooks/packer_fmt.py

0 → 100644
+22 −0
Original line number Diff line number Diff line
"""Rewrites all Packer configuration files to a canonical format."""

import argparse
import pathlib
import sys
import hooks.utils


def main():
    """Main entrypoint."""
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("file", nargs="+", type=pathlib.Path)
    args = parser.parse_args()
    hooks.utils.check_executable("packer")
    return hooks.utils.bulk_check(
        lambda x: hooks.utils.check_file(["packer", "fmt", "-diff", x]),
        args.file,
    )


if __name__ == "__main__":
    sys.exit(main())
Loading