From 932caf84ce51cca22ff1dd419b20570cbbfea2e2 Mon Sep 17 00:00:00 2001 From: Daniel Matthews Date: Thu, 14 Mar 2024 10:20:30 +0000 Subject: [PATCH] Add java role (#59) Adds a role to install java. The functionality is included in the `mirsg.infrastructure.tomcat` role but is needed as a standalone as a prerequisite to installing OMERO. - also added molecule test for the role, and a workflow for running the tests --------- Co-authored-by: Paul Smith --- .github/workflows/molecule-java.yml | 17 +++++++++++++ roles/install_java/README.md | 24 +++++++++++++++++++ roles/install_java/defaults/main.yml | 5 ++++ .../molecule/centos7/molecule.yml | 3 +++ .../molecule/resources/converge.yml | 7 ++++++ .../resources/inventory/group_vars/all.yml | 1 + .../molecule/resources/prepare.yml | 14 +++++++++++ .../molecule/resources/verify.yml | 16 +++++++++++++ .../install_java/molecule/rocky9/molecule.yml | 3 +++ roles/install_java/tasks/main.yml | 23 ++++++++++++++++++ roles/install_java/templates/java_home.sh.j2 | 1 + 11 files changed, 114 insertions(+) create mode 100644 .github/workflows/molecule-java.yml create mode 100644 roles/install_java/README.md create mode 100644 roles/install_java/defaults/main.yml create mode 100644 roles/install_java/molecule/centos7/molecule.yml create mode 100644 roles/install_java/molecule/resources/converge.yml create mode 100644 roles/install_java/molecule/resources/inventory/group_vars/all.yml create mode 100644 roles/install_java/molecule/resources/prepare.yml create mode 100644 roles/install_java/molecule/resources/verify.yml create mode 100644 roles/install_java/molecule/rocky9/molecule.yml create mode 100644 roles/install_java/tasks/main.yml create mode 100644 roles/install_java/templates/java_home.sh.j2 diff --git a/.github/workflows/molecule-java.yml b/.github/workflows/molecule-java.yml new file mode 100644 index 00000000..cf527165 --- /dev/null +++ b/.github/workflows/molecule-java.yml @@ -0,0 +1,17 @@ +--- +name: Test install_java + +# yamllint disable-line rule:truthy +on: + pull_request: + paths: + - "molecule_configs/*" + - "roles/install_java/**" + - ".github/workflows/molecule.yml" + - ".github/workflows/molecule-java.yml" + +jobs: + molecule-java: + uses: ./.github/workflows/molecule.yml + with: + tests-path: ansible_collections/mirsg/infrastructure/roles/install_java diff --git a/roles/install_java/README.md b/roles/install_java/README.md new file mode 100644 index 00000000..8f976f5a --- /dev/null +++ b/roles/install_java/README.md @@ -0,0 +1,24 @@ +# Ansible Role: mirsg.infrastructure.install_java + +A role to install and configure Java. + +## Role Variables + +`java_profile_d`: Defaults to "/etc/profile.d". + +`java_home`: Defaults to "/usr/lib/jvm/jre". + +`java_package`: Defaults to "java-1.8.0". + +`java_package_version`: Defaults to "devel". + +## Example Playbook + +Including an example of how to use your role (for instance, with variables +passed in as parameters) is always nice for users too: + +```yaml +- hosts: servers + roles: + - role: mirsg.infrastructure.install_java +``` diff --git a/roles/install_java/defaults/main.yml b/roles/install_java/defaults/main.yml new file mode 100644 index 00000000..b6cc4c0e --- /dev/null +++ b/roles/install_java/defaults/main.yml @@ -0,0 +1,5 @@ +--- +java_profile_d: /etc/profile.d +java_home: /usr/lib/jvm/jre +java_package: java-1.8.0-openjdk +java_package_version: devel diff --git a/roles/install_java/molecule/centos7/molecule.yml b/roles/install_java/molecule/centos7/molecule.yml new file mode 100644 index 00000000..d5e91358 --- /dev/null +++ b/roles/install_java/molecule/centos7/molecule.yml @@ -0,0 +1,3 @@ +--- +# test this scenario from the roles/install_java directory with the command +# molecule --base-config ../../molecule_configs/centos7_base_config.yml test --scenario centos7 diff --git a/roles/install_java/molecule/resources/converge.yml b/roles/install_java/molecule/resources/converge.yml new file mode 100644 index 00000000..6d30866c --- /dev/null +++ b/roles/install_java/molecule/resources/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Install Java + hosts: all + become: true + gather_facts: true + roles: + - role: mirsg.infrastructure.install_java diff --git a/roles/install_java/molecule/resources/inventory/group_vars/all.yml b/roles/install_java/molecule/resources/inventory/group_vars/all.yml new file mode 100644 index 00000000..ed97d539 --- /dev/null +++ b/roles/install_java/molecule/resources/inventory/group_vars/all.yml @@ -0,0 +1 @@ +--- diff --git a/roles/install_java/molecule/resources/prepare.yml b/roles/install_java/molecule/resources/prepare.yml new file mode 100644 index 00000000..c05c0789 --- /dev/null +++ b/roles/install_java/molecule/resources/prepare.yml @@ -0,0 +1,14 @@ +--- +- name: Prepare - install sudo and Java 11 + hosts: all + gather_facts: true + tasks: + - name: Install sudo + ansible.builtin.package: + name: sudo + state: present + + - name: Install Java 11 before the role installs Java 8 + ansible.builtin.package: + name: java-11-openjdk-devel + state: present diff --git a/roles/install_java/molecule/resources/verify.yml b/roles/install_java/molecule/resources/verify.yml new file mode 100644 index 00000000..fb8fa68a --- /dev/null +++ b/roles/install_java/molecule/resources/verify.yml @@ -0,0 +1,16 @@ +--- +- name: Verify correct java version is selected + hosts: all + tasks: + - name: Get Java version + ansible.builtin.shell: + cmd: | + set -o pipefail + java -version 2>&1 | grep version | awk '{print $3}' | sed 's/"//g' + register: java_version + changed_when: false + + - name: Check the java version is correct + ansible.builtin.assert: + that: + - java_version.stdout | split("_") | first is version('1.8.0') diff --git a/roles/install_java/molecule/rocky9/molecule.yml b/roles/install_java/molecule/rocky9/molecule.yml new file mode 100644 index 00000000..bd5c2591 --- /dev/null +++ b/roles/install_java/molecule/rocky9/molecule.yml @@ -0,0 +1,3 @@ +--- +# test this scenario from the roles/install_java directory with the command +# molecule --base-config ../../molecule_configs/rocky9_base_config.yml test --scenario rocky9 diff --git a/roles/install_java/tasks/main.yml b/roles/install_java/tasks/main.yml new file mode 100644 index 00000000..cc620f5d --- /dev/null +++ b/roles/install_java/tasks/main.yml @@ -0,0 +1,23 @@ +--- +- name: Ensure correct Java version is installed - {{ java_package }} + ansible.builtin.package: + name: "{{ java_package }}-{{ java_package_version }}" + state: installed + +- name: Set JAVA_HOME through shell script + ansible.builtin.template: + src: "java_home.sh.j2" + dest: "{{ java_profile_d }}/java_home.sh" + mode: "0644" + when: java_home is defined and java_home != '' + +- name: Get info for java package directory + ansible.builtin.stat: + path: "/usr/lib/jvm/{{ java_package }}" + register: java_package_info + +- name: Set the default java version - {{ java_package_info.stat.lnk_source }} + community.general.alternatives: + name: java + path: "{{ java_package_info.stat.lnk_source }}/jre/bin/java" + state: selected diff --git a/roles/install_java/templates/java_home.sh.j2 b/roles/install_java/templates/java_home.sh.j2 new file mode 100644 index 00000000..571fab8e --- /dev/null +++ b/roles/install_java/templates/java_home.sh.j2 @@ -0,0 +1 @@ +export JAVA_HOME="{{ java_home }}"