Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
No related merge requests found
Pipeline #2487 failed
...@@ -21,7 +21,7 @@ pre-commit-try-repo: ...@@ -21,7 +21,7 @@ pre-commit-try-repo:
/etc/apt/sources.list.d/hashicorp.list /etc/apt/sources.list.d/hashicorp.list
# yamllint enable rule:line-length # yamllint enable rule:line-length
- apt-get update - apt-get update
- apt-get install -y terraform - apt-get install -y terraform packer
script: script:
- pre-commit try-repo --all-files ./ - pre-commit try-repo --all-files ./
cache: cache:
......
...@@ -37,6 +37,27 @@ ...@@ -37,6 +37,27 @@
types: [terraform] types: [terraform]
entry: terraform-validate 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 - id: poetry-check
name: poetry check name: poetry check
description: Validate pyproject.toml files using Poetry description: Validate pyproject.toml files using Poetry
......
...@@ -22,6 +22,9 @@ A collection of [pre-commit](https://pre-commit.com/) hooks. ...@@ -22,6 +22,9 @@ A collection of [pre-commit](https://pre-commit.com/) hooks.
- id: docker-compose - id: docker-compose
- id: terraform-fmt - id: terraform-fmt
- id: terraform-validate - id: terraform-validate
- id: packer-fmt
- id: packer-fix
- id: packer-validate
- id: poetry-check - id: poetry-check
- id: branch-merge-conflict - id: branch-merge-conflict
``` ```
...@@ -50,6 +53,18 @@ Requires an installed `terraform`. ...@@ -50,6 +53,18 @@ Requires an installed `terraform`.
Validate Terraform modules using `terraform validate`. Validate Terraform modules using `terraform validate`.
Requires an installed `terraform`. 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` ### `poetry-check`
Validate `pyproject.toml` files using Poetry. Validate `pyproject.toml` files using Poetry.
......
"""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())
"""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())
"""Checks that the Packer template is valid."""
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", "validate", x]),
args.file,
)
if __name__ == "__main__":
sys.exit(main())
...@@ -19,6 +19,9 @@ setup( ...@@ -19,6 +19,9 @@ setup(
"docker-compose-validate=hooks.docker_compose_validate:main", "docker-compose-validate=hooks.docker_compose_validate:main",
"terraform-validate=hooks.terraform_validate:main", "terraform-validate=hooks.terraform_validate:main",
"terraform-fmt=hooks.terraform_fmt:main", "terraform-fmt=hooks.terraform_fmt:main",
"packer-fix=hooks.packer_fix:main",
"packer-fmt=hooks.packer_fmt:main",
"packer-validate=hooks.packer_validate:main",
"poetry-check=hooks.poetry_check:main", "poetry-check=hooks.poetry_check:main",
] ]
}, },
......
{
"variables": {
"access_key": "{{env `AWS_ACCESS_KEY_ID`}}",
"secret_key": "{{env `AWS_SECRET_ACCESS_KEY`}}"
},
"builders": [{
"type": "amazon-ebs",
"access_key": "{{user `access_key`}}",
"secret_key": "{{user `access_key`}}",
"region": "us-east-1",
"source_ami": "ami-de0d9eb7",
"instance_type": "t1.micro",
"ssh_username": "ubuntu",
"ami_name": "packer-example {{timestamp}}"
}]
}
build {
name = "alpine"
description = <<EOF
This build creates alpine images for versions :
* v3.12
For the following builders :
* virtualbox-iso
EOF
// the common fields of the source blocks are defined in the
// source.builder-type.pkr.hcl files, here we only set the fields specific to
// the different versions of ubuntu.
source "source.virtualbox-iso.base-alpine-amd64" {
name = "3.12"
iso_url = local.iso_url_alpine_312
iso_checksum = "file:${local.iso_checksum_url_alpine_312}"
output_directory = "virtualbox_iso_alpine_312_amd64"
boot_command = local.alpine_312_floppy_boot_command
boot_wait = "10s"
}
source "source.vsphere-iso.base-alpine-amd64" {
name = "3.12"
vm_name = "alpine-3.12"
iso_url = local.iso_url_alpine_312
iso_checksum = "file:${local.iso_checksum_url_alpine_312}"
boot_command = local.alpine_312_floppy_boot_command_vsphere
boot_wait = "10s"
}
source "source.vmware-iso.esxi-base-alpine-amd64" {
name = "3.12-from-esxi"
iso_url = local.iso_url_alpine_312
iso_checksum = "file:${local.iso_checksum_url_alpine_312}"
boot_command = local.alpine_312_floppy_boot_command_vsphere
}
provisioner "shell" {
inline = ["echo hi"]
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment