Skip to content

Commit

Permalink
Support key_file argument in do_provision. Use Ubuntu 24.04 LTS i…
Browse files Browse the repository at this point in the history
…mage. Change R install toolchain to use Posit R install instructions. Fix #42.
  • Loading branch information
meztez committed Jul 15, 2024
1 parent 6f2aa7e commit cf73136
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 46 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Description: Gives the ability to automatically deploy a plumber API
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
RoxygenNote: 7.3.1
Imports:
analogsea (>= 0.9.4),
ssh,
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

* Change certbot install procedure, since PPA deprecated.
* Add an apt update step before processing R install.
* Support `key_file` argument in `do_provision`.
* Use Ubuntu 24.04 LTS image.
* Change R install toolchain to use Posit R install instructions.

# plumberDeploy 0.2.1

Expand Down
60 changes: 32 additions & 28 deletions R/digital-ocean.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,23 @@ do_provision <- function(droplet, unstable=FALSE, example=TRUE, ...){
message("THIS ACTION COSTS YOU MONEY!")
message("Provisioning a new server for which you will get a bill from DigitalOcean.")

createArgs <- list(...)
createArgs$tags <- c(createArgs$tags, "plumber")
createArgs$image <- "ubuntu-24-04-x64"
keyfile <- createArgs$keyfile

# Check if local has ssh keys configured or keyfile provided
if (!length(ssh::ssh_key_info()) && is.null(keyfile)) {
stop("No local ssh key found with `ssh::ssh_key_info()` and `keyfile` argument not provided.")
}

# Check if DO has ssh keys configured
if (!length(analogsea::keys())) {
stop("Please add an ssh key to your Digital Ocean account before using this method. See `analogsea::key_create` method.")
}

createArgs <- list(...)
createArgs$tags <- c(createArgs$tags, "plumber")
createArgs$image <- "ubuntu-20-04-x64"

droplet <- do.call(analogsea::droplet_create, createArgs)

# Wait for the droplet to come online
analogsea::droplet_wait(droplet)

# I often still get a closed port after droplet_wait returns. Buffer for just a bit
Sys.sleep(25)

Expand All @@ -84,15 +87,16 @@ do_provision <- function(droplet, unstable=FALSE, example=TRUE, ...){
}

# Provision
lines <- droplet_capture(droplet, 'swapon | grep "/swapfile" | wc -l')
lines <- droplet_capture(droplet, 'swapon | grep "/swapfile" | wc -l', keyfile = keyfile)
if (lines != "1"){
analogsea::debian_add_swap(droplet)
analogsea::ubuntu_add_swap(droplet)
Sys.sleep(5)
}

do_install_plumber(droplet, unstable)

if (example){
do_deploy_api(droplet, "hello", system.file("plumber", "10-welcome", package = "plumber"), port=8000, forward=TRUE)
do_deploy_api(droplet, "hello", system.file("plumber", "10-welcome", package = "plumber"), port=8000, forward=TRUE, keyfile = keyfile)
}

invisible(droplet)
Expand All @@ -101,22 +105,35 @@ do_provision <- function(droplet, unstable=FALSE, example=TRUE, ...){
#' @export
#' @rdname do_provision
do_install_plumber = function(droplet, unstable, ...) {
install_new_r(droplet, ...)
analogsea::droplet_ssh(
droplet,
"sudo echo 'DEBIAN_FRONTEND=noninteractive' >> /etc/environment",
"export R_VERSION=%s.%s" |> sprintf(R.version[["major"]], R.version[["minor"]]),
"curl -O https://cdn.rstudio.com/r/ubuntu-2404/pkgs/r-${R_VERSION}_1_amd64.deb",
"sudo apt-get update -y",
"sudo apt-get install -y ./r-${R_VERSION}_1_amd64.deb",
"sudo ln -s -f /opt/R/${R_VERSION}/bin/R /usr/local/bin/R",
"sudo ln -s -f /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript",
...
)
Sys.sleep(3)
install_plumber(droplet, unstable, ...)
install_api(droplet, ...)
Sys.sleep(3)
install_nginx(droplet, ...)
Sys.sleep(3)
install_firewall(droplet, ...)
}

install_plumber <- function(droplet, unstable, ...){

analogsea::debian_apt_get_install(droplet, "libssl-dev", "make", "libsodium-dev", "libcurl4-openssl-dev", ...)
analogsea::ubuntu_apt_get_install(droplet, "libssl-dev", "make", "libsodium-dev", "libcurl4-openssl-dev", ...)

if (unstable){
analogsea::install_r_package(droplet, "remotes", repo = "https://packagemanager.rstudio.com/cran/__linux__/focal/latest", ...)
analogsea::install_r_package(droplet, "remotes", repo = "https://packagemanager.posit.co/cran/__linux__/noble/latest", ...)
analogsea::droplet_ssh(droplet, "Rscript -e \"remotes::install_github('rstudio/plumber')\"", ...)
} else {
analogsea::install_r_package(droplet, "plumber", repo = "https://packagemanager.rstudio.com/cran/__linux__/focal/latest", ...)
analogsea::install_r_package(droplet, "plumber", repo = "https://packagemanager.posit.co/cran/__linux__/noble/latest", ...)
}

}
Expand Down Expand Up @@ -159,7 +176,7 @@ install_firewall <- function(droplet, ...){
}

install_nginx <- function(droplet, ...){
analogsea::debian_apt_get_install(droplet, "nginx", ...)
analogsea::ubuntu_apt_get_install(droplet, "nginx", ...)
analogsea::droplet_ssh(droplet, "rm -f /etc/nginx/sites-enabled/default", ...) # Disable the default site
analogsea::droplet_ssh(droplet, "mkdir -p /var/certbot", ...)
analogsea::droplet_ssh(droplet, "mkdir -p /etc/nginx/sites-available/plumber-apis/", ...)
Expand All @@ -169,19 +186,6 @@ install_nginx <- function(droplet, ...){
analogsea::droplet_ssh(droplet, "systemctl reload nginx", ...)
}

install_new_r <- function(droplet, ...){
analogsea::droplet_ssh(droplet, "sudo echo 'DEBIAN_FRONTEND=noninteractive' >> /etc/environment", ...)
analogsea::droplet_ssh(droplet, "sudo apt-get update -qq")
analogsea::debian_apt_get_install(droplet, c("dirmngr", "gnupg","apt-transport-https", "ca-certificates", "software-properties-common"),
...)
analogsea::droplet_ssh(droplet, "apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9", ...)
analogsea::droplet_ssh(droplet, "add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'", ...)
analogsea::droplet_upload(droplet, local=system.file("server", "apt.conf.d", package="plumberDeploy"),
remote = "/etc/apt", ...)
analogsea::debian_apt_get_update(droplet, ...)
analogsea::debian_install_r(droplet, ...)
}

#' Add HTTPS to a plumber Droplet
#'
#' Adds TLS/SSL (HTTPS) to a droplet created using [do_provision()].
Expand Down
18 changes: 5 additions & 13 deletions inst/hosted/analogsea-provision.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ source("analog-keys.R")

install <- function(droplet){
droplet %>%
debian_add_swap() %>%
install_new_r() %>%
ubuntu_add_swap() %>%
ubuntu_install_r() %>%
install_docker() %>%
prepare_plumber()
}
Expand All @@ -18,21 +18,13 @@ install_docker <- function(droplet){
# Deprecated (Shutting down dockerproject.org APT and YUM repos 2020-03-31)
droplet_ssh(c("sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D",
"echo 'deb https://apt.dockerproject.org/repo ubuntu-focal main' > /etc/apt/sources.list.d/docker.list")) %>%
debian_apt_get_update() %>%
droplet_ssh("sudo apt-get install linux-image-extra-$(uname -r)") %>%
debian_apt_get_install("docker-engine") %>%
ubuntu_apt_get_update() %>%
droplet_ssh("sudo apt install linux-image-extra-$(uname -r)") %>%
ubuntu_apt_get_install("docker-engine") %>%
droplet_ssh(c("curl -L https://github.com/docker/compose/releases/download/1.7.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose",
"chmod +x /usr/local/bin/docker-compose"))
}

install_new_r <- function(droplet){
droplet %>%
droplet_ssh(c("echo 'deb https://cran.rstudio.com/bin/linux/ubuntu focal-cran40/' >> /etc/apt/sources.list",
"sudo apt-key adv --keyserver keys.gnupg.net --recv-key 'E298A3A825C0D65DFD57CBB651716619E084DAB9'")) %>%
debian_apt_get_update() %>%
debian_install_r()
}

prepare_plumber<- function(droplet){
droplet %>%
droplet_ssh("git clone https://github.com/rstudio/plumber.git") %>%
Expand Down
3 changes: 0 additions & 3 deletions inst/server/apt.conf.d/local

This file was deleted.

2 changes: 1 addition & 1 deletion inst/server/plumber.service
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Description=Plumber API

[Service]
ExecStart=/usr/bin/Rscript -e "pr <- plumber::pr('/var/plumber$PATH$/plumber.R'); pr$setDocs($DOCS$); $PREFLIGHT$ pr$run(port=$PORT$)"
ExecStart=Rscript -e "pr <- plumber::pr('/var/plumber$PATH$/plumber.R'); pr$setDocs($DOCS$); $PREFLIGHT$ pr$run(port=$PORT$)"
Restart=on-abnormal
WorkingDirectory=/var/plumber/$PATH$/

Expand Down

0 comments on commit cf73136

Please sign in to comment.