2013-08-10 07:14:57 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
'''
|
2013-08-11 05:50:47 +00:00
|
|
|
This script is used to test Salt from a Jenkins server, specifically
|
|
|
|
jenkins.saltstack.com.
|
2013-08-10 07:14:57 +00:00
|
|
|
|
2013-08-11 05:50:47 +00:00
|
|
|
This script is intended to be shell-centric!!
|
2013-08-10 07:14:57 +00:00
|
|
|
'''
|
|
|
|
|
2013-08-10 07:23:32 +00:00
|
|
|
# Import python libs
|
2013-08-13 04:54:47 +00:00
|
|
|
import os
|
|
|
|
import re
|
2013-08-10 07:23:32 +00:00
|
|
|
import sys
|
2013-08-10 07:14:57 +00:00
|
|
|
import subprocess
|
|
|
|
import hashlib
|
|
|
|
import random
|
|
|
|
import optparse
|
|
|
|
|
2013-08-13 04:54:47 +00:00
|
|
|
try:
|
|
|
|
from salt.utils.nb_popen import NonBlockingPopen
|
|
|
|
except ImportError:
|
|
|
|
# Salt not installed, or nb_popen was not yet shipped with it
|
|
|
|
salt_lib = os.path.abspath(
|
|
|
|
os.path.dirname(os.path.dirname(__file__))
|
|
|
|
)
|
|
|
|
if salt_lib not in sys.path:
|
|
|
|
sys.path.insert(0, salt_lib)
|
|
|
|
try:
|
|
|
|
# Let's try using the current checked out code
|
|
|
|
from salt.utils.nb_popen import NonBlockingPopen
|
|
|
|
except ImportError:
|
|
|
|
# Still an ImportError??? Let's use some "brute-force"
|
|
|
|
sys.path.insert(
|
|
|
|
0,
|
|
|
|
os.path.join(salt_lib, 'salt', 'utils')
|
|
|
|
)
|
|
|
|
from nb_popen import NonBlockingPopen
|
|
|
|
|
|
|
|
|
2013-08-17 06:59:13 +00:00
|
|
|
def cleanup(clean, vm_name):
|
|
|
|
if not clean:
|
|
|
|
return
|
|
|
|
|
|
|
|
cmd = 'salt-cloud -d {0} -y'.format(vm_name)
|
|
|
|
print('Running CMD: {0}'.format(cmd))
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
proc = NonBlockingPopen(
|
|
|
|
cmd,
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
2013-08-22 10:59:25 +00:00
|
|
|
stderr=subprocess.STDOUT,
|
2013-08-17 06:59:13 +00:00
|
|
|
stream_stds=True
|
|
|
|
)
|
|
|
|
proc.poll_and_read_until_finish()
|
|
|
|
proc.communicate()
|
|
|
|
|
|
|
|
|
2013-09-04 17:52:03 +00:00
|
|
|
def run(platform, provider, commit, clean, sls, pillar):
|
2013-08-10 07:14:57 +00:00
|
|
|
'''
|
|
|
|
RUN!
|
|
|
|
'''
|
2013-08-11 11:42:44 +00:00
|
|
|
htag = hashlib.md5(str(random.randint(1, 100000000))).hexdigest()[:6]
|
2013-08-17 05:06:51 +00:00
|
|
|
vm_name = 'ZZZ{0}{1}'.format(platform, htag)
|
|
|
|
cmd = 'salt-cloud -l debug --script-args "-D -n git {0}" -p {1}_{2} {3}'.format(
|
|
|
|
commit, provider, platform, vm_name)
|
2013-08-10 07:53:28 +00:00
|
|
|
print('Running CMD: {0}'.format(cmd))
|
2013-08-13 10:54:12 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
2013-08-13 04:54:47 +00:00
|
|
|
proc = NonBlockingPopen(
|
|
|
|
cmd,
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
2013-08-22 10:59:25 +00:00
|
|
|
stderr=subprocess.STDOUT,
|
2013-08-13 04:54:47 +00:00
|
|
|
stream_stds=True
|
|
|
|
)
|
|
|
|
proc.poll_and_read_until_finish()
|
2013-08-22 10:59:25 +00:00
|
|
|
proc.communicate()
|
2013-08-21 22:40:20 +00:00
|
|
|
|
2013-08-13 14:31:23 +00:00
|
|
|
if proc.returncode > 0:
|
2013-08-13 14:47:11 +00:00
|
|
|
print('Failed to bootstrap VM. Exit code: {0}'.format(proc.returncode))
|
|
|
|
sys.stdout.flush()
|
2013-08-17 06:59:13 +00:00
|
|
|
cleanup(clean, vm_name)
|
2013-08-13 14:31:23 +00:00
|
|
|
sys.exit(proc.returncode)
|
2013-08-13 10:54:12 +00:00
|
|
|
|
2013-08-17 05:06:51 +00:00
|
|
|
print('VM Bootstrapped. Exit code: {0}'.format(proc.returncode))
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
# Run tests here
|
2013-09-04 17:52:03 +00:00
|
|
|
cmd = 'salt -t 1800 {vm_name} state.sls {sls} pillar="{pillar}" --no-color'.format(
|
|
|
|
sls=sls,
|
|
|
|
pillar=pillar.format(commit=commit, vm_name=vm_name),
|
|
|
|
vm_name=vm_name,
|
|
|
|
commit=commit)
|
2013-08-17 05:06:51 +00:00
|
|
|
print('Running CMD: {0}'.format(cmd))
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2013-08-22 11:27:58 +00:00
|
|
|
#proc = NonBlockingPopen(
|
|
|
|
proc = subprocess.Popen(
|
2013-08-17 05:06:51 +00:00
|
|
|
cmd,
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
2013-08-22 10:59:25 +00:00
|
|
|
stderr=subprocess.STDOUT,
|
2013-08-22 11:27:58 +00:00
|
|
|
# stream_stds=True
|
2013-08-17 05:06:51 +00:00
|
|
|
)
|
2013-08-22 11:27:58 +00:00
|
|
|
#proc.poll_and_read_until_finish()
|
2013-08-17 05:06:51 +00:00
|
|
|
stdout, stderr = proc.communicate()
|
|
|
|
|
|
|
|
if stdout:
|
|
|
|
print(stdout)
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2013-08-13 04:54:47 +00:00
|
|
|
try:
|
|
|
|
match = re.search(r'Test Suite Exit Code: (?P<exitcode>[\d]+)', stdout)
|
|
|
|
retcode = int(match.group('exitcode'))
|
|
|
|
except AttributeError:
|
|
|
|
# No regex matching
|
2013-08-11 18:22:30 +00:00
|
|
|
retcode = 1
|
2013-08-13 04:54:47 +00:00
|
|
|
except ValueError:
|
|
|
|
# Not a number!?
|
|
|
|
retcode = 1
|
|
|
|
except TypeError:
|
|
|
|
# No output!?
|
|
|
|
retcode = 1
|
|
|
|
if stdout:
|
|
|
|
# Anything else, raise the exception
|
|
|
|
raise
|
|
|
|
|
2013-08-17 06:59:13 +00:00
|
|
|
cleanup(clean, vm_name)
|
2013-08-10 08:33:58 +00:00
|
|
|
return retcode
|
2013-08-10 07:23:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def parse():
|
|
|
|
'''
|
2013-08-11 05:50:47 +00:00
|
|
|
Parse the CLI options
|
2013-08-10 07:23:32 +00:00
|
|
|
'''
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('--platform',
|
|
|
|
dest='platform',
|
|
|
|
help='The target platform, choose from:\ncent6\ncent5\nubuntu12.04')
|
|
|
|
parser.add_option('--provider',
|
|
|
|
dest='provider',
|
|
|
|
help='The vm provider')
|
2013-08-10 07:40:47 +00:00
|
|
|
parser.add_option('--commit',
|
|
|
|
dest='commit',
|
|
|
|
help='The git commit to track')
|
2013-09-04 17:52:03 +00:00
|
|
|
parser.add_option('--sls',
|
|
|
|
dest='sls',
|
|
|
|
default='testrun',
|
|
|
|
help='The sls file to execute')
|
|
|
|
parser.add_option('--pillar',
|
|
|
|
dest='pillar',
|
|
|
|
default='{{git_commit: {commit}}}',
|
|
|
|
help='Pillar values to pass to the sls file')
|
2013-08-10 08:06:03 +00:00
|
|
|
parser.add_option('--no-clean',
|
2013-08-10 08:04:45 +00:00
|
|
|
dest='clean',
|
|
|
|
default=True,
|
|
|
|
action='store_false',
|
|
|
|
help='Clean up the built vm')
|
2013-08-10 07:23:32 +00:00
|
|
|
options, args = parser.parse_args()
|
2013-08-11 11:42:44 +00:00
|
|
|
if not options.platform:
|
|
|
|
parser.exit('--platform is required')
|
|
|
|
if not options.provider:
|
|
|
|
parser.exit('--provider is required')
|
|
|
|
if not options.commit:
|
|
|
|
parser.exit('--commit is required')
|
2013-08-10 07:43:40 +00:00
|
|
|
return options.__dict__
|
2013-08-10 07:23:32 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
opts = parse()
|
2013-08-10 08:04:45 +00:00
|
|
|
exit_code = run(**opts)
|
2013-08-10 07:40:47 +00:00
|
|
|
print('Exit Code: {0}'.format(exit_code))
|
2013-08-10 07:23:32 +00:00
|
|
|
sys.exit(exit_code)
|