Exemplo n.º 1
0
    def __init__(self, testbed_name, testbed, tenant_name=None, project_id=None):
        self.testbed_name = testbed_name
        self.tenant_name = None
        self.project_id = None
        self.testbed = testbed
        self.project_domain_name = self.testbed.get('project_domain_name') or 'Default'
        self.user_domain_name = self.testbed.get('user_domain_name') or 'Default'
        self.api_version = self.testbed.get('api_version')
        self.username = self.testbed.get('username')
        self.password = self.testbed.get('password')
        self.auth_url = self.testbed.get("auth_url")
        if self.auth_url.endswith('/'):
            self.auth_url = self.auth_url[:-1]
        self.admin_tenant_name = self.testbed.get("admin_tenant_name")
        self.admin_project_id = self.testbed.get("admin_project_id")
        if not self.admin_tenant_name and not self.admin_project_id:
            raise OpenstackClientError("Missing both admin project id and admin tenant name")
        if self.api_version == 2 and not self.admin_tenant_name:
            raise OpenstackClientError("Missing tenant name required if using v2")
        if self.api_version == 3 and not self.admin_project_id:
            raise OpenstackClientError("Missing project id required if using v3")

        self.neutron = None
        self.nova = None
        self.glance = None
        self.keypair = None
        self.sec_group = None
        self.os_tenant_id = None

        # logger.debug("Log level is: %s and DEBUG is %s" % (logger.getEffectiveLevel(), logging.DEBUG))
        # if logger.getEffectiveLevel() == logging.DEBUG:
        #     logging.basicConfig(level=logging.DEBUG)

        if not tenant_name and not project_id:

            self.keystone = self._create_keystone_client()
            logger.debug("Created Keystone client %s" % self.keystone)
        else:
            self.tenant_name = tenant_name
            self.project_id = project_id

            if self.api_version == 2 and not self.tenant_name:
                raise OpenstackClientError("Missing tenant name required if using v2")
            if self.api_version == 3 and not self.project_id:
                raise OpenstackClientError("Missing project id required if using v3")

            logger.debug("Creating keystone client")
            if self.api_version == 3:
                self.keystone = self._create_keystone_client(project_id)
                self.os_tenant_id = project_id
            else:
                self.keystone = self._create_keystone_client(tenant_name)
                self.os_tenant_id = self.project_id = self._get_tenant_id_from_name(tenant_name)

            logger.debug("Created Keystone client %s" % self.keystone)
            self.set_nova(self.os_tenant_id)
            self.set_neutron(self.os_tenant_id)
            self.set_glance(self.os_tenant_id)
Exemplo n.º 2
0
 def list_images(self, tenant_id=None):
     if not self.nova:
         if not tenant_id:
             logger.error("Missing tenant_id!")
             raise OpenstackClientError('Missing tenant_id!')
         self.set_nova(tenant_id)
     try:
         imgs = self.nova.images.list()
         return imgs
     except:
         self.set_glance(tenant_id)
         return self.glance.images.list()
Exemplo n.º 3
0
 def allocate_floating_ips(self, ext_net, fip_num=0):
     body = {
         "floatingip": {
             "floating_network_id": ext_net['id']
         }
     }
     for i in range(fip_num):
         try:
             self.neutron.create_floatingip(body=body)
         except IpAddressGenerationFailureClient as e:
             logger.error("Not able to allocate floatingips :(")
             raise OpenstackClientError("Not able to allocate floatingips :(")
Exemplo n.º 4
0
 def create_user(self, username, password=None, tenant_id=None):
     for u in self.list_users():
         if hasattr(u, 'username'):
             u_username = u.username
         else:
             u_username = u.name
         if u_username == username:
             return u
     if not password:
         raise OpenstackClientError("Paswsord is needed to create user")
     if self.api_version == 2:
         return self.keystone.users.create(username, password, tenant_id=tenant_id)
     else:
         return self.keystone.users.create(name=username, password=password,
                                           project=self.get_project_from_id(tenant_id))
Exemplo n.º 5
0
 def import_keypair(self, key_file, os_tenant_id=None):
     if not self.nova and not os_tenant_id:
         raise OpenstackClientError("Both os_tenant_id and nova obj are None")
     if not self.nova:
         self.set_nova(os_tenant_id=os_tenant_id)
     keypair_name = "softfire-key"
     self.keypair = keypair_name
     for keypair in self.list_keypairs(os_tenant_id):
         if keypair.name == keypair_name:
             return keypair
     if os.path.isfile(key_file):
         with open(key_file, "r") as sosftfire_ssh_pub_key:
             kargs = {"name": keypair_name,
                      "public_key": sosftfire_ssh_pub_key.read()}
             return self.nova.keypairs.create(**kargs)
     else:
         kargs = {"name": keypair_name,
                  "public_key": key_file}
         return self.nova.keypairs.create(**kargs)
Exemplo n.º 6
0
 def _get_session(self, tenant_id=None):
     if self.api_version == 2:
         tenant_name = self.tenant_name or self.admin_tenant_name
         auth = v2.Password(auth_url=self.auth_url,
                            username=self.username,
                            password=self.password,
                            tenant_name=tenant_name)
     elif self.api_version == 3:
         p_id = tenant_id or self.project_id or self.admin_project_id
         auth = v3.Password(auth_url=self.auth_url,
                            username=self.username,
                            password=self.password,
                            project_id=p_id,
                            project_domain_name=self.project_domain_name,
                            user_domain_name=self.user_domain_name)
     else:
         msg = "Wrong api version: %s" % self.api_version
         logger.error(msg)
         raise OpenstackClientError(msg)
     return session.Session(auth=auth)
Exemplo n.º 7
0
def _create_single_project(tenant_name, testbed, testbed_name, username, password):
    os_client = OSClient(testbed_name, testbed)
    logger.info("Created OSClient for testbed %s" % testbed_name)
    admin_user = os_client.get_user()

    logger.debug("Got User %s" % admin_user)
    admin_role = os_client.get_role('admin')
    try:
        user_role = os_client.get_role('_member_')
    except:
        user_role = os_client.get_role('member')

    logger.debug("Got Role %s" % admin_role)
    for tenant in os_client.list_tenants():
        if tenant.name == tenant_name:
            logger.warning("Tenant with name or id %s exists already! I assume a double registration i will not do "
                           "anything :)" % tenant_name)
            logger.warning("returning tenant id %s" % tenant.id)

            exp_user = os_client.get_user(username)
            if not exp_user:
                exp_user = os_client.create_user(username, password)
                os_client.add_user_role(user=exp_user, role=user_role, tenant=tenant.id)
                os_client.add_user_role(user=admin_user, role=admin_role, tenant=tenant.id)
            if os_client.api_version == 2:
                vim_instance = os_client.get_vim_instance(tenant_name=tenant_name, username=username, password=password)
            else:
                vim_instance = os_client.get_vim_instance(tenant_name=tenant.id, username=username, password=password)
            return tenant.id, vim_instance

    tenant = os_client.create_tenant(tenant_name=tenant_name, description='softfire tenant for user %s' % tenant_name)
    logger.debug("Created tenant %s" % tenant)
    os_tenant_id = tenant.id
    logger.info("Created tenant with id: %s" % os_tenant_id)

    exp_user = os_client.create_user(username, password, os_tenant_id)
    os_client.add_user_role(user=admin_user, role=admin_role, tenant=os_tenant_id)
    os_client.add_user_role(user=exp_user, role=user_role, tenant=os_tenant_id)

    os_client = OSClient(testbed_name, testbed, project_id=os_tenant_id, tenant_name=tenant_name)

    try:
        ext_net = os_client.get_ext_net(testbed.get('ext_net_name'))

        if ext_net is None:
            logger.error(
                "A shared External Network called %s must exist! "
                "Please create one in your openstack instance" % testbed.get('ext_net_name')
            )
            raise OpenstackClientError("A shared External Network called softfire-network must exist! "
                                       "Please create one in your openstack instance")
        # networks, subnets, router_id = os_client.create_networks_and_subnets(ext_net)
        # logger.debug("Created Network %s, Subnet %s, Router %s" % (networks, subnets, router_id))

        fips = testbed.get("allocate-fip")
        if fips is not None and int(fips) > 0:
            try:
                os_client.allocate_floating_ips(ext_net, int(fips))
            except OpenstackClientError as e:
                logger.warning(e.args)

    except:
        logger.warning("Not able to get ext net")

    os_client.create_security_group(os_tenant_id)
    if os_client.api_version == 2:
        vim_instance = os_client.get_vim_instance(tenant_name=tenant_name, username=username, password=password)
    else:
        vim_instance = os_client.get_vim_instance(tenant_name=tenant.id, username=username, password=password)
    return os_tenant_id, vim_instance
Exemplo n.º 8
0
 def get_project_from_id(self, tenant_id):
     for p in self.list_tenants():
         if p.id == tenant_id:
             return p
     raise OpenstackClientError("Project with id %s not found")
Exemplo n.º 9
0
 def list_keypairs(self, os_project_id=None):
     if not self.nova:
         if not os_project_id:
             raise OpenstackClientError("Missing project_id!")
         self.set_nova(os_project_id)
     return self.nova.keypairs.list()
Exemplo n.º 10
0
 def list_ports(self, project_id):
     if not self.neutron:
         if not project_id:
             raise OpenstackClientError("Missing project_id!")
         self.set_neutron(project_id)
     return self.neutron.list_ports(tenant_id=project_id)