Skip to content

Commit

Permalink
Add digitalocean driver (#1736)
Browse files Browse the repository at this point in the history
* Adding support for DigitalOcean driver

Signed-off-by: Ben Mildren <[email protected]>

* applied requested changes

Signed-off-by: Ben Mildren <[email protected]>

* removing superfluous wait for boot (we're waiting during the create)

Signed-off-by: Ben Mildren <[email protected]>

* fixed linting issues

Signed-off-by: Ben Mildren <[email protected]>

* more linting issues

Signed-off-by: Ben Mildren <[email protected]>

* Updating no_log behaviour to be inline with recent changes and driver support functional test

Signed-off-by: Ben Mildren <[email protected]>
  • Loading branch information
bmildren authored and ssbarnea committed Apr 9, 2019
1 parent 18c0659 commit 5c93b24
Show file tree
Hide file tree
Showing 26 changed files with 938 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/source/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ Delegated
.. autoclass:: molecule.driver.delegated.Delegated()
:undoc-members:


DigitalOcean
^^^^^^^^^^^^

.. autoclass:: molecule.driver.digitalocean.DigitalOcean()
:undoc-members:


Docker
^^^^^^

Expand Down
4 changes: 4 additions & 0 deletions molecule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from molecule.dependency import shell
from molecule.driver import azure
from molecule.driver import delegated
from molecule.driver import digitalocean
from molecule.driver import docker
from molecule.driver import ec2
from molecule.driver import gce
Expand Down Expand Up @@ -160,6 +161,8 @@ def driver(self):
driver = azure.Azure(self)
elif driver_name == 'delegated':
driver = delegated.Delegated(self)
elif driver_name == 'digitalocean':
driver = digitalocean.DigitalOcean(self)
elif driver_name == 'docker':
driver = docker.Docker(self)
elif driver_name == 'ec2':
Expand Down Expand Up @@ -491,6 +494,7 @@ def molecule_drivers():
return [
azure.Azure(None).name,
delegated.Delegated(None).name,
digitalocean.DigitalOcean(None).name,
docker.Docker(None).name,
ec2.EC2(None).name,
gce.GCE(None).name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ lint:
platforms:
{%- if cookiecutter.driver_name == 'azure' %}
- name: instance
{%- elif cookiecutter.driver_name == 'digitalocean' %}
- name: instance
region_id: nyc3
image_id: ubuntu-18-10-x64
size_id: s-1vcpu-1gb
{%- elif cookiecutter.driver_name == 'docker' %}
- name: instance
image: centos:7
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"molecule_directory": "molecule",
"role_name": "OVERRIDEN",
"scenario_name": "OVERRIDEN"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**************************************
DigitalOcean driver installation guide
**************************************

Requirements
============

* ``DO_API_KEY`` or ``DO_API_TOKEN`` exposed in your environment
* Only supported on Python 2.7, due to current dependency on dopy

Install
=======

Please refer to the `Virtual environment`_ documentation for installation best
practices. If not using a virtual environment, please consider passing the
widely recommended `'--user' flag`_ when invoking ``pip``.

.. _Virtual environment: https://virtualenv.pypa.io/en/latest/
.. _'--user' flag: https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site

.. code-block:: bash
$ pip install 'molecule[digitalocean]'
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
{% raw -%}
- name: Create
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ molecule_no_log }}"
vars:
ssh_user: root
ssh_port: 22

keypair_name: molecule_key
keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
tasks:
- name: Create local keypair
user:
name: "{{ lookup('env', 'USER') }}"
generate_ssh_key: true
ssh_key_file: "{{ keypair_path }}"
register: local_keypair

- name: Create remote keypair
digital_ocean_sshkey:
name: "{{ keypair_name }}"
ssh_pub_key: "{{ local_keypair.ssh_public_key }}"
state: present
register: remote_keypair

- name: Create molecule instance(s)
digital_ocean:
command: droplet
name: "{{ item.name }}"
unique_name: true
region_id: "{{ item.region_id }}"
image_id: "{{ item.image_id }}"
size_id: "{{ item.size_id }}"
ssh_key_ids: "{{ remote_keypair.data.ssh_key.id }}"
private_networking: true
wait: true
wait_timeout: 300
state: present
register: server
with_items: "{{ molecule_yml.platforms }}"
async: 7200
poll: 0

- name: Wait for instance(s) creation to complete
async_status:
jid: "{{ item.ansible_job_id }}"
register: digitalocean_jobs
until: digitalocean_jobs.finished
retries: 300
with_items: "{{ server.results }}"

# Mandatory configuration for Molecule to function.

- name: Populate instance config dict
set_fact:
instance_conf_dict: {
'instance': "{{ item.droplet.name }}",
'address': "{{ item.droplet.ip_address }}",
'user': "{{ ssh_user }}",
'port': "{{ ssh_port }}",
'identity_file': "{{ keypair_path }}",
'droplet_id': "{{ item.droplet.id }}", }
with_items: "{{ digitalocean_jobs.results }}"
register: instance_config_dict
when: server.changed | bool

- name: Convert instance config dict to a list
set_fact:
instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
when: server.changed | bool

- name: Dump instance config
copy:
content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
dest: "{{ molecule_instance_config }}"
when: server.changed | bool

- name: Wait for SSH
wait_for:
port: "{{ ssh_port }}"
host: "{{ item.address }}"
search_regex: SSH
delay: 10
timeout: 320
with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
{%- endraw %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
{% raw -%}
- name: Destroy
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ molecule_no_log }}"
tasks:
- block:
- name: Populate instance config
set_fact:
instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
skip_instances: false
rescue:
- name: Populate instance config when file missing
set_fact:
instance_conf: {}
skip_instances: true

- name: Destroy molecule instance(s)
digital_ocean:
name: "{{ item.instance }}"
id: "{{ item.droplet_id }}"
state: absent
register: server
with_items: "{{ instance_conf }}"
when: not skip_instances
async: 7200
poll: 0

- name: Wait for instance(s) deletion to complete
async_status:
jid: "{{ item.ansible_job_id }}"
register: digitalocean_jobs
until: digitalocean_jobs.finished
retries: 300
with_items: "{{ server.results }}"


# Mandatory configuration for Molecule to function.

- name: Populate instance config
set_fact:
instance_conf: {}

- name: Dump instance config
copy:
content: "{{ instance_conf | molecule_to_yaml | molecule_header }}"
dest: "{{ molecule_instance_config }}"
when: server.changed | bool
{%- endraw %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Converge
hosts: all
roles:
- role: {{ cookiecutter.role_name }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
{% raw -%}
- name: Prepare
hosts: all
gather_facts: false
tasks:
- name: Install python for Ansible
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
become: true
changed_when: false
{%- endraw %}
138 changes: 138 additions & 0 deletions molecule/driver/digitalocean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Copyright (c) 2019 Red Hat, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from molecule import logger
from molecule.driver import base

from molecule import util

log = logger.get_logger(__name__)


class DigitalOcean(base.Base):
"""
This class is responsible for managing `DigitalOcean`_ instances.
`DigitalOcean`_ is **not** the default driver used in Molecule.
Molecule leverages Ansible's `digital_ocean_module`_, by mapping variables
from ``molecule.yml`` into ``create.yml`` and ``destroy.yml``.
.. _`digital_ocean_module`: https://docs.ansible.com/ansible/latest/modules/digital_ocean_module.html#digital-ocean-module
.. code-block:: yaml
driver:
name: digitalocean
platforms:
- name: instance
.. code-block:: bash
$ pip install 'molecule[digitalocean]'
Change the options passed to the ssh client.
.. code-block:: yaml
driver:
name: digitalocean
ssh_connection_options:
-o ControlPath=~/.ansible/cp/%r@%h-%p
.. important::
Molecule does not merge lists, when overriding the developer must
provide all options.
Provide the files Molecule will preserve upon each subcommand execution.
.. code-block:: yaml
driver:
name: digitalocean
safe_files:
- foo
.. _`DigitalOcean`: https://www.digitalocean.com
""" # noqa

def __init__(self, config):
super(DigitalOcean, self).__init__(config)
self._name = 'digitalocean'

@property
def name(self):
return self._name

@name.setter
def name(self, value):
self._name = value

@property
def login_cmd_template(self):
connection_options = ' '.join(self.ssh_connection_options)

return ('ssh {{address}} '
'-l {{user}} '
'-p {{port}} '
'-i {{identity_file}} '
'{}').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):
d = {'instance': instance_name}

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 {
'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),
}
except StopIteration:
return {}
except IOError:
# Instance has yet to be provisioned , therefore the
# instance_config is not on disk.
return {}

def _get_instance_config(self, instance_name):
instance_config_dict = util.safe_load_file(
self._config.driver.instance_config)

return next(item for item in instance_config_dict
if item['instance'] == instance_name)
1 change: 1 addition & 0 deletions molecule/model/schema_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def pre_validate_base_schema(env, keep_string):
'allowed': [
'azure',
'delegated',
'digitalocean',
'docker',
'ec2',
'gce',
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ docs =
Sphinx
azure =
ansible[azure]
digitalocean =
dopy; python_version<"3.0"
docker =
docker>=2.0.0
ec2 =
Expand Down
Loading

0 comments on commit 5c93b24

Please sign in to comment.