2019-09-27 20:00:44 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from molecule import logger, util
|
|
|
|
from molecule.api import Driver
|
2019-10-23 11:04:57 +00:00
|
|
|
from molecule.util import lru_cache, sysexit_with_message
|
2019-09-27 20:00:44 +00:00
|
|
|
|
|
|
|
log = logger.get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class HetznerCloud(Driver):
|
|
|
|
def __init__(self, config=None):
|
|
|
|
super(HetznerCloud, self).__init__(config)
|
2019-10-23 11:04:57 +00:00
|
|
|
self._name = "hetznercloud"
|
2019-09-27 20:00:44 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@name.setter
|
|
|
|
def name(self, value):
|
|
|
|
self._name = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def login_cmd_template(self):
|
2019-10-23 11:04:57 +00:00
|
|
|
connection_options = " ".join(self.ssh_connection_options)
|
2019-09-27 20:00:44 +00:00
|
|
|
|
|
|
|
return (
|
2019-10-23 11:04:57 +00:00
|
|
|
"ssh {{address}} "
|
|
|
|
"-l {{user}} "
|
|
|
|
"-p {{port}} "
|
|
|
|
"-i {{identity_file}} "
|
|
|
|
"{}"
|
2019-09-27 20:00:44 +00:00
|
|
|
).format(connection_options)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def default_safe_files(self):
|
|
|
|
return [self.instance_config]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def default_ssh_connection_options(self):
|
|
|
|
return self._get_ssh_connection_options()
|
|
|
|
|
|
|
|
def login_options(self, instance_name):
|
2019-10-23 11:04:57 +00:00
|
|
|
d = {"instance": instance_name}
|
2019-09-27 20:00:44 +00:00
|
|
|
|
|
|
|
return util.merge_dicts(d, self._get_instance_config(instance_name))
|
|
|
|
|
|
|
|
def ansible_connection_options(self, instance_name):
|
|
|
|
try:
|
|
|
|
d = self._get_instance_config(instance_name)
|
|
|
|
|
|
|
|
return {
|
2019-10-23 11:04:57 +00:00
|
|
|
"ansible_user": d["user"],
|
|
|
|
"ansible_host": d["address"],
|
|
|
|
"ansible_port": d["port"],
|
|
|
|
"ansible_private_key_file": d["identity_file"],
|
|
|
|
"connection": "ssh",
|
|
|
|
"ansible_ssh_common_args": " ".join(self.ssh_connection_options),
|
2019-09-27 20:00:44 +00:00
|
|
|
}
|
|
|
|
except StopIteration:
|
|
|
|
return {}
|
|
|
|
except IOError:
|
|
|
|
return {}
|
|
|
|
|
2019-11-16 02:00:48 +00:00
|
|
|
def template_dir(self):
|
|
|
|
return os.path.join(
|
|
|
|
os.path.dirname(__file__),
|
|
|
|
"cookiecutter/scenario/driver/{}".format(self.name),
|
|
|
|
)
|
|
|
|
|
2019-09-27 20:00:44 +00:00
|
|
|
def _get_instance_config(self, instance_name):
|
|
|
|
instance_config_dict = util.safe_load_file(self._config.driver.instance_config)
|
|
|
|
|
|
|
|
return next(
|
2019-10-23 11:04:57 +00:00
|
|
|
item for item in instance_config_dict if item["instance"] == instance_name
|
2019-09-27 20:00:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@lru_cache()
|
|
|
|
def sanity_checks(self):
|
|
|
|
"""Hetzner Cloud driver sanity checks."""
|
|
|
|
|
|
|
|
log.info("Sanity checks: '{}'".format(self._name))
|
|
|
|
|
|
|
|
try:
|
|
|
|
import hcloud # noqa
|
|
|
|
except ImportError:
|
|
|
|
msg = (
|
2019-10-23 11:04:57 +00:00
|
|
|
"Missing Hetzner Cloud driver dependency. Please "
|
2019-11-16 02:02:54 +00:00
|
|
|
"install the 'molecule-hetznercloud' package or "
|
|
|
|
"refer to your INSTALL.rst driver documentation file"
|
2019-09-27 20:00:44 +00:00
|
|
|
)
|
|
|
|
sysexit_with_message(msg)
|
|
|
|
|
2019-10-23 11:04:57 +00:00
|
|
|
if "HCLOUD_TOKEN" not in os.environ:
|
2019-09-27 20:00:44 +00:00
|
|
|
msg = (
|
2019-10-23 11:04:57 +00:00
|
|
|
"Missing Hetzner Cloud API token. Please expose "
|
|
|
|
"the HCLOUD_TOKEN environment variable with your "
|
|
|
|
"account API token value"
|
2019-09-27 20:00:44 +00:00
|
|
|
)
|
|
|
|
sysexit_with_message(msg)
|
2020-04-27 13:02:43 +00:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
"""Destroy all resources managed by this plugin."""
|
|
|
|
# TODO(decentral1se): implement if ever needed
|
|
|
|
pass
|