Exemplo n.º 1
0
# service "NSX Manager"

import os
import time
import json
from vCenterCommon import deleteVMs, vmPower

with open(r'c:\ProgramData\QualiSystems\Shells.log', 'a') as f:
    f.write(
        time.strftime('%Y-%m-%d %H:%M:%S') + ': ' +
        __file__.split('\\')[-1].replace('.py', '') + ': ' + str(os.environ) +
        '\r\n')

resource = json.loads(os.environ['RESOURCECONTEXT'])
resource_name = resource['name']
attrs = resource['attributes']

vcenter_ip = attrs['vCenter IP']
vcenter_user = attrs['vCenter Administrator Username']
vcenter_password = attrs['vCenter Administrator Password']

#remove all vms
vm_name = attrs['NSX VM Name']
vmPower(vm_name, 'stop', vcenter_ip, vcenter_user, vcenter_password)
deleteVMs(vm_name, vcenter_ip, vcenter_user, vcenter_password)

vm_name = attrs['Controller Name']
vmPower(vm_name, 'stop', vcenter_ip, vcenter_user, vcenter_password)
deleteVMs(vm_name, vcenter_ip, vcenter_user, vcenter_password)
# Add Gateway to the dictionary
siodic['Gateway'] = [svm4_mgmt_ip, svm4_data1_ip, svm4_data2_ip, gatewayesx[1], gatewayesx[0]]

# Add UUID attribute to the VMs
notify_user(api, reservationId, "Power on VMs")
try:
    for name in siodic:
        _vm_name = vm_name + '-' + name
        script = '''Add-PSSnapin VMware.VimAutomation.Core\n
        Connect-VIServer -Server ''' + vcenter_ip + ''' -User ''' + vcenter_user + ''' -Password ''' + vcenter_password + ''' -WarningAction SilentlyContinue\n
        $v = Get-VM -Name ''' + _vm_name + '''
        New-AdvancedSetting -Entity $v -Name disk.EnableUUID -Value TRUE -Confirm:$false -Force:$true
        '''
        powershell(script)
        vmPower(_vm_name, 'start', vcenter_ip, vcenter_user, vcenter_password)
except Exception, e:
    print e
    with open(r'c:\ProgramData\QualiSystems\Shells.log', 'a') as f:
        f.write(time.strftime('%Y-%m-%d %H:%M:%S') + ' Got Error: ' + str(e) + '\r\n')
    exit(1)

time.sleep(60)
notify_user(api, reservationId, "Configure VMs network")
try:
    for name in siodic:
        _vm_name = vm_name + '-' + name
        # Reconfigure VM NICs for Data network
        changeVMadapter(_vm_name, ("2", "3"), (sio_data1_portgroup, sio_data2_portgroup), vcenter_ip, vcenter_user, vcenter_password)
        # Configure SVM ETHs
        if siodic[name][0] != '':