mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Allow for a stepped Jenkins execution.
This commit is contained in:
parent
16e1c86130
commit
363e6fa3d2
1 changed files with 98 additions and 38 deletions
136
tests/jenkins.py
136
tests/jenkins.py
|
@ -19,11 +19,11 @@ 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(
|
||||
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)
|
||||
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
|
||||
|
@ -31,15 +31,25 @@ except ImportError:
|
|||
# Still an ImportError??? Let's use some "brute-force"
|
||||
sys.path.insert(
|
||||
0,
|
||||
os.path.join(salt_lib, 'salt', 'utils')
|
||||
os.path.join(SALT_LIB, 'salt', 'utils')
|
||||
)
|
||||
from nb_popen import NonBlockingPopen
|
||||
|
||||
|
||||
def cleanup(clean, vm_name):
|
||||
if not clean:
|
||||
return
|
||||
def generate_vm_name(platform):
|
||||
'''
|
||||
Generate a random enough vm name
|
||||
'''
|
||||
return 'ZZZ-{0}-{1}'.format(
|
||||
platform,
|
||||
hashlib.md5(str(random.randint(1, 100000000))).hexdigest()[:6]
|
||||
)
|
||||
|
||||
|
||||
def delete_vm(vm_name):
|
||||
'''
|
||||
Stop a VM
|
||||
'''
|
||||
cmd = 'salt-cloud -d {0} -y'.format(vm_name)
|
||||
print('Running CMD: {0}'.format(cmd))
|
||||
sys.stdout.flush()
|
||||
|
@ -55,14 +65,34 @@ def cleanup(clean, vm_name):
|
|||
proc.communicate()
|
||||
|
||||
|
||||
def run(platform, provider, commit, clean, sls, pillar):
|
||||
def echo_parseable_environment(platform, provider):
|
||||
'''
|
||||
Echo NAME=VAL parseable output
|
||||
'''
|
||||
name = generate_vm_name(platform)
|
||||
output = (
|
||||
'SALTCLOUD_VM_PROVIDER="{provider}"\n'
|
||||
'SALTCLOUD_VM_PLATFORM="{platform}"\n'
|
||||
'SALTCLOUD_VM_NAME="{name}"\n').format(name=name,
|
||||
provider=provider,
|
||||
platform=platform)
|
||||
sys.stdout.write(output)
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
def run(opts):
|
||||
'''
|
||||
RUN!
|
||||
'''
|
||||
htag = hashlib.md5(str(random.randint(1, 100000000))).hexdigest()[:6]
|
||||
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)
|
||||
vm_name = os.environ.get(
|
||||
'SALTCLOUD_VM_PLATFORM',
|
||||
generate_vm_name(opts.platform)
|
||||
)
|
||||
|
||||
cmd = (
|
||||
'salt-cloud -l debug --script-args "-D -n git {commit}" -p '
|
||||
'{provider}_{platform} {0}'.format(vm_name, **opts)
|
||||
)
|
||||
print('Running CMD: {0}'.format(cmd))
|
||||
sys.stdout.flush()
|
||||
|
||||
|
@ -80,18 +110,23 @@ def run(platform, provider, commit, clean, sls, pillar):
|
|||
if retcode != 0:
|
||||
print('Failed to bootstrap VM. Exit code: {0}'.format(retcode))
|
||||
sys.stdout.flush()
|
||||
cleanup(clean, vm_name)
|
||||
if opts.clean:
|
||||
delete_vm(vm_name)
|
||||
sys.exit(retcode)
|
||||
|
||||
print('VM Bootstrapped. Exit code: {0}'.format(retcode))
|
||||
sys.stdout.flush()
|
||||
|
||||
# Run tests here
|
||||
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)
|
||||
cmd = (
|
||||
'salt -t 1800 {vm_name} state.sls {sls} pillar="{pillar}" '
|
||||
'--no-color'.format(
|
||||
sls=opts.sls,
|
||||
pillar=opts.pillar.format(commit=opts.commit),
|
||||
vm_name=vm_name,
|
||||
commit=opts.commit
|
||||
)
|
||||
)
|
||||
print('Running CMD: {0}'.format(cmd))
|
||||
sys.stdout.flush()
|
||||
|
||||
|
@ -126,7 +161,8 @@ def run(platform, provider, commit, clean, sls, pillar):
|
|||
# Anything else, raise the exception
|
||||
raise
|
||||
|
||||
cleanup(clean, vm_name)
|
||||
if opts.clean:
|
||||
delete_vm(vm_name)
|
||||
return retcode
|
||||
|
||||
|
||||
|
@ -136,38 +172,62 @@ def parse():
|
|||
'''
|
||||
parser = optparse.OptionParser()
|
||||
parser.add_option('--platform',
|
||||
dest='platform',
|
||||
help='The target platform, choose from:\ncent6\ncent5\nubuntu12.04')
|
||||
dest='platform',
|
||||
default=os.environ.get('SALTCLOUD_VM_PLATFORM', None),
|
||||
help='The target platform, choose from:\ncent6\ncent5\nubuntu12.04')
|
||||
parser.add_option('--provider',
|
||||
dest='provider',
|
||||
help='The vm provider')
|
||||
dest='provider',
|
||||
default=os.environ.get('SALTCLOUD_VM_PROVIDER', None),
|
||||
help='The vm provider')
|
||||
parser.add_option('--commit',
|
||||
dest='commit',
|
||||
help='The git commit to track')
|
||||
dest='commit',
|
||||
help='The git commit to track')
|
||||
parser.add_option('--sls',
|
||||
dest='sls',
|
||||
default='testrun',
|
||||
help='The sls file to execute')
|
||||
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')
|
||||
dest='pillar',
|
||||
default='{{git_commit: {commit}}}',
|
||||
help='Pillar values to pass to the sls file')
|
||||
parser.add_option('--no-clean',
|
||||
dest='clean',
|
||||
default=True,
|
||||
action='store_false',
|
||||
help='Clean up the built vm')
|
||||
dest='clean',
|
||||
default=True,
|
||||
action='store_false',
|
||||
help='Clean up the built vm')
|
||||
parser.add_option(
|
||||
'--echo-parseable-environment',
|
||||
default=False,
|
||||
action='store_true',
|
||||
help='Print a parseable KEY=VAL output'
|
||||
)
|
||||
parser.add_option(
|
||||
'--delete-vm',
|
||||
default=os.environ.get('SALTCLOUD_VM_NAME', None),
|
||||
help='Print a parseable KEY=VAL output'
|
||||
)
|
||||
|
||||
options, args = parser.parse_args()
|
||||
|
||||
if options.delete_vm is not None:
|
||||
delete_vm(options.delete_vm)
|
||||
parser.exit(0)
|
||||
|
||||
if not options.platform:
|
||||
parser.exit('--platform is required')
|
||||
|
||||
if not options.provider:
|
||||
parser.exit('--provider is required')
|
||||
|
||||
if options.echo_parseable_environment:
|
||||
echo_parseable_environment(options.platform, options.provider)
|
||||
parser.exit(0)
|
||||
|
||||
if not options.commit:
|
||||
parser.exit('--commit is required')
|
||||
return options.__dict__
|
||||
return options
|
||||
|
||||
if __name__ == '__main__':
|
||||
opts = parse()
|
||||
exit_code = run(**opts)
|
||||
exit_code = run(parse())
|
||||
print('Exit Code: {0}'.format(exit_code))
|
||||
sys.exit(exit_code)
|
||||
|
|
Loading…
Add table
Reference in a new issue