Skip to content

Commit

Permalink
Add java role (#59)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
drmatthews and p-j-smith authored Mar 14, 2024
1 parent df344c3 commit 932caf8
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/molecule-java.yml
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions roles/install_java/README.md
Original file line number Diff line number Diff line change
@@ -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
```
5 changes: 5 additions & 0 deletions roles/install_java/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions roles/install_java/molecule/centos7/molecule.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions roles/install_java/molecule/resources/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Install Java
hosts: all
become: true
gather_facts: true
roles:
- role: mirsg.infrastructure.install_java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
14 changes: 14 additions & 0 deletions roles/install_java/molecule/resources/prepare.yml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions roles/install_java/molecule/resources/verify.yml
Original file line number Diff line number Diff line change
@@ -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')
3 changes: 3 additions & 0 deletions roles/install_java/molecule/rocky9/molecule.yml
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions roles/install_java/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions roles/install_java/templates/java_home.sh.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export JAVA_HOME="{{ java_home }}"

0 comments on commit 932caf8

Please sign in to comment.