From 7068e3bdaa69ec3e59e50cab2c57f470ed63ca7d Mon Sep 17 00:00:00 2001
From: Eesaan Atluri <atlurie@uab.edu>
Date: Wed, 2 Oct 2024 18:23:30 -0500
Subject: [PATCH] feat(proxy): Add packer template for proxy build

---
 openstack-proxy/README.md         |   1 +
 openstack-proxy/nodeimage.pkr.hcl |  58 +++++++++++++++++
 openstack-proxy/variables.pkr.hcl | 101 ++++++++++++++++++++++++++++++
 3 files changed, 160 insertions(+)
 create mode 100644 openstack-proxy/README.md
 create mode 100644 openstack-proxy/nodeimage.pkr.hcl
 create mode 100644 openstack-proxy/variables.pkr.hcl

diff --git a/openstack-proxy/README.md b/openstack-proxy/README.md
new file mode 100644
index 00000000..d9287a3a
--- /dev/null
+++ b/openstack-proxy/README.md
@@ -0,0 +1 @@
+This contains packer hcl files for creating images. For documentation on packer, see [here](https://www.packer.io/docs); for information about the openstack-specific builder, see [here](https://www.packer.io/plugins/builders/openstack)
diff --git a/openstack-proxy/nodeimage.pkr.hcl b/openstack-proxy/nodeimage.pkr.hcl
new file mode 100644
index 00000000..ec858cba
--- /dev/null
+++ b/openstack-proxy/nodeimage.pkr.hcl
@@ -0,0 +1,58 @@
+packer {
+  required_plugins {
+    openstack = {
+      version = "~> 1"
+      source  = "github.com/hashicorp/openstack"
+    }
+    ansible = {
+      version = "~> 1"
+      source  = "github.com/hashicorp/ansible"
+    }
+  }
+}
+
+locals {
+  local_image_name = "${var.image_name}${var.image_date_suffix ? formatdate("-YYYYMMDDHHmm", timestamp()) : ""}"
+}
+
+source "openstack" "image" {
+  skip_create_image         = var.skip_create_image
+  image_name                = local.local_image_name
+  source_image              = var.source_image
+  image_members             = var.image_membership
+  image_auto_accept_members = var.auto_accept_members
+  image_tags                = var.image_tags
+  image_disk_format         = var.image_format
+  volume_size               = var.volume_size
+  flavor                    = var.flavor
+  instance_name             = var.build_instance_name
+  use_blockstorage_volume   = true
+  floating_ip_network       = var.floating_ip_network
+  networks                  = var.networks
+  security_groups           = var.security_groups
+  ssh_username              = var.ssh_username
+}
+
+build {
+  sources = ["source.openstack.image"]
+
+  provisioner "ansible" {
+    use_proxy     = false
+    user          = var.ssh_username
+    groups        = ["proxy"]
+    playbook_file = "./ansible/cheaha.yml"
+    roles_path    = "./ansible/roles"
+    ansible_env_vars = ["ANSIBLE_TIMEOUT=60"]
+    extra_arguments = [
+      "--extra-vars", "root_ssh_key='${var.root_ssh_key}'"
+    ]
+  }
+
+  provisioner "ansible" {
+    use_proxy        = false
+    user             = var.ssh_username
+    groups           = ["proxy"]
+    ansible_env_vars = ["ANSIBLE_HOST_KEY_CHECKING=False", "ANSIBLE_RUN_TAGS=var.ANSIBLE_RUN_TAGS"]
+    playbook_file    = "./CRI_XCBC/proxy.yaml"
+  }
+}
diff --git a/openstack-proxy/variables.pkr.hcl b/openstack-proxy/variables.pkr.hcl
new file mode 100644
index 00000000..c57d2794
--- /dev/null
+++ b/openstack-proxy/variables.pkr.hcl
@@ -0,0 +1,101 @@
+variable "root_ssh_key" {
+  type        = string
+  default     = ""
+  description = "The root key to use for ssh"
+}
+
+variable "image_name" {
+  type        = string
+  default     = "cluster-image"
+  description = "Name of the image in openstack"
+}
+
+variable "image_format" {
+  type        = string
+  default     = "qcow2"
+  description = "The format of the resulting image"
+}
+
+variable "image_date_suffix" {
+  type        = bool
+  default     = false
+  description = "Append a date to the image name (in YYYYMMDDHHMMSS format)"
+}
+
+variable "image_tags" {
+  type        = list(string)
+  default     = []
+  description = "List of tags to be associated to the resulting image"
+}
+
+variable "image_membership" {
+  type        = list(string)
+  default     = []
+  description = "Projects/tenants to share the image in openstack with"
+}
+
+variable "auto_accept_members" {
+  type        = bool
+  default     = false
+  description = "A boolean value for auto accepting image in the projects/tenants defined in image_membership."
+}
+
+variable "skip_create_image" {
+  type        = bool
+  default     = false
+  description = "A boolean value for skipping image creation at the end of the build"
+}
+
+variable "source_image" {
+  type        = string
+  default     = ""
+  description = "The name of the source image to use"
+}
+
+variable "flavor" {
+  type        = string
+  default     = ""
+  description = "The name of the flavor to use"
+}
+
+variable "floating_ip_network" {
+  type        = string
+  default     = "uab-campus"
+  description = "floating ip network to use with (temporary) ip assignmnet to a vm"
+}
+
+variable "networks" {
+  type        = list(string)
+  default     = []
+  description = "List of network UUIDs to assign to the network"
+}
+
+variable "security_groups" {
+  type        = list(string)
+  default     = []
+  description = "A list of security groups to add - you should make sure ssh access is open to the machine"
+}
+
+variable "build_instance_name" {
+  type        = string
+  default     = "ood"
+  description = "A name of build instance used for image build"
+}
+
+variable "ssh_username" {
+  type        = string
+  default     = "centos"
+  description = "The default username to use for SSH"
+}
+
+variable "volume_size" {
+  type        = number
+  default     = 20
+  description = "The default volume size for building iamge"
+}
+
+variable "ANSIBLE_RUN_TAGS" {
+  type        = list(string)
+  default     = ["sshpiper"]
+  description = "Run selective roles based on the tags"
+}
-- 
GitLab