diff --git a/.travis.yml b/.travis.yml index 60015f4..2687786 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,7 @@ services: docker env: global: - ROLE_NAME: k3s + - MOLECULE_SCENARIO: default matrix: - MOLECULE_DISTRO: geerlingguy/docker-centos8-ansible:latest - MOLECULE_DISTRO: geerlingguy/docker-centos7-ansible:latest @@ -36,7 +37,7 @@ env: # Test multiple masters in control plane with PostgreSQL - MOLECULE_DISTRO: geerlingguy/docker-centos8-ansible:latest - MOLECULE_PLAYBOOK: playbook-ha-postgres.yml + MOLECULE_SCENATIO: highavailability install: # Install test dependencies. @@ -45,12 +46,12 @@ install: before_script: # Use actual Ansible Galaxy role name for the project directory. - cd ../ - - mv ansible-role-$ROLE_NAME xanmanning.$ROLE_NAME - - cd xanmanning.$ROLE_NAME + - mv ansible-role-${ROLE_NAME} xanmanning.${ROLE_NAME} + - cd xanmanning.${ROLE_NAME} script: # Run tests. - - molecule test + - molecule test --scenario-name "${MOLECULE_SCENARIO}" notifications: webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 5a3ae42..b0173c6 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -34,14 +34,6 @@ platforms: pre_build_image: ${MOLECULE_PREBUILT:-true} networks: - name: k3snet - - name: database - image: postgres:11-alpine - pre_build_image: true - command: "postgres" - env: - POSTGRES_PASSWORD: "verybadpass" - networks: - - name: k3snet provisioner: name: ansible options: diff --git a/molecule/default/playbook-docker-altport-wireguard.yml b/molecule/default/playbook-docker-altport-wireguard.yml index e417c05..1db8114 100644 --- a/molecule/default/playbook-docker-altport-wireguard.yml +++ b/molecule/default/playbook-docker-altport-wireguard.yml @@ -1,6 +1,6 @@ --- - name: Converge - hosts: node* + hosts: all become: true vars: molecule_is_test: true diff --git a/molecule/default/playbook-docker.yml b/molecule/default/playbook-docker.yml index 9d93375..a780b33 100644 --- a/molecule/default/playbook-docker.yml +++ b/molecule/default/playbook-docker.yml @@ -1,6 +1,6 @@ --- - name: Converge - hosts: node* + hosts: all become: true vars: molecule_is_test: true diff --git a/molecule/default/playbook-download.yml b/molecule/default/playbook-download.yml index 5f869bc..9e3efc4 100644 --- a/molecule/default/playbook-download.yml +++ b/molecule/default/playbook-download.yml @@ -1,6 +1,6 @@ --- - name: Converge - hosts: node* + hosts: all become: true vars: molecule_is_test: true diff --git a/molecule/default/playbook-no-deploy.yml b/molecule/default/playbook-no-deploy.yml index fece13b..16a8ab4 100644 --- a/molecule/default/playbook-no-deploy.yml +++ b/molecule/default/playbook-no-deploy.yml @@ -1,6 +1,6 @@ --- - name: Converge - hosts: node* + hosts: all become: true vars: molecule_is_test: true diff --git a/molecule/default/playbook-restart-cluster.yml b/molecule/default/playbook-restart-cluster.yml index 45add58..d89782b 100644 --- a/molecule/default/playbook-restart-cluster.yml +++ b/molecule/default/playbook-restart-cluster.yml @@ -1,6 +1,6 @@ --- - name: Converge - hosts: node* + hosts: all become: true vars: molecule_is_test: true diff --git a/molecule/default/playbook-start-cluster.yml b/molecule/default/playbook-start-cluster.yml index bc9fbbf..e206333 100644 --- a/molecule/default/playbook-start-cluster.yml +++ b/molecule/default/playbook-start-cluster.yml @@ -1,6 +1,6 @@ --- - name: Converge - hosts: node* + hosts: all become: true vars: molecule_is_test: true diff --git a/molecule/default/playbook-stop-cluster.yml b/molecule/default/playbook-stop-cluster.yml index 1c1ab52..477b431 100644 --- a/molecule/default/playbook-stop-cluster.yml +++ b/molecule/default/playbook-stop-cluster.yml @@ -1,6 +1,6 @@ --- - name: Converge - hosts: node* + hosts: all become: true vars: molecule_is_test: true diff --git a/molecule/default/playbook-version.yml b/molecule/default/playbook-version.yml index 5f8cac9..740d9c9 100644 --- a/molecule/default/playbook-version.yml +++ b/molecule/default/playbook-version.yml @@ -1,6 +1,6 @@ --- - name: Converge - hosts: node* + hosts: all become: true vars: molecule_is_test: true diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml index 92c2184..394481e 100644 --- a/molecule/default/playbook.yml +++ b/molecule/default/playbook.yml @@ -1,6 +1,6 @@ --- - name: Converge - hosts: node* + hosts: all become: true vars: molecule_is_test: true diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index 567499e..5aed1ec 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -1,6 +1,6 @@ --- - name: Prepare - hosts: node* + hosts: all tasks: - name: Debug Message debug: diff --git a/molecule/highavailability/Dockerfile.j2 b/molecule/highavailability/Dockerfile.j2 new file mode 100644 index 0000000..e6aa95d --- /dev/null +++ b/molecule/highavailability/Dockerfile.j2 @@ -0,0 +1,14 @@ +# Molecule managed + +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates && apt-get clean; \ + elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python*-dnf bash && dnf clean all; \ + elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash && 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 && 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 && xbps-remove -O; fi diff --git a/molecule/highavailability/INSTALL.rst b/molecule/highavailability/INSTALL.rst new file mode 100644 index 0000000..6a44bde --- /dev/null +++ b/molecule/highavailability/INSTALL.rst @@ -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]' diff --git a/molecule/highavailability/molecule.yml b/molecule/highavailability/molecule.yml new file mode 100644 index 0000000..83e4c58 --- /dev/null +++ b/molecule/highavailability/molecule.yml @@ -0,0 +1,67 @@ +--- + +dependency: + name: galaxy +driver: + name: docker +lint: + name: yamllint +platforms: + - name: node1 + image: "${MOLECULE_DISTRO:-geerlingguy/docker-centos8-ansible:latest}" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + privileged: true + pre_build_image: ${MOLECULE_PREBUILT:-true} + networks: + - name: k3snet + - name: node2 + image: "${MOLECULE_DISTRO:-geerlingguy/docker-centos8-ansible:latest}" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + privileged: true + pre_build_image: ${MOLECULE_PREBUILT:-true} + networks: + - name: k3snet + - name: node3 + image: "${MOLECULE_DISTRO:-geerlingguy/docker-centos8-ansible:latest}" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + privileged: true + pre_build_image: ${MOLECULE_PREBUILT:-true} + networks: + - name: k3snet + - name: database + image: postgres:11-alpine + pre_build_image: true + command: "postgres" + env: + POSTGRES_PASSWORD: "verybadpass" + networks: + - name: k3snet + - name: loadbalancer + image: nginx:1.16-alpine + pre_build_image: true + ports: + - "6443:6443" + volumes: + - ${MOLECULE_SCENARIO_DIRECTORY}/nginx-loadbalancer.conf:/etc/nginx/nginx.conf:ro + command: "nginx -g 'daemon off;'" + networks: + - name: k3snet +provisioner: + name: ansible + options: + verbose: true + lint: + name: ansible-lint + playbooks: + prepare: ${MOLECULE_PREPARE_PLAYBOOK:-prepare.yml} + converge: ${MOLECULE_PLAYBOOK:-playbook.yml} +verifier: + name: testinfra + lint: + name: flake8 diff --git a/molecule/highavailability/nginx-loadbalancer.conf b/molecule/highavailability/nginx-loadbalancer.conf new file mode 100644 index 0000000..5c2464e --- /dev/null +++ b/molecule/highavailability/nginx-loadbalancer.conf @@ -0,0 +1,20 @@ +events { } + +http { + # Set up our upstream of control (master) nodes. The default load balancing + # algorithm for nginx is to round-robin. Perfect! + upstream control_plane { + server node2:6443; + server node3:6443; + } + + # Listen on port 6443, this is our default control plane port, then pass + # all traffic to one of the control (master) nodes. + server { + listen 6443; + + location / { + proxy_pass http://control_plane; + } + } +} diff --git a/molecule/default/playbook-ha-postgres.yml b/molecule/highavailability/playbook.yml similarity index 89% rename from molecule/default/playbook-ha-postgres.yml rename to molecule/highavailability/playbook.yml index c1a613b..fcc3562 100644 --- a/molecule/default/playbook-ha-postgres.yml +++ b/molecule/highavailability/playbook.yml @@ -4,6 +4,7 @@ become: true vars: molecule_is_test: true + k3s_control_node_address: loadbalancer k3s_datastore_endpoint: "postgres://postgres:verybadpass@database:5432/postgres?sslmode=disable" pre_tasks: - name: Set each node to be a control node diff --git a/molecule/highavailability/prepare.yml b/molecule/highavailability/prepare.yml new file mode 100644 index 0000000..567499e --- /dev/null +++ b/molecule/highavailability/prepare.yml @@ -0,0 +1,7 @@ +--- +- name: Prepare + hosts: node* + tasks: + - name: Debug Message + debug: + msg: No prepare steps required diff --git a/molecule/highavailability/tests/test_default.py b/molecule/highavailability/tests/test_default.py new file mode 100644 index 0000000..eedd64a --- /dev/null +++ b/molecule/highavailability/tests/test_default.py @@ -0,0 +1,14 @@ +import os + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_hosts_file(host): + f = host.file('/etc/hosts') + + assert f.exists + assert f.user == 'root' + assert f.group == 'root' diff --git a/molecule/highavailability/tests/test_default.pyc b/molecule/highavailability/tests/test_default.pyc new file mode 100644 index 0000000..28c2e2d Binary files /dev/null and b/molecule/highavailability/tests/test_default.pyc differ