<s> '''<STR_LIT>''' <EOL> import atexit <EOL> import argparse <EOL> import sys <EOL> import time <EOL> from pyVmomi import vim , vmodl <EOL> from pyVim import connect <EOL> from pyVim . connect import Disconnect <EOL> inputs = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def get_obj ( content , vimtype , name ) : <EOL> """<STR_LIT>""" <EOL> obj = None <EOL> container = content . viewManager . CreateContainerView ( content . rootFolder , vimtype , True ) <EOL> for c in container . view : <EOL> if c . name == name : <EOL> obj = c <EOL> break <EOL> return obj <EOL> def wait_for_task ( task , actionName = '<STR_LIT>' , hideResult = False ) : <EOL> """<STR_LIT>""" <EOL> while task . info . state == vim . TaskInfo . State . running : <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> if task . info . state == vim . TaskInfo . State . success : <EOL> if task . info . result is not None and not hideResult : <EOL> out = '<STR_LIT>' % ( actionName , task . info . result ) <EOL> print out <EOL> else : <EOL> out = '<STR_LIT>' % actionName <EOL> print out <EOL> else : <EOL> out = '<STR_LIT>' % ( actionName , task . info . error ) <EOL> raise task . info . error <EOL> print out <EOL> return task . info . result <EOL> def main ( ) : <EOL> try : <EOL> si = None <EOL> try : <EOL> print "<STR_LIT>" <EOL> si = connect . Connect ( inputs [ '<STR_LIT>' ] , <NUM_LIT> , inputs [ '<STR_LIT>' ] , inputs [ '<STR_LIT>' ] ) <EOL> except IOError , e : <EOL> pass <EOL> atexit . register ( Disconnect , si ) <EOL> print "<STR_LIT>" <EOL> content = si . RetrieveContent ( ) <EOL> datacenter = get_obj ( content , [ vim . Datacenter ] , inputs [ '<STR_LIT>' ] ) <EOL> vmFolder = datacenter . vmFolder <EOL> vm_name = inputs [ '<STR_LIT>' ] <EOL> vm = get_obj ( content , [ vim . VirtualMachine ] , vm_name ) <EOL> if vm . runtime . powerState != '<STR_LIT>' : <EOL> print "<STR_LIT>" <EOL> sys . exit ( ) <EOL> target_host = get_obj ( content , [ vim . HostSystem ] , inputs [ '<STR_LIT>' ] ) <EOL> relocate_spec = vim . vm . RelocateSpec ( ) <EOL> for datastore in target_host . datastore : <EOL> if datastore . summary . type == '<STR_LIT>' : <EOL> print "<STR_LIT>" % datastore . name <EOL> relocate_spec . datastore = datastore <EOL> break <EOL> relocate_spec . host = target_host <EOL> relocate_spec . pool = vm . resourcePool <EOL> cloneSpec = vim . vm . CloneSpec ( powerOn = False , template = False , location = relocate_spec ) <EOL> print "<STR_LIT>" <EOL> task = vm . Clone ( name = '<STR_LIT>' , folder = vmFolder , spec = cloneSpec ) <EOL> job_status = wait_for_task ( task , si ) <EOL> if job_status : <EOL> vmconf = vim . vm . ConfigSpec ( ) <EOL> vmconf . name = vm . config . name <EOL> vmconf . uuid = vm . config . uuid <EOL> print "<STR_LIT>" <EOL> task = vm . Destroy ( ) <EOL> wait_for_task ( task , si ) <EOL> print "<STR_LIT>" <EOL> new_vm = get_obj ( content , [ vim . VirtualMachine ] , '<STR_LIT>' ) <EOL> task = new_vm . ReconfigVM_Task ( vmconf ) <EOL> wait_for_task ( task , si ) <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" <EOL> except vmodl . MethodFault , e : <EOL> print "<STR_LIT>" % e . msg <EOL> return <NUM_LIT:1> <EOL> except Exception , e : <EOL> print "<STR_LIT>" % str ( e ) <EOL> return <NUM_LIT:1> <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
