2017-05-05 10:23:00 +02:00
|
|
|
"""CloudStack plugin for integration tests."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2017-05-05 14:01:27 +02:00
|
|
|
import json
|
2017-05-05 10:23:00 +02:00
|
|
|
import os
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from . import (
|
2017-05-05 10:23:00 +02:00
|
|
|
CloudProvider,
|
|
|
|
CloudEnvironment,
|
2019-03-01 03:25:49 +01:00
|
|
|
CloudEnvironmentConfig,
|
2017-05-05 10:23:00 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..util import (
|
2017-05-05 10:23:00 +02:00
|
|
|
ApplicationError,
|
|
|
|
display,
|
2018-09-21 20:38:22 +02:00
|
|
|
ConfigParser,
|
2017-05-05 10:23:00 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..http import (
|
2017-05-05 10:23:00 +02:00
|
|
|
urlparse,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..docker_util import (
|
2018-12-17 22:39:15 +01:00
|
|
|
docker_exec,
|
2021-02-02 20:47:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
from ..containers import (
|
|
|
|
run_support_container,
|
|
|
|
wait_for_file,
|
2017-05-05 10:23:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CsCloudProvider(CloudProvider):
|
|
|
|
"""CloudStack cloud provider plugin. Sets up cloud resources before delegation."""
|
|
|
|
DOCKER_SIMULATOR_NAME = 'cloudstack-sim'
|
|
|
|
|
|
|
|
def __init__(self, args):
|
|
|
|
"""
|
|
|
|
:type args: TestConfig
|
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
super(CsCloudProvider, self).__init__(args)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2020-09-24 20:58:37 +02:00
|
|
|
self.image = os.environ.get('ANSIBLE_CLOUDSTACK_CONTAINER', 'quay.io/ansible/cloudstack-test-container:1.4.0')
|
2017-05-05 10:23:00 +02:00
|
|
|
self.host = ''
|
|
|
|
self.port = 0
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
self.uses_docker = True
|
|
|
|
self.uses_config = True
|
2017-05-05 10:23:00 +02:00
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""Setup the cloud resource before delegation and register a cleanup callback."""
|
|
|
|
super(CsCloudProvider, self).setup()
|
|
|
|
|
|
|
|
if self._use_static_config():
|
|
|
|
self._setup_static()
|
|
|
|
else:
|
|
|
|
self._setup_dynamic()
|
|
|
|
|
|
|
|
def _setup_static(self):
|
|
|
|
"""Configure CloudStack tests for use with static configuration."""
|
2018-09-21 20:38:22 +02:00
|
|
|
parser = ConfigParser()
|
2017-05-05 10:23:00 +02:00
|
|
|
parser.read(self.config_static_path)
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
endpoint = parser.get('cloudstack', 'endpoint')
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
parts = urlparse(endpoint)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
|
|
|
self.host = parts.hostname
|
|
|
|
|
|
|
|
if not self.host:
|
2021-02-02 20:47:38 +01:00
|
|
|
raise ApplicationError('Could not determine host from endpoint: %s' % endpoint)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
|
|
|
if parts.port:
|
|
|
|
self.port = parts.port
|
|
|
|
elif parts.scheme == 'http':
|
|
|
|
self.port = 80
|
|
|
|
elif parts.scheme == 'https':
|
|
|
|
self.port = 443
|
|
|
|
else:
|
2021-02-02 20:47:38 +01:00
|
|
|
raise ApplicationError('Could not determine port from endpoint: %s' % endpoint)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
|
|
|
display.info('Read cs host "%s" and port %d from config: %s' % (self.host, self.port, self.config_static_path), verbosity=1)
|
|
|
|
|
|
|
|
def _setup_dynamic(self):
|
|
|
|
"""Create a CloudStack simulator using docker."""
|
|
|
|
config = self._read_config_template()
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
self.port = 8888
|
2017-05-05 14:01:27 +02:00
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
ports = [
|
|
|
|
self.port,
|
|
|
|
]
|
|
|
|
|
|
|
|
descriptor = run_support_container(
|
|
|
|
self.args,
|
|
|
|
self.platform,
|
|
|
|
self.image,
|
|
|
|
self.DOCKER_SIMULATOR_NAME,
|
|
|
|
ports,
|
|
|
|
allow_existing=True,
|
|
|
|
cleanup=True,
|
|
|
|
)
|
2017-05-05 14:01:27 +02:00
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
descriptor.register(self.args)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
# apply work-around for OverlayFS issue
|
|
|
|
# https://github.com/docker/for-linux/issues/72#issuecomment-319904698
|
|
|
|
docker_exec(self.args, self.DOCKER_SIMULATOR_NAME, ['find', '/var/lib/mysql', '-type', 'f', '-exec', 'touch', '{}', ';'])
|
2017-05-05 10:23:00 +02:00
|
|
|
|
|
|
|
if self.args.explain:
|
|
|
|
values = dict(
|
|
|
|
HOST=self.host,
|
|
|
|
PORT=str(self.port),
|
|
|
|
)
|
|
|
|
else:
|
2021-02-02 20:47:38 +01:00
|
|
|
credentials = self._get_credentials(self.DOCKER_SIMULATOR_NAME)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
|
|
|
values = dict(
|
2021-02-02 20:47:38 +01:00
|
|
|
HOST=self.DOCKER_SIMULATOR_NAME,
|
2017-05-05 10:23:00 +02:00
|
|
|
PORT=str(self.port),
|
|
|
|
KEY=credentials['apikey'],
|
|
|
|
SECRET=credentials['secretkey'],
|
|
|
|
)
|
|
|
|
|
2019-09-17 06:01:37 +02:00
|
|
|
display.sensitive.add(values['SECRET'])
|
|
|
|
|
2017-05-05 10:23:00 +02:00
|
|
|
config = self._populate_config_template(config, values)
|
|
|
|
|
|
|
|
self._write_config(config)
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
def _get_credentials(self, container_name):
|
2017-05-05 10:23:00 +02:00
|
|
|
"""Wait for the CloudStack simulator to return credentials.
|
2021-02-02 20:47:38 +01:00
|
|
|
:type container_name: str
|
2017-05-05 10:23:00 +02:00
|
|
|
:rtype: dict[str, str]
|
|
|
|
"""
|
2021-02-02 20:47:38 +01:00
|
|
|
def check(value):
|
|
|
|
# noinspection PyBroadException
|
|
|
|
try:
|
|
|
|
json.loads(value)
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
return False # sometimes the file exists but is not yet valid JSON
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
return True
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
stdout = wait_for_file(self.args, container_name, '/var/www/html/admin.json', sleep=10, tries=30, check=check)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
return json.loads(stdout)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CsCloudEnvironment(CloudEnvironment):
|
|
|
|
"""CloudStack cloud environment plugin. Updates integration test environment after delegation."""
|
2019-03-01 03:25:49 +01:00
|
|
|
def get_environment_config(self):
|
2017-05-05 10:23:00 +02:00
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
:rtype: CloudEnvironmentConfig
|
2017-05-05 10:23:00 +02:00
|
|
|
"""
|
2019-03-01 03:25:49 +01:00
|
|
|
parser = ConfigParser()
|
|
|
|
parser.read(self.config_path)
|
|
|
|
|
|
|
|
config = dict(parser.items('default'))
|
|
|
|
|
|
|
|
env_vars = dict(
|
|
|
|
CLOUDSTACK_ENDPOINT=config['endpoint'],
|
|
|
|
CLOUDSTACK_KEY=config['key'],
|
|
|
|
CLOUDSTACK_SECRET=config['secret'],
|
|
|
|
CLOUDSTACK_TIMEOUT=config['timeout'],
|
2017-05-05 10:23:00 +02:00
|
|
|
)
|
|
|
|
|
2019-09-17 06:01:37 +02:00
|
|
|
display.sensitive.add(env_vars['CLOUDSTACK_SECRET'])
|
|
|
|
|
2019-03-01 03:25:49 +01:00
|
|
|
ansible_vars = dict(
|
|
|
|
cs_resource_prefix=self.resource_prefix,
|
|
|
|
)
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2019-03-01 03:25:49 +01:00
|
|
|
return CloudEnvironmentConfig(
|
|
|
|
env_vars=env_vars,
|
|
|
|
ansible_vars=ansible_vars,
|
|
|
|
)
|