first commit
This commit is contained in:
173
Jenkinsfile
vendored
Normal file
173
Jenkinsfile
vendored
Normal file
@@ -0,0 +1,173 @@
|
||||
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'
|
||||
}
|
||||
|
||||
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 --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('Wait for Machine to Boot') {
|
||||
steps {
|
||||
script {
|
||||
def targetHost = "${params.HOSTNAME}.lan"
|
||||
echo "Waiting for ${targetHost} to become available..."
|
||||
|
||||
// Wait up to 3 minutes for the machine to respond to ping
|
||||
timeout(time: 3, unit: 'MINUTES') {
|
||||
waitUntil {
|
||||
def result = sh(
|
||||
script: "ping -c 1 ${targetHost} > /dev/null 2>&1",
|
||||
returnStatus: true
|
||||
)
|
||||
return result == 0
|
||||
}
|
||||
}
|
||||
|
||||
// Additional wait for SSH to be ready
|
||||
sleep(time: 30, unit: 'SECONDS')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Copy Jenkins SSH Key') {
|
||||
steps {
|
||||
script {
|
||||
def targetHost = "${params.HOSTNAME}.lan"
|
||||
|
||||
// Use sshpass or expect to handle the initial connection
|
||||
// This assumes the template has a default user that accepts the key
|
||||
sh """
|
||||
sudo -u jenkins ssh-copy-id -i /var/lib/jenkins/.ssh/id_ed25519.pub -o StrictHostKeyChecking=no jenkins@${targetHost}
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Configure Machine') {
|
||||
steps {
|
||||
script {
|
||||
def targetHost = "${params.HOSTNAME}.lan"
|
||||
|
||||
// Create a temporary inventory file with the new host
|
||||
writeFile file: 'temp_inventory.yml', text: """---
|
||||
all:
|
||||
hosts:
|
||||
new_host:
|
||||
ansible_host: ${targetHost}
|
||||
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"
|
||||
}
|
||||
failure {
|
||||
echo "Failed to provision ${params.PROVISION_TYPE} '${params.HOSTNAME}'"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user