packer {
  required_plugins {
    amazon = {
      version = ">= 1.3.10"
      source  = "github.com/hashicorp/amazon"
    }
  }
}

# source "amazon-ebs" "debian" {
#   ami_name        = "debian-12-amd64-base-ami-${formatdate("YYYY-MM-DD_hh-mm-ss", timestamp())}"
#   ami_description = "Debian base-ami. Installs packages, configures aide and SELinux. Build: https://github.com/theorchard/collab/blob/master/jtorp/debian-base-ami/debian-ami.pkr.hcl"
#   ena_support     = true
#   ami_users = [
# "437795906767",
# "103233932089",
# "290365479392",
# "619719722105",
# "926734670777",
# "734458723522",
# "989790945997",
# "375914681009",
# "086679231553",
# "031099521156",
# "079637511089",
# "285943604611",
# "737325105821",
# "725764004555",
# "801577982711",
# "745243859288",
# "807702934854",
# "475275892927",
# "323555055331",
# "483193324480",
# "890208661333",
# "313114233826",
# "582412345909",
# "885200931856",
# "703740486246",
# "322697034778",
# "814622134907",
# "009424872958",
# "311141540202",
# "710271914708",
# "682033496752",
# "591204808501",
# "699475937692",
# "635057242373",
# "588827373449",
# "918817604416",
# "613871678587",
# "554998489284",
# "799833541840",
# "765134955759",
# "811473064144",
# "997286397029",
# "969009837416",
# "058029036333",
# "023180329437",
# "793676410512",
# "887829410671",
# "156555176793",
# "280383026186",
# "718729873097",
# "729038418454",
# "107577002119",
# "635220336377",
# "230315258045",
# "104858398257",
# "638682502729",
# "715641587426",
# "844930103112",
# "130080192946",
# "361463692799",
# "981599956623",
# "504436705349",
#   ]
#   encrypt_boot    = true
#   imds_support    = "v2.0"
#   instance_type   = "t3a.large"
#   kms_key_id      = "arn:aws:kms:us-east-1:086679231553:key/1d9b335c-ceed-471f-b705-617d871014b2"
#   region          = "us-east-1"
#   skip_create_ami = false # useful for testing build success
#   source_ami_filter {
#     filters = {
#       name                = "debian-12-amd64-*"
#       root-device-type    = "ebs"
#       virtualization-type = "hvm"
#     }
#     most_recent = true
#     owners      = ["amazon"]
#   }
#   ssh_username = "admin"
#   subnet_filter {
#     filters = {
#       "tag:tier" : "private"
#     }
#     most_free = true
#   }
#   tags = {
#     Name       = "debian-12-amd64-base-ami-${formatdate("YYYY-MM-DD_hh-mm-ss", timestamp())}"
#     packerized = true
#     source     = "https://github.com/theorchard/collab/blob/master/jtorp/debian-base-ami/debian-ami.pkr.hcl"
#     os_name    = "debian"
#     os_version = "12"
#   }
# }

# build {
#   name    = "debian-base-ami"
#   sources = ["source.amazon-ebs.debian"]

#   provisioner "shell" {
#     expect_disconnect = true
#     environment_vars = [
#       "DEBIAN_FRONTEND=noninteractive"
#     ]
#     inline = [
#       "sudo apt-get update -qq",
#       "echo 'debconf debconf/frontend select Noninteractive' | sudo debconf-set-selections",
#       "echo 'Dpkg::Options {\n\"--force-confdef\";\n\"--force-confnew\";\n}' | sudo tee /etc/apt/apt.conf.d/70confnew",
#       "sudo apt-get upgrade -qq",
#       "sudo localectl set-locale en_US.UTF-8",
#       "sudo apt-get install -qq dmidecode htop lsof mlocate nethogs nmap psmisc",
#       "sudo apt-get install -qq python3-pip strace bind9-dnsutils cron git",
#       "sudo apt-get install -qq iotop-c sysstat aide checkpolicy openssl unzip",
#       "sudo apt-get install -qq zip policycoreutils make selinux-basics",
#       "sudo apt-get install -qq selinux-policy-default selinux-policy-dev",
#       "sudo apt-get install -qq setools semodule-utils make selinux-utils",
#       "sudo apt-get install -qq auditd policycoreutils-python-utils",
#       "sudo aide --init --config /etc/aide/aide.conf",
#       "sudo mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db",
#       "sudo selinux-activate",
#       "sudo reboot",
#     ]
#   }

#   provisioner "shell" {
#     expect_disconnect = true
#     pause_before      = "20s"
#     max_retries       = 10
#     inline = [
#       "sudo getenforce",
#       "sudo setsebool -P systemd_tmpfiles_manage_all=on",
#       "sudo rm /etc/apt/apt.conf.d/70confnew",
#     ]
#   }
# }


source "amazon-ebs" "debian-13-amd64" {
  ami_name        = "debian-13-amd64-base-ami-${formatdate("YYYY-MM-DD_hh-mm-ss", timestamp())}"
  ami_description = "Debian base-ami. Installs packages, configures aide and SELinux. Build: https://github.com/theorchard/collab/blob/master/jtorp/debian-base-ami/debian-ami.pkr.hcl"
  ena_support     = true
  ami_users = [
    "437795906767",
    "103233932089",
    "290365479392",
    "619719722105",
    "926734670777",
    "734458723522",
    "989790945997",
    "375914681009",
    "086679231553",
    "031099521156",
    "079637511089",
    "285943604611",
    "737325105821",
    "725764004555",
    "801577982711",
    "745243859288",
    "807702934854",
    "475275892927",
    "323555055331",
    "483193324480",
    "890208661333",
    "313114233826",
    "582412345909",
    "885200931856",
    "703740486246",
    "322697034778",
    "814622134907",
    "009424872958",
    "311141540202",
    "710271914708",
    "682033496752",
    "591204808501",
    "699475937692",
    "635057242373",
    "588827373449",
    "918817604416",
    "613871678587",
    "554998489284",
    "799833541840",
    "765134955759",
    "811473064144",
    "997286397029",
    "969009837416",
    "058029036333",
    "023180329437",
    "793676410512",
    "887829410671",
    "156555176793",
    "280383026186",
    "718729873097",
    "729038418454",
    "107577002119",
    "635220336377",
    "230315258045",
    "104858398257",
    "638682502729",
    "715641587426",
    "844930103112",
    "130080192946",
    "361463692799",
    "981599956623",
    "504436705349",
  ]
  encrypt_boot    = true
  imds_support    = "v2.0"
  instance_type   = "t3a.large"
  kms_key_id      = "arn:aws:kms:us-east-1:086679231553:key/1d9b335c-ceed-471f-b705-617d871014b2"
  region          = "us-east-1"
  skip_create_ami = false # useful for testing build success
  source_ami_filter {
    filters = {
      name                = "debian-13-amd64-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["amazon"]
  }
  ssh_username = "admin"
  subnet_filter {
    filters = {
      "tag:tier" : "private"
    }
    most_free = true
  }
  tags = {
    Name       = "debian-13-amd64-base-ami-${formatdate("YYYY-MM-DD_hh-mm-ss", timestamp())}"
    packerized = true
    source     = "https://github.com/theorchard/collab/blob/master/jtorp/debian-base-ami/debian-ami.pkr.hcl"
    os_name    = "debian"
    os_version = "13"
    architecture = "amd64"
  }
}

build {
  name    = "debian-base-ami-13-amd64"
  sources = ["source.amazon-ebs.debian-13-amd64"]

  provisioner "shell" {
    expect_disconnect = true
    environment_vars = [
      "DEBIAN_FRONTEND=noninteractive"
    ]
    inline = [
      "sudo apt-get update -qq",
      "echo 'debconf debconf/frontend select Noninteractive' | sudo debconf-set-selections",
      "echo 'Dpkg::Options {\n\"--force-confdef\";\n\"--force-confnew\";\n}' | sudo tee /etc/apt/apt.conf.d/70confnew",
      "sudo apt-get upgrade -qq",
      "sudo localectl set-locale en_US.UTF-8",
      "sudo apt-get install -qq dmidecode htop lsof nethogs nmap psmisc",
      "sudo apt-get install -qq python3-pip strace bind9-dnsutils cron git",
      "sudo apt-get install -qq iotop-c sysstat aide checkpolicy openssl unzip",
      "sudo apt-get install -qq zip policycoreutils make selinux-basics",
      "sudo apt-get install -qq selinux-policy-default selinux-policy-dev",
      "sudo apt-get install -qq setools semodule-utils make selinux-utils",
      "sudo apt-get install -qq auditd policycoreutils-python-utils",
      "sudo aide --init --config /etc/aide/aide.conf",
      "sudo mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db",
      "sudo selinux-activate",
      "sudo reboot",
    ]
  }

  provisioner "shell" {
    expect_disconnect = true
    pause_before      = "20s"
    max_retries       = 10
    inline = [
      "sudo getenforce",
      "sudo setsebool -P systemd_tmpfiles_manage_all=on",
      "sudo rm /etc/apt/apt.conf.d/70confnew",
    ]
  }
}

source "amazon-ebs" "debian-13-arm64" {
  ami_name        = "debian-13-arm64-base-ami-${formatdate("YYYY-MM-DD_hh-mm-ss", timestamp())}"
  ami_description = "Debian base-ami. Installs packages, configures aide and SELinux. Build: https://github.com/theorchard/collab/blob/master/jtorp/debian-base-ami/debian-ami.pkr.hcl"
  ena_support     = true
  ami_users = [
    "437795906767",
    "103233932089",
    "290365479392",
    "619719722105",
    "926734670777",
    "734458723522",
    "989790945997",
    "375914681009",
    "086679231553",
    "031099521156",
    "079637511089",
    "285943604611",
    "737325105821",
    "725764004555",
    "801577982711",
    "745243859288",
    "807702934854",
    "475275892927",
    "323555055331",
    "483193324480",
    "890208661333",
    "313114233826",
    "582412345909",
    "885200931856",
    "703740486246",
    "322697034778",
    "814622134907",
    "009424872958",
    "311141540202",
    "710271914708",
    "682033496752",
    "591204808501",
    "699475937692",
    "635057242373",
    "588827373449",
    "918817604416",
    "613871678587",
    "554998489284",
    "799833541840",
    "765134955759",
    "811473064144",
    "997286397029",
    "969009837416",
    "058029036333",
    "023180329437",
    "793676410512",
    "887829410671",
    "156555176793",
    "280383026186",
    "718729873097",
    "729038418454",
    "107577002119",
    "635220336377",
    "230315258045",
    "104858398257",
    "638682502729",
    "715641587426",
    "844930103112",
    "130080192946",
    "361463692799",
    "981599956623",
    "504436705349",
  ]
  encrypt_boot    = true
  imds_support    = "v2.0"
  instance_type   = "t4g.large"
  kms_key_id      = "arn:aws:kms:us-east-1:086679231553:key/1d9b335c-ceed-471f-b705-617d871014b2"
  region          = "us-east-1"
  skip_create_ami = false # useful for testing build success
  source_ami_filter {
    filters = {
      name                = "debian-13-arm64-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["amazon"]
  }
  ssh_username = "admin"
  subnet_filter {
    filters = {
      "tag:tier" : "private"
    }
    most_free = true
  }
  tags = {
    Name       = "debian-13-arm64-base-ami-${formatdate("YYYY-MM-DD_hh-mm-ss", timestamp())}"
    packerized = true
    source     = "https://github.com/theorchard/collab/blob/master/jtorp/debian-base-ami/debian-ami.pkr.hcl"
    os_name    = "debian"
    os_version = "13"
    architecture = "arm64"
  }
}

build {
  name    = "debian-base-ami-13-arm64"
  sources = ["source.amazon-ebs.debian-13-arm64"]

  provisioner "shell" {
    expect_disconnect = true
    environment_vars = [
      "DEBIAN_FRONTEND=noninteractive"
    ]
    inline = [
      "sudo apt-get update -qq",
      "echo 'debconf debconf/frontend select Noninteractive' | sudo debconf-set-selections",
      "echo 'Dpkg::Options {\n\"--force-confdef\";\n\"--force-confnew\";\n}' | sudo tee /etc/apt/apt.conf.d/70confnew",
      "sudo apt-get upgrade -qq",
      "sudo localectl set-locale en_US.UTF-8",
      "sudo apt-get install -qq dmidecode htop lsof nethogs nmap psmisc",
      "sudo apt-get install -qq python3-pip strace bind9-dnsutils cron git",
      "sudo apt-get install -qq iotop-c sysstat aide checkpolicy openssl unzip",
      "sudo apt-get install -qq zip policycoreutils make selinux-basics",
      "sudo apt-get install -qq selinux-policy-default selinux-policy-dev",
      "sudo apt-get install -qq setools semodule-utils make selinux-utils",
      "sudo apt-get install -qq auditd policycoreutils-python-utils",
      "sudo aide --init --config /etc/aide/aide.conf",
      "sudo mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db",
      "sudo selinux-activate",
      "sudo reboot",
    ]
  }

  provisioner "shell" {
    expect_disconnect = true
    pause_before      = "20s"
    max_retries       = 10
    inline = [
      "sudo getenforce",
      "sudo setsebool -P systemd_tmpfiles_manage_all=on",
      "sudo rm /etc/apt/apt.conf.d/70confnew",
    ]
  }
}
