From 4a4eec4589dfddd42945a88cc703f912138f804c Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Tue, 21 Sep 2021 16:32:28 +0200 Subject: [PATCH] Option to select a specific version of a github pkg. --- defaults/main.yml | 3 ++- tasks/r-packages-install.yml | 2 +- templates/update_r_packages.sh.j2 | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 26debe7..273ea51 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -256,8 +256,9 @@ r_plugins_from_sources: [] # The 'repo' scope is required # r_plugins_from_github_api_token: 'Put it into a vault file' +# The 'version' parameter is optional. The default is 'master' r_plugins_from_github: [] -# - { plugin_name: 'RFigisGeo', github_user: 'openfigis' } +# - { plugin_name: 'RFigisGeo', github_user: 'openfigis', version: 'v0.2-beta1' } # - { plugin_name: 'rsdmx', github_user: 'opensdmx' } # diff --git a/tasks/r-packages-install.yml b/tasks/r-packages-install.yml index c17e37b..848652e 100644 --- a/tasks/r-packages-install.yml +++ b/tasks/r-packages-install.yml @@ -85,7 +85,7 @@ - name: Install R packages from github command: > - Rscript --slave --no-site-file --no-init-file --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.github_user }}/{{ item.plugin_name }}'); print('Added'); } else { print('Already Installed'); }" + Rscript --slave --no-site-file --no-init-file --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.github_user }}/{{ item.plugin_name }}', ref = {{ item.version | default('master') }}, upgrade='never'); 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" diff --git a/templates/update_r_packages.sh.j2 b/templates/update_r_packages.sh.j2 index 2d4a013..3cfa553 100644 --- a/templates/update_r_packages.sh.j2 +++ b/templates/update_r_packages.sh.j2 @@ -58,6 +58,7 @@ R_VERSIONED_PKGS_LIST= GITHUB_PKGS_SKIP=0 R_PKGS_FROM_GITHUB_LIST_URL="{{ r_github_packages_list_url | default('') }}" R_PKGS_GITHUB= +R_PKGS_ALWAYS_UPGRADE_GITHUB={{ r_github_always_upgrade | default(True) }} trap "logger 'update_r_packages: trap intercepted, exiting.' ; cleanup" SIGHUP SIGINT SIGTERM @@ -70,7 +71,7 @@ function cleanup() { function usage() { if [ $PARAMS -ne 1 ] ; then echo "Need at least an argument: 'upgrade' or 'install'." - echo "- 'upgrade' installs new packages and upgrades the existin ones when needed." + echo "- 'upgrade' installs new packages and upgrades the already installed ones." echo "- 'install' installs new packages." cleanup exit 1 @@ -281,11 +282,18 @@ function r_github_pkgs() { logger "update_r_packages: Installing R packages from Github" for l in $( cat $R_PKGS_GITHUB ) ; do pkg=$( echo $l | cut -d "/" -f 2 ) - if [ "$ACTION" == "upgrade" ] ; then - Rscript $RSCRIPT_OPTIONS -e "require(devtools); require(methods); require(jsonlite) ; package_to_install <- '$l' ; refs <- jsonlite::read_json(sprintf('https://api.github.com/repos/%s/releases', package_to_install)) ; ref_to_install <- 'master'; if(length(refs)>0) { ref_to_install <- refs[[1]][['tag_name']] } ; devtools::install_github(package_to_install, ref = ref_to_install, upgrade='never')" + github_pkg=$( echo $l | awk -F ":" '{ print $1 }' - ) + github_pkg_version=$( echo $l | awk -F ":" '{ print $2 }' - ) + if [ ! -z "$github_pkg_version" ] ; then + github_version_to_install="master" + else + github_version_to_install="$github_pkg_version" + fi + if [ "$ACTION" == "upgrade" -o "$R_PKGS_ALWAYS_UPGRADE_GITHUB" ] ; then + Rscript $RSCRIPT_OPTIONS -e "require(devtools); require(methods); require(jsonlite) ; package_to_install <- '$github_pkg' ; refs <- jsonlite::read_json(sprintf('https://api.github.com/repos/%s/releases', package_to_install)) ; ref_to_install <- '$github_pkg_version'; if(length(refs)>0) { ref_to_install <- refs[[1]][['tag_name']] } ; devtools::install_github(package_to_install, ref = ref_to_install, upgrade='never')" else - Rscript $RSCRIPT_OPTIONS -e "if (! ('$pkg' %in% installed.packages()[,'Package'])) { require(devtools); require(methods); require(jsonlite) ; package_to_install <- '$l' ; refs <- jsonlite::read_json(sprintf('https://api.github.com/repos/%s/releases', package_to_install)) ; ref_to_install <- 'master'; if(length(refs)>0) { ref_to_install <- refs[[1]][['tag_name']] } ; devtools::install_github(package_to_install, ref = ref_to_install, upgrade='never') }" + Rscript $RSCRIPT_OPTIONS -e "if (! ('$pkg' %in% installed.packages()[,'Package'])) { require(devtools); require(methods); require(jsonlite) ; package_to_install <- '$github_pkg' ; refs <- jsonlite::read_json(sprintf('https://api.github.com/repos/%s/releases', package_to_install)) ; ref_to_install <- '$github_pkg_version'; if(length(refs)>0) { ref_to_install <- refs[[1]][['tag_name']] } ; devtools::install_github(package_to_install, ref = ref_to_install, upgrade='never') }" fi done else