예제 #1
0
    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()
예제 #2
0
파일: vm.py 프로젝트: abelboldu/kahuna
    def find_by_template(self, args):
        """ Find virtual machine(s) given a template """
        # Parse user input to get the name of the virtual machine
        parser = OptionParser(usage="vm find_by_template <options>")
        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)
        template = options.template
        if not template:
            parser.print_help()
            return

        # Once user input has been read, find the virtual machine
        try:
            cloud = self._context.getCloudService()
            vms = cloud.listVirtualMachines()
            vmts = []
            for vm in vms:
                if template in vm.getTemplate().getName():
                    vmts.append(vm)
            if not vmts:
                print "No virtual machine found with template: %s" % template
            else:
                pprint_vms(vmts,options.verbose)
        except (AbiquoException, AuthorizationException), ex:
            print "Error: %s" % ex.getMessage()
예제 #3
0
    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()
예제 #4
0
    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()
예제 #5
0
파일: vm.py 프로젝트: abelboldu/kahuna
    def create(self, args):
        """ Creates a virtual machine based on a given template """
        # Parse user input to get the name of the virtual machine
        parser = OptionParser(usage="vm create <options>")
        parser.add_option("-t", "--template-id", dest="template",
                type="int", help="The id of the template to use")
        parser.add_option("-c", "--cpu", dest="cpu", type="int",
                help="The number of cores")
        parser.add_option("-r", "--ram", dest="ram", type="int",
                help="The RAM in MB")
        parser.add_option("-d", "--deploy", dest="deploy",
                action="store_true",
                help="Deploy the virtual machine after creating it")
        (options, args) = parser.parse_args(args)
        if not options.template:
            parser.print_help()
            return

        try:
            api_context = self._context.getApiContext()
            template = helper.find_template_by_id(self._context,
                    options.template)
            if not template:
                print "No template was found with id %s" % options.template
                return
            log.debug("Using template: %s" % template.getName())

            vdc = helper.get_virtual_datacenter_for_template(self._context,
                    template)
            if not vdc:
                print ("Could not create a compatible virtual datacenter "
                    "for %s") % template.getName()
                return
            log.debug("Using virtual datacenter: %s" % vdc.getName())

            name = "Kahuna-" + api_context.getIdentity()
            vapp = vdc.findVirtualAppliance(
                    VirtualAppliancePredicates.name(name))
            if not vapp:
                log.debug(("Virtual appliance %s not found. "
                    "Creating it...") % name)
                vapp = VirtualAppliance.builder(api_context, vdc) \
                        .name(name) \
                        .build()
                vapp.save()

            builder = VirtualMachine.builder(api_context, vapp, template)
            if options.cpu:
                builder.cpu(options.cpu)
            if options.ram:
                builder.ram(options.ram)
            vm = builder.build()
            vm.save()

            if options.deploy:
                vm = helper.deploy_vm(self._context, vm)

            pprint_vms([vm])
        except (AbiquoException, AuthorizationException), ex:
            print "Error: %s" % ex.getMessage()
예제 #6
0
 def list(self, args):
     """ List all virtual machines """
     try:
         cloud = self._context.getCloudService()
         vms = cloud.listVirtualMachines()
         pprint_vms(vms)
     except (AbiquoException, AuthorizationException), ex:
         print "Error: %s" % ex.getMessage()
예제 #7
0
파일: vm.py 프로젝트: abelboldu/kahuna
 def list(self, args):
     """ List all virtual machines """
     try:
         cloud = self._context.getCloudService()
         vms = cloud.listVirtualMachines()
         pprint_vms(vms)
     except (AbiquoException, AuthorizationException), ex:
         print "Error: %s" % ex.getMessage()
예제 #8
0
    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()
예제 #9
0
파일: vm.py 프로젝트: abelboldu/kahuna
    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.name(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()
예제 #10
0
파일: vm.py 프로젝트: ssedano/kahuna
    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()
예제 #11
0
파일: vm.py 프로젝트: rubiojr/kahuna
    def create(self, args):
        """ Creates a virtual machine based on a given template. """
        # Parse user input to get the name of the virtual machine
        parser = OptionParser(usage="vm create <options>")
        parser.add_option("-t", "--template-id", help="The id of the template to use",
                type="int", action="store", dest="template")
        (options, args) = parser.parse_args(args)
        if not options.template:
            parser.print_help()
            return

        context = ContextLoader().load_context()
        try:
            template = helper.find_template_by_id(context, options.template)
            if not template:
                print "No template was found with id %s" % options.template
                return
            log.debug("Using template: %s" % template.getName())

            vdc = helper.find_compatible_virtual_datacenter(context, template)
            if not vdc:
                print "No virtual datacenter found for: %s" % template.getDiskFormatType()
                return
            log.debug("Using virtual datacenter: %s" % vdc.getName())

            name = "Kahuna-" + context.getIdentity()
            vapp = vdc.findVirtualAppliance(VirtualAppliancePredicates.name(name))
            if not vapp:
                log.debug("Virtual appliance %s not found. Creating it..." % name)
                vapp = VirtualAppliance.builder(context, vdc).name(name).build()
                vapp.save()

            vm = VirtualMachine.builder(context, vapp, template).build()
            vm.save()

            pprint_vms([vm])
        except (AbiquoException, AuthorizationException), ex:
            print "Error: %s" % ex.getMessage()
예제 #12
0
    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()
예제 #13
0
파일: vm.py 프로젝트: fmontserrat/kahuna
    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()
예제 #14
0
    def create(self, args):
        """ Creates a virtual machine based on a given template """
        # Parse user input to get the name of the virtual machine
        parser = OptionParser(usage="vm create <options>")
        parser.add_option("-t",
                          "--template-id",
                          dest="template",
                          type="int",
                          help="The id of the template to use")
        parser.add_option("-c",
                          "--cpu",
                          dest="cpu",
                          type="int",
                          help="The number of cores")
        parser.add_option("-r",
                          "--ram",
                          dest="ram",
                          type="int",
                          help="The RAM in MB")
        parser.add_option("-d",
                          "--deploy",
                          dest="deploy",
                          action="store_true",
                          help="Deploy the virtual machine after creating it")
        (options, args) = parser.parse_args(args)
        if not options.template:
            parser.print_help()
            return

        try:
            api_context = self._context.getApiContext()
            template = helper.find_template_by_id(self._context,
                                                  options.template)
            if not template:
                print "No template was found with id %s" % options.template
                return
            log.debug("Using template: %s" % template.getName())

            vdc = helper.get_virtual_datacenter_for_template(
                self._context, template)
            if not vdc:
                print(
                    "Could not create a compatible virtual datacenter "
                    "for %s") % template.getName()
                return
            log.debug("Using virtual datacenter: %s" % vdc.getName())

            name = "Kahuna-" + api_context.getIdentity()
            vapp = vdc.findVirtualAppliance(
                VirtualAppliancePredicates.name(name))
            if not vapp:
                log.debug(("Virtual appliance %s not found. "
                           "Creating it...") % name)
                vapp = VirtualAppliance.builder(api_context, vdc) \
                        .name(name) \
                        .build()
                vapp.save()

            builder = VirtualMachine.builder(api_context, vapp, template)
            if options.cpu:
                builder.cpu(options.cpu)
            if options.ram:
                builder.ram(options.ram)
            vm = builder.build()
            vm.save()

            if options.deploy:
                vm = helper.deploy_vm(self._context, vm)

            pprint_vms([vm])
        except (AbiquoException, AuthorizationException), ex:
            print "Error: %s" % ex.getMessage()