Skip to content

Commit

Permalink
Implement apptainer parameter for use_rang ref #110 [no ci] (#156)
Browse files Browse the repository at this point in the history
* Not using ... ref #110

* Update doc

* Correct docs

* Change the default CRAN_mirror

* Revert "Change the default CRAN_mirror"

This reverts commit 98e03b0.

* Apptainer

* MF
  • Loading branch information
chainsawriot authored Oct 6, 2023
1 parent 608ba52 commit 984583b
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 13 deletions.
18 changes: 13 additions & 5 deletions R/use_rang.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#' @param verbose logical, whether to print out messages
#' @param force logical, whether to overwrite files (`inst/rang/update.R`, `Makefile`, `.here`) if they
#' exist.
#' @param apptainer logical, whether to use apptainer. `FALSE` indicates using Docker
#' @return path, invisibly
#' @details The infrastructure being added to your path consists of:
#' * `inst/rang` directory in the project root
Expand All @@ -24,7 +25,7 @@
#' you might need to edit this manually.
#' @export
use_rang <- function(path = ".", add_makefile = TRUE, add_here = TRUE,
verbose = TRUE, force = FALSE) {
verbose = TRUE, force = FALSE, apptainer = FALSE) {
if (isFALSE(dir.exists(path))) {
stop("'path' does not exist")
}
Expand All @@ -35,11 +36,18 @@ use_rang <- function(path = ".", add_makefile = TRUE, add_here = TRUE,
} else {
.vcat(verbose, "`inst/rang` exists.")
}
if (apptainer) {
source_update_file <- "update_apptainer.R"
make_file <- "apptainer/Makefile"
} else {
source_update_file <- "update.R"
make_file <- "Makefile"
}
if (isFALSE(file.exists(file.path(base_dir, "update.R"))) || isTRUE(force)) {
file.copy(system.file("update.R", package = "rang"), file.path(base_dir, "update.R"), overwrite = TRUE)
file.copy(system.file(source_update_file, package = "rang"), file.path(base_dir, "update.R"), overwrite = TRUE)
}
if (isTRUE(add_makefile) && (isFALSE(file.exists(file.path(path, "Makefile"))) || isTRUE(force))) {
file.copy(system.file("Makefile", package = "rang"), file.path(path, "Makefile"), overwrite = TRUE)
file.copy(system.file(make_file, package = "rang"), file.path(path, "Makefile"), overwrite = TRUE)
.vcat(verbose, "`Makefile` added.")
}
if (isTRUE(add_here) && (isFALSE(file.exists(file.path(path, ".here"))) || isTRUE(force))) {
Expand Down Expand Up @@ -80,7 +88,7 @@ use_rang <- function(path = ".", add_makefile = TRUE, add_here = TRUE,
#' [The Turing Way: Research Compendia](https://the-turing-way.netlify.app/reproducible-research/compendia.html)
#' Gorman, KB, Williams TD. and Fraser WR (2014). Ecological Sexual Dimorphism and Environmental Variability within a Community of Antarctic Penguins (Genus Pygoscelis). PLoS ONE 9(3):e90081. \doi{10.1371/journal.pone.0090081}
#' @export
create_turing <- function(path, add_rang = TRUE, add_makefile = TRUE, add_here = TRUE, verbose = TRUE, force = FALSE) {
create_turing <- function(path, add_rang = TRUE, add_makefile = TRUE, add_here = TRUE, verbose = TRUE, force = FALSE, apptainer = FALSE) {
if (isTRUE(dir.exists(path))) {
stop("`path` exists.")
}
Expand All @@ -91,7 +99,7 @@ create_turing <- function(path, add_rang = TRUE, add_makefile = TRUE, add_here =
dir.create(file.path(path, "figures"))
dir.create(file.path(path, "data_clean"))
if (isTRUE(add_rang)) {
use_rang(path, add_makefile = add_makefile, add_here = add_here, verbose = verbose, force = force)
use_rang(path, add_makefile = add_makefile, add_here = add_here, verbose = verbose, force = force, apptainer = apptainer)
}
invisible(path)
}
10 changes: 5 additions & 5 deletions inst/Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
## Autogenerated by rang, you might want to change the handle
handle=yourproject
.PHONY: update build dockerlaunch dockerbash dockerdaemon dockerstop export rebuild
.PHONY: update build launch bash daemon stop export rebuild

update:
Rscript inst/rang/update.R
build: Dockerfile
docker build -t ${handle}img .
dockerlaunch:
launch:
docker run --rm --name "${handle}container" -ti ${handle}img
dockerbash:
bash:
docker run --rm --name "${handle}container" --entrypoint bash -ti ${handle}img
dockerdaemon:
daemon:
docker run -d --rm --name "${handle}container" -ti ${handle}img
dockerstop:
stop:
docker stop ${handle}container
export:
docker save ${handle}img | gzip > ${handle}img.tar.gz
Expand Down
16 changes: 16 additions & 0 deletions inst/apptainer/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Autogenerated by rang, you might want to change the handle
handle=yourproject
.PHONY: update build apptainerlaunch apptainerbash apptainerdaemon apptainerstop export

update:
Rscript inst/rang/update.R
build: container.def
apptainer build "${handle}img.sif" container.def
launch:
apptainer run "${handle}img.sif" R
bash:
apptainer shell "${handle}img.sif"
daemon:
apptainer instance start "${handle}img.sif" "${handle}container"
stop:
apptainer instance stop "${handle}container"
2 changes: 1 addition & 1 deletion inst/update.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rang <- resolve(here::here(),

## You might want to edit `post_installation_steps` or `cache`
dockerize(rang, output_dir = here::here(), verbose = TRUE, cache = TRUE,
post_installation_steps = c(recipes[['make']], recipes[['texlive']], recipes[['clean']]),
post_installation_steps = c(recipes[["make"]], recipes[["texlive"]], recipes[["clean"]]),
insert_readme = FALSE,
copy_all = TRUE,
cran_mirror = cran_mirror)
21 changes: 21 additions & 0 deletions inst/update_apptainer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
library(rang)
library(here)

cran_mirror <- "https://cloud.r-project.org/"

## Please note that the project scanning result should be checked manually.
## 1. Github packages must be added manually
## as_pkgrefs(here::here())
## 2. You might also want to change the `snapshot_date` to a fix date, when
## the project is finalized.

rang <- resolve(here::here(),
snapshot_date = NA,
verbose = TRUE)

## You might want to edit `post_installation_steps` or `cache`
apptainerize(rang, output_dir = here::here(), verbose = TRUE, cache = TRUE,
post_installation_steps = c(recipes[["make"]], recipes[["texlive"]], recipes[["clean"]]),
insert_readme = FALSE,
copy_all = TRUE,
cran_mirror = cran_mirror)
8 changes: 7 additions & 1 deletion man/create_turing.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/use_rang.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/testthat/test_use_rang.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,19 @@ test_that("Existing components .here", {
content <- readLines(file.path(tempdir, ".here"))
expect_unequal(content, dummy) ## got overwritten
})

test_that("Apptainer", {
tempdir <- .generate_temp_dir()
dir.create(tempdir)
msg <- capture_messages(use_rang(tempdir, apptainer = TRUE))
expect_true(any(grepl("infrastructure", msg)))
tempdir <- .generate_temp_dir()
dir.create(tempdir)
expect_silent(use_rang(tempdir, verbose = FALSE, apptainer = TRUE))
expect_true(dir.exists(file.path(tempdir, "inst/rang")))
expect_true(file.exists(file.path(tempdir, "inst/rang/update.R")))
expect_true(file.exists(file.path(tempdir, ".here")))
expect_true(file.exists(file.path(tempdir, "Makefile")))
expect_true(any(grepl("apptainer", readLines(file.path(tempdir, "inst/rang/update.R")))))
expect_true(any(grepl("apptainer", readLines(file.path(tempdir, "Makefile")))))
})

0 comments on commit 984583b

Please sign in to comment.