Skip to content

Commit

Permalink
Add molecule tests and fix lint issues (#2)
Browse files Browse the repository at this point in the history
* Add autodetection of disk setup from lsblk

* Sort disks when autodetecting, do not overwrite RAID setup

* Confirm installimage run for every host in the play

* Set up basic molecule tests & fix lint issues

* Run molecule lint in travis

* Extract config tasks and use them in tests

* Add testcase for raid1 though not for autodetect

* Add testcase for raid1 though not for autodetect

* Run all scenarios from molecule in travis

* Add tests for installed image

* Remove dependency from molecule config

* Extract LICENSE, adjust README
  • Loading branch information
dirkaholic authored Nov 13, 2019
1 parent e95edb6 commit efd2b7f
Show file tree
Hide file tree
Showing 21 changed files with 369 additions and 95 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/meta/.galaxy_install_info

.vscode/settings.json
ansible.cfg
*.pyc
31 changes: 13 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
---
language: python
python: "2.7"
services: docker

sudo: required
dist: trusty

# Install ansible
addons:
apt:
packages:
- python-pip
env:
global:
- ROLE_NAME: hetzner_installimage

install:
# Install ansible
- pip install ansible

# Check ansible version
- ansible --version
# Install test dependencies.
- pip install molecule docker

# Create ansible.cfg with correct roles_path
- printf '[defaults]\nroles_path=../' >ansible.cfg
before_script:
# Use actual Ansible Galaxy role name for the project directory.
- cd ../
- mv ansible-role-$ROLE_NAME nl2go.$ROLE_NAME
- cd nl2go.$ROLE_NAME

script:
# Basic role syntax check
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
# Run tests.
- molecule test --all

notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
33 changes: 33 additions & 0 deletions .yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
# Based on ansible-lint config
extends: default

rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
colons:
max-spaces-after: -1
level: error
commas:
max-spaces-after: -1
level: error
comments: disable
comments-indentation: disable
document-start: disable
empty-lines:
max: 3
level: error
hyphens:
level: error
indentation: disable
key-duplicates: enable
line-length: disable
new-line-at-end-of-file: disable
new-lines:
type: unix
trailing-spaces: disable
truthy: disable
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 newsletter2go.com

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.
78 changes: 30 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
hetzner_installimage
====================
# hetzner_installimage

[![Build Status](https://travis-ci.com/nl2go/ansible-role-hetzner_installimage.svg?branch=master)](https://travis-ci.com/nl2go/ansible-role-hetzner_installimage)

Use this role to base provision your hetzner machines with the hetzner installimage script and your public ssh key (but all automated through ansible).

Requirements
------------
## Requirements

This role requires you to have a server at hetzner.de and some api credentials as well as your provisioning key uploaded to the hetzner robot. Read more about that under https://wiki.hetzner.de/index.php/Robot_Webservice

Also for security reasons, the role will not reboot and install the machine, if it can't access it and check for the existance of a hostcode file. Therefor you need to have your provisioning key allready installed on the machine(s) you want to install (can be done through robot).

Config Variables
---------------
## Config Variables

The following variables are suggested to be set within your ansible.cfg file

Expand All @@ -29,17 +26,10 @@ The following variables are suggested to be set within your ansible.cfg file
scp_if_ssh = True
control_path = %(directory)s/%%h-%%r

Role Variables
--------------
## Role Variables

The default set of variables defines the installimage and needs at best to be overwritten in group_vars/host_vars

hetzner_installimage_install_drives:
- DRIVE1 /dev/sda
- DRIVE2 /dev/sdb
hetzner_installimage_install_raid:
- SWRAID 1
- SWRAIDLEVEL 1
hetzner_installimage_install_bootloader: grub
hetzner_installimage_install_hostname: Debian-87-jessie-64-minimal
hetzner_installimage_install_partitions:
Expand All @@ -48,7 +38,20 @@ The default set of variables defines the installimage and needs at best to be ov
- PART / ext4 all
hetzner_installimage_install_image: /root/.oldroot/nfs/images/Ubuntu-1604-xenial-64-minimal.tar.gz

The following mandatory variables need to be set in group_vars/host_vars to allow communication with the webservice and deployment of the public key
The role includes an autodetection of RAID values and setup. It will configure no RAID if one disk is found and
RAID1 if 2 disks are found. The automatic RAID config can be overwritten with the following variables:

hetzner_installimage_install_drives:
- DRIVE1 /dev/sda
- DRIVE2 /dev/sdb
hetzner_installimage_install_raid:
- SWRAID 1
- SWRAIDLEVEL 0

It is also possible to just set `hetzner_installimage_install_raid` and let the autodetection find the respective disks.

The following mandatory variables need to be set in group_vars/host_vars or as extra vars to allow communication with
the webservice and deployment of the public key:

hetzner_installimage_webservice_username: username
hetzner_installimage_webservice_password: password
Expand All @@ -58,15 +61,17 @@ The following variable can be set optionally, to set the hostname within the het

hetzner_server_name: __YOUR_SERVER_NAME__

Example Playbook
----------------
## Example Playbook

- hosts: hetzner
roles:
- { role: nl2go.hetzner_installimage }
vars:
hetzner_installimage_webservice_username: "{{ hetzner_robot_api_user}}"
hetzner_installimage_webservice_password: "{{ hetzner_robot_api_pass}}"
hetzner_server_name: "{{ inventory_hostname }}"

Installation Steps
------------------
## Installation Steps

* Install a new machine
1. Enter your hetzner robot (robot.your-server.de)
Expand All @@ -88,8 +93,7 @@ Installation Steps

If you are sure, you will not accidentally purge a running machine, allready in use, you can directly run the role with the extra variable **--extra-vars "{ hetzner_installimage_ignore_hostcode: True }"**. This way the role will not check the machine for an existing /etc/hostcode file but will also not prevent the machine from being purged accidentally!

Available images
----------------
## Available images

The OS images are located in the folder /root/.oldroot/nfs/images/ inside the rescue system. The
following images are available at 25 Oct 2019:
Expand All @@ -109,33 +113,11 @@ following images are available at 25 Oct 2019:
* Ubuntu-1804-bionic-64-nextcloud.tar.gz
* Ubuntu-1904-disco-64-minimal.tar.gz

License
-------

MIT License

Copyright (c) 2019 newsletter2go.com

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.
## License

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.
See the [LICENSE.md](LICENSE.md) file for details

Author Information
------------------
## Maintainers

* https://github.com/andrelohmann
* https://github.com/nl2go
- [andrelohmann](https://github.com/andrelohmann)
- [dirkaholic](https://github.com/dirkaholic)
6 changes: 3 additions & 3 deletions meta/main.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
galaxy_info:
author: andrelohmann
description: ansible galaxy role to talk to the hetzner.de api to provision your boxes with installimage but through ansible
company: smartformer UG
author: nl2go
description: ansible galaxy role to talk to the hetzner.de api to provision your boxes with installimage through ansible
company: Newsletter2Go GmbH

license: MIT

Expand Down
22 changes: 22 additions & 0 deletions molecule/default/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Molecule managed

{% if item.registry is defined %}
FROM {{ item.registry.url }}/{{ item.image }}
{% else %}
FROM {{ item.image }}
{% endif %}

{% if item.env is defined %}
{% for var, value in item.env.items() %}
{% if value %}
ENV {{ var }} {{ value }}
{% endif %}
{% endfor %}
{% endif %}

RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates iproute2 && apt-get clean; \
elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash iproute && dnf clean all; \
elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash iproute && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \
elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml iproute2 && zypper clean -a; \
elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \
elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates iproute2 && xbps-remove -O; fi
22 changes: 22 additions & 0 deletions molecule/default/INSTALL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
*******
Docker driver installation guide
*******

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

* Docker Engine

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[docker]'
22 changes: 22 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
driver:
name: docker
lint:
name: yamllint
platforms:
- name: debian9
image: debian:9
- name: debian10
image: debian:10
- name: Ubuntu16
image: ubuntu:16.04
- name: Ubuntu18
image: ubuntu:18.04
provisioner:
name: ansible
lint:
name: ansible-lint
verifier:
name: testinfra
lint:
name: flake8
8 changes: 8 additions & 0 deletions molecule/default/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Converge
hosts: all
tasks:
- name: Execute config task writing the installimage config file
include_role:
name: nl2go.hetzner_installimage
tasks_from: config
26 changes: 26 additions & 0 deletions molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os

import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']
).get_hosts('all')


def test_autosetupfile_exists(host):
f = host.file('/autosetup')

assert f.exists
assert f.user == 'root'
assert f.group == 'root'


def test_autosetupfile_has_correct_config(host):
f = host.file('/autosetup')

assert f.contains('SWRAID 0')
assert f.contains('SWRAIDLEVEL 0')
assert f.contains('DRIVE1')
assert not f.contains('DRIVE2')

assert f.contains('Ubuntu-1604-xenial-64-minimal')
22 changes: 22 additions & 0 deletions molecule/raid1/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Molecule managed

{% if item.registry is defined %}
FROM {{ item.registry.url }}/{{ item.image }}
{% else %}
FROM {{ item.image }}
{% endif %}

{% if item.env is defined %}
{% for var, value in item.env.items() %}
{% if value %}
ENV {{ var }} {{ value }}
{% endif %}
{% endfor %}
{% endif %}

RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates iproute2 && apt-get clean; \
elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash iproute && dnf clean all; \
elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash iproute && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \
elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml iproute2 && zypper clean -a; \
elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \
elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates iproute2 && xbps-remove -O; fi
Loading

0 comments on commit efd2b7f

Please sign in to comment.