Files
proxmox-template-clone/Jenkinsfile
2026-02-02 23:22:36 -05:00

222 lines
8.1 KiB
Groovy

pipeline {
agent any
parameters {
choice(
name: 'PROVISION_TYPE',
choices: ['VM', 'LXC'],
description: 'Select whether to provision a VM or LXC container'
)
choice(
name: 'TARGET_NODE',
choices: ['homeapp1', 'homeapp2', 'homestrg1', 'homeflux1'],
description: 'Select the Proxmox node to provision on'
)
string(
name: 'HOSTNAME',
defaultValue: '',
description: 'Hostname for the new VM/LXC (required)'
)
string(
name: 'CPU_CORES',
defaultValue: '2',
description: 'Number of CPU cores'
)
string(
name: 'RAM_GB',
defaultValue: '2',
description: 'RAM in GB'
)
booleanParam(
name: 'INSTALL_DOCKER',
defaultValue: false,
description: 'Install Docker and Docker Compose'
)
booleanParam(
name: 'INSTALL_NFS',
defaultValue: false,
description: 'Install NFS and mount NFSFolder'
)
}
environment {
ANSIBLE_HOST_KEY_CHECKING = 'False'
ANSIBLE_FORCE_COLOR = 'true'
OPNSENSE_HOST = '192.168.0.1'
}
stages {
stage('Validate Parameters') {
steps {
script {
if (!params.HOSTNAME?.trim()) {
error("HOSTNAME is required")
}
if (!params.CPU_CORES.isInteger() || params.CPU_CORES.toInteger() < 1) {
error("CPU_CORES must be a positive integer")
}
if (!params.RAM_GB.isInteger() || params.RAM_GB.toInteger() < 1) {
error("RAM_GB must be a positive integer")
}
}
}
}
stage('Install Ansible Collections') {
steps {
sh 'ansible-galaxy collection install -r requirements.yml -p ./collections --force'
}
}
stage('Provision VM/LXC') {
steps {
withCredentials([string(credentialsId: 'proxmox-resource-creator', variable: 'PROXMOX_TOKEN')]) {
script {
// Parse the token: format is user@realm!tokenid=secret
def tokenParts = PROXMOX_TOKEN.split('!')
def apiUser = tokenParts[0]
def tokenIdAndSecret = tokenParts[1].split('=')
def apiTokenId = tokenIdAndSecret[0]
def apiTokenSecret = tokenIdAndSecret[1]
sh """
ansible-playbook playbooks/provision.yml \
-e "proxmox_api_user=${apiUser}" \
-e "proxmox_api_token_id=${apiTokenId}" \
-e "proxmox_api_token_secret=${apiTokenSecret}" \
-e "provision_type=${params.PROVISION_TYPE}" \
-e "target_node=${params.TARGET_NODE}" \
-e "vm_hostname=${params.HOSTNAME}" \
-e "cpu_cores=${params.CPU_CORES}" \
-e "ram_gb=${params.RAM_GB}"
"""
}
}
}
}
stage('Get VM IP from DHCP') {
steps {
withCredentials([usernamePassword(credentialsId: 'opnsense-api', usernameVariable: 'OPNSENSE_KEY', passwordVariable: 'OPNSENSE_SECRET')]) {
script {
def templateHost = params.PROVISION_TYPE == 'VM' ? 'ubuntu24vm' : 'ubuntu24lxc'
echo "Looking up DHCP lease for hostname: ${templateHost}"
// Wait for DHCP lease to appear
def vmIp = ''
timeout(time: 5, unit: 'MINUTES') {
waitUntil {
sleep(time: 10, unit: 'SECONDS')
// Query OPNSense DHCP leases API and parse with jq
vmIp = sh(
script: '''
curl -s -k -u "${OPNSENSE_KEY}:${OPNSENSE_SECRET}" \
"https://''' + OPNSENSE_HOST + '''/api/dhcpv4/leases/searchLease" | \
jq -r '.rows[] | select(.hostname == "''' + templateHost + '''" or .["client-hostname"] == "''' + templateHost + '''") | .address' | head -1
''',
returnStdout: true
).trim()
if (vmIp) {
echo "Found IP address: ${vmIp} for hostname: ${templateHost}"
return true
}
echo "DHCP lease not found yet, retrying..."
return false
}
}
// Store IP for subsequent stages
env.VM_IP = vmIp
echo "VM IP address: ${env.VM_IP}"
// Wait for SSH to be ready
timeout(time: 3, unit: 'MINUTES') {
waitUntil {
def result = sh(
script: "nc -z -w5 ${env.VM_IP} 22",
returnStatus: true
)
return result == 0
}
}
sleep(time: 10, unit: 'SECONDS')
}
}
}
}
stage('Copy Jenkins SSH Key') {
steps {
script {
sh """
sudo -u jenkins ssh-copy-id -i /var/lib/jenkins/.ssh/id_ed25519.pub -o StrictHostKeyChecking=no jenkins@${env.VM_IP}
"""
}
}
}
stage('Set Hostname') {
steps {
script {
def newHostname = params.HOSTNAME
// Create a temporary inventory file with the VM IP
writeFile file: 'temp_inventory.yml', text: """---
all:
hosts:
new_host:
ansible_host: ${env.VM_IP}
ansible_user: jenkins
ansible_ssh_private_key_file: /var/lib/jenkins/.ssh/id_ed25519
"""
sh """
ansible-playbook playbooks/set_hostname.yml \
-i temp_inventory.yml \
-e "new_hostname=${newHostname}"
"""
}
}
}
stage('Configure Machine') {
steps {
script {
// Create a temporary inventory file - still use IP since DNS may not have updated
writeFile file: 'temp_inventory.yml', text: """---
all:
hosts:
new_host:
ansible_host: ${env.VM_IP}
ansible_user: jenkins
ansible_ssh_private_key_file: /var/lib/jenkins/.ssh/id_ed25519
"""
sh """
ansible-playbook playbooks/configure.yml \
-i temp_inventory.yml \
-e "install_docker=${params.INSTALL_DOCKER}" \
-e "install_nfs=${params.INSTALL_NFS}"
"""
}
}
}
}
post {
always {
// Clean up temporary inventory file
sh 'rm -f temp_inventory.yml || true'
}
success {
echo "Successfully provisioned ${params.PROVISION_TYPE} '${params.HOSTNAME}' on ${params.TARGET_NODE}"
echo "Machine is accessible at ${params.HOSTNAME}.lan (IP: ${env.VM_IP})"
}
failure {
echo "Failed to provision ${params.PROVISION_TYPE} '${params.HOSTNAME}'"
}
}
}