#!/usr/bin/env python
'''
This script is used to test salt from a jenkins server, specifically
jenkins.satstack.com.

This script is intended to be shell centric!!
'''

# Import python libs
import sys
import subprocess
import hashlib
import random
import optparse


def run(platform, provider, commit):
    '''
    RUN!
    '''
    htag = hashlib.md5(str(random.randint(1, 100000000))).hexdigest()[:6] 
    vm_name = '{0}{1}'.format(platform, htag)
    cmd = 'salt-cloud -p {0}_{1} {2}'.format(provider, platform, vm_name)
    print('Running CMD: {0}'.format(cmd))
    subprocess.call(
            cmd,
            shell=True)
    # Run tests here
    cmd = 'salt {0} state.sls testrun pillar="{{git_commit: {1}}}"'.format(
                vm_name,
                commit),
    print('Running CMD: {0}'.format(cmd))
    subprocess.call(
            cmd,
            shell=True)
    # Clean up the vm
    cmd = 'salt-cloud -d {0} -y'.format(vm_name),
    print('Running CMD: {0}'.format(cmd))
    subprocess.call(
            cmd,
            shell=True)
    return 0


def parse():
    '''
    Parse the cli options
    '''
    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')
    parser.add_option('--commit',
            dest='commit',
            help='The git commit to track')
    options, args = parser.parse_args()
    return options.__dict__

if __name__ == '__main__':
    opts = parse()
    exit_code = run(opts['platform'], opts['provider'], opts['commit'])
    print('Exit Code: {0}'.format(exit_code))
    sys.exit(exit_code)

@@ -293,19 +293,26 @@
 , commit
+, clean
 ):%0A
-
     '''%0A
@@ -900,16 +900,34 @@
  the vm%0A
+    if clean:%0A    
     cmd 
@@ -968,16 +968,20 @@
 _name),%0A
+    
     prin
@@ -1006,32 +1006,36 @@
 %7D'.format(cmd))%0A
+    
     subprocess.c
@@ -1043,37 +1043,45 @@
 ll(%0A            
+    
 cmd,%0A
+    
             shel
@@ -1547,16 +1547,177 @@
 track')%0A
+    parser.add_option('--clean',%0A            dest='clean',%0A            default=True,%0A            action='store_false',%0A            help='Clean up the built vm')%0A
     opti
@@ -1847,58 +1847,14 @@
 run(
+**
 opts
-%5B'platform'%5D, opts%5B'provider'%5D, opts%5B'commit'%5D
 )%0A  

Allow builds to not clean up