From 93ee01e71c7383409de8b8d6b1cbab123c87741b Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Wed, 2 Sep 2015 21:49:16 +0200 Subject: [PATCH 1/2] Add functionality for installing and removing R packages Fixes #2 --- README.md | 13 ++++++-- defaults/main.yml | 4 +++ tasks/main.yml | 6 ++++ tasks/packages.yml | 32 ++++++++++++++++++++ templates/empty | 0 templates/usr/local/bin/R-install-package.j2 | 17 +++++++++++ templates/usr/local/bin/R-remove-package.j2 | 16 ++++++++++ tests/test.yml | 14 +++++++-- vars/main.yml | 1 + 9 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 tasks/packages.yml delete mode 100644 templates/empty create mode 100644 templates/usr/local/bin/R-install-package.j2 create mode 100644 templates/usr/local/bin/R-remove-package.j2 diff --git a/README.md b/README.md index 2e28ce6..3f87c45 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,22 @@ Set up the latest version of R in Ubuntu systems. #### Requirements -None +* `littler` (will be installed) #### Variables * `r_cran mirror`: [default: `http://cran.rstudio.com/`]: Your favorite [CRAN mirror](http://cran.r-project.org/mirrors.html) * `r_install_dev`: [default: `false`]: Whether or not install the `r-base-dev` package -* `r_install`: [default: `[]`]: Additional packages to install (e.g. `r-recommended`, `littler`) +* `r_install`: [default: `[]`]: Additional packages to install (e.g. `r-recommended`) + +* `r_packages_lib`: [default: `/usr/local/lib/R/site-library`]: The (default) library directory to install packages to +* `r_packages_repos`: [default: `"{{ r_cran_mirror }}"`]: The (default) URL to install packages from + +* `r_packages`: [default: `[]`]: Packages to install or remove +* `r_packages.{n}.name`: [required]: The name of the package +* `r_packages.{n}.state`: [optional, default: `present`]: The state of the package +* `r_packages.{n}.lib`: [optional, default: `r_packages_lib`]: The library directory to install the package to +* `r_packages.{n}.repos`: [optional, default: `r_packages_repos`]: The URL to install the package from ## Dependencies diff --git a/defaults/main.yml b/defaults/main.yml index 6b6cfdf..20ae948 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -3,3 +3,7 @@ r_cran_mirror: http://cran.rstudio.com/ r_install_dev: false r_install: [] + +r_packages_repos: "{{ r_cran_mirror }}" +r_packages_lib: /usr/local/lib/R/site-library +r_packages: [] diff --git a/tasks/main.yml b/tasks/main.yml index 8ef5f1f..2bd821a 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -11,3 +11,9 @@ - configuration - r - r-install + +- include: packages.yml + tags: + - configuration + - r + - r-packages diff --git a/tasks/packages.yml b/tasks/packages.yml new file mode 100644 index 0000000..30d486c --- /dev/null +++ b/tasks/packages.yml @@ -0,0 +1,32 @@ +# tasks file for r +--- +- name: packages | copy littler scripts + template: + src: "usr/local/bin/{{ item.src }}" + dest: "/usr/local/bin/{{ item.dest }}" + owner: root + group: root + mode: 0755 + with_items: + - src: R-install-package.j2 + dest: R-install-package + - src: R-remove-package.j2 + dest: R-remove-package + +- name: packages | install + command: "R-install-package {{ item.name }}{% if item.lib is defined %} {{ item.lib }}{% endif %}{% if item.repos is defined %} {{ item.repos }}{% endif %}" + register: r_install_package + changed_when: "r_install_package.stdout_lines[-1] is defined and r_install_package.stdout_lines[-1] == 'changed'" + with_items: r_packages + when: item.state is undefined or item.state == 'present' + tags: + - r-packages-install + +- name: packages | remove + command: "R-remove-package {{ item.name }} {{ item.lib if item.lib is defined else '' }}" + register: r_remove_package + changed_when: "r_remove_package.stdout_lines[-1] is defined and r_remove_package.stdout_lines[-1] == 'changed'" + with_items: r_packages + when: item.state is defined and item.state == 'absent' + tags: + - r-packages-remove diff --git a/templates/empty b/templates/empty deleted file mode 100644 index e69de29..0000000 diff --git a/templates/usr/local/bin/R-install-package.j2 b/templates/usr/local/bin/R-install-package.j2 new file mode 100644 index 0000000..14db6b1 --- /dev/null +++ b/templates/usr/local/bin/R-install-package.j2 @@ -0,0 +1,17 @@ +#!/usr/bin/env r + +if (is.null(argv) | length(argv) < 1) { + cat("Usage: R-install-package package [lib] [repos]\n"); + q(status = 1); +} + +package = argv[1] +lib = ifelse(is.na(argv[2]), '{{ r_packages_lib }}', argv[2]); +repos = ifelse(is.na(argv[3]), '{{ r_packages_repos }}', argv[3]); + +if (!(package %in% installed.packages(lib.loc = lib)[,'Package'])) { + withCallingHandlers(install.packages(package, lib, repos), warning = stop); + cat("changed\n"); +} else { + cat("unchanged\n"); +} diff --git a/templates/usr/local/bin/R-remove-package.j2 b/templates/usr/local/bin/R-remove-package.j2 new file mode 100644 index 0000000..3f6a666 --- /dev/null +++ b/templates/usr/local/bin/R-remove-package.j2 @@ -0,0 +1,16 @@ +#!/usr/bin/env r + +if (is.null(argv) | length(argv) < 1) { + cat("Usage: R-remove-package package [lib]\n"); + q(status = 1); +} + +package = argv[1] +lib = ifelse(is.na(argv[2]), '{{ r_packages_lib }}', argv[2]); + +if (package %in% installed.packages(lib.loc = lib)[,'Package']) { + withCallingHandlers(remove.packages(package, lib), warning = stop); + cat("changed\n"); +} else { + cat("unchanged\n"); +} diff --git a/tests/test.yml b/tests/test.yml index 5d6e714..b22cf86 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -5,5 +5,15 @@ roles: - ../../ vars: - r_install: - - littler + r_packages: + - name: DBI + state: present + + - name: DBI + repos: 'http://cran-mirror.cs.uu.nl/' + lib: /tmp + + - name: colorspace + + - name: foreign + state: absent diff --git a/vars/main.yml b/vars/main.yml index 17c6309..66df8e8 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -5,4 +5,5 @@ r_repository: url: "{{ r_cran_mirror }}/bin/linux/ubuntu {{ ansible_distribution_release }}/" r_dependencies: - r-base + - littler - "{{ 'r-base-dev' if r_install_dev else '' }}" From 1964464f7e878e58bc12287506bb268598015bd2 Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Wed, 2 Sep 2015 22:07:42 +0200 Subject: [PATCH 2/2] Consistency change [ci skip] --- tasks/packages.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/packages.yml b/tasks/packages.yml index 30d486c..68d6212 100644 --- a/tasks/packages.yml +++ b/tasks/packages.yml @@ -23,7 +23,7 @@ - r-packages-install - name: packages | remove - command: "R-remove-package {{ item.name }} {{ item.lib if item.lib is defined else '' }}" + command: "R-remove-package {{ item.name }}{% if item.lib is defined %} {{ item.lib }}{% endif %}" register: r_remove_package changed_when: "r_remove_package.stdout_lines[-1] is defined and r_remove_package.stdout_lines[-1] == 'changed'" with_items: r_packages