From 4662445ea77000b3fb9d12f58f532f33efd38a5a Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Tue, 16 Feb 2016 20:26:44 +0100 Subject: [PATCH] d4science-ghn-cluster: New dataminer version and R packages list. library/roles/R: A script that remove the old R installation, on demand. --- R/defaults/main.yml | 1 + R/files/r_packages_cleanup.sh | 13 ++++ R/tasks/main.yml | 108 +-------------------------------- R/tasks/r-installation.yml | 106 ++++++++++++++++++++++++++++++++ R/tasks/r-packages_cleanup.yml | 8 +++ 5 files changed, 131 insertions(+), 105 deletions(-) create mode 100644 R/files/r_packages_cleanup.sh create mode 100644 R/tasks/r-installation.yml create mode 100644 R/tasks/r-packages_cleanup.yml diff --git a/R/defaults/main.yml b/R/defaults/main.yml index b8b97f66..c3215d4b 100644 --- a/R/defaults/main.yml +++ b/R/defaults/main.yml @@ -21,6 +21,7 @@ r_needs_additional_distro_pkgs: False r_plugins_from_deb: True r_plugins_install_latest_source: True r_plugins_install_specific_source: True +r_packages_cleanup: False r_source_plugins_dest_dir: /var/cache/R diff --git a/R/files/r_packages_cleanup.sh b/R/files/r_packages_cleanup.sh new file mode 100644 index 00000000..d17d47eb --- /dev/null +++ b/R/files/r_packages_cleanup.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Put the base packages to unhold +for package in r-base r-base-core r-base-dev r-base-html r-cran-boot r-cran-class r-cran-cluster r-cran-codetools r-cran-foreign r-cran-kernsmooth r-cran-lattice r-cran-mass r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet r-cran-rpart r-cran-spatial r-cran-survival r-doc-html r-recommended ; do apt-mark unhold $package; done + +# Remove the old r packages +apt-get purge r-base-* r-cran-* -y + +# Remove the CRAN packages +rm -fr /usr/lib/R /usr/local/lib/R +rm -fr /var/cache/R/* + +exit 0 diff --git a/R/tasks/main.yml b/R/tasks/main.yml index bd74f03c..b03dddba 100644 --- a/R/tasks/main.yml +++ b/R/tasks/main.yml @@ -1,106 +1,4 @@ --- -- name: Manage the cran repository key - apt_key: id=E084DAB9 keyserver=keyserver.ubuntu.com state={{ r_install_cran_repo }} - tags: [ 'r_software', 'r_repo' ] - -- name: Manage the cran repository definition - apt_repository: repo='deb http://cran.rstudio.com/bin/linux/ubuntu {{ ansible_distribution_release }}/' state={{ r_install_cran_repo }} update_cache=yes - tags: [ 'r_software', 'r_repo' ] - -- name: Remove the hold state from the debian R packages - shell: apt-mark unhold {{ item }} - with_items: r_base_packages_hold_list - when: r_base_packages_hold_list is defined - ignore_errors: True - tags: [ 'r_software', 'r_pkg', 'r_pkg_hold' ] - -- name: Install the R base packages. - apt: pkg={{ item }} state={{ r_packages_main_state }} force=yes - with_items: r_base_packages_list - tags: [ 'r_software', 'r_pkg' ] - -- name: When we install specific R deb packages, put them on hold - shell: apt-mark hold {{ item }} - with_items: r_base_packages_hold_list - when: r_base_specific_version - ignore_errors: True - tags: [ 'r_software', 'r_pkg', 'r_pkg_hold' ] - -- name: Install the R additional modules from the deb repo - apt: pkg={{ item }} state={{ r_packages_state }} force=yes - with_items: r_plugins_packages_list - when: r_plugins_from_deb - tags: [ 'r_software', 'r_pkg' ] - -- name: Configure the default CRAN mirror - template: src=Rprofile.site.j2 dest=/etc/R/Rprofile.site owner=root group=root mode=0444 - when: r_install_cran_repo == 'present' - tags: [ 'r_software', 'r_profile', 'r_pkg' ] - -- name: Install some packages needed by R packages when installed from source - apt: pkg={{ item }} state={{ r_packages_state }} update_cache=yes force=yes - with_items: r_distribution_required_packages - when: r_needs_additional_distro_pkgs - tags: [ 'r_software', 'r_pkg' ] - -- name: Ensure that the R packages sources directory exists - file: dest={{ r_source_plugins_dest_dir }} state=directory owner=root group=root - when: r_plugins_from_sources is defined - tags: [ 'r_software', 'r_pkg', 'r_plugins', 'r_plugins_version' ] - -- name: Install R packages from the cran sources repo, latest available version - command: > - Rscript --slave --no-save --no-restore-history -e "if (! ('{{ item }}' %in% installed.packages()[,'Package'])) { install.packages(pkgs='{{ item }}', repos=c('{{ r_cran_mirror_site }}/')); print('Added'); } else { print('Already installed'); }" - register: install_plugins_result - failed_when: "install_plugins_result.rc != 0 or 'had non-zero exit status' in install_plugins_result.stderr" - changed_when: "'Added' in install_plugins_result.stdout" - with_items: r_plugins_list_to_install - when: r_plugins_install_latest_source - tags: [ 'r_software', 'r_pkg', 'r_plugins' ] - -- name: Get the R packages sources that need to be installed - get_url: url={{ item.url }} dest={{ r_source_plugins_dest_dir }} - with_items: r_plugins_from_sources - when: r_plugins_install_specific_source - tags: [ 'r_software', 'r_pkg', 'r_plugins', 'r_plugins_version' ] - -- name: Install R packages from the cran sources, specific versions - command: > - Rscript --slave --no-save --no-restore-history -e "if (! ('{{ item.name }}' %in% installed.packages()[,'Package'])) { install.packages('{{ r_source_plugins_dest_dir }}/{{ item.source }}', repos = NULL, type='source'); print('Added'); } else if (packageVersion('{{ item.name }}') != '{{ item.version }}') { install.packages('{{ r_source_plugins_dest_dir }}/{{ item.source }}', repos = NULL, type='source'); print('Added'); } else { print('Already Installed'); }" - register: install_s_plugins_result - failed_when: "install_s_plugins_result.rc != 0 or 'had non-zero exit status' in install_s_plugins_result.stderr" - changed_when: '"Added" in install_s_plugins_result.stdout' - with_items: r_plugins_from_sources - when: r_plugins_install_specific_source - ignore_errors: True - tags: [ 'r_software', 'r_pkg', 'r_plugins', 'r_plugins_version' ] - -- name: Install R packages from the cran sources, specific versions. Second round, to avoid circular dependencies - command: > - Rscript --slave --no-save --no-restore-history -e "if (! ('{{ item.name }}' %in% installed.packages()[,'Package'])) { install.packages('{{ r_source_plugins_dest_dir }}/{{ item.source }}', repos = NULL, type='source'); print('Added'); } else if (packageVersion('{{ item.name }}') != '{{ item.version }}') { install.packages('{{ r_source_plugins_dest_dir }}/{{ item.source }}', repos = NULL, type='source'); print('Added'); } else { print('Already Installed'); }" - register: install_s_plugins_result - failed_when: "install_s_plugins_result.rc != 0 or 'had non-zero exit status' in install_s_plugins_result.stderr" - changed_when: '"Added" in install_s_plugins_result.stdout' - with_items: r_plugins_from_sources - when: r_plugins_install_specific_source - tags: [ 'r_software', 'r_pkg', 'r_plugins', 'r_plugins_version' ] - -- name: Install R packages from github - command: > - Rscript --slave --no-save --no-restore-history -e "if (! ('{{ item.plugin_name }}' %in% installed.packages()[,'Package'])) { require(devtools); require(methods) ; options(repos='{{ r_cran_mirror_site }}/') ; install_github('{{ item.plugin_name }}', '{{ item.github_user }}'); print('Added'); } else { print('Already Installed'); }" - register: install_github_plugins_result - failed_when: "install_github_plugins_result.rc != 0 or 'had non-zero exit status' in install_github_plugins_result.stderr" - changed_when: "'Added' in install_github_plugins_result.stdout" - with_items: r_plugins_from_github - when: r_plugins_from_github is defined - tags: [ 'r_software', 'r_pkg', 'r_plugins', 'r_plugins_github' ] - -- name: Remove R unwanted packages - command: > - Rscript --slave --no-save --no-restore-history -e "if (! ('{{ item }}' %in% installed.packages()[,'Package'])) { print('Not installed'); } else { remove.packages(pkgs='{{ item }}'); print('Removed'); }" - register: remove_plugins_result - failed_when: remove_plugins_result.rc != 0 - changed_when: "'Removed' in remove_plugins_result.stdout" - with_items: r_plugins_list_to_remove - when: r_plugins_list_to_remove is defined - tags: [ 'r_software', 'r_pkg', 'r_plugins' ] +- include: r-packages_cleanup.yml + when: r_packages_cleanup +- include: r-installation.yml diff --git a/R/tasks/r-installation.yml b/R/tasks/r-installation.yml new file mode 100644 index 00000000..bd74f03c --- /dev/null +++ b/R/tasks/r-installation.yml @@ -0,0 +1,106 @@ +--- +- name: Manage the cran repository key + apt_key: id=E084DAB9 keyserver=keyserver.ubuntu.com state={{ r_install_cran_repo }} + tags: [ 'r_software', 'r_repo' ] + +- name: Manage the cran repository definition + apt_repository: repo='deb http://cran.rstudio.com/bin/linux/ubuntu {{ ansible_distribution_release }}/' state={{ r_install_cran_repo }} update_cache=yes + tags: [ 'r_software', 'r_repo' ] + +- name: Remove the hold state from the debian R packages + shell: apt-mark unhold {{ item }} + with_items: r_base_packages_hold_list + when: r_base_packages_hold_list is defined + ignore_errors: True + tags: [ 'r_software', 'r_pkg', 'r_pkg_hold' ] + +- name: Install the R base packages. + apt: pkg={{ item }} state={{ r_packages_main_state }} force=yes + with_items: r_base_packages_list + tags: [ 'r_software', 'r_pkg' ] + +- name: When we install specific R deb packages, put them on hold + shell: apt-mark hold {{ item }} + with_items: r_base_packages_hold_list + when: r_base_specific_version + ignore_errors: True + tags: [ 'r_software', 'r_pkg', 'r_pkg_hold' ] + +- name: Install the R additional modules from the deb repo + apt: pkg={{ item }} state={{ r_packages_state }} force=yes + with_items: r_plugins_packages_list + when: r_plugins_from_deb + tags: [ 'r_software', 'r_pkg' ] + +- name: Configure the default CRAN mirror + template: src=Rprofile.site.j2 dest=/etc/R/Rprofile.site owner=root group=root mode=0444 + when: r_install_cran_repo == 'present' + tags: [ 'r_software', 'r_profile', 'r_pkg' ] + +- name: Install some packages needed by R packages when installed from source + apt: pkg={{ item }} state={{ r_packages_state }} update_cache=yes force=yes + with_items: r_distribution_required_packages + when: r_needs_additional_distro_pkgs + tags: [ 'r_software', 'r_pkg' ] + +- name: Ensure that the R packages sources directory exists + file: dest={{ r_source_plugins_dest_dir }} state=directory owner=root group=root + when: r_plugins_from_sources is defined + tags: [ 'r_software', 'r_pkg', 'r_plugins', 'r_plugins_version' ] + +- name: Install R packages from the cran sources repo, latest available version + command: > + Rscript --slave --no-save --no-restore-history -e "if (! ('{{ item }}' %in% installed.packages()[,'Package'])) { install.packages(pkgs='{{ item }}', repos=c('{{ r_cran_mirror_site }}/')); print('Added'); } else { print('Already installed'); }" + register: install_plugins_result + failed_when: "install_plugins_result.rc != 0 or 'had non-zero exit status' in install_plugins_result.stderr" + changed_when: "'Added' in install_plugins_result.stdout" + with_items: r_plugins_list_to_install + when: r_plugins_install_latest_source + tags: [ 'r_software', 'r_pkg', 'r_plugins' ] + +- name: Get the R packages sources that need to be installed + get_url: url={{ item.url }} dest={{ r_source_plugins_dest_dir }} + with_items: r_plugins_from_sources + when: r_plugins_install_specific_source + tags: [ 'r_software', 'r_pkg', 'r_plugins', 'r_plugins_version' ] + +- name: Install R packages from the cran sources, specific versions + command: > + Rscript --slave --no-save --no-restore-history -e "if (! ('{{ item.name }}' %in% installed.packages()[,'Package'])) { install.packages('{{ r_source_plugins_dest_dir }}/{{ item.source }}', repos = NULL, type='source'); print('Added'); } else if (packageVersion('{{ item.name }}') != '{{ item.version }}') { install.packages('{{ r_source_plugins_dest_dir }}/{{ item.source }}', repos = NULL, type='source'); print('Added'); } else { print('Already Installed'); }" + register: install_s_plugins_result + failed_when: "install_s_plugins_result.rc != 0 or 'had non-zero exit status' in install_s_plugins_result.stderr" + changed_when: '"Added" in install_s_plugins_result.stdout' + with_items: r_plugins_from_sources + when: r_plugins_install_specific_source + ignore_errors: True + tags: [ 'r_software', 'r_pkg', 'r_plugins', 'r_plugins_version' ] + +- name: Install R packages from the cran sources, specific versions. Second round, to avoid circular dependencies + command: > + Rscript --slave --no-save --no-restore-history -e "if (! ('{{ item.name }}' %in% installed.packages()[,'Package'])) { install.packages('{{ r_source_plugins_dest_dir }}/{{ item.source }}', repos = NULL, type='source'); print('Added'); } else if (packageVersion('{{ item.name }}') != '{{ item.version }}') { install.packages('{{ r_source_plugins_dest_dir }}/{{ item.source }}', repos = NULL, type='source'); print('Added'); } else { print('Already Installed'); }" + register: install_s_plugins_result + failed_when: "install_s_plugins_result.rc != 0 or 'had non-zero exit status' in install_s_plugins_result.stderr" + changed_when: '"Added" in install_s_plugins_result.stdout' + with_items: r_plugins_from_sources + when: r_plugins_install_specific_source + tags: [ 'r_software', 'r_pkg', 'r_plugins', 'r_plugins_version' ] + +- name: Install R packages from github + command: > + Rscript --slave --no-save --no-restore-history -e "if (! ('{{ item.plugin_name }}' %in% installed.packages()[,'Package'])) { require(devtools); require(methods) ; options(repos='{{ r_cran_mirror_site }}/') ; install_github('{{ item.plugin_name }}', '{{ item.github_user }}'); print('Added'); } else { print('Already Installed'); }" + register: install_github_plugins_result + failed_when: "install_github_plugins_result.rc != 0 or 'had non-zero exit status' in install_github_plugins_result.stderr" + changed_when: "'Added' in install_github_plugins_result.stdout" + with_items: r_plugins_from_github + when: r_plugins_from_github is defined + tags: [ 'r_software', 'r_pkg', 'r_plugins', 'r_plugins_github' ] + +- name: Remove R unwanted packages + command: > + Rscript --slave --no-save --no-restore-history -e "if (! ('{{ item }}' %in% installed.packages()[,'Package'])) { print('Not installed'); } else { remove.packages(pkgs='{{ item }}'); print('Removed'); }" + register: remove_plugins_result + failed_when: remove_plugins_result.rc != 0 + changed_when: "'Removed' in remove_plugins_result.stdout" + with_items: r_plugins_list_to_remove + when: r_plugins_list_to_remove is defined + tags: [ 'r_software', 'r_pkg', 'r_plugins' ] diff --git a/R/tasks/r-packages_cleanup.yml b/R/tasks/r-packages_cleanup.yml new file mode 100644 index 00000000..76e87209 --- /dev/null +++ b/R/tasks/r-packages_cleanup.yml @@ -0,0 +1,8 @@ +--- +- name: Install the script that cleans up the R deb and cran packages + copy: src=r_packages_cleanup.sh dest=/usr/local/bin/r_packages_cleanup owner=root group=root mode=0500 + tags: [ 'r_software', 'r_pkgs', 'r_cleanup' ] + +- name: Remove all the old R deb and cran packages. Otherwise the upgrade will fail miserably + shell: /usr/local/bin/r_packages_cleanup + tags: [ 'r_software', 'r_pkgs', 'r_cleanup' ]