Skip to content

Commit

Permalink
Add Linode driver (v3 API) (ansible#1458)
Browse files Browse the repository at this point in the history
* Add Linode driver.

Closes ansible#1458.

Signed-off-by: Luke Murphy <[email protected]>

* Fix incorrect copying of schema for validation.

Full credit to @mickelmayers in ansible#1639.

Originall submitted with message like:

Platforms_base_schema destroy copy.deepcopy(base_schema). Then when
validating multiply molecules it's running with errors. Eg. two moleule
with different drivers. One Azure second docker . If docker will be
validated first from it schema we are getting. Moving
platforms_base_schema to base_schema and changing the util.merge_dicts
fixes the issue.

Closes ansible#1639.

Signed-off-by: Luke Murphy <[email protected]>

* Merge schema only once.

This change works towards stopping schema merging clobbering themselves.
Without this change, the `test_dependency_shell_has_errors` test is
failing due to an incorrect schema.

Signed-off-by: Luke Murphy <[email protected]>

* Remove incorrect driver for fixture.

The `test_platforms_docker_has_errors` test is failing otherwise.

Signed-off-by: Luke Murphy <[email protected]>
  • Loading branch information
lwm authored and ssbarnea committed Jan 31, 2019
1 parent 6682cdf commit 67db085
Show file tree
Hide file tree
Showing 25 changed files with 992 additions and 34 deletions.
6 changes: 6 additions & 0 deletions doc/source/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ GCE
.. autoclass:: molecule.driver.gce.GCE()
:undoc-members:

Linode
^^^^^^

.. autoclass:: molecule.driver.linode.Linode()
:undoc-members:

LXC
^^^

Expand Down
4 changes: 4 additions & 0 deletions molecule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from molecule.driver import docker
from molecule.driver import ec2
from molecule.driver import gce
from molecule.driver import linode
from molecule.driver import lxc
from molecule.driver import lxd
from molecule.driver import openstack
Expand Down Expand Up @@ -163,6 +164,8 @@ def driver(self):
driver = ec2.EC2(self)
elif driver_name == 'gce':
driver = gce.GCE(self)
elif driver_name == 'linode':
driver = linode.Linode(self)
elif driver_name == 'lxc':
driver = lxc.LXC(self)
elif driver_name == 'lxd':
Expand Down Expand Up @@ -479,6 +482,7 @@ def molecule_drivers():
docker.Docker(None).name,
ec2.EC2(None).name,
gce.GCE(None).name,
linode.Linode(None).name,
lxc.LXC(None).name,
lxd.LXD(None).name,
openstack.Openstack(None).name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ platforms:
image: debian-8
{%- elif cookiecutter.driver_name == 'lxc' %}
- name: instance
{%- elif cookiecutter.driver_name == 'linode' %}
- name: instance
plan: 1
datacenter: 7
distribution: 129
{%- elif cookiecutter.driver_name == 'lxd' %}
- name: instance
{%- elif cookiecutter.driver_name == 'openstack' %}
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,17 @@
********************************
Linode driver installation guide
********************************

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

* ``LINODE_API_KEY`` exposed in your environment
* ``linode-python`` Python dependency installed
* Supports Python 2.7+ but is not Python 3.X compatible.

Install
=======

.. code-block:: bash
$ pip install 'molecule[linode]'
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
{% raw -%}
- name: Create
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
vars:
ssh_user: root
ssh_port: 22
ssh_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
ssh_pass: "{{ lookup('password', lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') + '/ssh_pass') }}"
tasks:
- name: Create SSH key
user:
name: "{{ lookup('env', 'USER') }}"
generate_ssh_key: true
ssh_key_file: "{{ ssh_path }}"
register: generated_ssh_key

- name: Create molecule instance(s)
linode:
name: "{{ item.name }}"
api_key: "{{ lookup('env', 'LINODE_API_KEY') }}"
datacenter: "{{ item.datacenter }}"
plan: "{{ item.plan }}"
distribution: "{{ item.distribution }}"
ssh_pub_key: "{{ generated_ssh_key.ssh_public_key }}"
password: "{{ ssh_pass }}"
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: linode_jobs
until: linode_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.instance.name }}",
'ssh_pass': "{{ ssh_pass }}",
'address': "{{ item.instance.ipv4 }}",
'user': "{{ ssh_user }}",
'port': "{{ ssh_port }}",
'identity_file': "{{ ssh_path }}",
'linode_id': "{{ item.instance.id }}", }
with_items: "{{ linode_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 | 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
with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
{%- endraw %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
{% raw -%}
- name: Destroy
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
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)
linode:
name: "{{ item.instance }}"
linode_id: "{{ item.linode_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: linode_jobs
until: linode_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 }}
153 changes: 153 additions & 0 deletions molecule/driver/linode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Copyright (c) 2018-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.driver import base

from molecule import util


class Linode(base.Base):
"""
The class responsible for managing `Linode`_ instances. `Linode`_
is `not` the default driver used in Molecule.
Molecule leverages Ansible's `linode_module`_, by mapping variables
from `molecule.yml` into `create.yml` and `destroy.yml`.
.. important::
Please note, the Ansible Linode module is currently using the deprecated
API and there are a number of outstanding usability issues with the module.
However, there is ongoing work to migrate to the new API (v4) and migrate
this driver when that time comes. In the mean time, this driver can be
considered at somewhat of an Alpha status quality.
.. _`linode_module`: https://docs.ansible.com/ansible/latest/modules/linode_module.html
.. code-block:: yaml
driver:
name: linode
platforms:
- name: instance
plan: 1
datacenter: 7
distribution: 129
.. code-block:: bash
$ pip install 'molecule[linode]'
Change the options passed to the ssh client.
.. code-block:: yaml
driver:
name: linode
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: linode
safe_files:
- foo
.. _`Linode`: https://www.linode.com/
""" # noqa

def __init__(self, config):
super(Linode, self).__init__(config)
self._name = 'linode'

@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_ssh_pass': d['ssh_pass'],
'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 any((
# NOTE(lwm): Handle both because of transitioning label logic
# https://github.com/ansible/ansible/pull/44719
item['instance'] == '{}_{}'.format(item['linode_id'],
instance_name),
item['instance'] == '{}-{}'.format(item['linode_id'],
instance_name))))
Loading

0 comments on commit 67db085

Please sign in to comment.