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

Molecule multipass driver #15

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions roles/molecule_multipass/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Molecule Multipass driver
=========================

Multipass driver for Molecule.

Requirements
------------

This role requires the following tools to be installed:
- [multipass](https://multipass.run/)
- [molecule](https://ansible.readthedocs.io/projects/molecule/)

How to use?
-----------

This section is being written. Coming soon...

License
-------

GNU General Public License v3.0 or later

Author Information
------------------

Created by Kenneth KOFFI, you can reach him on the following platforms:
- medium: https://theko2fi.medium.com
- github: https://github.com/theko2fi
- linkedin: https://www.linkedin.com/in/kenneth-koffi-6b1218178

3 changes: 3 additions & 0 deletions roles/molecule_multipass/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
# defaults file for theko2fi.multipass.molecule
dest: "{{ ('molecule/' + (scenario_name | default('default'))) | realpath }}"
2 changes: 2 additions & 0 deletions roles/molecule_multipass/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# handlers file for theko2fi.multipass.molecule
52 changes: 52 additions & 0 deletions roles/molecule_multipass/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)

# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker

# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)

min_ansible_version: 2.1

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:

#
# Provide a list of supported platforms, and for each platform a list of versions.
# If you don't wish to enumerate all versions for a particular platform, use 'all'.
# To view available platforms and versions (or releases), visit:
# https://galaxy.ansible.com/api/v1/platforms/
#
# platforms:
# - name: Fedora
# versions:
# - all
# - 25
# - name: SomePlatform
# versions:
# - all
# - 1.0
# - 7
# - 99.99

galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.

dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.
27 changes: 27 additions & 0 deletions roles/molecule_multipass/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
# tasks file for theko2fi.multipass.molecule

- name: Check if destination folder exists
ansible.builtin.file:
path: "{{ dest }}"
state: directory
mode: "0700"
- name: Check if destination folder is empty
ansible.builtin.find:
paths: "{{ dest }}"
register: dest_content
- name: Fail if destination folder is not empty
when: dest_content.matched > 0
ansible.builtin.fail:
msg: Refused to expand templates as destination folder '{{ dest }}' as it already has content in it.
- name: Expand templates
vars:
dest_file: "{{ dest }}/{{ item | basename | regex_replace('\\.j2$', '') }}"
ansible.builtin.template:
src: "{{ item }}"
dest: "{{ dest_file }}"
mode: "0644"
with_fileglob:
- templates/scenario/*.j2
loop_control:
label: "{{ dest_file | relpath }}"
8 changes: 8 additions & 0 deletions roles/molecule_multipass/templates/scenario/converge.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Converge
hosts: all
gather_facts: true
tasks:
- name: Replace this task with one that validates your content
ansible.builtin.debug:
msg: "This is the effective test"
44 changes: 44 additions & 0 deletions roles/molecule_multipass/templates/scenario/create.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
{% raw -%}
- name: Create
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ molecule_no_log }}"
vars:
molecule_labels:
owner: molecule
tags:
- always
tasks:
- name: Set async_dir for HOME env
ansible.builtin.set_fact:
ansible_async_dir: "{{ lookup('env', 'HOME') }}/.ansible_async/"
when: (lookup('env', 'HOME'))

- name: Create molecule instance(s)
theko2fi.multipass.multipass_vm:
name: "{{ item.name }}"
cloud_init: "{{ item.cloud_init | default(omit) }}"
cpus: "{{ item.cpu | default(omit) }}"
disk: "{{ item.disk | default(omit) }}"
image: "{{ item.image | default(omit) }}"
memory: "{{ item.memory | default(omit) }}"
recreate: "{{ item.recreate | default(omit) }}"
state: 'started'
register: server
with_items: "{{ molecule_yml.platforms }}"
loop_control:
label: "{{ item.name }}"
no_log: false
async: 7200
poll: 0

- name: Wait for instance(s) creation to complete
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
register: multipass_jobs
until: multipass_jobs.finished
retries: 300
with_items: "{{ server.results }}"
{%- endraw %}
39 changes: 39 additions & 0 deletions roles/molecule_multipass/templates/scenario/destroy.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
{% raw -%}
- name: Destroy
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ molecule_no_log }}"
tags:
- always
tasks:
- name: Set async_dir for HOME env
ansible.builtin.set_fact:
ansible_async_dir: "{{ lookup('env', 'HOME') }}/.ansible_async/"
when: (lookup('env', 'HOME'))

- name: Destroy molecule instance(s)
theko2fi.multipass.multipass_vm:
name: "{{ item.name }}"
state: absent
purge: true
register: server
loop: "{{ molecule_yml.platforms }}"
loop_control:
label: "{{ item.name }}"
no_log: false
async: 7200
poll: 0

- name: Wait for instance(s) deletion to complete
ansible.builtin.async_status:
jid: "{{ item.ansible_job_id }}"
register: multipass_jobs
until: multipass_jobs.finished
retries: 300
loop: "{{ server.results }}"
loop_control:
label: "{{ item.item.name }}"

{%- endraw %}
15 changes: 15 additions & 0 deletions roles/molecule_multipass/templates/scenario/molecule.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
dependency:
name: galaxy
driver:
options:
managed: False
login_cmd_template: 'multipass exec {instance} -- bash'
ansible_connection_options:
ansible_connection: theko2fi.multipass.multipass
platforms:
- name: instance-molecule
provisioner:
name: ansible
verifier:
name: ansible
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
collections:
- theko2fi.multipass
2 changes: 2 additions & 0 deletions roles/molecule_multipass/tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
localhost

8 changes: 8 additions & 0 deletions roles/molecule_multipass/tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Create a new molecule scenario
hosts: localhost
connection: local
remote_user: root
gather_facts: false
roles:
- theko2fi.multipass.molecule_multipass
2 changes: 2 additions & 0 deletions roles/molecule_multipass/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# vars file for theko2fi.multipass.molecule
Loading