Exemple #1
0
    def get_floating_ip(cls, cloudname, floating_ip_or_id):
        """
        Method to get floating ip info
        :param cloudname:
        :param floating_ip_or_id:
        :return: floating ip info
        """
        try:
            cloud_provider = CloudProvider(cloudname).provider
            result = None

            # check if argument is ip or uuid
            if cls.isIPAddr(ip_or_id=floating_ip_or_id):
                # get floating ip list
                floating_ips = cls.get_floating_ip_list(cloudname)
                for floating_ip in floating_ips.values():
                    ip_addr = floating_ip["ip"]

                    # if argument ip matches floating ip addr
                    if ip_addr == floating_ip_or_id:
                        result = floating_ip
                        break
            else:
                # find by floating ip uuid
                result = cloud_provider.get_floating_ip(floating_ip_id=floating_ip_or_id)

            # Could not find floating IP from given args
            if result is None:
                return None

            instance_id = result["instance_id"]
            instance_name = None

            if instance_id is not None:
                # lookup instance_name from id
                instance_name = cls.find_instance_name(cloudname=cloudname, instance_id=instance_id)

            # add instance_name to dict
            result["instance_name"] = instance_name

            # add misc details to response
            result["cloud"] = cloudname
            result["user"] = cloud_provider.cloud_details["credentials"]["OS_USERNAME"]
            result["project"] = cloud_provider.cloud_details["credentials"]["OS_TENANT_NAME"]

            return attribute_printer(result, header=["name", "value"])
        except Exception as ex:
            # auto detect floating-ip-id
            floating_ips = cls.get_floating_ip_list(cloudname)
            # for each floating-ip from list
            for floating_ip in floating_ips.values():
                if floating_ip["id"].startswith(floating_ip_or_id) or floating_ip["ip"].startswith(floating_ip_or_id):
                    # confirm choice with user
                    print("Did you mean floating-ip [{}] ? (y/n)".format(floating_ip["ip"]))
                    choice = raw_input().lower()
                    # if yes, return dict
                    if choice == "y":
                        return attribute_printer(floating_ip, header=["name", "value"])
                        # Console.error(ex.message)
        return
Exemple #2
0
 def stop(cls, **kwargs):
     arg = dotdict(kwargs)
     cloud_provider = CloudProvider(arg.cloud).provider
     for server in kwargs["servers"]:
         cloud_provider.stop_vm(server)
         Console.ok("Machine {:} is being stopped on {:} Cloud...".format(
             server, cloud_provider.cloud))
Exemple #3
0
    def test_003(self):
        """
        test vm list
        :return:
        """
        HEADING()

        from pprint import pprint

        cloud = "chameleon-ec2"
        provider = CloudProvider(cloud).provider

        print(provider, type(provider))

        # pprint (provider.__dict__)
        # pprint (dir(provider))

        #r = provider.list_flavor(cloud)
        #pprint(r)

        for kind in [
                "image"
        ]:  # ["image", "vm", "flavor", "key"]: # , "flavor", "vm", "limits", "quota"]:
            r = provider.list(kind, cloud)
            pprint(r)

        assert True
Exemple #4
0
    def test_002(self):
        """ test flavor list :return:  """
        HEADING()

        from pprint import pprint

        cloud = "chameleon-ec2"
        provider = CloudProvider(cloud).provider

        # print (provider, type(provider))

        # r = provider.list_flavor(cloud)
        # pprint(r)

        kind = 'flavor'

        r = provider.list(kind, cloud)
        pprint(r)

        assert 't2.small' in str(r)

        r = provider.list_flavor(cloud)
        pprint(r)

        assert 't2.small' in str(r)

        r = provider.provider.list_sizes(cloud)
        pprint(r)

        assert 't2.small' in str(r)
Exemple #5
0
    def delete_rule(cls, cloud, secgroup, from_port, to_port, protocol, cidr):
        try:
            args = {
                "groupid": secgroup.uuid,
                "fromPort": from_port,
                "toPort": to_port,
                "protocol": protocol,
                "cidr": cidr
            }

            rule = cls.cm_db.find("secgrouprule", output="object",
                                  **args).first()

            if rule is not None:
                # get the nova client for cloud
                cloud_provider = CloudProvider(cloud).provider
                # delete the rule from the cloud
                cloud_provider.delete_secgroup_rule(rule.uuid)
                # delete the local db record
                cls.cm_db.delete(rule)
                return "Rule [{} | {} | {} | {}] deleted" \
                    .format(from_port, to_port, protocol, cidr)
            else:
                return None

        except Exception as ex:
            Console.error(ex.message, ex)

        return
Exemple #6
0
    def delete_rule(cls, cloud, secgroup, from_port, to_port, protocol, cidr):
        try:
            args = {
                "group": secgroup["uuid"],
                "fromPort": from_port,
                "toPort": to_port,
                "protocol": protocol,
                "cidr": cidr
            }

            rule = cls.cm.find(kind="secgrouprule",
                               output="object",
                               scope="first",
                               **args)

            if rule is not None:
                # get the nova client for cloud
                cloud_provider = CloudProvider(cloud).provider
                # delete the rule from the cloud
                cloud_provider.delete_secgroup_rule(rule.uuid)
                # delete the local db record
                cls.cm.delete(rule)
                return "Rule [{fromPort} | {toPort} | {protocol} | {cidr}] deleted" \
                    .format(**args)
            else:
                return None

        except Exception as ex:
            Console.error("delete rule")

        return
Exemple #7
0
    def delete(cls,
               category='general',
               group=None,
               name=None):
        # name is anme of the rule

        if category=='general':
            if name is None and group is not None:

                # delete the entire group
                cls.cm.delete(kind="secgrouprule", group=group)


            elif name is not None and group is not None:
                # delete specific rule
                cls.cm.delete(name=name, kind="secgrouprule", group=group)
            elif name is None and group is None:
                # delete all groups
                cls.cm.delete(kind="secgrouprule")

            if group == Default.secgroup or Default.secgroup is None:
                cls.reset_defaults()

        else:
            provider = CloudProvider(category).provider

            # delete on cloud
            if  group is not None:
                provider.delete_secgroup(name)
                # delete the entire group
            elif group is None:
                # delete all groups
                pass
Exemple #8
0
    def delete_rule(cls, cloud, secgroup, from_port, to_port, protocol, cidr):
        try:
            args = {
                "groupid": secgroup.uuid,
                "fromPort": from_port,
                "toPort": to_port,
                "protocol": protocol,
                "cidr": cidr
            }

            rule = cls.cm_db.find("secgrouprule", output="object",
                                  **args).first()

            if rule is not None:
                # get the nova client for cloud
                cloud_provider = CloudProvider(cloud).provider
                # delete the rule from the cloud
                cloud_provider.delete_secgroup_rule(rule.uuid)
                # delete the local db record
                cls.cm_db.delete(rule)
                return "Rule [{} | {} | {} | {}] deleted" \
                    .format(from_port, to_port, protocol, cidr)
            else:
                return None

        except Exception as ex:
            Console.error(ex.message, ex)

        return
Exemple #9
0
    def delete_rule(cls, cloud, secgroup, from_port, to_port, protocol, cidr):
        try:
            args = {
                "group": secgroup["uuid"],
                "fromPort": from_port,
                "toPort": to_port,
                "protocol": protocol,
                "cidr": cidr
            }

            rule = cls.cm.find(kind="secgrouprule",
                               output="object",
                               scope="first",
                               **args)

            if rule is not None:
                # get the nova client for cloud
                cloud_provider = CloudProvider(cloud).provider
                # delete the rule from the cloud
                cloud_provider.delete_secgroup_rule(rule.uuid)
                # delete the local db record
                cls.cm.delete(rule)
                return "Rule [{fromPort} | {toPort} | {protocol} | {cidr}] deleted" \
                    .format(**args)
            else:
                return None

        except Exception as ex:
            Console.error("delete rule")

        return
Exemple #10
0
    def boot(cls, **kwargs):
        cloud_provider = CloudProvider(kwargs["cloud"]).provider

        if kwargs["cloud"] == 'kilo':
            # For kilo, we use a different network interface
            net_id = None

            # Get list of networks in kilo
            network_list = cloud_provider.provider.networks.list()

            # Validate each interface
            for network in network_list:
                # Check if interface name is fg478-net,
                # Get the interface id
                if network.label == "fg478-net":
                    net_id = network.id

            nics = [{"net-id": net_id}]
            # TODO: BUG: nics seems unexpected argument
            vm_id = cloud_provider.boot_vm(kwargs["name"],
                                           kwargs["image"],
                                           kwargs["flavor"],
                                           key=kwargs["key_name"],
                                           secgroup=kwargs["secgroup_list"],
                                           nics=nics)
        else:
            vm_id = cloud_provider.boot_vm(kwargs["name"], kwargs["image"], kwargs["flavor"], key=kwargs["key_name"],
                                           secgroup=kwargs["secgroup_list"])
        print("Machine {:} is being booted on {:} Cloud...".format(kwargs["name"], cloud_provider.cloud))

        # Explicit refresh called after VM boot, to update db.
        # cls.refresh(cloud=kwargs["cloud"])

        return vm_id
Exemple #11
0
 def stop(cls, **kwargs):
     arg = dotdict(kwargs)
     cloud_provider = CloudProvider(arg.cloud).provider
     for server in kwargs["servers"]:
         cloud_provider.stop_vm(server)
         Console.ok("Machine {:} is being stopped on {:} Cloud..."
                    .format(server, cloud_provider.cloud))
    def test_002(self):
        """ test flavor list :return:  """
        HEADING()

        from pprint import pprint

        cloud = "chameleon-ec2"
        provider = CloudProvider(cloud).provider

        # print (provider, type(provider))

        # r = provider.list_flavor(cloud)
        # pprint(r)

        kind = 'flavor'

        r = provider.list(kind, cloud)
        pprint(r)

        assert 't2.small' in str(r)

        r = provider.list_flavor(cloud)
        pprint(r)

        assert 't2.small' in str(r)

        r = provider.provider.list_sizes(cloud)
        pprint(r)

        assert 't2.small' in str(r)
Exemple #13
0
    def delete(cls,
               category='general',
               group=None,
               name=None):
        # name is anme of the rule

        if category=='general':
            if name is None and group is not None:

                # delete the entire group
                cls.cm.delete(kind="secgrouprule", group=group)


            elif name is not None and group is not None:
                # delete specific rule
                cls.cm.delete(name=name, kind="secgrouprule", group=group)
            elif name is None and group is None:
                # delete all groups
                cls.cm.delete(kind="secgrouprule")

            if group == Default.secgroup or Default.secgroup is None:
                cls.reset_defaults()

        else:
            provider = CloudProvider(category).provider

            # delete on cloud
            if  group is not None:
                provider.delete_secgroup(name)
                # delete the entire group
            elif group is None:
                # delete all groups
                pass
Exemple #14
0
 def delete_secgroup(cls, name=None, cloud=None):
     try:
         # Find the secgroup from the cloud
         cloud_provider = CloudProvider(cloud).provider
         result = cloud_provider.delete_secgroup(name)
         return result
     except Exception as ex:
         Console.error("delete group")
Exemple #15
0
 def delete_all_rules_cloud(cls, cloud, groupname):
     rules = cls.list_rules_cloud(cloud, groupname)
     provider = CloudProvider(cloud).provider
     if rules:
         for rule in rules:
             ruleid = rule['id']
             provider.delete_secgroup_rule(ruleid)
     return
Exemple #16
0
    def delete(cls, **kwargs):
        cloud_provider = CloudProvider(kwargs["cloud"]).provider
        for server in kwargs["servers"]:
            cloud_provider.delete_vm(server)
            print("Machine {:} is being deleted on {:} Cloud...".format(server, cloud_provider.cloud))

            # Explicit refresh called after VM delete, to update db.
            cls.refresh(cloud=kwargs["cloud"])
Exemple #17
0
 def delete_all_rules_cloud(cls, cloud, groupname):
     rules = cls.list_rules_cloud(cloud, groupname)
     provider = CloudProvider(cloud).provider
     if rules:
         for rule in rules:
             ruleid = rule['id']
             provider.delete_secgroup_rule(ruleid)
     return
Exemple #18
0
 def delete_secgroup(cls, label, cloud):
     try:
         # Find the secgroup from the cloud
         cloud_provider = CloudProvider(cloud).provider
         result = cloud_provider.delete_secgroup(label)
         return result
     except Exception as ex:
         Console.error(ex.message, ex)
Exemple #19
0
    def delete(cls, **kwargs):
        cloud_provider = CloudProvider(kwargs["cloud"]).provider
        for server in kwargs["servers"]:
            cloud_provider.delete_vm(server)
            print("Machine {:} is being deleted on {:} Cloud...".format(server, cloud_provider.cloud))

            # Explicit refresh called after VM delete, to update db.
            cls.refresh(cloud=kwargs["cloud"])
Exemple #20
0
 def delete_secgroup(cls, name=None, cloud=None):
     try:
         # Find the secgroup from the cloud
         cloud_provider = CloudProvider(cloud).provider
         result = cloud_provider.delete_secgroup(name)
         return result
     except Exception as ex:
         Console.error("delete group")
Exemple #21
0
 def list_rules_cloud(cls, cloud, groupname):
     provider = CloudProvider(cloud).provider
     groups = provider.list_secgroup(cloud)
     for id in groups:
         group = groups[id]
         if groupname == group["name"]:
             return group["rules"]
     return None
Exemple #22
0
 def delete_secgroup(cls, label, cloud):
     try:
         # Find the secgroup from the cloud
         cloud_provider = CloudProvider(cloud).provider
         result = cloud_provider.delete_secgroup(label)
         return result
     except Exception as ex:
         Console.error(ex.message, ex)
Exemple #23
0
 def list_rules_cloud(cls, cloud, groupname):
     provider = CloudProvider(cloud).provider
     groups = provider.list_secgroup(cloud)
     for id in groups:
         group = groups[id]
         if groupname == group["name"]:
             return group["rules"]
     return None
Exemple #24
0
    def rename(cls, **kwargs):
        cloud_provider = CloudProvider(kwargs["cloud"]).provider
        new_name = kwargs["new_name"]
        for server in kwargs["servers"]:
            cloud_provider.rename_vm(server, new_name)
            print("Machine {:} renamed to {:} on {:} Cloud...".format(server, new_name, cloud_provider.cloud))

            # Explicit refresh called after VM delete, to update db.
            cls.refresh(cloud=kwargs["cloud"])
Exemple #25
0
 def get_group_cloud(cls, cloud, groupname):
     provider = CloudProvider(cloud).provider
     groups = provider.list_secgroup(cloud)
     ret = None
     for groupkey in groups:
         group = groups[groupkey]
         if group["name"] == groupname:
             ret = group
             break
     return ret
Exemple #26
0
 def delete_rule_cloud(cls, cloud, groupname, rulename):
     ret = None
     provider = CloudProvider(cloud).provider
     ruleid = cls.get_rule_cloud(cloud, groupname, rulename)
     if ruleid:
         ret = provider.delete_secgroup_rule(ruleid)
     #else:
     #    Console.error("Rule does not exist - Rule:{}, Group:{}"\
     #                  .format(rulename, groupname), traceflag=False)
     return ret
Exemple #27
0
 def get_group_cloud(cls, cloud, groupname):
     provider = CloudProvider(cloud).provider
     groups = provider.list_secgroup(cloud)
     ret = None
     for groupkey in groups:
         group = groups[groupkey]
         if group["name"] == groupname:
             ret = group
             break
     return ret
Exemple #28
0
 def delete_rule_cloud(cls, cloud, groupname, rulename):
     ret = None
     provider = CloudProvider(cloud).provider
     ruleid = cls.get_rule_cloud(cloud, groupname, rulename)
     if ruleid:
         ret = provider.delete_secgroup_rule(ruleid)
     #else:
     #    Console.error("Rule does not exist - Rule:{}, Group:{}"\
     #                  .format(rulename, groupname), traceflag=False)
     return ret
Exemple #29
0
 def get_floating_ip_list(cls, cloudname):
     """
     Method to get the floating IP list
     :param cloudname:
     :return: floating_ips
     """
     try:
         cloud_provider = CloudProvider(cloudname).provider
         floating_ips = cloud_provider.list_floating_ips()
         return floating_ips
     except Exception as ex:
         Console.error(ex.message, ex)
Exemple #30
0
 def delete_all_rules_cloud(cls, cloud, groupname):
     rules = cls.list_rules_cloud(cloud, groupname)
     provider = CloudProvider(cloud).provider
     if rules:
         for rule in rules:
             ruleid = rule['id']
             # only refresh those defined with a protocol
             # This leaves the default rule defined by
             # allowing the same secgroup untouched
             if rule['ip_protocol']:
                 provider.delete_secgroup_rule(ruleid)
     return
Exemple #31
0
 def delete_all_rules_cloud(cls, cloud, groupname):
     rules = cls.list_rules_cloud(cloud, groupname)
     provider = CloudProvider(cloud).provider
     if rules:
         for rule in rules:
             ruleid = rule['id']
             # only refresh those defined with a protocol
             # This leaves the default rule defined by
             # allowing the same secgroup untouched
             if rule['ip_protocol']:
                 provider.delete_secgroup_rule(ruleid)
     return
Exemple #32
0
 def get_floating_ip_list(cls, cloudname):
     """
     Method to get the floating IP list
     :param cloudname:
     :return: floating_ips
     """
     try:
         cloud_provider = CloudProvider(cloudname).provider
         floating_ips = cloud_provider.list_floating_ips()
         return floating_ips
     except Exception as ex:
         Console.error(ex.message)
Exemple #33
0
    def list(cls, cloud, output="table", tenant=None):
        try:
            provider = CloudProvider(cloud).provider
            # if tenant is None:
            #     tenant = provider.tenant

            result = provider.list_limits(tenant)["absolute"]

            (order, header) = CloudProvider(cloud).get_attributes("limits")

            return attribute_printer(result, header=header, output=output)
        except Exception as e:
            return e
Exemple #34
0
    def list(cls, cloud, output="table", tenant=None):
        try:
            provider = CloudProvider(cloud).provider

            result = provider.list_limits(tenant)["absolute"]

            (order, header) = CloudProvider(cloud).get_attributes("limits")

            return attribute_printer(result,
                                     header=header,
                                     output=output)
        except Exception, e:
            return e
Exemple #35
0
 def unreserve_fixed_ip(cls, cloudname, fixed_ip_addr):
     """
     Unreserve a fixed ip address
     :param cloudname:
     :param fixed_ip_addr:
     :return:
     """
     try:
         cloud_provider = CloudProvider(cloudname).provider
         cloud_provider.unreserve_fixed_ip(fixed_ip_addr=fixed_ip_addr)
         return "Success."
     except Exception as ex:
         Console.error(ex.message)
         return ex
Exemple #36
0
 def unreserve_fixed_ip(cls, cloudname, fixed_ip_addr):
     """
     Unreserve a fixed ip address
     :param cloudname:
     :param fixed_ip_addr:
     :return:
     """
     try:
         cloud_provider = CloudProvider(cloudname).provider
         cloud_provider.unreserve_fixed_ip(fixed_ip_addr=fixed_ip_addr)
         return "Success."
     except Exception as ex:
         Console.error(ex.message)
         return ex
Exemple #37
0
    def list(cls, cloud, tenant, output="table"):
        try:

            provider = CloudProvider(cloud).provider

            result = provider.list_quota(cloud)

            (order, header) = CloudProvider(cloud).get_attributes("quota")

            return attribute_printer(result, header=header, output=output)
        except Exception as e:
            import sys
            print(sys.exc_info()[0])
            return e
Exemple #38
0
 def create_assign_floating_ip(cls, cloudname, instance_name):
     """
     Method to create a new floating-ip
     and associate it with the instance
     :param cloudname: cloud
     :param instance_name: name of instance
     :return: floating_ip
     """
     try:
         cloud_provider = CloudProvider(cloudname).provider
         floating_ip = cloud_provider.create_assign_floating_ip(instance_name)
         return floating_ip
     except Exception as ex:
         Console.error(ex.message, ex)
         return
Exemple #39
0
    def list(cls, cloud, tenant, output="table"):
        try:

            provider = CloudProvider(cloud).provider

            result = provider.list_quota(cloud)

            (order, header) = CloudProvider(cloud).get_attributes("quota")

            return attribute_printer(result, header=header, output=output)
        except Exception, e:
            import sys

            print(sys.exc_info()[0])
            return e
    def add_key_to_cloud(self, user, keyname, cloud, name_on_cloud):

        sshdb = SSHKeyDBManager()
        key_from_db = sshdb.find(keyname)

        if key_from_db is None:
            Console.error("Key with the name {:} not found in database.".format(keyname))
            return

        # Add map entry
        sshdb.add_key_cloud_map_entry(user, keyname, cloud, name_on_cloud)

        print("Adding key {:} to cloud {:} as {:}".format(keyname, cloud, name_on_cloud))
        cloud_provider = CloudProvider(cloud).provider
        cloud_provider.add_key_to_cloud(name_on_cloud, key_from_db["value"])
Exemple #41
0
    def list(cls, **kwargs):
        """
        This method lists all VMs of the cloud
        """

        try:
            if "name_or_id" in kwargs and kwargs["name_or_id"] is not None:
                if cls.isUuid(kwargs["name_or_id"]):
                    elements = cls.cm.find("vm",
                                           category=kwargs["cloud"],
                                           uuid=kwargs["name_or_id"])
                else:
                    elements = cls.cm.find("vm",
                                           category=kwargs["cloud"],
                                           label=kwargs["name_or_id"])
            else:
                elements = cls.cm.find("vm",
                                       category=kwargs["cloud"])

            # print(elements)

            # order = ['id', 'uuid', 'name', 'cloud']
            (order, header) = CloudProvider(kwargs["cloud"]).get_attributes("vm")

            # order = None
            if "name_or_id" in kwargs and kwargs["name_or_id"] is not None:
                return attribute_printer(list(elements.values())[0],
                                         output=kwargs["output_format"])
            else:
                return dict_printer(elements,
                                    order=order,
                                    output=kwargs["output_format"])
        except Exception as ex:
            Console.error(ex.message, ex)
Exemple #42
0
    def get_fixed_ip(cls, cloudname, fixed_ip_addr):
        """
        Method retrieves fixed ip info
        :param cloudname:
        :param fixed_ip_addr:
        :return: fixed_ip_info
        """
        try:
            cloud_provider = CloudProvider(cloudname).provider
            result = cloud_provider.get_fixed_ip(fixed_ip_addr=fixed_ip_addr)

            return attribute_printer(result, header=["name", "value"])
        except Exception as ex:
            Console.error(ex.message, ex)

        return
Exemple #43
0
    def list_floating_ip(cls, cloudname):
        """
        Method to list floating ips
        :param cloudname:
        :return: floating ip list
        """
        try:
            floating_ips = cls.get_floating_ip_list(cloudname)

            for floating_ip in list(floating_ips.values()):
                # Get instance_id associated with instance
                instance_id = floating_ip["instance_id"]

                if instance_id is not None:
                    try:
                        instance_name = cls.find_instance_name(
                            cloudname=cloudname, instance_id=instance_id)
                        # Assign it to the dict
                        floating_ip["instance_name"] = instance_name
                    except Exception as ex:
                        Console.error(ex.message)
                        continue
                else:
                    # If no instance associated, keep None
                    floating_ip["instance_name"] = None

            (order,
             header) = CloudProvider(cloudname).get_attributes("floating_ip")

            return dict_printer(floating_ips, order=order, header=header)
        except Exception as ex:
            Console.error(ex.message, ex)

        return
Exemple #44
0
    def get_fixed_ip(cls, cloudname, fixed_ip_addr):
        """
        Method retrieves fixed ip info
        :param cloudname:
        :param fixed_ip_addr:
        :return: fixed_ip_info
        """
        try:
            cloud_provider = CloudProvider(cloudname).provider
            result = cloud_provider.get_fixed_ip(fixed_ip_addr=fixed_ip_addr)

            return attribute_printer(result, header=["name", "value"])
        except Exception as ex:
            Console.error(ex.message, ex)

        return
Exemple #45
0
    def list(cls, cloud=None, names=None, output='table', live=False):

        try:

            if live:
                cls.refresh(cloud)

            elements = cls.cm.find(kind="vm", category=cloud)
            result = []
            if "all" in names:
                for element in elements:
                    result.append(element)
            elif names is not None:
                for element in elements:
                    if element["name"] in names:
                        result.append(element)

            (order, header) = CloudProvider(cloud).get_attributes("ip")

            return Printer.write(result,
                                 order=order,
                                 header=header,
                                 output=output)
        except Exception as ex:
            Console.error(ex.message)
Exemple #46
0
    def __init__(self, *args, **kwargs):

        # Use the table defined in the model, but we need to look up
        # the provider object dynamically

        kwargs['cloud'] = kwargs.get('cloud', Default.cloud)
        kwargs['image'] = kwargs.get('image', Default.image)
        kwargs['username'] = kwargs.get('username', Image.guess_username(kwargs['image']))
        kwargs['flavor'] = kwargs.get('flavor', Default.flavor)
        kwargs['key'] = kwargs.get('key', Default.key)
        kwargs['secgroup'] = kwargs.get('secgroup', Default.secgroup)


        super(Cluster, self).__init__(*args, **kwargs)
        self.provider = CloudProvider(self.cloud).provider.cloud_type

        # put this cluster in the database, the 'name' attribute must
        # be unique

        try:
            self.cm.insert(self)
        except IntegrityError as e:
            line = 'UNIQUE constraint failed: {}.name'\
                   .format(self.__tablename__)
            if line in e.message:
                raise ClusterNameClashException(self.__tablename__,
                                                self.name)
Exemple #47
0
    def delete_secgroup(cls, label, cloud, tenant):
        try:
            # Find the secgroup from the cloud
            nova_client = CloudProvider.set(cloud)
            sec_group = nova_client.security_groups.find(name=label)
            if not sec_group:
                return None

            # delete the secgroup in the cloud
            nova_client.security_groups.delete(sec_group)

            # perform local db deletion
            sec_group = cls.get(label, tenant, cloud)
            if sec_group:
                # Delete all rules for group
                cls.delete_all_rules(sec_group)
                cls.cm_db.delete(sec_group)
                return "Security Group [{}] for cloud [{}], & tenant [{}] deleted".format(label, cloud, tenant)
            else:
                return None

        except Exception as ex:
            Console.error(ex.message, ex)

        return
Exemple #48
0
    def enable_ssh(cls, secgroup_name='default', cloud="general"):
        ret = False
        cloud_provider = CloudProvider(cloud).provider.provider
        secgroups = cloud_provider.security_groups.list()
        for asecgroup in secgroups:
            if asecgroup.name == secgroup_name:
                rules = asecgroup.rules
                rule_exists = False
                # structure of a secgroup rule:
                # {u'from_port': 22, u'group': {}, u'ip_protocol': u'tcp', u'to_port': 22, u'parent_group_id': u'UUIDHERE', u'ip_range': {u'cidr': u'0.0.0.0/0'}, u'id': u'UUIDHERE'}
                for arule in rules:
                    if arule["from_port"] == 22 and \
                                    arule["to_port"] == 22 and \
                                    arule["ip_protocol"] == 'tcp' and \
                                    arule["ip_range"] == {'cidr': '0.0.0.0/0'}:
                        # print (arule["id"])
                        rule_exists = True
                        break
                if not rule_exists:
                    cloud_provider.security_group_rules.create(
                        asecgroup.id,
                        ip_protocol='tcp',
                        from_port=22,
                        to_port=22,
                        cidr='0.0.0.0/0')
                # else:
                #    print ("The rule allowing ssh login did exist!")
                ret = True
                break

        # print ("*" * 80)
        # d = SecGroup.convert_list_to_dict(secgroups)
        # print (d)
        return ret
Exemple #49
0
    def list(cls, project, cloud="general"):
        """
        This method queries the database to fetch list of secgroups
        filtered by cloud, tenant.
        :param project:
        :param cloud:
        :return:
        """
        # noinspection PyUnreachableCode
        try:
            """
            elements = cls.cm_db.query(model.SECGROUP).filter(
                model.SECGROUP.cloud == cloud,
                model.SECGROUP.project == project
            ).all()

            d = cls.toDict(elements)
            """

            nova_client = CloudProvider.set(cloud)
            os_result = nova_client.security_groups.list()
            d = SecGroup.convert_list_to_dict(os_result)

            return dict_printer(d, order=["Id", "Name", "Description"], output="table")

        except Exception as ex:
            Console.error(ex.message, ex)
Exemple #50
0
 def create_assign_floating_ip(cls, cloudname, instance_name):
     """
     Method to create a new floating-ip
     and associate it with the instance
     :param cloudname: cloud
     :param instance_name: name of instance
     :return: floating_ip
     """
     try:
         cloud_provider = CloudProvider(cloudname).provider
         floating_ip = cloud_provider.create_assign_floating_ip(
             instance_name)
         return floating_ip
     except Exception as ex:
         Console.error(ex.message)
         return
Exemple #51
0
    def list_floating_ip_pool(cls, cloudname):
        """
        Method to list floating ip pool
        :param cloudname:
        :return:
        """
        try:
            cloud_provider = CloudProvider(cloudname).provider
            floating_ip_pools = cloud_provider.list_floating_ip_pools()

            (order, header) = CloudProvider(cloudname).get_attributes("floating_ip_pool")

            return dict_printer(floating_ip_pools, order=order, header=header)

        except Exception as ex:
            Console.error(ex.message, ex)
        pass
Exemple #52
0
    def list_floating_ip_pool(cls, cloudname):
        """
        Method to list floating ip pool
        :param cloudname:
        :return:
        """
        try:
            cloud_provider = CloudProvider(cloudname).provider
            floating_ip_pools = cloud_provider.list_floating_ip_pools()

            (order, header
             ) = CloudProvider(cloudname).get_attributes("floating_ip_pool")

            return Printer.write(floating_ip_pools, order=order, header=header)

        except Exception as ex:
            Console.error(ex.message)
        pass
Exemple #53
0
    def create_floating_ip(cls, cloudname, floating_pool=None):
        """
        Method to create a floating ip address under a pool
        :param cloudname:
        :param floating_pool:
        :return: floating ip addr
        """
        try:
            cloud_provider = CloudProvider(cloudname).provider
            # If floating pool is not given,
            # get first from list
            if floating_pool is None:
                floating_pool = cloud_provider.provider.floating_ip_pools.list()[0].name
                Console.ok("Floating pool not provided, selected [{}] as the pool.".format(floating_pool))

            floating_ip = cloud_provider.create_floating_ip(float_pool=floating_pool)
            return floating_ip
        except Exception as ex:
            Console.error(ex.message, ex)
            return
Exemple #54
0
 def list(cls, cloud, kind):
     """
     This method lists all flavors of the cloud
     :param cloud: the cloud name
     :param kind: the kind of the attribute
     """
     try:
         (order, header) = CloudProvider(cloud).get_attributes(kind)
         return order, header
     except Exception as ex:
         Console.error(ex.message)
Exemple #55
0
    def list_on_cloud(cls, cloud, live=False, format="table"):
        """
        This method lists all flavors of the cloud
        :param cloud: the cloud name
        """
        try:
            keys = CloudProvider(cloud).provider.list_key(cloud)
            for key in keys:
                keys[key]["category"] = cloud
            if keys is None or keys is []:
                return None

            (order, header) = CloudProvider(cloud).get_attributes("key")

            return Printer.write(keys,
                                 order=order,
                                 header=header,
                                 output=format)
        except Exception as ex:
            Console.error(ex.message)
Exemple #56
0
    def get_from_cloud(cls, cloud, live=False, format="table"):
        """
        This method lists all keys of the cloud
        :param cloud: the cloud name
        :return: a SSHKeyManager (dict of keys)
        """
        try:
            return CloudProvider(cloud).provider.list_key(cloud)

        except Exception as ex:
            Console.error(ex.message)
Exemple #57
0
    def add_rule(cls, cloud, secgroup, from_port, to_port, protocol, cidr):
        try:
            # Get the nova client object
            cloud_provider = CloudProvider(cloud).provider

            # Create add secgroup rules to the cloud
            args = {
                'uuid': secgroup.uuid,
                'protocol': protocol,
                'from_port': from_port,
                'to_port': to_port,
                'cidr': cidr
            }
            rule_id = cloud_provider.add_secgroup_rule(**args)

            # create local db record
            ruleObj = cls.cm_db.db_obj_dict("secgrouprule",
                                            uuid=str(rule_id),
                                            name=secgroup.name,
                                            groupid=secgroup.uuid,
                                            category=secgroup.category,
                                            user=secgroup.user,
                                            project=secgroup.project,
                                            fromPort=from_port,
                                            toPort=to_port,
                                            protocol=protocol,
                                            cidr=cidr)

            cls.cm_db.add_obj(ruleObj)
            cls.cm_db.save()

            Console.ok("Added rule [{} | {} | {} | {}] to secgroup [{}]"
                       .format(from_port, to_port, protocol, cidr,
                               secgroup.name))
        except Exception as ex:
            if "This rule already exists" in ex.message:
                Console.ok("Rule already exists. Added rule.")
                return
            else:
                Console.error(ex.message, ex)
        return
Exemple #58
0
 def add_rule_cloud(cls, cloud, groupname, rulename):
     ret = None
     provider = CloudProvider(cloud).provider
     # fetch rule from db
     db_rule = cls.cm.find(kind="secgrouprule",
                           category="general",
                           group=groupname,
                           name=rulename,
                           scope='first',
                           output='dict')
     kwargs = {}
     kwargs["protocol"] = db_rule["protocol"]
     kwargs["cidr"] = db_rule["cidr"]
     kwargs["from_port"] = db_rule["fromPort"]
     kwargs["to_port"] = db_rule["toPort"]
     group = cls.get_group_cloud(cloud, groupname)
     if group:
         groupid = group["id"]
         kwargs["uuid"] = groupid
         ret = provider.add_secgroup_rule(**kwargs)
     return ret
Exemple #59
0
    def add_key_to_cloud(cls, user, keyname, cloud):
        """

        :param user:
        :param keyname:
        :param cloud:
        :param name_on_cloud:
        """

        key = cls.cm.find(kind="key", name=keyname, scope="first")
        if key is None:
            Console.error("Key with the name {:} not found in database.".format(keyname))
            return

        try:
            if cloud is not None:
                print("Adding key {:} to cloud {:}".format(keyname, cloud))
                cloud_provider = CloudProvider(cloud).provider
                cloud_provider.add_key_to_cloud(keyname, key["value"])
        except Exception as e:
            Console.error("problem uploading key {} to cloud {}: {}".format(keyname, cloud, e.message), traceflag=False)