Skip to content
Snippets Groups Projects
Commit 3fc732b1 authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Allow to install conda as a specific user

JIRA INFRA-2156 #action In Progress
parent b8e07b7e
No related branches found
No related tags found
No related merge requests found
[flake8] [flake8]
ignore = E501 ignore = E501,W503,E203
...@@ -11,6 +11,8 @@ This role includes as well: ...@@ -11,6 +11,8 @@ This role includes as well:
```yaml ```yaml
conda_version: 4.8.3 conda_version: 4.8.3
conda_owner: root
conda_group: root
conda_channel_alias: https://artifactory.esss.lu.se/artifactory/api/conda conda_channel_alias: https://artifactory.esss.lu.se/artifactory/api/conda
# List of conda channels to use # List of conda channels to use
conda_channels: conda_channels:
...@@ -36,6 +38,7 @@ conda_env_files: ...@@ -36,6 +38,7 @@ conda_env_files:
``` ```
Note that this is deprecated as it has some drawbacks: 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 - 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` - handler can't be used when using `include_role`
......
--- ---
conda_version: 4.8.3 conda_version: 4.8.3
conda_owner: root
conda_group: root
conda_channel_alias: https://artifactory.esss.lu.se/artifactory/api/conda conda_channel_alias: https://artifactory.esss.lu.se/artifactory/api/conda
# List of conda channels to use # List of conda channels to use
conda_channels: conda_channels:
......
...@@ -17,6 +17,8 @@ provisioner: ...@@ -17,6 +17,8 @@ provisioner:
- "{{ playbook_dir }}/tests/python36_env.yml" - "{{ 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 - 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_default_env: python36
conda_owner: vagrant
conda_group: vagrant
ics-ans-role-conda-rpm: ics-ans-role-conda-rpm:
conda_channels: conda_channels:
- conda-forge - conda-forge
......
--- ---
- name: Prepare - name: Prepare ics-ans-role-conda-rpm
hosts: ics-ans-role-conda-rpm hosts: ics-ans-role-conda-rpm
gather_facts: false gather_facts: false
tasks: tasks:
...@@ -11,3 +11,11 @@ ...@@ -11,3 +11,11 @@
file: file:
path: /opt/conda/.condarc path: /opt/conda/.condarc
state: touch 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
import pytest
import os import os
import testinfra.utils.ansible_runner import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( 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): 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.rc == 0
assert cmd.stdout.strip() == 'conda 4.8.3' assert cmd.stdout.strip() == "conda 4.8.3"
def test_conda_path(host): def test_conda_path(host):
cmd = host.run('su -l -c "type conda" root') 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): def test_condarc_file(host):
condarc = host.file("/opt/conda/.condarc") condarc = host.file("/opt/conda/.condarc")
if host.ansible.get_variables()["inventory_hostname"] == "ics-ans-role-conda-rpm": 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: channels:
- conda-forge - conda-forge
- anaconda-main - anaconda-main
default_channels: [] default_channels: []
auto_update_conda: False auto_update_conda: False
use_only_tar_bz2: True""" use_only_tar_bz2: True"""
)
else: 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: channels:
- conda-e3 - conda-e3
- ics-conda - ics-conda
...@@ -36,12 +43,34 @@ channels: ...@@ -36,12 +43,34 @@ channels:
default_channels: [] default_channels: []
auto_update_conda: False auto_update_conda: False
use_only_tar_bz2: True""" use_only_tar_bz2: True"""
)
def test_condaz_default_env_file(host): def test_condaz_default_env_file(host):
condaz_default_env = host.file("/etc/profile.d/condaz_default_env.sh") 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": if (
assert condaz_default_env.content_string.strip() == r"""export PS1='[\u@\h \W]\$ ' 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""" conda activate python36"""
)
else: else:
assert not condaz_default_env.exists 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
...@@ -3,16 +3,17 @@ import os ...@@ -3,16 +3,17 @@ import os
import testinfra.utils.ansible_runner import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( 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): def test_conda_env_python36(host):
assert host.file('/opt/conda/envs/python36/bin').exists assert host.file("/opt/conda/envs/python36/bin").exists
cmd = host.run('/opt/conda/envs/python36/bin/python --version 2>&1') cmd = host.run("/opt/conda/envs/python36/bin/python --version 2>&1")
assert cmd.stdout.startswith('Python 3.6') assert cmd.stdout.startswith("Python 3.6")
def test_conda_env_python27(host): def test_conda_env_python27(host):
assert host.file('/opt/conda/envs/python27/bin').exists assert host.file("/opt/conda/envs/python27/bin").exists
cmd = host.run('/opt/conda/envs/python27/bin/python --version 2>&1') cmd = host.run("/opt/conda/envs/python27/bin/python --version 2>&1")
assert cmd.stdout.startswith('Python 2.7') assert cmd.stdout.startswith("Python 2.7")
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
copy: copy:
src: "{{ env_file }}" src: "{{ env_file }}"
dest: "{{ env_file_path }}" dest: "{{ env_file_path }}"
owner: root owner: "{{ conda_owner }}"
group: root group: "{{ conda_group }}"
mode: 0644 mode: 0644
when: not env_file | regex_search('^http') when: not env_file | regex_search('^http')
tags: conda-env tags: conda-env
...@@ -19,8 +19,8 @@ ...@@ -19,8 +19,8 @@
url: "{{ env_file }}" url: "{{ env_file }}"
dest: "{{ env_file_path }}" dest: "{{ env_file_path }}"
force: true force: true
owner: root owner: "{{ conda_owner }}"
group: root group: "{{ conda_group }}"
mode: 0644 mode: 0644
when: env_file | regex_search('^http') when: env_file | regex_search('^http')
tags: conda-env tags: conda-env
...@@ -36,4 +36,6 @@ ...@@ -36,4 +36,6 @@
state: present state: present
force: true force: true
file: "{{ env_file_path }}" file: "{{ env_file_path }}"
become: true
become_user: "{{ conda_owner }}"
tags: conda-env tags: conda-env
...@@ -18,8 +18,8 @@ ...@@ -18,8 +18,8 @@
path: "{{ conda_path }}" path: "{{ conda_path }}"
state: directory state: directory
mode: 0755 mode: 0755
owner: root owner: "{{ conda_owner }}"
group: root group: "{{ conda_group }}"
- name: download miniconda installer - name: download miniconda installer
get_url: get_url:
...@@ -32,14 +32,16 @@ ...@@ -32,14 +32,16 @@
- name: install conda - name: install conda
command: > command: >
bash {{ conda_path }}/{{ conda_installer }} -p {{ conda_path }} -b -f bash {{ conda_path }}/{{ conda_installer }} -p {{ conda_path }} -b -f
become: true
become_user: "{{ conda_owner }}"
when: run_conda_version.rc != 0 when: run_conda_version.rc != 0
- name: create condarc file - name: create condarc file
template: template:
src: system_condarc.j2 src: system_condarc.j2
dest: "{{ conda_path }}/.condarc" dest: "{{ conda_path }}/.condarc"
owner: root owner: "{{ conda_owner }}"
group: root group: "{{ conda_group }}"
mode: 0644 mode: 0644
- name: delete miniconda installer - name: delete miniconda installer
...@@ -53,6 +55,8 @@ ...@@ -53,6 +55,8 @@
- name: install conda version {{ conda_version }} - name: install conda version {{ conda_version }}
command: > command: >
/opt/conda/bin/conda install -y -n base -c conda-forge conda={{ conda_version }} /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) when: (run_conda_version.rc != 0) or (run_conda_version.stdout != "conda " ~ conda_version)
- name: create link to conda.sh profile - name: create link to conda.sh profile
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment