def delete(self, args): """ Delete a virtual machine given its name. """ # Parse user input to get the name of the virtual machine parser = OptionParser(usage="vm delete <options>") parser.add_option("-n", "--name", dest="name", help="The name of the virtual machine to delete") parser.add_option( "-u", "--undeploy", dest="undeploy", action="store_true", help="undeploy the virtual machine before deleting it", ) (options, args) = parser.parse_args(args) name = options.name if not name: parser.print_help() return context = ContextLoader().load() try: cloud = context.getCloudService() vm = cloud.findVirtualMachine(VirtualMachinePredicates.name(name)) if vm: state = vm.getState() if not options.undeploy and state.existsInHypervisor(): print ("Virtual machine is deployed. " "Undeploy it before deleting.") elif options.undeploy and state.existsInHypervisor(): vm = helper.undeploy_vm(context, vm) vm.delete() else: vm.delete() else: print "No virtual machine found with name: %s" % name except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()
def deploy(self, args): """ Deploy an existing virtual machine given its name. """ # Parse user input to get the name of the virtual machine parser = OptionParser(usage="vm deploy <options>") parser.add_option("-n", "--name", help="The name of the virtual machine to deploy", action="store", dest="name") (options, args) = parser.parse_args(args) name = options.name if not name: parser.print_help() return # Once user input has been read, find the VM context = ContextLoader().load_context() try: cloud = context.getCloudService() monitor = context.getMonitoringService().getVirtualMachineMonitor() vm = cloud.findVirtualMachine(VirtualMachinePredicates.name(name)) if vm: print "Deploying virtual machine %s... This may take some time." % name vm.deploy() monitor.awaitCompletionDeploy(vm) else: print "No virtual machine found with name: %s" % name except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()
def find(self, args): """ Find a virtual machine given its name. """ # Parse user input to get the name of the virtual machine parser = OptionParser(usage="vm find <options>") parser.add_option("-n", "--name", dest="name", help="The name of the virtual machine to find") parser.add_option( "-v", "--verbose", dest="verbose", action="store_true", help="Show virtual machine extended information" ) (options, args) = parser.parse_args(args) name = options.name if not name: parser.print_help() return # Once user input has been read, find the virtual machine context = ContextLoader().load() try: cloud = context.getCloudService() vm = cloud.findVirtualMachine(VirtualMachinePredicates.name(name)) if vm: pprint_vms([vm], options.verbose) else: print "No virtual machine found with name: %s" % name except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()
def list(self, args): """ List all virtual machines. """ context = ContextLoader().load_context() try: cloud = context.getCloudService() vms = cloud.listVirtualMachines() pprint_vms(vms) except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()
def list(self, args): """ List all available virtual datacenters. """ context = ContextLoader().load() try: cloud = context.getCloudService() vdcs = cloud.listVirtualDatacenters() pprint_vdcs(vdcs) except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()
def list(self, args): """ List all available volumes. """ context = ContextLoader().load() try: cloud = context.getCloudService() vdcs = cloud.listVirtualDatacenters() volumes = [] [volumes.extend(vdc.listVolumes()) for vdc in vdcs] pprint_volumes(volumes) except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()
def start(self, args): """ Deploys and undeploys the first virtual appliance N times. """ # Parse user input to get the number of deployments and undeployments parser = OptionParser(usage="deployer start <options>") parser.add_option("-n", "--num", dest="num", help="The number of deployments to execute") (options, args) = parser.parse_args(args) if not options.num: parser.print_help() return # Once user input has been read, find the VM max = int(options.num) context = ContextLoader().load() try: cloud = context.getCloudService() monitor = context.getMonitoringService() \ .getVirtualApplianceMonitor() vdc = cloud.listVirtualDatacenters()[0] vapp = vdc.listVirtualAppliances()[0] num_vms = len(vapp.listVirtualMachines()) print "Starting %s deployment iterations for %s (%s vms)" % (max, vapp.getName(), num_vms) for i in range(0, max): print "Iteration #%s" % (i + 1) print " Deploying %s (%s vms)" % (vapp.getName(), num_vms) vapp.deploy() monitor.awaitCompletionDeploy(vapp) # Bypass current issues with state by waiting a bit # before undeploying time.sleep(5) print " Undeploying %s (%s vms)" % (vapp.getName(), num_vms) vapp.undeploy() monitor.awaitCompletionUndeploy(vapp) # Currently there is a minor issue when undeploying that puts # the vm in state UNKNOWN. Wait a bit so it gets in # NOT_ALLOCATED state before deploying again time.sleep(5) except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()
def deploy(self, args): """ Deploy an existing virtual machine given its name. """ # Parse user input to get the name of the virtual machine parser = OptionParser(usage="vm deploy <options>") parser.add_option("-n", "--name", dest="name", help="The name of the virtual machine to deploy") (options, args) = parser.parse_args(args) name = options.name if not name: parser.print_help() return # Once user input has been read, find the VM context = ContextLoader().load() try: cloud = context.getCloudService() vm = cloud.findVirtualMachine(VirtualMachinePredicates.name(name)) if vm: vm = helper.deploy_vm(context, vm) pprint_vms([vm]) else: print "No virtual machine found with name: %s" % name except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()
def find(self, args): """ Find a virtual machine given its name. """ # Parse user input to get the name of the virtual machine parser = OptionParser(usage="vm find <options>") parser.add_option("-n", "--name", help="The name of the virtual machine to find", action="store", dest="name") parser.add_option("-v", "--verbose", help="Show virtual machine extended information", action="store_true", dest="verbose") (options, args) = parser.parse_args(args) name = options.name if not name: parser.print_help() return # Once user input has been read, find the virtual machine context = ContextLoader().load_context() try: cloud = context.getCloudService() vm = cloud.findVirtualMachine(VirtualMachinePredicates.name(name)) if vm: pprint_vms([vm]) if options.verbose: print "Found virtual machine in: " print " %s" % vm.getVirtualAppliance() print " %s" % vm.getVirtualDatacenter() print " %s" % vm.getEnterprise() if vm.getState().existsInHypervisor(): admin = context.getAdministrationService() machine = admin.findMachine(MachinePredicates.ip(vm.getVncAddress())) print " %s" % machine else: print " Machine [None (VM not deployed)]" else: print "No virtual machine found with name: %s" % name except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()
def find(self, args): """ Find a virtual datacenter given its name. """ # Parse user input to get the name of the virtual datacenter parser = OptionParser(usage="vdc find <options>") parser.add_option("-n", "--name", help="The name of the virtual datacenter to find", dest="name") (options, args) = parser.parse_args(args) name = options.name if not name: parser.print_help() return # Once user input has been read, find the virtual datacenter context = ContextLoader().load() try: cloud = context.getCloudService() vdc = cloud.findVirtualDatacenter( VirtualDatacenterPredicates.name(name)) if vdc: pprint_vdcs([vdc]) else: print "No virtual datacenter found with name: %s" % name except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()
def attach(self, args): """ Attach a volume to the given virtual machine. """ parser = OptionParser(usage="volume attach <options>") parser.add_option("-n", "--name", dest="name", help="The name of the volume to attach") parser.add_option("-v", "--vm", dest="name", help=("The name of the virtual machine " "where the volume will be attached")) (options, args) = parser.parse_args(args) if not options.name or not options.vm: parser.print_help() return context = ContextLoader().load() try: volume = helper.find_volume(context, options.name) if not volume: print "No volume found with name: %s" % options.name return cloud = context.getCloudService() vm = cloud.findVirtualMachine( VirtualMachinePredicates.name(options.vm)) if not vm: print "No virtual machine found with name: %s" % options.vm return log.debug("Attaching volume %s to %s..." % (options.name, options.vm)) if vm.getState().existsInHypervisor(): print "Attaching volume to a running virtual machine.", print "This may take some time..." vm.attachVolumes(volume) pprint_volumes([helper.refresh_volume(context, volume)]) except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()