<s> import sys <EOL> print ( "<STR_LIT:\n>" . join ( sys . path ) ) <EOL> from pyVmomi import vim <EOL> from pyVim . connect import SmartConnect , Disconnect <EOL> import time <EOL> import vmutils <EOL> import subprocess <EOL> import configparser <EOL> import json <EOL> from passlib . hash import md5_crypt <EOL> import os <EOL> import re <EOL> def checkHostname ( hostname ) : <EOL> validString = re . search ( r'<STR_LIT>' % ( <NUM_LIT:5> , <NUM_LIT:20> ) , hostname ) <EOL> if ( validString == None ) : <EOL> return False <EOL> else : <EOL> return True <EOL> def checkRecipe ( configParser , recipe ) : <EOL> recipes = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> if ( recipe in recipes ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def cloneVM ( configParser , hostname ) : <EOL> folder = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> template = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> cspec = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> domain = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> config = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> knifeargcreate = '<STR_LIT>' + hostname + '<STR_LIT>' + config + '<STR_LIT>' + template + '<STR_LIT>' + folder + '<STR_LIT>' + cspec + '<STR_LIT>' + hostname + '<STR_LIT>' + domain + '<STR_LIT>' <EOL> subprocess . check_call ( knifeargcreate , shell = True ) <EOL> def getIP ( configParser , hostname ) : <EOL> vchost = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> vcuser = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> vcpassword = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> vcport = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> si = SmartConnect ( host = vchost , user = vcuser , pwd = vcpassword , port = vcport ) <EOL> vm = vmutils . get_vm_by_name ( si , hostname ) <EOL> ipAddress = vm . summary . guest . ipAddress <EOL> while ( not isinstance ( ipAddress , str ) ) : <EOL> ipAddress = vm . summary . guest . ipAddress <EOL> return ipAddress <EOL> def bootstrapVM ( configParser , ipAddress , hostname , recipe ) : <EOL> recipes = "<STR_LIT>" + recipe + "<STR_LIT:]>" <EOL> sshuser = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> sshpassword = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> config = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> secretkeyfile = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> bootstrapargcreate = '<STR_LIT>' + ipAddress + '<STR_LIT>' + config + '<STR_LIT>' + hostname + '<STR_LIT>' + sshuser + '<STR_LIT>' + sshpassword + '<STR_LIT>' + secretkeyfile + '<STR_LIT>' + recipes <EOL> subprocess . check_call ( bootstrapargcreate , shell = True ) <EOL> def createVM ( request_id , hostname , recipe , updateProgress ) : <EOL> configParser = configparser . RawConfigParser ( ) <EOL> configFilePath = r'<STR_LIT>' <EOL> configParser . read ( configFilePath ) <EOL> subdomain = configParser . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> progress = <NUM_LIT:0> <EOL> validHostname = checkHostname ( hostname ) <EOL> if ( validHostname == False ) : <EOL> return None , None , '<STR_LIT>' , progress <EOL> fqdn = hostname + "<STR_LIT:.>" + subdomain <EOL> validRecipe = checkRecipe ( configParser , recipe ) <EOL> if ( validRecipe == False ) : <EOL> return None , None , '<STR_LIT>' , progress <EOL> updateProgress ( request_id , progress , "<STR_LIT>" ) <EOL> try : <EOL> cloneVM ( configParser , hostname ) <EOL> except subprocess . CalledProcessError : <EOL> print ( "<STR_LIT>" ) <EOL> return None , None , '<STR_LIT>' , progress <EOL> progress = <NUM_LIT> <EOL> updateProgress ( request_id , progress , "<STR_LIT>" ) <EOL> ipAddress = None <EOL> try : <EOL> ipAddress = getIP ( configParser , hostname ) <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return None , None , '<STR_LIT>' , progress <EOL> progress = <NUM_LIT> <EOL> updateProgress ( request_id , progress , "<STR_LIT>" ) <EOL> try : <EOL> bootstrapVM ( configParser , ipAddress , hostname , recipe ) <EOL> except subprocess . CalledProcessError : <EOL> print ( "<STR_LIT>" ) <EOL> return None , None , '<STR_LIT>' , progress <EOL> url = "<STR_LIT>" + fqdn + "<STR_LIT:/>" + recipe <EOL> progress = <NUM_LIT:100> <EOL> updateProgress ( request_id , progress , "<STR_LIT>" , url ) <EOL> return ipAddress , fqdn , None , progress </s>
