Skip to content

Commit

Permalink
adding docker-ubuntu-base-python-install.hcl
Browse files Browse the repository at this point in the history
  • Loading branch information
arnol377 committed Sep 10, 2024
1 parent 887768e commit 750c21b
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions docker-ubuntu-base-python-install.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
packer {
required_plugins {
# Docker plugin for Packer
docker = {
source = "github.com/hashicorp/docker"
version = "~> 1"
}
# Ansible plugin for Packer
ansible = {
version = "v1.1.1"
source = "github.com/hashicorp/ansible"
}
# Amazon plugin for Packer
amazon = {
version = ">= 1.2.8"
source = "github.com/hashicorp/amazon"
}
}
}

variable ansible_dir {
type = string
default = "./"
}

variable project_name {
type = string
default = "my-project"
}

# ecr_repository_name
data amazon-parameterstore dest_image {
name = "/image-pipeline/${var.project_name}/dest_image"
}

data amazon-parameterstore dest_tag {
name = "/image-pipeline/${var.project_name}/dest_tag"
}

data amazon-parameterstore dest_docker_repo {
name = "/image-pipeline/${var.project_name}/dest_docker_repo"
}


data amazon-parameterstore source_image {
name = "/image-pipeline/${var.project_name}/source_image"
}

data amazon-parameterstore source_tag {
name = "/image-pipeline/${var.project_name}/source_tag"
}

data amazon-parameterstore source_docker_repo {
name = "/image-pipeline/${var.project_name}/source_docker_repo"
}

data amazon-parameterstore aws_region {
name = "/image-pipeline/${var.project_name}/region"
}

data amazon-parameterstore aws_account_id {
name = "/image-pipeline/${var.project_name}/aws_account_id"
}

data amazon-parameterstore playbook {
name = "/image-pipeline/${var.project_name}/playbook"
}



locals {
aws_account_id = data.amazon-parameterstore.aws_account_id.value
aws_region = data.amazon-parameterstore.aws_region.value
source_image = data.amazon-parameterstore.source_image.value
source_tag = data.amazon-parameterstore.source_tag.value
source_docker_repo = data.amazon-parameterstore.source_docker_repo.value
dest_image = data.amazon-parameterstore.dest_image.value
dest_tag = data.amazon-parameterstore.dest_tag.value
dest_docker_repo = data.amazon-parameterstore.dest_docker_repo.value
}

source "docker" "docker" {
image = "${local.aws_account_id}.dkr.ecr.${local.aws_region}.amazonaws.com/${local.source_docker_repo}/${local.source_image}:${local.source_tag}"
commit = true
ecr_login = true
login_server = "${local.aws_account_id}.dkr.ecr.${local.aws_region}.amazonaws.com"
changes = [
"CMD [\"sleep\", \"30\"]"
]
}

build {
sources = ["source.docker.docker"]

# Provisioner to run the Ansible playbook
provisioner "shell" {
inline = [
"apt-get update",
"apt-get install -y sudo python3"
]
}
provisioner "ansible" {
command = "/root/.local/bin/ansible-playbook"
playbook_file = "${var.ansible_dir}/${data.amazon-parameterstore.playbook.value}"
roles_path = "${var.ansible_dir}/roles"
ansible_env_vars = ["ANSIBLE_STDOUT_CALLBACK=yaml", "ANSIBLE_NOCOLOR=True"]
}

post-processors {
post-processor "docker-tag" {
repository = "${local.aws_account_id}.dkr.ecr.${local.aws_region}.amazonaws.com/${local.dest_docker_repo}/${local.dest_image}"
tag = [local.dest_tag]
}

post-processor "docker-push" {
ecr_login = true
login_server = "${local.aws_account_id}.dkr.ecr.${local.aws_region}.amazonaws.com"
}
}

# Post-processor to write the build name to a file and print the AMI ID
post-processor "manifest" {
output = "ami_id.json"
strip_path = true
}

post-processor "shell-local" {
inline = [
"cat ami_id.json",
"cat ami_id.json | jq -r '.builds[0].artifact_id' > ami_id.txt",
"aws ssm put-parameter --name '/image-pipeline/${var.project_name}/image_id' --type 'String' --value ${local.aws_account_id}.dkr.ecr.${local.aws_region}.amazonaws.com/${local.dest_docker_repo}/${local.dest_image}:${local.dest_tag} --overwrite"
]
}
}

0 comments on commit 750c21b

Please sign in to comment.