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 try: cloud = self._context.getCloudService() vm = cloud.findVirtualMachine( VirtualMachinePredicates.internalName(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(self._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", 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 try: cloud = self._context.getCloudService() vm = cloud.findVirtualMachine( VirtualMachinePredicates.internalName(name)) if vm: vm = helper.deploy_vm(self._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 __change_state(self, state_name, new_state, args): """ Generic method to change the state of a virtual machine """ parser = OptionParser(usage="vm %s <options>" % state_name) parser.add_option("-n", "--name", dest="name", help="The name of the virtual machine to %s" % state_name) (options, args) = parser.parse_args(args) name = options.name if not name: parser.print_help() return try: cloud = self._context.getCloudService() vm = cloud.findVirtualMachine( VirtualMachinePredicates.internalName(name)) if vm: helper.change_state_vm(self._context, vm, new_state) pprint_vms([vm]) else: print "No virtual machine 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="vm", 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 try: volume = helper.find_volume(self._context, options.name) if not volume: print "No volume found with name: %s" % options.name return cloud = self._context.getCloudService() vm = cloud.findVirtualMachine( VirtualMachinePredicates.internalName(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(self._context, volume)]) except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()
def get_attached_vm(context, volume): """ Get the virtual machine where the volume is attached """ # TODO: Add parent navigation in jclouds.abiquo link = volume.unwrap().searchLink("virtualmachine") if link: name = link.getTitle() cloud = context.getCloudService() return cloud.findVirtualMachine( VirtualMachinePredicates.internalName(name))
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("-t", "--template", dest="template", help="The name of the template") 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 template = options.template if not name and not template: parser.print_help() return # Once user input has been read, find the virtual machine try: cloud = self._context.getCloudService() if name: # Find by name vm = cloud.findVirtualMachine( VirtualMachinePredicates.internalName(name)) if vm: pprint_vms([vm], options.verbose) else: print "No virtual machine found with name: %s" % name else: # Find by template vms = cloud.listVirtualMachines() vmts = filter( lambda vm: template in vm.getTemplate().getName(), vms) if len(vmts) == 0: print("No virtual machine found " "matching template: %s" % template) else: pprint_vms(vmts, options.verbose) 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="vm", 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 try: volume = helper.find_volume(self._context, options.name) if not volume: print "No volume found with name: %s" % options.name return cloud = self._context.getCloudService() vm = cloud.findVirtualMachine( VirtualMachinePredicates.internalName(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..." disks = list(vm.listVirtualDisks()) disks.append(volume) vm.setVirtualDisks(disks) pprint_volumes([helper.refresh_volume(self._context, volume)]) 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("-t", "--template", dest="template", help="The name of the template") 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 template = options.template if not name and not template: parser.print_help() return # Once user input has been read, find the virtual machine try: cloud = self._context.getCloudService() if name: # Find by name vm = cloud.findVirtualMachine( VirtualMachinePredicates.internalName(name)) if vm: pprint_vms([vm], options.verbose) else: print "No virtual machine found with name: %s" % name else: # Find by template vms = cloud.listVirtualMachines() vmts = filter(lambda vm: template in vm.getTemplate().getName(), vms) if len(vmts) == 0: print ("No virtual machine found " "matching template: %s" % template) else: pprint_vms(vmts, options.verbose) except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()
def undeploy(self, args): """ Undeploy an existing virtual machine given its name """ # Parse user input to get the name of the virtual machine parser = OptionParser(usage="vm undeploy <options>") parser.add_option("-n", "--name", dest="name", help="The name of the virtual machine to undeploy") (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 try: cloud = self._context.getCloudService() vm = cloud.findVirtualMachine( VirtualMachinePredicates.internalName(name)) if vm: vm = helper.undeploy_vm(self._context, vm) pprint_vms([vm]) else: print "No virtual machine found with name: %s" % name except (AbiquoException, AuthorizationException), ex: print "Error: %s" % ex.getMessage()