Python VirtualAppliancePredicates Example

说明

python virtualappliancepredicates示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Python

命名空间/包名称: orgjcloudsabiquopredicatescloud

示例#1
文件: 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()

示例#2
文件: 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()

展开阅读全文