diff --git a/.gitignore b/.gitignore index 5dde50f..903e0c8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ tests/.cache tests/__pycache__ .molecule .vagrant +.cache diff --git a/CHANGELOG.md b/CHANGELOG.md index bcbfebf..19955dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,13 @@ # Change Log All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a changelog](https://github.com/olivierlacan/keep-a-changelog). -## [Unreleased] +## [Unreleased](https://github.com/idealista/java-role/tree/develop) -## [1.0.0] +## [1.1.0](https://github.com/idealista/java-role/tree/1.1.0) +### Added +- *Enable openjdk* @jmonterrubio + +## [1.0.0](https://github.com/idealista/java-role/tree/1.0.0) ### Added - *First commit* @agarcia diff --git a/defaults/main.yml b/defaults/main.yml index bf35a27..5341406 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,5 +1,7 @@ --- -java_repo: 'deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main' -java_ppa: 'webupd8team/java' -java_version: '8' -java_set_version: '8' +## GLOBAL +java_version: 8 +java_set_version: 8 + +# IMPLEMENTATION +java_implementation: oracle # [oracle, openjdk] diff --git a/molecule.yml b/molecule.yml index 59cc250..1f1b452 100644 --- a/molecule.yml +++ b/molecule.yml @@ -28,6 +28,10 @@ ansible: # configuration options for the internal call to vagrant vagrant: # molecule's --platform option will look for these names + raw_config_args: + - "landrush.enabled = true" + - "landrush.tld = 'vm'" + - "landrush.guest_redirect_dns = false" platforms: - name: Debian box: debian/jessie64 @@ -43,9 +47,16 @@ vagrant: cpus: 1 instances: - - name: vagrant + - name: oracle ansible_groups: - - group01 + - oracle + interfaces: + - network_name: private_network + type: dhcp + auto_config: true + - name: openjdk + ansible_groups: + - openjdk interfaces: - network_name: private_network type: dhcp diff --git a/tasks/install_openjdk.yml b/tasks/install_openjdk.yml new file mode 100644 index 0000000..8c2afa9 --- /dev/null +++ b/tasks/install_openjdk.yml @@ -0,0 +1,35 @@ +--- + +- name: JAVA | Debian repo key + apt_repository: + repo: "{{ java_repo_openjdk }}" + when: ansible_distribution in [ 'Debian' ] + +- name: JAVA | Installing dependencies + apt: + pkg: "{{ item }}" + update_cache: yes + state: present + default_release: jessie-backports + with_items: "{{ required_libs_openjdk }}" + +- name: JAVA | Install Java + apt: + name: "openjdk-{{ item }}-jdk" + state: present + update_cache: yes + default_release: jessie-backports + with_items: "{{ java_version }}" + +- name: JAVA | Update Env + alternatives: + name: java + path: "/usr/lib/jvm/java-{{ java_set_version }}-openjdk-amd64/jre/bin/java" + #command: update-java-alternatives --set java-1.{{ java_set_version }}.0-openjdk-amd64 + +- name: JAVA | Ensure Defaults on etc/alternatives systems + file: + src: /usr/lib/jvm/java-{{ java_set_version }}-openjdk-amd64 + dest: /usr/lib/jvm/default-java + force: yes + state: link diff --git a/tasks/install_oracle.yml b/tasks/install_oracle.yml new file mode 100644 index 0000000..978832b --- /dev/null +++ b/tasks/install_oracle.yml @@ -0,0 +1,50 @@ +--- +- name: Debian repo + apt_key: + keyserver: hkp://keyserver.ubuntu.com:80 + id: EEA14886 + when: ansible_distribution in [ 'Debian' ] + +- name: Debian repo key + apt_repository: + repo: "{{ java_repo_oracle }}" + when: ansible_distribution in [ 'Debian' ] + +- name: Add Java ppa to Ubuntu + apt_repository: + repo: ppa:{{ java_ppa_oracle }} + when: ansible_distribution in [ 'Ubuntu' ] + +- name: Autoaccept license for Java + debconf: + name: oracle-java{{ item }}-installer + question: shared/accepted-oracle-license-v1-1 + value: 'true' + vtype: select + with_items: "{{ java_version }}" + +- name: Install Java + apt: + pkg: oracle-java{{ item }}-installer + state: present + install_recommends: yes + update_cache: yes + cache_valid_time: 3600 + with_items: "{{ java_version }}" + tags: skip_ansible_lint + +- name: Set Java Env + apt: + pkg: oracle-java{{ java_set_version }}-set-default + state: present + install_recommends: yes + update_cache: yes + cache_valid_time: 3600 + tags: skip_ansible_lint + +- name: Ensure Defaults on etc/alternatives systems + file: + src: /usr/lib/jvm/java-{{ java_set_version }}-oracle + dest: /usr/lib/jvm/default-java + force: yes + state: link diff --git a/tasks/main.yml b/tasks/main.yml index 076e6da..9a1db89 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,50 +1,13 @@ --- -- name: Debian repo - apt_key: - keyserver: hkp://keyserver.ubuntu.com:80 - id: EEA14886 - when: ansible_distribution in [ 'Debian' ] -- name: Debian repo key - apt_repository: - repo: "{{ java_repo }}" - when: ansible_distribution in [ 'Debian' ] - -- name: Add Java ppa to Ubuntu - apt_repository: - repo: ppa:{{ java_ppa }} - when: ansible_distribution in [ 'Ubuntu' ] - -- name: Autoaccept license for Java - debconf: - name: oracle-java{{ item }}-installer - question: shared/accepted-oracle-license-v1-1 - value: 'true' - vtype: select - with_items: "{{ java_version }}" - -- name: Install Java - apt: - pkg: oracle-java{{ item }}-installer - state: latest - install_recommends: yes - update_cache: yes - cache_valid_time: 3600 - with_items: "{{ java_version }}" - tags: skip_ansible_lint - -- name: Set Java Env - apt: - pkg: oracle-java{{ java_set_version }}-set-default - state: latest - install_recommends: yes - update_cache: yes - cache_valid_time: 3600 - tags: skip_ansible_lint - -- name: Ensure Defaults on etc/alternatives systems - file: - src: /usr/lib/jvm/java-{{ java_set_version }}-oracle - dest: /usr/lib/jvm/default-java - force: yes - state: link +- name: JAVA | Install Oracle Implementation + include: install_oracle.yml + when: java_implementation == 'oracle' + tags: + - install + +- name: JAVA | Install OpenJDK Implementation + include: install_openjdk.yml + when: java_implementation == 'openjdk' + tags: + - install diff --git a/tests/group_vars/openjdk.yml b/tests/group_vars/openjdk.yml new file mode 100644 index 0000000..3968d5b --- /dev/null +++ b/tests/group_vars/openjdk.yml @@ -0,0 +1,7 @@ +--- + +java_version: [7,8] + +java_set_version: 7 + +java_implementation: openjdk diff --git a/tests/group_vars/oracle.yml b/tests/group_vars/oracle.yml new file mode 100644 index 0000000..546f802 --- /dev/null +++ b/tests/group_vars/oracle.yml @@ -0,0 +1,6 @@ +--- + +java_version: [6, 7, 8] +java_set_version: 7 + +java_implementation: oracle diff --git a/tests/playbook.yml b/tests/playbook.yml index 6bff310..d373c3e 100644 --- a/tests/playbook.yml +++ b/tests/playbook.yml @@ -8,6 +8,10 @@ changed_when: false tags: skip_ansible_lint -- hosts: all +- hosts: oracle roles: - - role: java-role + - java-role + +- hosts: openjdk + roles: + - java-role diff --git a/tests/test_ansible.py b/tests/test_ansible.py index a031b73..1a914a6 100644 --- a/tests/test_ansible.py +++ b/tests/test_ansible.py @@ -2,38 +2,61 @@ @pytest.fixture() -def AnsibleDefaults(Ansible): - return Ansible("include_vars", "defaults/main.yml")["ansible_facts"] +def AnsibleDefaultVars(Ansible): + return Ansible("include_vars", "vars/main.yml")["ansible_facts"] @pytest.fixture() -def AnsibleVars(Ansible): - return Ansible("include_vars", "tests/group_vars/group01.yml")["ansible_facts"] +def AnsibleDefaults(Ansible): + return Ansible("include_vars", "defaults/main.yml")["ansible_facts"] @pytest.fixture() -def AnsibleDistribution(Ansible): - return Ansible("setup")["ansible_facts"]["ansible_distribution"] +def AnsibleVarsOracle(Ansible): + return Ansible("include_vars", "tests/group_vars/oracle.yml")["ansible_facts"] -def test_java_resources(File, AnsibleDefaults, AnsibleDistribution): - if AnsibleDistribution == "Debian": - java_resources = File("/etc/apt/sources.list.d/ppa_launchpad_net_webupd8team_java_ubuntu.list") - assert java_resources.contains(AnsibleDefaults["java_repo"]) - elif AnsibleDistribution == "Ubuntu": - java_resources = File("/etc/apt/sources.list.d/ppa_webupd8team_java_xenial.list") - assert java_resources.contains("deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main") - else: - raise ValueError("Unsupported distribution: " + AnsibleDistribution) +@pytest.fixture() +def AnsibleVarsOpenjdk(Ansible): + return Ansible("include_vars", "tests/group_vars/openjdk.yml")["ansible_facts"] -def test_java_package(Package, AnsibleVars): - for version in AnsibleVars["java_version"]: - assert Package("oracle-java" + version + "-installer").is_installed +@pytest.fixture() +def AnsibleDistribution(Ansible): + return Ansible("setup")["ansible_facts"]["ansible_distribution"] -def test_java_default(File, Package, Command, AnsibleVars): - assert Package("oracle-java" + AnsibleVars["java_set_version"] + "-set-default").is_installed - assert AnsibleVars["java_set_version"] in Command("java -version").stderr +@pytest.fixture() +def Hostname(TestinfraBackend): + return TestinfraBackend.get_hostname() + + +def test_java_resources(File, AnsibleDefaultVars, AnsibleDistribution, Hostname): + if "oracle" in Hostname: + if AnsibleDistribution == "Debian": + java_resources = File("/etc/apt/sources.list.d/ppa_launchpad_net_webupd8team_java_ubuntu.list") + assert java_resources.contains(AnsibleDefaultVars["java_repo_oracle"]) + elif AnsibleDistribution == "Ubuntu": + java_resources = File("/etc/apt/sources.list.d/ppa_webupd8team_java_xenial.list") + assert java_resources.contains("deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main") + else: + raise ValueError("Unsupported distribution: " + AnsibleDistribution) + + +def test_java_package(Package, AnsibleVarsOracle, AnsibleVarsOpenjdk, Hostname): + if "oracle" in Hostname: + for version in AnsibleVarsOracle["java_version"]: + assert Package("oracle-java" + str(version) + "-installer").is_installed + if "openjdk" in Hostname: + for version in AnsibleVarsOpenjdk["java_version"]: + assert Package("openjdk-" + str(version) + "-jdk").is_installed + + +def test_java_default(File, Package, Command, AnsibleVarsOracle, AnsibleVarsOpenjdk, Hostname): + if "oracle" in Hostname: + assert Package("oracle-java" + str(AnsibleVarsOracle["java_set_version"]) + "-set-default").is_installed + assert File("/usr/lib/jvm/default-java").linked_to == "/usr/lib/jvm/java-" + str((AnsibleVarsOracle["java_set_version"])) + "-oracle" + assert str(AnsibleVarsOracle["java_set_version"]) in Command("java -version").stderr + if "openjdk" in Hostname: + assert str(AnsibleVarsOpenjdk["java_set_version"]) in Command("java -version").stderr assert File("/usr/lib/jvm/default-java").is_symlink - assert File("/usr/lib/jvm/default-java").linked_to == "/usr/lib/jvm/java-" + (AnsibleVars["java_set_version"]) + "-oracle" diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..3030b3b --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,10 @@ +--- + +required_libs_oracle: [] +java_repo_oracle: "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" +java_ppa_oracle: "webupd8team/java" + +required_libs_openjdk: + - ca-certificates-java + - openjdk-8-jre-headless +java_repo_openjdk: "deb http://http.debian.net/debian jessie-backports main"