Skip to content

Commit

Permalink
Update OpenStack Nova client initialization code.
Browse files Browse the repository at this point in the history
This follows the recommendations of the OpenStack Python SDK docs (as of the
time of writing) and works with the current release of
`python-novaclient` (v7.0.0).

I am not sure, though, how much backwards-compatible it can be.
  • Loading branch information
riccardomurri committed Feb 3, 2017
1 parent 5dc1684 commit 2b74317
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
47 changes: 34 additions & 13 deletions gc3libs/backends/openstack.py
Expand Up @@ -27,7 +27,19 @@

# OpenStack APIs
try:
from novaclient import client as NovaClient
import os_client_config
except ImportError as err:
from gc3libs.exceptions import ConfigurationError
raise ConfigurationError(
"The OpenStack backend is used but the `os_client_config` module"
" cannot be used: {err}. Please, either install it with"
"`pip install os-client-config` and verify that it works"
" by running `python -c 'import os_client_config'`,"
" then try again, or update your configuration file and"
" disable any OpenStack-based resources."
.format(err=err))

try:
from novaclient.exceptions import NotFound
except ImportError as err:
from gc3libs.exceptions import ConfigurationError
Expand Down Expand Up @@ -101,6 +113,10 @@ def __init__(self, name,
vm_os_overhead=gc3libs.Default.VM_OS_OVERHEAD,
# extra args are used to instanciate "sub-resources"
**extra_args):

# Note: this creates attributes from key/value pairs given in the
# `extra_args` parameters. In particular, the `self.type` attribute
# (referenced below) is set in this chained constructor...
LRMS.__init__(
self, name,
architecture, max_cores, max_cores_per_job,
Expand All @@ -120,6 +136,7 @@ def __init__(self, name,
"Value for `vm_pool_max_size` must be an integer,"
" was %s instead." % vm_pool_max_size)

# pylint: disable=no-member
self.subresource_type = self.type.split('+', 1)[1]
if self.subresource_type not in available_subresource_types:
raise UnrecoverableError("Invalid resource type: %s" % self.type)
Expand Down Expand Up @@ -195,15 +212,9 @@ def __init__(self, name,
raise ConfigurationError(
"No `image_id` specified in the configuration file.")

# Only API version 1.1 has been tested so far
self.compute_api_version = '1.1'

# "Connect" to the cloud (connection is actually performed
# only when needed by the `Client` class.
self.client = NovaClient.Client(
self.compute_api_version, self.os_username, self.os_password,
self.os_tenant_name, self.os_auth_url,
region_name=self.os_region_name)
self.client = self._new_client()

# Set up the VMPool persistent class. This has been delayed
# until here because otherwise self._conn is None
Expand All @@ -214,8 +225,19 @@ def __init__(self, name,
# to set self.max_cores and self.max_memory_per_core
# self._connect()

def _new_client(self):
self.__connected = False
return os_client_config.make_client(
'compute',
auth_url=self.os_auth_url,
username=self.os_username,
password=self.os_password,
project_name=self.os_tenant_name,
region_name=self.os_region_name,
)

def _connect(self):
if not self.client.client.auth_token or not self._flavors:
if not self.__connected:
self.client.authenticate()
# Fill the flavors and update the resource.
self._flavors = self.client.flavors.list()
Expand All @@ -224,6 +246,7 @@ def _connect(self):
flavor.name)
self['max_cores'] = self['max_cores_per_job'] = flavor.vcpus
self['max_memory_per_core'] = flavor.ram * MiB
self.__connected = True

def _create_instance(self, image_id, name='gc3pie-instance',
instance_type=None, user_data=None):
Expand Down Expand Up @@ -1089,14 +1112,12 @@ def __getstate__(self):
# not pickle-compliant
state = self.__dict__.copy()
del state['client']
del state['__connected']
return state

def __setstate__(self, state):
self.__dict__.update(state)
self.client = NovaClient.Client(
self.compute_api_version, self.os_username, self.os_password,
self.os_tenant_name, self.os_auth_url,
region_name=self.os_region_name)
self.client = self._new_client()
self._connect()


Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -159,6 +159,7 @@ def run_tests(self):
# version 3.0.0 of the client libraries, we have to include separate
# dependecy lists for Python 2.7+ and Python 2.6
'python-novaclient',
'os-client-config',
],
'openstack:python_version=="2.6"': [
# The following Python modules are required by GC3Pie's `openstack`
Expand Down

0 comments on commit 2b74317

Please sign in to comment.