Last active
April 8, 2022 06:32
-
-
Save VLogin/535bbe57d1c3cbb573930763452a45d2 to your computer and use it in GitHub Desktop.
[CreateSession] scripting task #vRO #scripting_task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var spanSet = {} | |
for each (var vm in allVms) { | |
var vmOpts = allSessions.filter(function(x){ return x.vmName == vm.name })[0] | |
System.log(vmOpts) | |
System.log(vmOpts.parentName) | |
for each (var dev in vm.config.hardware.device) { | |
if ( | |
!(dev instanceof VcVirtualVmxnet2) && | |
!(dev instanceof VcVirtualVmxnet3) && | |
!(dev instanceof VcVirtualE1000) | |
) continue | |
if (!dev.backing.port || !dev.backing.port.switchUuid) | |
continue | |
var dc = getDc(vm) | |
var dvsId = dev.backing.port.switchUuid | |
var hashKey = vmOpts.parentName + '/' + dvsId | |
if (!spanSet[hashKey]) { | |
spanSet[hashKey] = { | |
dvs: getDVS(dc, dev.backing.port.switchUuid), | |
visibleName: vmOpts.parentName, | |
ports: [], | |
type: vmOpts.type, | |
target: vmOpts.target | |
} | |
} | |
spanSet[hashKey].ports.push(dev.backing.port.portKey) | |
} // end of device loop | |
} // end of VMs loop | |
for each (var span in spanSet) { | |
System.log('Going to reconfigure DVSwitch: ' + span.dvs.uuid) | |
System.log('Span session visible name ' + span.visibleName + ' with type ' + span.type) | |
System.log('Span session sources: ' + span.ports.join(',')) | |
System.log('Span session destination: ' + span.target) | |
var srcPort = new VcVMwareVspanPort() | |
srcPort.portKey = span.ports | |
var spanSession = new VcVMwareVspanSession() | |
spanSession.name = span.visibleName | |
var dstPort = new VcVMwareVspanPort() | |
if (span.type == typePhy) { | |
dstPort.uplinkPortName = [ span.target ] | |
spanSession.sessionType = VcVMwareDVSVspanSessionType.remoteMirrorSource | |
spanSession.encapsulationVlanId = 1 | |
} | |
if (span.type == typeGRE) { | |
dstPort.ipAddress = [ span.target ] | |
// encapType has been introsuced in vsphere 6.5, but we still use 6.0 | |
// spanSession.encapType = VcVMwareDVSVspanSessionEncapType._gre | |
spanSession.sessionType = VcVMwareDVSVspanSessionType.encapsulatedRemoteMirrorSource | |
} | |
spanSession.enabled = true | |
spanSession.samplingRate = 1 | |
spanSession.mirroredPacketLength = packetLengthLimit | |
spanSession.stripOriginalVlan = true | |
spanSession.destinationPort = dstPort | |
spanSession.sourcePortReceived = srcPort | |
spanSession.sourcePortTransmitted = srcPort | |
// decide add new or update existing sessions | |
System.log('Looking for span session ' + span.visibleName) | |
var action = "add" | |
for each (var elem in span.dvs.config.vspanSession) { | |
if (elem.name == span.visibleName) { | |
System.log('Span session ' + span.visibleName + ' was found and will be updated') | |
spanSession.key = elem.key | |
action = "edit" | |
break | |
} | |
// (elem.encapType == spanSession.encapType) | |
if (JSON.stringify(elem.destinationPort.ipAddress) == JSON.stringify(spanSession.destinationPort.ipAddress)) { | |
System.log(JSON.stringify(elem.destinationPort.ipAddress)) | |
System.log(JSON.stringify(spanSession.destinationPort.ipAddress)) | |
System.log('Span session to the same destination was found and will be updated. Key is ' + elem.key) | |
spanSession.key = elem.key | |
action = "edit" | |
break | |
} | |
if (JSON.stringify(elem.destinationPort.uplinkPortName) == JSON.stringify(spanSession.destinationPort.uplinkPortName)) { | |
System.log(JSON.stringify(elem.destinationPort.uplinkPortName)) | |
System.log(JSON.stringify(spanSession.destinationPort.uplinkPortName)) | |
System.log('Span session to the same destination was found and will be updated. Key is ' + elem.key) | |
spanSession.key = elem.key | |
action = "edit" | |
break | |
} | |
} | |
var spanConfig = new VcVMwareDVSVspanConfigSpec() | |
spanConfig.operation = action | |
spanConfig.vspanSession = spanSession | |
var dvsConfig = new VcVMwareDVSConfigSpec() | |
dvsConfig.configVersion = span.dvs.config.configVersion | |
dvsConfig.vspanConfigSpec = [ spanConfig ] | |
var task = span.dvs.reconfigureDvs_Task(dvsConfig) | |
if (!task) { | |
throw "Cannot run reconfiguration task for " + span.dvs.uuid | |
} | |
System.log("Task " + task.id + " has started") | |
var maxPollCount = 30 | |
var count = 0 | |
while (['error', 'success'].indexOf(task.info.state.value) < 0) { | |
System.log("progress: " + task.info.progress) | |
// if (maxPollCount < ++count) { | |
// System.log('Task ' + task.id + ' has stuck. Cancelling...') | |
// task.cancelTask() | |
// throw "Task " + task.id + " execution timed out" | |
// } | |
System.sleep(10) | |
} | |
System.log("Task " + task.id + " has completed with status: " + task.info.state.value) | |
if (task.info.state.value != "success" ) { | |
throw "Task " + task.id + " failed. " + task.info.error.localizedMessage | |
} | |
} | |
// PODVAL | |
function getDc(mo) { | |
if (!mo || (mo instanceof VcDatacenter)) { | |
return mo | |
} | |
return getDc(mo.parent) | |
} | |
function getDVS(dc, swUuid) { | |
if (!dc || !(dc instanceof VcDatacenter)) { | |
throw "Datacenter must be defined to looking up DVS" | |
} | |
for each (var mo in dc.networkFolder.childEntity) { | |
if ( (mo instanceof VcVmwareDistributedVirtualSwitch) && (mo.uuid == swUuid)) { | |
return mo | |
} | |
} | |
throw "Switch with uuid " + swUuid + " not found in datacenter" + dc.name | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment