From 86c07792e597e148bd5d95b5d387f743e04d5fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20=C3=9Acar?= Date: Thu, 24 Jun 2021 15:50:12 +0200 Subject: [PATCH] close #27: add an optional automated passwordless detection mechanism --- DESCRIPTION | 2 +- NEWS.md | 5 ++++- R/init.R | 14 ++++++++++++++ R/manager.R | 5 +++++ inst/tinytest/test_manager_ci.R | 14 ++++++++++++++ man/manager.Rd | 5 +++++ 6 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 R/init.R diff --git a/DESCRIPTION b/DESCRIPTION index a40469f..e61e7b7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: bspm Type: Package Title: Bridge to System Package Manager -Version: 0.3.7.1 +Version: 0.3.8 Authors@R: c( person("IƱaki", "Ucar", email="iucar@fedoraproject.org", role=c("aut", "cph", "cre"), comment=c(ORCID="0000-0001-6403-5550"))) diff --git a/NEWS.md b/NEWS.md index 65b1afc..537c37d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,10 @@ -# bspm 0.3.7.1 +# bspm 0.3.8 - Fix spurious error with `options(bspm.always.install.deps=TRUE)` (#25). - Ensure that `options(bspm.sudo=TRUE)` forces `sudo` unconditionally (#28). +- Add new `options(bspm.sudo.autodetect=TRUE)` (not set by default) to enable + passwordless `sudo` autodetection on package load, which then sets + `options(bspm.sudo=TRUE)` accordingly (#27). # bspm 0.3.7 diff --git a/R/init.R b/R/init.R new file mode 100644 index 0000000..fc0546c --- /dev/null +++ b/R/init.R @@ -0,0 +1,14 @@ +.onLoad <- function(libname, pkgname) { + if (getOption("bspm.sudo.autodetect", FALSE)) + sudo_autodetect() +} + +sudo_autodetect <- function() { + nopass <- !system2nowarn("sudo", c("-n", "true"), stdout=FALSE, stderr=FALSE) + toolbox <- file.exists("/run/.toolboxenv") # see #27 + + if (nopass || toolbox) { + packageStartupMessage("bspm: passwordless sudo detected and enabled") + options(bspm.sudo=TRUE) + } +} diff --git a/R/manager.R b/R/manager.R index 2e4f46c..9473d31 100644 --- a/R/manager.R +++ b/R/manager.R @@ -16,6 +16,11 @@ #' that, if you want to fall back to \code{sudo} in a non-interactive session, #' you need to set \code{options(bspm.sudo=TRUE)}. #' +#' If \code{options(bspm.sudo.autodetect=TRUE)} on start-up, \pkg{bspm} tries +#' to detect whether it is running in an environment where password-less +#' \code{sudo} can be used (e.g., in a containerized environment such as a +#' Fedora Toolbox), and then sets \code{options(bspm.sudo=TRUE)} accordingly. +#' #' By default, if a package is not available in the system repositories, it is #' installed from R's configured repositories along with all its dependencies. #' This behavior can be changed via \code{options(bspm.always.install.deps=TRUE)}, diff --git a/inst/tinytest/test_manager_ci.R b/inst/tinytest/test_manager_ci.R index 96ca48d..dbd1b37 100644 --- a/inst/tinytest/test_manager_ci.R +++ b/inst/tinytest/test_manager_ci.R @@ -1,6 +1,20 @@ if (!at_home() || !bspm:::root()) exit_file("not in a CI environment") +sudo.avail <- unname(nchar(Sys.which("sudo")) > 0) +in.toolbox <- file.exists("/run/.toolboxenv") +expect_false(getOption("bspm.sudo", FALSE)) +bspm:::sudo_autodetect() +if (sudo.avail || in.toolbox) { + expect_true(getOption("bspm.sudo", FALSE)) +} else { + expect_false(getOption("bspm.sudo", FALSE)) + file.create("/run/.toolboxenv") + bspm:::sudo_autodetect() + expect_true(getOption("bspm.sudo", FALSE)) +} +options(bspm.sudo = NULL) + if (requireNamespace("Rcpp", quietly=TRUE)) exit_file("not in a clean environment") diff --git a/man/manager.Rd b/man/manager.Rd index b1836f7..f096692 100644 --- a/man/manager.Rd +++ b/man/manager.Rd @@ -34,6 +34,11 @@ If not, these methods fall back on using \code{sudo} to elevate permissions that, if you want to fall back to \code{sudo} in a non-interactive session, you need to set \code{options(bspm.sudo=TRUE)}. +If \code{options(bspm.sudo.autodetect=TRUE)} on start-up, \pkg{bspm} tries +to detect whether it is running in an environment where password-less +\code{sudo} can be used (e.g., in a containerized environment such as a +Fedora Toolbox), and then sets \code{options(bspm.sudo=TRUE)} accordingly. + By default, if a package is not available in the system repositories, it is installed from R's configured repositories along with all its dependencies. This behavior can be changed via \code{options(bspm.always.install.deps=TRUE)},