{
  "variables": {
    "version": "focal-20.04",
    "tag": "{{env `TAG`}}"
  },
  "builders": [
    {
      "type": "amazon-ebs",
      "region": "us-east-1",
      "ami_name": "public-lux-ubuntu-{{user `version` }}-{{timestamp}}",
      "source_ami_filter": {
        "filters": {
          "virtualization-type": "hvm",
          "name": "ubuntu/images/*ubuntu-{{ user `version` }}-*-server-*",
          "root-device-type": "ebs",
          "architecture": "x86_64"
        },
        "most_recent": true,
        "owners": [
          "099720109477"
        ]
      },
      "ssh_username": "ubuntu",
      "instance_type": "t2.micro",
      "ami_groups": "all",
      "tags": {
        "Name": "public-lux-ubuntu-{{user `version`}}-{{ isotime | clean_resource_name }}",
        "Release": "{{ user `version` }}",
        "Base_AMI_Name": "{{ .SourceAMIName }}"
      }
    }
  ],
  "provisioners": [
    {
      "type": "shell",
        "inline": [
        "while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
        "wait_apt=$(ps aux | grep apt | wc -l)",
        "while [ \"$wait_apt\" -gt \"1\" ]; do echo \"waiting for apt to be ready....\"; wait_apt=$(ps aux | grep apt | wc -l); sleep 5; done",
        "sudo apt-get -y update",
        "sudo apt-get install -y python3-boto3 golang"
        ]
    },
    {
      "type": "ansible",
      "playbook_file": ".github/packer/create_public_ami.yml",
      "roles_path": ".github/packer/roles/",
      "extra_arguments": ["-e", "component=public-ami build=packer os_release=focal tag={{user `tag`}}"]
    },
    {
      "type": "shell",
      "script": ".github/packer/clean-public-ami.sh",
      "execute_command": "sudo bash -x {{.Path}}"
    }
  ]
}

