init commit
This commit is contained in:
146
Jenkinsfile.vm_party
Normal file
146
Jenkinsfile.vm_party
Normal file
@ -0,0 +1,146 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
// Define parameters
|
||||
parameters {
|
||||
|
||||
string(name: 'host_ip', description: 'Target System Address')
|
||||
string(name: 'new_hostname', defaultValue: 'vm-party-host', description: 'Update Hostname')
|
||||
booleanParam(name: 'update_party', defaultValue: false, description: 'Only update VM Party service')
|
||||
booleanParam(name: 'service_only', defaultValue: false, description: 'Only update VM Party python code')
|
||||
booleanParam(name: 'autologin', defaultValue: true, description: 'Enable autologin with stats on screen')
|
||||
|
||||
}
|
||||
|
||||
|
||||
environment {
|
||||
safe_subnet = true
|
||||
ANSIBLE_FORCE_COLOR = '1'
|
||||
cosmos_password = credentials('cosmos_password')
|
||||
cosmos_root_password = credentials('cosmos_root_password')
|
||||
vm_party_username_password = credentials('vm_party_username_password')
|
||||
matt_public_key = credentials('matt_public_key')
|
||||
jenkins_public_key = credentials('jenkins_public_key')
|
||||
}
|
||||
|
||||
options {
|
||||
ansiColor('xterm')
|
||||
}
|
||||
|
||||
stages {
|
||||
|
||||
stage('Initialize Environment') {
|
||||
|
||||
|
||||
steps {
|
||||
script {
|
||||
// Get the current date (in a consistent format) and hash it
|
||||
def date = sh(script: "date +%Y-%m-%dT%H:%M:%S", returnStdout: true).trim()
|
||||
def hash = sh(script: "echo -n '${date}' | sha256sum | cut -c1-8", returnStdout: true).trim()
|
||||
|
||||
// Set it as an environment variable
|
||||
env.STAGE_TWO_HASH = hash
|
||||
|
||||
}
|
||||
// Generate the dynamic inventory files
|
||||
sh """
|
||||
cd /var/jenkins_home/ansible
|
||||
chmod +x /var/jenkins_home/ansible/inventory/inventory.sh
|
||||
/var/jenkins_home/ansible/inventory/inventory.sh ${params.host_ip}
|
||||
echo Stage Two Hash: ${env.STAGE_TWO_HASH}
|
||||
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
stage('Ansible Playbook Stage One') {
|
||||
when { expression { env.safe_subnet } }
|
||||
|
||||
steps {
|
||||
//Run the cosmos-base ansible playbook
|
||||
// /workspace/ansible/playbooks/cosmos-base.yaml
|
||||
sh """
|
||||
echo ${params.host_ip}
|
||||
hash=\$(echo -n ${params.host_ip} | md5sum | cut -c 1-8)
|
||||
inventory_file="/var/jenkins_home/ansible/.inv/inventory-\$hash.yml"
|
||||
|
||||
cd /var/jenkins_home/ansible
|
||||
|
||||
ansible-playbook -i \$inventory_file /var/jenkins_home/ansible/playbooks/vm_party.yaml \
|
||||
--ssh-common-args='-o StrictHostKeyChecking=no' --extra-vars \
|
||||
"new_hostname=${params.new_hostname} matt_public_key='${env.matt_public_key}' \
|
||||
cosmos_password='${env.cosmos_password}' cosmos_root_password='${env.cosmos_root_password}' \
|
||||
STAGE_TWO_HASH=${env.STAGE_TWO_HASH} update_party=${params.update_party} run_stage_two=false \
|
||||
vm_party_username_password=${env.vm_party_username_password} autologin=${params.autologin} service_only=${params.service_only} "
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
stage('Ansible Playbook Stage Two') {
|
||||
// Skip stage two when update_party is set
|
||||
when { expression { params.update_party != true && env.safe_subnet } }
|
||||
|
||||
steps {
|
||||
script {
|
||||
echo "Sleep 5 seconds..."
|
||||
sleep(5)
|
||||
|
||||
echo "Waiting for reboot"
|
||||
|
||||
def ip = sh(script: "cat /var/jenkins_home/ansible/.inv/inventory-${env.STAGE_TWO_HASH}.yml | grep host_ip | cut -d ';' -f 2", returnStdout: true).trim()
|
||||
echo "Detected IP: ${ip}"
|
||||
|
||||
def timeoutSeconds = 300 // Total timeout: 5 minutes
|
||||
def waitInterval = 5 // Wait 5 seconds between pings
|
||||
def startTime = System.currentTimeMillis()
|
||||
def responded = false
|
||||
|
||||
while ((System.currentTimeMillis() - startTime) < (timeoutSeconds * 1000)) {
|
||||
def status = sh(script: "ping -c 1 ${ip}", returnStatus: true)
|
||||
if (status == 0) {
|
||||
responded = true
|
||||
echo "IP ${ip} is now responding to ping."
|
||||
break
|
||||
}
|
||||
// echo "Waiting for ${ip} to respond to ping..."
|
||||
sleep(waitInterval)
|
||||
}
|
||||
}
|
||||
// check for forbidden subnet
|
||||
|
||||
|
||||
|
||||
// Run the stage two playbook
|
||||
// using the hash
|
||||
sh """
|
||||
|
||||
inventory_file="/var/jenkins_home/ansible/.inv/inventory-${env.STAGE_TWO_HASH}.yml"
|
||||
|
||||
cd /var/jenkins_home/ansible
|
||||
|
||||
ansible-playbook -i \$inventory_file /var/jenkins_home/ansible/playbooks/vm_party.yaml \
|
||||
--ssh-common-args='-o StrictHostKeyChecking=no' --extra-vars \
|
||||
" run_stage_two=true vm_party_username_password=${env.vm_party_username_password} service_only=${params.service_only} "
|
||||
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
always {
|
||||
// Remove dynamic Inventory files
|
||||
sh """
|
||||
hash=\$(echo -n "${params.host_ip}" | md5sum | cut -c 1-8)
|
||||
inventory_file="/var/jenkins_home/ansible/.inv/inventory-\$hash.yml"
|
||||
rm \$inventory_file || true
|
||||
|
||||
inventory_file="/var/jenkins_home/ansible/.inv/inventory-${env.STAGE_TWO_HASH}.yml"
|
||||
rm \$inventory_file || true
|
||||
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user