Skip to content

Commit

Permalink
feat: Add VM publish feature in vsphere-supervisor
Browse files Browse the repository at this point in the history
  • Loading branch information
cyclexuxu authored and nywilken committed Jun 7, 2023
1 parent 882a19b commit 601e82c
Show file tree
Hide file tree
Showing 26 changed files with 1,234 additions and 74 deletions.
119 changes: 119 additions & 0 deletions builder/vsphere/examples/supervisor/general-template.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# A Packer template to deploy and publish a VM-Service VM using the vsphere-supervisor builder.

# VM-Service source VM configs.
variable "image_name" {
type = string
}
variable "class_name" {
type = string
}
variable "storage_class" {
type = string
}
variable "source_name" {
type = string
default = null
}
variable "bootstrap_provider" {
type = string
default = "CloudInit"
}
variable "bootstrap_data_file" {
type = string
default = null
}

# Supervisor cluster configs.
variable "kubeconfig_path" {
type = string
default = null
}
variable "supervisor_namespace" {
type = string
default = null
}

# SSH connection configs.
variable "communicator" {
type = string
default = "ssh"
}
variable "ssh_username" {
type = string
default = "packer"
}
variable "ssh_password" {
type = string
default = "packer"
sensitive = true
}
variable "ssh_bastion_host" {
type = string
default = null
}
variable "ssh_bastion_username" {
type = string
default = null
}
variable "ssh_bastion_password" {
type = string
default = null
sensitive = true
}

# Whether to keep the created source VM after the build.
variable "keep_input_artifact" {
type = bool
default = false
}

# VM publishing configs.
variable "publish_location_name" {
type = string
default = null
}
variable "publish_image_name" {
type = string
default = null
}

# Watch timeout related configs.
variable "watch_source_timeout_sec" {
type = number
default = 1800
}
variable "watch_publish_timeout_sec" {
type = number
default = 600
}

source "vsphere-supervisor" "vm" {
kubeconfig_path = "${var.kubeconfig_path}"
supervisor_namespace = "${var.supervisor_namespace}"
class_name = "${var.class_name}"
image_name = "${var.image_name}"
source_name = "${var.source_name}"
storage_class = "${var.storage_class}"
bootstrap_provider = "${var.bootstrap_provider}"
bootstrap_data_file = "${var.bootstrap_data_file}"
communicator = "${var.communicator}"
ssh_username = "${var.ssh_username}"
ssh_password = "${var.ssh_password}"
ssh_bastion_host = "${var.ssh_bastion_host}"
ssh_bastion_username = "${var.ssh_bastion_username}"
ssh_bastion_password = "${var.ssh_bastion_password}"
keep_input_artifact = "${var.keep_input_artifact}"
publish_location_name = "${var.publish_location_name}"
publish_image_name = "${var.publish_image_name}"
watch_source_timeout_sec = "${var.watch_source_timeout_sec}"
watch_publish_timeout_sec = "${var.watch_publish_timeout_sec}"
}

build {
sources = ["source.vsphere-supervisor.vm"]
provisioner "shell" {
inline = [
"echo 'Hello from Packer!' > ./hello-packer.txt",
]
}
}
143 changes: 143 additions & 0 deletions builder/vsphere/examples/supervisor/jenkins-template.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# A Packer template to deploy a VM-Service VM using the vsphere-supervisor builder.
# It installs Jenkins and runs a sample hello-world job in the deployed VM.

# VM-Service source VM configs.
variable "image_name" {
type = string
}
variable "class_name" {
type = string
}
variable "storage_class" {
type = string
}
variable "source_name" {
type = string
default = null
}
variable "bootstrap_provider" {
type = string
default = "CloudInit"
}
variable "bootstrap_data_file" {
type = string
default = null
}

# Supervisor cluster configs.
variable "kubeconfig_path" {
type = string
default = null
}
variable "supervisor_namespace" {
type = string
default = null
}

# SSH connection configs.
variable "communicator" {
type = string
default = "ssh"
}
variable "ssh_username" {
type = string
default = "packer"
}
variable "ssh_password" {
type = string
default = "packer"
sensitive = true
}
variable "ssh_bastion_host" {
type = string
default = null
}
variable "ssh_bastion_username" {
type = string
default = null
}
variable "ssh_bastion_password" {
type = string
default = null
sensitive = true
}

# Whether to keep the created source VM after the build.
variable "keep_input_artifact" {
type = bool
default = false
}

# VM publishing configs.
variable "publish_location_name" {
type = string
default = null
}
variable "publish_image_name" {
type = string
default = null
}

source "vsphere-supervisor" "vm" {
kubeconfig_path = "${var.kubeconfig_path}"
supervisor_namespace = "${var.supervisor_namespace}"
class_name = "${var.class_name}"
image_name = "${var.image_name}"
source_name = "${var.source_name}"
storage_class = "${var.storage_class}"
bootstrap_provider = "${var.bootstrap_provider}"
bootstrap_data_file = "${var.bootstrap_data_file}"
communicator = "${var.communicator}"
ssh_username = "${var.ssh_username}"
ssh_password = "${var.ssh_password}"
ssh_bastion_host = "${var.ssh_bastion_host}"
ssh_bastion_username = "${var.ssh_bastion_username}"
ssh_bastion_password = "${var.ssh_bastion_password}"
keep_input_artifact = "${var.keep_input_artifact}"
publish_location_name = "${var.publish_location_name}"
publish_image_name = "${var.publish_image_name}"
}

build {
sources = ["source.vsphere-supervisor.vm"]

# Jenkins job configuration file.
provisioner "file" {
destination = "/tmp/sample-job.xml"
content = <<EOF
<?xml version='1.1' encoding='UTF-8'?>
<project>
<description>A sample job</description>
<builders>
<hudson.tasks.Shell>
<command>echo "Hello VM-Service from Jenkins"</command>
</hudson.tasks.Shell>
</builders>
</project>
EOF
}

provisioner "shell" {
inline = [
# Install Jenkins and its dependencies.
"curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null",
"echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null",
# Sometimes apt-get uses IPv6 and causes failure, force to use IPv4 address.
"sudo apt-get -qq -o Acquire::ForceIPv4=true update",
"sudo apt-get -qq -o Acquire::ForceIPv4=true install -f -y ca-certificates openjdk-11-jre-headless",
"sudo apt-get -qq -o Acquire::ForceIPv4=true install -f -y jenkins",
# Restart Jenkins service, in case it didn't initialize successfully.
"sudo systemctl restart jenkins",

"export JENKINS_URL=http://localhost:8080/",
"export USER=admin",
"export PASSWORD=$(sudo cat /var/lib/jenkins/secrets/initialAdminPassword)",
# Download Jenkins CLI to create and check job status.
"wget -q -O /tmp/jenkins-cli.jar $JENKINS_URL/jnlpJars/jenkins-cli.jar",
# Create a new job from the above sample-job.xml file.
"java -jar /tmp/jenkins-cli.jar -s $JENKINS_URL -auth $USER:$PASSWORD create-job sample-job < /tmp/sample-job.xml",
# Build and wait for a successful completion of the job.
"java -jar /tmp/jenkins-cli.jar -s $JENKINS_URL -auth $USER:$PASSWORD build sample-job -s -v",
]
}
}
Loading

0 comments on commit 601e82c

Please sign in to comment.