Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand LXD driver functionality #1436

Merged
merged 14 commits into from
Sep 15, 2018
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion molecule/command/init/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Template(base.Base):
"""
.. program:: molecule init template --url https://example.com/user/cookiecutter-repo

.. option:: molecule init init template --url https://example.com/user/cookiecutter-repo
.. option:: molecule init template --url https://example.com/user/cookiecutter-repo

Initialize a new role from a Cookiecutter URL.
""" # noqa
Expand Down

This file was deleted.

This file was deleted.

20 changes: 20 additions & 0 deletions molecule/driver/lxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ class LXD(base.Base):

driver:
name: lxd
platforms:
- name: instance
url: https://127.0.0.1:8443
cert_file: /root/.config/lxc/client.crt
key_file: /root/.config/lxc/client.key
trust_password: password
source:
type: image
mode: pull|local
server: https://images.linuxcontainers.org
protocol: lxd|simplestreams
alias: ubuntu/xenial/amd64
architecture: x86_64|i686
devices:
kvm:
path: /dev/kvm
type: unix-char
profiles:
- default
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you have implemented this API for molecule.yml we need to validate the schema. Can you update the v2 schema similar to what we did with dockder and vagrant.

https://github.com/metacloud/molecule/blob/master/molecule/model/schema_v2.py#L596
https://github.com/metacloud/molecule/blob/master/molecule/model/schema_v2.py#L862

And we will need to update tests with working and failing tests:

https://github.com/metacloud/molecule/blob/master/test/unit/model/v2/test_platforms_section.py

force_stop: True|False

Provide the files Molecule will preserve upon each subcommand execution.

Expand Down
81 changes: 81 additions & 0 deletions molecule/model/schema_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,85 @@ def pre_validate_base_schema(env, keep_string):
},
}

platforms_lxd_schema = {
'platforms': {
'type': 'list',
'schema': {
'type': 'dict',
'schema': {
'name': {
'type': 'string',
},
'url': {
'type': 'string',
},
'cert_file': {
'type': 'string',
},
'key_file': {
'type': 'string',
},
'trust_password': {
'type': 'string',
},
'source': {
'type': 'dict',
'schema': {
'type': {
'type': 'string',
},
'mode': {
'type': 'string',
'allowed': [
'pull',
'local',
],
},
'server': {
'type': 'string',
},
'protocol': {
'type': 'string',
'allowed': [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the schema as specific as possible, these are the only values valid for the LXD api. There are now allowed values (source).

'lxd',
'simplestreams',
],
},
'alias': {
'type': 'string',
},
},
},
'architecture': {
'type': 'string',
'allowed': [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here (source)

'x86_64',
'i686',
],
},
'devices': {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a good look at the devices schema, that is the one I struggled with since I am not familiar with the markup. Might be not 100% correct

'type': 'dict',
'schema': {
'type': 'dict',
'keyschema': {
'type': 'string',
},
},
},
'profiles': {
'type': 'list',
'schema': {
'type': 'string'
}
},
'force_stop': {
'type': 'boolean',
},
}
}
},
}

dependency_command_nullable_schema = {
'dependency': {
'type': 'dict',
Expand Down Expand Up @@ -863,6 +942,8 @@ def validate(c):
elif c['driver']['name'] == 'vagrant':
util.merge_dicts(schema, driver_vagrant_provider_section_schema)
util.merge_dicts(schema, platforms_vagrant_schema)
elif c['driver']['name'] == 'lxd':
util.merge_dicts(schema, platforms_lxd_schema)
else:
util.merge_dicts(schema, platforms_base_schema)

Expand Down
29 changes: 29 additions & 0 deletions molecule/provisioner/ansible/playbooks/lxd/create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
- 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) }}"
tasks:
- name: Create default source variable
set_fact:
default_source:
type: image
server: https://images.linuxcontainers.org
alias: ubuntu/xenial/amd64

- name: Create molecule instance(s)
lxd_container:
url: "{{ item.url | default(omit) }}"
cert_file: "{{ item.cert_file | default(omit) }}"
key_file: "{{ item.key_file | default(omit) }}"
trust_password: "{{ item.trust_password | default(omit) }}"
name: "{{ item.name }}"
state: started
source: "{{ default_source | combine(item.source | default({})) }}"
architecture: "{{ item.architecture | default(omit) }}"
devices: "{{ item.devices | default(omit) }}"
profiles: "{{ item.profiles | default(omit) }}"
wait_for_ipv4_addresses: true
timeout: 600
with_items: "{{ molecule_yml.platforms }}"
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
{% raw %}
- name: Destroy
hosts: localhost
connection: local
Expand All @@ -8,8 +7,11 @@
tasks:
- name: Destroy molecule instance(s)
lxd_container:
url: "{{ item.url | default(omit)}}"
cert_file: "{{ item.cert_file | default(omit) }}"
key_file: "{{ item.key_file | default(omit) }}"
trust_password: "{{ item.trust_password | default(omit) }}"
name: "{{ item.name }}"
state: absent
force_stop: "{{ item.force_stop | default(true) }}"
with_items: "{{ molecule_yml.platforms }}"
{%- endraw %}
53 changes: 53 additions & 0 deletions molecule/provisioner/ansible/playbooks/lxd/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
- name: Prepare
hosts: all
gather_facts: false
tasks:
- name: Test if Python is installed
raw: python -V # execute Python
register: python_version
changed_when: "'not found' in python_version.stderr"
failed_when: false

- name: Install Python when not installed
raw: |
if [ -x "$(command -v apt-get)" ]
then
apt-get update && apt-get install -y python
elif [ -x "$(command -v yum)" ]
then
yum install -y python
elif [ -x "$(command -v zypper)" ]
then
zypper -n --gpg-auto-import-keys refresh && zypper -n install -y python python-xml
elif [ -x "$(command -v apk)" ]
then
apk update && apk add python
elif [ -x "$(command -v pacman)" ]
then
pacman -Syu --noconfirm python2
elif [ -x "$(command -v dnf)" ]
then
dnf --assumeyes install python python-devel python2-dnf
elif [ -x "$(command -v emerge)" ]
then
emerge --ask n dev-lang/python-2
fi
when: "'not found' in python_version.stderr"

- name: Gathering Facts
setup: # aka gather_facts

- name: Install equery on Gentoo based containers when not available
raw: test -e /usr/bin/equery || emerge --ask n gentoolkit
register: equery_status
changed_when: equery_status.stdout != ''
when: ansible_os_family == 'Gentoo'

- name: Install some basic packages because LXC containers are bare
package:
name:
- ca-certificates
- curl
- sudo
state: present
22 changes: 15 additions & 7 deletions test/resources/playbooks/lxd/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@
gather_facts: false
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
tasks:
- name: Create default source variable
set_fact:
default_source:
type: image
server: https://images.linuxcontainers.org
alias: ubuntu/xenial/amd64

- name: Create molecule instance(s)
lxd_container:
url: "{{ item.url | default(omit) }}"
cert_file: "{{ item.cert_file | default(omit) }}"
key_file: "{{ item.key_file | default(omit) }}"
trust_password: "{{ item.trust_password | default(omit) }}"
name: "{{ item.name }}"
state: started
source:
type: image
mode: pull
server: https://images.linuxcontainers.org
protocol: lxd
alias: ubuntu/xenial/amd64
profiles: ["default"]
source: "{{ default_source | combine(item.source | default({})) }}"
architecture: "{{ item.architecture | default(omit) }}"
devices: "{{ item.devices | default(omit) }}"
profiles: "{{ item.profiles | default(omit) }}"
wait_for_ipv4_addresses: true
timeout: 600
with_items: "{{ molecule_yml.platforms }}"
4 changes: 4 additions & 0 deletions test/resources/playbooks/lxd/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
tasks:
- name: Destroy molecule instance(s)
lxd_container:
url: "{{ item.url | default(omit)}}"
cert_file: "{{ item.cert_file | default(omit) }}"
key_file: "{{ item.key_file | default(omit) }}"
trust_password: "{{ item.trust_password | default(omit) }}"
name: "{{ item.name }}"
state: absent
force_stop: "{{ item.force_stop | default(true) }}"
Expand Down
9 changes: 0 additions & 9 deletions test/scenarios/driver/lxd/molecule/default/prepare.yml

This file was deleted.

9 changes: 0 additions & 9 deletions test/scenarios/driver/lxd/molecule/multi-node/prepare.yml

This file was deleted.

Loading