Skip to content

Instantly share code, notes, and snippets.

@shannonmitchell
Created November 28, 2016 21:40
Show Gist options
  • Save shannonmitchell/7c77f6b2741bd904d057a9b05a541840 to your computer and use it in GitHub Desktop.
Save shannonmitchell/7c77f6b2741bd904d057a9b05a541840 to your computer and use it in GitHub Desktop.
[root@shannon osic]# cat /data/osic/openstack-deploy.py
#!/usr/bin/python
import os
import sys
import time
import argparse
import datetime
import threading
import subprocess
class bcolors:
HEADER = '\033[94m'
NOTE = '\033[95m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def ansible_worker(playbook, logdir):
# initalize the return value
retval = 0
# Let the user know the playbook run is starting
#print "\n\n%s#### Starting run for playbook %s%s####\n" % (bcolors.NOTE, playbook, bcolors.ENDC)
# Get the current data(military style)
logfile = "%s/%s.%s" % (logdir, playbook, time.strftime("%d%b%y_%H:%M").upper())
# Start the command and let the user know to check a file for output
ansiblecmd = "openstack-ansible %s -f 100" % (playbook)
curfile = open(logfile, 'w')
#print "\n%sRunning \"%s > %s\"%s" % (bcolors.OKGREEN, ansiblecmd, logfile, bcolors.ENDC)
ret = subprocess.call(ansiblecmd, shell=True, stdout=curfile, stderr=curfile)
if ret == 0:
print "%s[Success] Play %s finished with return value %s%s." % (bcolors.OKGREEN, playbook, ret, bcolors.ENDC)
print "%s Note: For the full output see %s.%s" % (bcolors.NOTE, logfile, bcolors.ENDC)
else:
print "%s[Failure] Play %s finished with return value %s%s." % (bcolors.FAIL, playbook, ret, bcolors.ENDC)
print "%s Note: For the full output see %s.%s" % (bcolors.NOTE, logfile, bcolors.ENDC)
return retval
def main():
# Define some variables for later use
repo_playbooks = ['repo-install.yml']
infrastructure_playbooks = ['memcached-install.yml', 'galera-install.yml', 'rabbitmq-install.yml', 'utility-install.yml', 'rsyslog-install.yml']
openstack_playbooks = ['os-keystone-install.yml', 'os-glance-install.yml', 'os-cinder-install.yml', 'os-nova-install.yml', 'os-neutron-install.yml', 'os-heat-install.yml', 'os-horizon-install.yml', 'os-swift-install.yml']
threads = []
##########################
# Parse script arguments #
##########################
# Parse the command line arguments
parser = argparse.ArgumentParser(description="Script to deploy openstack components.", prog='openstack-deploy.py')
parser.add_argument('--skiprepo', help='Run repo playbook.', action='store_true')
parser.add_argument('--infrastructure', help='Run infrastructure playbooks.', action='store_true')
parser.add_argument('--openstack', help='Run openstack playbooks.', action='store_true')
# parse the arguments
args = parser.parse_args()
# Check the arguments
if args.openstack == False and args.infrastructure == False and args.skiprepo == True:
parser.print_help()
sys.exit(1)
# Create a deploy_logs directory if it doesn't exist
logdir = './deploy_logs'
if not os.path.exists(logdir):
print "Creating %s" % logdir
os.mkdir(logdir)
# Always run the repo unless disabled
if args.skiprepo != True:
# Define threads and throw them in a list for later use
for playbook in repo_playbooks:
t = threading.Thread(target=ansible_worker, args=(playbook,logdir))
threads.append(t)
# Start all the threads
print "\n\n%s###########################################%s" % (bcolors.HEADER, bcolors.ENDC)
print "%s#### Starting run for repo playbook %s ####%s" % (bcolors.HEADER, playbook, bcolors.ENDC)
print "%s###########################################%s\n" % (bcolors.HEADER, bcolors.ENDC)
for t in threads:
t.start()
# Wait for them to finish
print "\n%s#### Waiting for play to complete ####%s\n" % (bcolors.HEADER, bcolors.ENDC)
for t in threads:
t.join()
# Run the infrastructure playbooks in parallel
if args.infrastructure:
# Define threads and throw them in a list for later use
for playbook in infrastructure_playbooks:
t = threading.Thread(target=ansible_worker, args=(playbook,logdir))
threads.append(t)
# Start all the threads
print "\n\n%s############################################################%s" % (bcolors.HEADER, bcolors.ENDC)
print "%s#### Starting parallel run for infrastructure playbooks #### %s" % (bcolors.HEADER, bcolors.ENDC)
print "%s############################################################%s\n" % (bcolors.HEADER, bcolors.ENDC)
for t in threads:
time.sleep(1)
t.start()
# Wait for them to finish
time.sleep(2)
print "\n%s#### Waiting for plays to complete ####%s\n" % (bcolors.HEADER, bcolors.ENDC)
for t in threads:
t.join()
# Run the openstack playbooks in parallel
if args.openstack:
# Define threads and throw them in a list for later use
for playbook in openstack_playbooks:
t = threading.Thread(target=ansible_worker, args=(playbook,logdir))
threads.append(t)
# Start all the threads
print "\n\n%s#######################################################%s" % (bcolors.HEADER, bcolors.ENDC)
print "%s#### Starting parallel run for openstack playbooks #### %s" % (bcolors.HEADER, bcolors.ENDC)
print "%s#######################################################%s\n" % (bcolors.HEADER, bcolors.ENDC)
for t in threads:
time.sleep(1)
t.start()
# Wait for them to finish
time.sleep(2)
print "\n%s#### Waiting for plays to complete ####%s\n" % (bcolors.HEADER, bcolors.ENDC)
for t in threads:
t.join()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment