Skip to content

Commit

Permalink
WIP: Proof-Of-Concept for driver sanity checks
Browse files Browse the repository at this point in the history
Something for ansible#1666 (comment).

Signed-off-by: Luke Murphy <[email protected]>
  • Loading branch information
decentral1se committed Mar 26, 2019
1 parent 6805318 commit ee2adb7
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 1 deletion.
4 changes: 4 additions & 0 deletions molecule/driver/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,7 @@ def _get_instance_config(self, instance_name):

return next(item for item in instance_config_dict
if item['instance'] == instance_name)

def sanity_checks(self):
# FIXME(decentral1se): Implement sanity checks
pass
13 changes: 13 additions & 0 deletions molecule/driver/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ def ansible_connection_options(self, instance_name): # pragma: no cover
"""
pass

@abc.abstractmethod
def sanity_checks(self):
"""
Sanity checks to ensure the driver can do work successfully. For
example, when using the Docker driver, we want to know that the Docker
daemon is running and we have the correct Docker Python dependency.
Each driver implementation can decide what is the most stable sanity
check for itself.
:returns: None
"""
pass

@property
def options(self):
return self._config.config['driver']['options']
Expand Down
3 changes: 3 additions & 0 deletions molecule/driver/delegated.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,6 @@ def _get_instance_config(self, instance_name):

return next(item for item in instance_config_dict
if item['instance'] == instance_name)

def sanity_checks(self):
pass
31 changes: 30 additions & 1 deletion molecule/driver/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

from molecule import logger
from molecule.driver import base
from molecule.util import sysexit_with_message

LOG = logger.get_logger(__name__)
log = logger.get_logger(__name__)


class Docker(base.Base):
Expand Down Expand Up @@ -183,3 +184,31 @@ def login_options(self, instance_name):

def ansible_connection_options(self, instance_name):
return {'ansible_connection': 'docker'}

def sanity_checks(self):
log.info("Sanity checks: '{}'".format(self._name))

try:
from ansible.module_utils.docker_common import HAS_DOCKER_PY
if not HAS_DOCKER_PY:
msg = ('Missing Docker driver dependency. Please '
"install via 'molecule[docker]' or refer to "
'your INSTALL.rst driver documentation')
sysexit_with_message(msg)
except ImportError:
msg = ('Unable to import Ansible driver dependency logic. '
'Please check your Ansible installation')
sysexit_with_message(msg)

try:
import docker
import requests
docker_client = docker.from_env()
docker_client.ping()
except requests.exceptions.ConnectionError:
msg = ('Unable to contact the Docker daemon. '
'Please refer to https://docs.docker.com/config/daemon/ '
'for managing the daemon')
sysexit_with_message(msg)

self._config.change_state('sanity_checked', True)
4 changes: 4 additions & 0 deletions molecule/driver/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,7 @@ def _get_instance_config(self, instance_name):

return next(item for item in instance_config_dict
if item['instance'] == instance_name)

def sanity_checks(self):
# FIXME(decentral1se): Implement sanity checks
pass
4 changes: 4 additions & 0 deletions molecule/driver/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,7 @@ def _get_instance_config(self, instance_name):

return next(item for item in instance_config_dict
if item['instance'] == instance_name)

def sanity_checks(self):
# FIXME(decentral1se): Implement sanity checks
pass
4 changes: 4 additions & 0 deletions molecule/driver/linode.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,7 @@ def _get_instance_config(self, instance_name):
instance_name),
item['instance'] == '{}-{}'.format(item['linode_id'],
instance_name))))

def sanity_checks(self):
# FIXME(decentral1se): Implement sanity checks
pass
4 changes: 4 additions & 0 deletions molecule/driver/lxc.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ def login_options(self, instance_name):

def ansible_connection_options(self, instance_name):
return {'ansible_connection': 'lxc'}

def sanity_checks(self):
# FIXME(decentral1se): Implement sanity checks
pass
4 changes: 4 additions & 0 deletions molecule/driver/lxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ def login_options(self, instance_name):

def ansible_connection_options(self, instance_name):
return {'ansible_connection': 'lxd'}

def sanity_checks(self):
# FIXME(decentral1se): Implement sanity checks
pass
4 changes: 4 additions & 0 deletions molecule/driver/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,7 @@ def _get_instance_config(self, instance_name):

return next(item for item in instance_config_dict
if item['instance'] == instance_name)

def sanity_checks(self):
# FIXME(decentral1se): Implement sanity checks
pass
4 changes: 4 additions & 0 deletions molecule/driver/vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,7 @@ def _get_instance_config(self, instance_name):

return next(item for item in instance_config_dict
if item['instance'] == instance_name)

def sanity_checks(self):
# FIXME(decentral1se): Implement sanity checks
pass
2 changes: 2 additions & 0 deletions molecule/provisioner/ansible_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def bake(self):
if options.get('become'):
del options['become']

self._config.driver.sanity_checks()

self._ansible_command = sh.ansible_playbook.bake(
options,
self._playbook,
Expand Down
5 changes: 5 additions & 0 deletions molecule/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def driver(self):
def prepared(self):
return self._data.get('prepared')

@property
def sanity_checked(self):
return self._data.get('sanity_checked')

@marshal
def reset(self):
self._data = self._default_data()
Expand Down Expand Up @@ -127,6 +131,7 @@ def _default_data(self):
'created': False,
'driver': None,
'prepared': None,
'sanity_checked': False,
}

def _load_file(self):
Expand Down
1 change: 1 addition & 0 deletions test/unit/provisioner/test_ansible_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def test_bake(_inventory_directory, _instance):

def test_bake_removes_non_interactive_options_from_non_converge_playbooks(
_inventory_directory, _instance):

_instance.bake()

x = [
Expand Down

0 comments on commit ee2adb7

Please sign in to comment.