Skip to content

Instantly share code, notes, and snippets.

@alexmerser
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save alexmerser/fc919935fe72abcb8a98 to your computer and use it in GitHub Desktop.

Select an option

Save alexmerser/fc919935fe72abcb8a98 to your computer and use it in GitHub Desktop.
import os
import subprocess
#import argparse
import sys
from time import sleep
#parser = argparse.ArgumentParser(description='Build script for creating VM and running tests against it.')
#parser.add_argument('user', metavar='U', type=str, nargs='+',
# help='The user of the hostmachine. Required to create the VM through SSH.')
#parser.add_argument('host', metavar='H', type=str, nargs='+',
# help='The IP of the hostmachine. Required to create the VM through SSH.')
#parser.add_argument('vm_name', metavar='N', type=str, nargs='+',
# help='The name of the VM to be created.')
#parser.add_argument('path_to_iso', metavar='P', type=str, nargs='+',
# help='The absolute path to the ISO file used to create the VM.')
#parser.add_argument('mac_address', metavar='M', type=str, nargs='+',
# help='The MAC address of the VM.')
#parser.add_argument('vm_host_name', metavar='V', type=str, nargs='+',
# help='The hostname of the VM.')
#args = parser.parse_args()
#USER=args.user
#HOST=args.host
#VM_NAME=args.vm_name
#ABSOLUTE_PATH_TO_ISO=args.path_to_iso
#MAC_ADDRESS=args.mac_address
#VM_HOST_NAME=args.vm_host_name
USER=sys.argv[1]
HOST=sys.argv[2]
VM_NAME=sys.argv[3]
ABSOLUTE_PATH_TO_ISO=sys.argv[4]
MAC_ADDRESS=sys.argv[5]
VM_HOST_NAME=sys.argv[6]
print("Arguments passed in the build script:")
print("user: " + USER)
print("host: " + HOST)
print("vm_name: " + VM_NAME)
print("path_to_iso: " + ABSOLUTE_PATH_TO_ISO)
print("mac_address: " + MAC_ADDRESS)
print("vm_host_name: " + VM_HOST_NAME)
VAR_DIR_PUPPET="/data/puppet"
DATA_DIR_PUPPET="/data/puppet"
VM_PUPPET_REPORTS_DIR=VAR_DIR_PUPPET + "/reports/" + VM_HOST_NAME
JENKINS_REPORTS_DIR=os.getenv('WORKSPACE') + "/build/reports"
def execute(cmd, user="puppet"):
print("Executing command: " + "sudo -u " + user + " " + cmd)
#returncode = subprocess.call(["sudo", "-u", user, cmd])
returncode = subprocess.call(["sudo -u " + user, cmd])
print("Command executed with returncode [" + str(returncode) + "]")
def cleanDirectory(dir):
if not os.path.isdir(dir):
print("Creating directory: " + dir)
execute("mkdir " + dir)
else:
print("Directory " + dir +" exists. No need to create it...")
execute("rm -rf " + dir + "/*")
# We should fetch the iso from somewhere or create it at hoc
# Remove all existing puppet reports of this test vm
execute("rm -f " + VAR_DIR_PUPPET + "/yaml/facts/" + VM_HOST_NAME + ".yaml")
execute("rm -f " + VAR_DIR_PUPPET + "/yaml/node/" + VM_HOST_NAME + ".yaml")
# This is the actual report dir - implemented workaround for PuppetLabs Bug #5578: http://projects.puppetlabs.com/issues/5578
cleanDirectory(VM_PUPPET_REPORTS_DIR)
# Jenkins Job Workspace reports dir for archiving test results
cleanDirectory(JENKINS_REPORTS_DIR)
# Clean puppet modules, manifests and features directories
cleanDirectory(DATA_DIR_PUPPET + "/modules")
cleanDirectory(DATA_DIR_PUPPET + "/manifests")
cleanDirectory(DATA_DIR_PUPPET + "/features")
execute("rm -rf " + DATA_DIR_PUPPET + "/features/.[a-z]*")
# Copy puppet files from Jenkins Job workspace to the puppetmaster workspace
execute("cp -r puppet/dist/modules/* " + DATA_DIR_PUPPET + "/modules/")
execute("cp puppet/dist/site.pp " + DATA_DIR_PUPPET + "/manifests/")
execute("cp puppet/site/ventouris/nodes.pp " + DATA_DIR_PUPPET + "/manifests/")
# Copy cucumber-puppet tests from Jenkins Job workspace to the puppetmaster workspace
execute("cp -r puppet/tests/* " + DATA_DIR_PUPPET + "/")
# Create symlinks for running cucumber-puppet tests - hack because of wrong setup in cucumber-puppet framework
execute("ln -s " + DATA_DIR_PUPPET + "/modules /etc/puppet/modules")
execute("ln -s " + DATA_DIR_PUPPET + "/manifests /etc/puppet/manifests")
# Run cucumber-puppet tests on the puppetmaster
returnCodeForCucumberPuppetTests = subprocess.call([os.getenv('WORKSPACE') + "/build/run-cucumber-puppet-tests.sh", DATA_DIR_PUPPET])
print("Executed cucumber-puppet tests with return code [" + str(returnCodeForCucumberPuppetTests) + "]")
# Copy everything the host needs to create and destroy VMs
subprocess.call(["scp", "-o", "StrictHostKeyChecking=false", "build/createVM.sh", USER + "@" + HOST + ":~/createVM.sh"])
subprocess.call(["scp", "-o", "StrictHostKeyChecking=false", "build/destroyVM.sh", USER + "@" + HOST + ":~/destroyVM.sh"])
subprocess.call(["scp", "-o", "StrictHostKeyChecking=false", USER + "@" + HOST , "mkdir", "iso_to_test"])
subprocess.call(["scp", "-o", "StrictHostKeyChecking=false", USER + "@" + HOST, ABSOLUTE_PATH_TO_ISO, USER + "@" + HOST + ":~/iso_to_test/jeos.iso"])
# Create test VM
subprocess.call(["ssh", "-o", "StrictHostKeyChecking=false", USER + "@" + HOST, "./createVM.sh", VM_NAME, "./iso_to_test/jeos.iso", MAC_ADDRESS])
# Wait for the puppet client (with timeout)
TIMEOUT=600
print("Waiting for puppet client to contact the master, timeout after " + TIMEOUT + " seconds.")
while not os.path.exists(VAR_DIR_PUPPET + "/yaml/node/" + VM_HOST_NAME + ".yaml") and TIMEOUT > 0:
print(TIMEOUT + " seconds left, still waiting for puppet client ...")
sleep(10)
TIMEOUT-=10
#if TIMEOUT == 0:
if not TIMEOUT:
print("VM did not start or puppet client did not contact the puppetmaster")
subprocess.call(["ssh", "-o", "StrictHostKeyChecking=false", USER + "@" + HOST, "./destroyVM.sh", VM_NAME])
exit(1)
print("Puppet client contacted the master")
# Wait for the puppet report (with timeout)
TIMEOUT=600
print("Waiting for puppet report after catalog run, timeout after " + TIMEOUT + " seconds.")
while not os.listdir(VAR_DIR_PUPPET + "/reports/" + VM_HOST_NAME) and TIMEOUT > 0:
print(TIMEOUT + " seconds left, still waiting for puppet report...")
sleep(10)
TIMEOUT-=10
#if TIMEOUT == 0:
if not TIMEOUT:
print("Puppet client did not finish in time.")
subprocess.call(["ssh", "-o", "StrictHostKeyChecking=false", USER + "@" + HOST, "./destroyVM.sh", VM_NAME])
exit(1)
print("Puppet client applied the catalog and sent its report")
# Copy Puppet node yaml and reports to Jenkins Job Workspace for archiving
subprocess.call(["cp", VAR_DIR_PUPPET + "/yaml/node/" + VM_HOST_NAME + ".yaml", JENKINS_REPORTS_DIR])
subprocess.call(["cp", VAR_DIR_PUPPET + "/reports/" + VM_HOST_NAME + "/*", JENKINS_REPORTS_DIR])
# Run acceptance tests on the VM (requires cucumber,net-ssh, rspec on the jenkinsmaster)
returnCodeForAcceptanceTests = subprocess.call([os.getenv('WORKSPACE') + "/build/run-acceptance-tests.sh", VM_HOST_NAME])
print("Executed acceptance tests with return code [" + str(returnCodeForAcceptanceTests) + "]")
# Destroy test VM
subprocess.call(["ssh", "-o", "StrictHostKeyChecking=false", USER + "@" + HOST, "./destroyVM.sh", VM_NAME])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment