diff --git a/.flake8 b/.flake8 index e44b81084185c2f2d2a5f93214c2749e3f95a172..5384053b3bc5bbf41dac3f7dab8906d0959e747f 100644 --- a/.flake8 +++ b/.flake8 @@ -1,2 +1,2 @@ [flake8] -ignore = E501 +ignore = E501,W503,E203 diff --git a/README.md b/README.md index 31b757155ac28bf86f5f5c6764a510bacf9f6da4..a96fabfbf1e95efeb1232d7188be1b7ca29deb5b 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ This role includes as well: ```yaml conda_version: 4.8.3 +conda_owner: root +conda_group: root conda_channel_alias: https://artifactory.esss.lu.se/artifactory/api/conda # List of conda channels to use conda_channels: @@ -36,6 +38,7 @@ conda_env_files: ``` Note that this is deprecated as it has some drawbacks: + - the update is only based on the file content (via a handler) and not the environment itself - handler can't be used when using `include_role` diff --git a/defaults/main.yml b/defaults/main.yml index b76fd09b0d030f43314262c482b26f8e627b3f00..41c853e806310fa16e2fd2f083367dbc04da6e65 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,5 +1,7 @@ --- conda_version: 4.8.3 +conda_owner: root +conda_group: root conda_channel_alias: https://artifactory.esss.lu.se/artifactory/api/conda # List of conda channels to use conda_channels: diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index b27597d016f39fc801597d87aa4370ce2cbfd702..0bb8022c6156a2fd556a037fc84a38293ecc30fa 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -17,6 +17,8 @@ provisioner: - "{{ playbook_dir }}/tests/python36_env.yml" - https://gitlab.esss.lu.se/ics-ansible-galaxy/ics-ans-role-conda/raw/master/molecule/default/tests/python27_env.yml conda_default_env: python36 + conda_owner: vagrant + conda_group: vagrant ics-ans-role-conda-rpm: conda_channels: - conda-forge diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index 4e2b0437fc78be7464bc637ec130091480ec9e51..e5a1ea2bb49d1620e841f7a455da28bc321a508d 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -1,5 +1,5 @@ --- -- name: Prepare +- name: Prepare ics-ans-role-conda-rpm hosts: ics-ans-role-conda-rpm gather_facts: false tasks: @@ -11,3 +11,11 @@ file: path: /opt/conda/.condarc state: touch +- name: Prepare ics-ans-role-conda-create-env + hosts: ics-ans-role-conda-create-env + gather_facts: false + tasks: + - name: create vagrant user + user: + name: vagrant + state: present diff --git a/molecule/default/tests/test_conda.py b/molecule/default/tests/test_conda.py index 0f6dafedfe64c6fca99fd58b738d3ca1212ab5d2..775e54bf2a14e86ddae8af7d2bf09438eed193a6 100644 --- a/molecule/default/tests/test_conda.py +++ b/molecule/default/tests/test_conda.py @@ -1,33 +1,40 @@ +import pytest import os import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + os.environ["MOLECULE_INVENTORY_FILE"] +).get_hosts("all") def test_conda_version(host): - cmd = host.run('/opt/conda/bin/conda --version 2>&1') + cmd = host.run("/opt/conda/bin/conda --version 2>&1") assert cmd.rc == 0 - assert cmd.stdout.strip() == 'conda 4.8.3' + assert cmd.stdout.strip() == "conda 4.8.3" def test_conda_path(host): cmd = host.run('su -l -c "type conda" root') - assert cmd.stdout.startswith('conda is a function') + assert cmd.stdout.startswith("conda is a function") def test_condarc_file(host): condarc = host.file("/opt/conda/.condarc") if host.ansible.get_variables()["inventory_hostname"] == "ics-ans-role-conda-rpm": - assert condarc.content_string.strip() == """channel_alias: https://artifactory.esss.lu.se/artifactory/api/conda + assert ( + condarc.content_string.strip() + == """channel_alias: https://artifactory.esss.lu.se/artifactory/api/conda channels: - conda-forge - anaconda-main default_channels: [] auto_update_conda: False use_only_tar_bz2: True""" + ) else: - assert condarc.content_string.strip() == """channel_alias: https://artifactory.esss.lu.se/artifactory/api/conda + assert ( + condarc.content_string.strip() + == """channel_alias: https://artifactory.esss.lu.se/artifactory/api/conda channels: - conda-e3 - ics-conda @@ -36,12 +43,34 @@ channels: default_channels: [] auto_update_conda: False use_only_tar_bz2: True""" + ) def test_condaz_default_env_file(host): condaz_default_env = host.file("/etc/profile.d/condaz_default_env.sh") - if host.ansible.get_variables()["inventory_hostname"] == "ics-ans-role-conda-create-env": - assert condaz_default_env.content_string.strip() == r"""export PS1='[\u@\h \W]\$ ' + if ( + host.ansible.get_variables()["inventory_hostname"] + == "ics-ans-role-conda-create-env" + ): + assert ( + condaz_default_env.content_string.strip() + == r"""export PS1='[\u@\h \W]\$ ' conda activate python36""" + ) else: assert not condaz_default_env.exists + + +@pytest.mark.parametrize("name", ["/opt/conda", "/opt/conda/envs", "/opt/conda/pkgs"]) +def test_conda_owner(host, name): + if ( + host.ansible.get_variables()["inventory_hostname"] + == "ics-ans-role-conda-create-env" + ): + user = "vagrant" + group = "vagrant" + else: + user = "root" + group = "root" + assert host.file(name).user == user + assert host.file(name).group == group diff --git a/molecule/default/tests/test_conda_create_env.py b/molecule/default/tests/test_conda_create_env.py index 8959227505cd7699a034c2e4116c8b6450ce2a56..5ca06ca0d4bb0892105f6a44e0d776a757587eb2 100644 --- a/molecule/default/tests/test_conda_create_env.py +++ b/molecule/default/tests/test_conda_create_env.py @@ -3,16 +3,17 @@ import os import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('ics-ans-role-conda-create-env') + os.environ["MOLECULE_INVENTORY_FILE"] +).get_hosts("ics-ans-role-conda-create-env") def test_conda_env_python36(host): - assert host.file('/opt/conda/envs/python36/bin').exists - cmd = host.run('/opt/conda/envs/python36/bin/python --version 2>&1') - assert cmd.stdout.startswith('Python 3.6') + assert host.file("/opt/conda/envs/python36/bin").exists + cmd = host.run("/opt/conda/envs/python36/bin/python --version 2>&1") + assert cmd.stdout.startswith("Python 3.6") def test_conda_env_python27(host): - assert host.file('/opt/conda/envs/python27/bin').exists - cmd = host.run('/opt/conda/envs/python27/bin/python --version 2>&1') - assert cmd.stdout.startswith('Python 2.7') + assert host.file("/opt/conda/envs/python27/bin").exists + cmd = host.run("/opt/conda/envs/python27/bin/python --version 2>&1") + assert cmd.stdout.startswith("Python 2.7") diff --git a/tasks/create_conda_env.yml b/tasks/create_conda_env.yml index 709706627ed0b3b74adf61b9dd08af95b18940e2..9c2b4edb32392a3406d1172dd577ebcbf69581ad 100644 --- a/tasks/create_conda_env.yml +++ b/tasks/create_conda_env.yml @@ -8,8 +8,8 @@ copy: src: "{{ env_file }}" dest: "{{ env_file_path }}" - owner: root - group: root + owner: "{{ conda_owner }}" + group: "{{ conda_group }}" mode: 0644 when: not env_file | regex_search('^http') tags: conda-env @@ -19,8 +19,8 @@ url: "{{ env_file }}" dest: "{{ env_file_path }}" force: true - owner: root - group: root + owner: "{{ conda_owner }}" + group: "{{ conda_group }}" mode: 0644 when: env_file | regex_search('^http') tags: conda-env @@ -36,4 +36,6 @@ state: present force: true file: "{{ env_file_path }}" + become: true + become_user: "{{ conda_owner }}" tags: conda-env diff --git a/tasks/install.yml b/tasks/install.yml index 163d767c1d05faf778ffa08b7849e1ceb4be75e4..902e78766bcb8a79a8f7038c3034a18500ee395b 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -18,8 +18,8 @@ path: "{{ conda_path }}" state: directory mode: 0755 - owner: root - group: root + owner: "{{ conda_owner }}" + group: "{{ conda_group }}" - name: download miniconda installer get_url: @@ -32,14 +32,16 @@ - name: install conda command: > bash {{ conda_path }}/{{ conda_installer }} -p {{ conda_path }} -b -f + become: true + become_user: "{{ conda_owner }}" when: run_conda_version.rc != 0 - name: create condarc file template: src: system_condarc.j2 dest: "{{ conda_path }}/.condarc" - owner: root - group: root + owner: "{{ conda_owner }}" + group: "{{ conda_group }}" mode: 0644 - name: delete miniconda installer @@ -53,6 +55,8 @@ - name: install conda version {{ conda_version }} command: > /opt/conda/bin/conda install -y -n base -c conda-forge conda={{ conda_version }} + become: true + become_user: "{{ conda_owner }}" when: (run_conda_version.rc != 0) or (run_conda_version.stdout != "conda " ~ conda_version) - name: create link to conda.sh profile