Fix: Update mono-install molecule role 27/70527/15
authorAnil Belur <abelur@linuxfoundation.org>
Tue, 23 Aug 2022 01:09:53 +0000 (11:09 +1000)
committerAnil Belur <abelur@linuxfoundation.org>
Sat, 10 Feb 2024 00:47:56 +0000 (10:47 +1000)
Modernize the molecule test formats and update the role
to build with the latest version of ansible.
Update tox and pre-commit dependencies.

Ref: https://github.com/ansible/molecule/issues/1042
Change-Id: Ifc40c99516a92a1525d2b088a30d7efe34b3b17d
Signed-off-by: Anil Belur <abelur@linuxfoundation.org>
16 files changed:
.pre-commit-config.yaml
.yamllint
meta/main.yml
molecule/default/Dockerfile.j2 [deleted file]
molecule/default/INSTALL.rst [deleted file]
molecule/default/converge.yml [new file with mode: 0644]
molecule/default/create.yml [deleted file]
molecule/default/destroy.yml [deleted file]
molecule/default/molecule.yml
molecule/default/playbook.yml [deleted file]
molecule/default/prepare.yml
molecule/default/tests/test_default.py [deleted file]
tasks/Debian.yml
tasks/RedHat.yml
tasks/main.yml
tox.ini

index c1879a6..1af1238 100644 (file)
@@ -1,11 +1,11 @@
 ---
 repos:
   - repo: https://github.com/pre-commit/pre-commit-hooks
-    rev: v1.3.0
+    rev: v4.5.0
     hooks:
       - id: trailing-whitespace
 
   - repo: https://github.com/jorisroovers/gitlint
-    rev: v0.11.0
+    rev: v0.19.1
     hooks:
       - id: gitlint
index 3a2255e..894450c 100644 (file)
--- a/.yamllint
+++ b/.yamllint
@@ -1,3 +1,4 @@
+---
 extends: default
 
 rules:
@@ -8,6 +9,7 @@ rules:
     max-spaces-inside: 1
     level: error
   line-length: disable
-  # NOTE(retr0h): Templates no longer fail this lint rule.
-  #               Uncomment if running old Molecule templates.
-  # truthy: disable
+  truthy: disable
+
+ignore: |
+  .tox/
index 6c40f23..b8bd566 100644 (file)
@@ -1,5 +1,7 @@
 ---
 galaxy_info:
+  namespace: lfit
+  role_name: mono_install
   author: Linux Foundation Release Engineering
   description: Install mono project. (http://www.mono-project.com/)
   company: The Linux Foundation
@@ -13,9 +15,12 @@ galaxy_info:
     - name: EL
       versions:
         - 7
+
     - name: Ubuntu
       versions:
-        - xenial
+        - bionic
+        - focal
+        - jammy
 
   galaxy_tags:
     - mono
diff --git a/molecule/default/Dockerfile.j2 b/molecule/default/Dockerfile.j2
deleted file mode 100644 (file)
index f8b4e75..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-# Molecule managed
-
-FROM {{ item.image }}
-
-RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get upgrade -y && 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 python2-dnf bash && dnf clean all; \
-    elif [ $(command -v yum) ]; then yum makecache fast && yum update -y && 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 update -y && 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; fi
diff --git a/molecule/default/INSTALL.rst b/molecule/default/INSTALL.rst
deleted file mode 100644 (file)
index 43a584f..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-*******
-Install
-*******
-
-Requirements
-============
-
-* Docker Engine
-* docker-py
-
-Install
-=======
-
-.. code-block:: bash
-
-   $ sudo pip install docker-py
diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml
new file mode 100644 (file)
index 0000000..1e30adc
--- /dev/null
@@ -0,0 +1,5 @@
+---
+- name: Converge
+  hosts: all
+  roles:
+    - role: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') | basename }}"
diff --git a/molecule/default/create.yml b/molecule/default/create.yml
deleted file mode 100644 (file)
index bfb2149..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
----
-- name: Create
-  hosts: localhost
-  connection: local
-  gather_facts: false
-  no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
-  vars:
-    molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
-    molecule_ephemeral_directory: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}"
-    molecule_scenario_directory: "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}"
-    molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
-  tasks:
-    - name: Create Dockerfiles from image names
-      template:
-        src: "{{ molecule_scenario_directory }}/Dockerfile.j2"
-        dest: "{{ molecule_ephemeral_directory }}/Dockerfile_{{ item.image | regex_replace('[^a-zA-Z0-9_]', '_') }}"
-      with_items: "{{ molecule_yml.platforms }}"
-      register: platforms
-
-    - name: Discover local Docker images
-      docker_image_facts:
-        name: "molecule_local/{{ item.item.name }}"
-      with_items: "{{ platforms.results }}"
-      register: docker_images
-
-    - name: Build an Ansible compatible image
-      docker_image:
-        path: "{{ molecule_ephemeral_directory }}"
-        name: "molecule_local/{{ item.item.image }}"
-        dockerfile: "{{ item.item.dockerfile | default(item.invocation.module_args.dest) }}"
-        force: "{{ item.item.force | default(true) }}"
-      with_items: "{{ platforms.results }}"
-      when: platforms.changed or docker_images.results | map(attribute='images') | select('equalto', []) | list | count >= 0
-
-    - name: Create molecule instance(s)
-      docker_container:
-        name: "{{ item.name }}"
-        hostname: "{{ item.name }}"
-        image: "molecule_local/{{ item.image }}"
-        state: started
-        recreate: false
-        log_driver: syslog
-        command: "{{ item.command | default('bash -c \"while true; do sleep 10000; done\"') }}"
-        privileged: "{{ item.privileged | default(omit) }}"
-        volumes: "{{ item.volumes | default(omit) }}"
-        capabilities: "{{ item.capabilities | default(omit) }}"
-        ports: "{{ item.exposed_ports | default(omit) }}"
-      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: docker_jobs
-      until: docker_jobs.finished
-      retries: 300
-      with_items: "{{ server.results }}"
diff --git a/molecule/default/destroy.yml b/molecule/default/destroy.yml
deleted file mode 100644 (file)
index 3ce7478..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
----
-- name: Destroy
-  hosts: localhost
-  connection: local
-  gather_facts: false
-  no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
-  vars:
-    molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
-    molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
-  tasks:
-    - name: Destroy molecule instance(s)
-      docker_container:
-        name: "{{ item.name }}"
-        state: absent
-        force_kill: "{{ item.force_kill | default(true) }}"
-      register: server
-      with_items: "{{ molecule_yml.platforms }}"
-      async: 7200
-      poll: 0
-
-    - name: Wait for instance(s) deletion to complete
-      async_status:
-        jid: "{{ item.ansible_job_id }}"
-      register: docker_jobs
-      until: docker_jobs.finished
-      retries: 300
-      with_items: "{{ server.results }}"
index daae1be..23a56f1 100644 (file)
@@ -3,20 +3,18 @@ dependency:
   name: galaxy
 driver:
   name: docker
-lint:
-  name: yamllint
 platforms:
   - name: centos7
     image: centos:7
-  - name: ubuntu1604
-    image: ubuntu:16.04
+  - name: ubuntu1804
+    image: ubuntu:18.04
+  - name: ubuntu2004
+    image: ubuntu:20.04
+  - name: ubuntu2204
+    image: ubuntu:22.04
 provisioner:
   name: ansible
-  lint:
-    name: ansible-lint
 scenario:
   name: default
 verifier:
   name: testinfra
-  lint:
-    name: flake8
diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml
deleted file mode 100644 (file)
index 3f86091..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
----
-- name: Converge
-  hosts: all
-  roles:
-    - role: mono-install
index 5358b3b..0ae381f 100644 (file)
@@ -1,5 +1,26 @@
 ---
 - name: Prepare
   hosts: all
-  gather_facts: false
-  tasks: []
+  gather_facts: true
+
+  tasks:
+    - name: Update APT cache
+      apt:
+        update_cache: yes
+      when: ansible_distribution == 'Ubuntu'
+      become: true
+
+    - name: Install cron
+      yum:
+        name: cronie
+        state: present
+      when: ansible_os_family == 'RedHat'
+      become: true
+
+    - name: Install packages
+      package:
+        name:
+          - zip
+          - unzip
+        state: present
+      become: yes
diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py
deleted file mode 100644 (file)
index eedd64a..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-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'
index 3ea3141..439caf3 100644 (file)
@@ -1,6 +1,6 @@
 ---
 - name: Install mono-complete and nuget
-  apt:
+  ansible.builtin.package:
     name:
       - mono-complete
       - nuget
index d23e0bf..74606c3 100644 (file)
@@ -1,12 +1,12 @@
 ---
 - name: Add Mono RPM Key
-  rpm_key:
+  ansible.builtin.rpm_key:
     key: http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
     state: present
   become: true
 
 - name: Add Mono repository
-  yum_repository:
+  ansible.builtin.yum_repository:
     name: mono-project
     description: Mono Project CentOS repository
     baseurl: http://download.mono-project.com/repo/centos
@@ -16,7 +16,7 @@
   become: true
 
 - name: Install mono-complete and nuget
-  yum:
+  ansible.builtin.yum:
     name:
       - mono-complete
       - nuget
index b11578e..cfcd808 100644 (file)
@@ -1,4 +1,5 @@
 ---
-- include_tasks: '{{ item }}'
+- name: Include tasks from '{{ ansible_os_family }}'
+  ansible.builtin.include_tasks: '{{ item }}'
   with_first_found:
     - '{{ ansible_os_family }}.yml'
diff --git a/tox.ini b/tox.ini
index c486b78..5ad109a 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -2,7 +2,8 @@
 minversion = 1.6
 envlist =
     molecule,
-    pre-commit
+    pre-commit,
+    lint
 skipsdist=true
 
 [testenv:molecule]
@@ -11,13 +12,46 @@ deps =
     ansible
     docker
     molecule
+    molecule-docker
 passenv = *
 commands =
     ./molecule.sh
+allowlist_externals =
+    ./molecule.sh
 
 [testenv:pre-commit]
-basepython = python3
+allowlist_externals =
+    /bin/sh
 deps = pre-commit
+passenv = HOME
+commands =
+    pre-commit run --all-files --show-diff-on-failure
+    /bin/sh -c 'if ! git config --get user.name > /dev/null; then \
+        git config --global --add user.name "CI"; \
+        touch .git/REMOVE_USERNAME; fi'
+    /bin/sh -c 'if ! git config --get user.email > /dev/null; then \
+        git config --global --add user.email "ci@example.org"; \
+        touch .git/REMOVE_USEREMAIL; fi'
+    /bin/sh -c "if [ -f .git/COMMIT_EDITMSG ]; then \
+        cp .git/COMMIT_EDITMSG .git/COMMIT_MSGTOX; else \
+        git log HEAD -n1 | tail -n +5 | cut -c 5- > .git/COMMIT_MSGTOX; fi"
+    pre-commit run gitlint --hook-stage commit-msg --commit-msg-filename .git/COMMIT_MSGTOX
+    /bin/sh -c "rm -f .git/COMMIT_MSGTOX"
+    /bin/sh -c "if [ -f .git/REMOVE_USERNAME ]; then \
+        git config --global --unset user.name; \
+        rm -f .git/REMOVE_USERNAME; fi"
+    /bin/sh -c "if [ -f .git/REMOVE_USEREMAIL ]; then \
+        git config --global --unset user.email; \
+        rm -f .git/REMOVE_USEREMAIL; fi"
+
+[testenv:lint]
+basepython = python310
+deps =
+    ansible-lint
+    yamllint
 commands =
-    pre-commit install --hook-type commit-msg
-    pre-commit run --all-files
+    /bin/bash -c "ansible-lint tasks/*.yml"
+    yamllint .
+allowlist_externals =
+    /bin/bash
+    yamllint