Skip to content

Commit 9829470

Browse files
committed
Working prototype
1 parent 7677b85 commit 9829470

File tree

3 files changed

+27
-57
lines changed

3 files changed

+27
-57
lines changed

R/launchers.R

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,9 @@ launch_remote <- function(n = 1L, remote = remote_config(), ..., tls = NULL, .co
9494
dots <- if (...length()) parse_dots(envir, ...) else envir[["dots"]]
9595
if (is.null(tls)) tls <- envir[["tls"]]
9696

97-
if (length(remote) == 2L && remote[["platform"]] == "posit_workbench") {
98-
args <- remote[["args"]]
99-
tools <- posit_tools()
100-
is.environment(tools) || stop(._[["posit_api"]])
101-
return(posit_workbench_launch(n, args, tools))
97+
if (length(remote) == 2L) {
98+
remote[["platform"]] == "posit_workbench" || stop(._[["posit_api"]])
99+
return(posit_workbench_launch(n, args, Sys.getenv("PWB_API_KEY")))
102100
}
103101

104102
command <- remote[["command"]]
@@ -404,6 +402,8 @@ cluster_config <- function(command = "sbatch", options = "", rscript = "Rscript"
404402
#' Generates a remote configuration for launching daemons via the default
405403
#' configured Posit Workbench launcher method.
406404
#'
405+
#' @param apikey your 32 character Posit Workbench API key.
406+
#'
407407
#' @inherit remote_config return
408408
#'
409409
#' @seealso [ssh_config()], [cluster_config()], and [remote_config()] for other
@@ -420,10 +420,9 @@ cluster_config <- function(command = "sbatch", options = "", rscript = "Rscript"
420420
#'
421421
#' @export
422422
#'
423-
posit_workbench_config <- function() {
424-
tools <- posit_tools()
425-
is.null(tools) && stop(._[["posit_api"]])
426-
args <- posit_get_info(tools)
423+
posit_workbench_config <- function(apikey = Sys.getenv("PWB_API_KEY")) {
424+
args <- posit_get_info(apikey)
425+
is.null(args) && stop(._[["posit_api"]])
427426
list(platform = "posit_workbench", args = args)
428427
}
429428

@@ -512,48 +511,32 @@ find_dot <- function(args) {
512511
sel
513512
}
514513

515-
posit_tools <- function() {
516-
from_json <- get0(".rs.fromJSON")
517-
cookie <- Sys.getenv("RS_SESSION_RPC_COOKIE")
514+
posit_get_info <- function(apikey) {
518515
server <- Sys.getenv("RS_SERVER_ADDRESS")
519-
is.function(from_json) && nzchar(cookie) && nzchar(server) || return()
520-
list(from_json = from_json, cookie = cookie, server = server)
521-
}
522-
523-
posit_get_info <- function(tools) {
516+
nzchar(server) || return()
524517
info <- ncurl(
525-
url = file.path(tools[["server"]], "api", "get_compute_envs"),
526-
headers = c(cookie = tools[["cookie"]])
518+
url = file.path(server, "api", "get_compute_envs"),
519+
headers = c(Authorization = sprintf("Bearer %s", apikey))
527520
)
528-
data <- tools[["from_json"]](info[["data"]])
529-
cluster <- .subset2(data, c(1L, 1L, 1L))
530-
list(name = cluster[["name"]], image = cluster[["defaultImage"]])
521+
info[["status"]] == 200L || return()
522+
json <- info[["data"]]
523+
image <- sub('.*"defaultImage"\\s*:\\s*"([^"]*)".*', '\\1', json, perl = TRUE)
524+
list(name = "Kubernetes", image = image)
531525
}
532526

533-
posit_workbench_launch <- function(n, args, tools) {
534-
tools <- posit_tools()
527+
posit_workbench_launch <- function(n, args, apikey) {
528+
server <- Sys.getenv("RS_SERVER_ADDRESS")
529+
nzchar(server) || return()
535530
json <- sprintf(
536-
'{"method":"launch_job","kwparams":{"job":{"cluster":"%s","container":{"image":"%s"},"name":"mirai_daemon","exe":"Rscript","args":["-e","mirai::daemon(\\"%s\\")"]}}}',
531+
'{"method":"launch_job","kwparams":{"job":{"cluster":"%s","container":{"image":"%s"},"resourceProfile":"default","name":"mirai_daemon","exe":"Rscript","args":["-e","mirai::daemon(\\"%s\\")"]}}}',
537532
args[["name"]],
538533
args[["image"]],
539534
nextget("url")
540535
)
541-
info <- ncurl(
542-
url = file.path(tools[["server"]], "api", "launch_job"),
536+
res <- ncurl(
537+
url = file.path(server, "api", "launch_job"),
543538
method = "POST",
544-
headers = c(cookie = tools[["cookie"]]),
539+
headers = c(Authorization = sprintf("Bearer %s", apikey)),
545540
data = json
546541
)
547-
cluster <- args[["name"]]
548-
container <- new_container(args[["image"]])
549-
cmds <- launch_remote(n)
550-
lapply(cmds, function(cmd)
551-
submit_job(
552-
sprintf("mirai_daemon_%s", random(3L)),
553-
cluster = cluster,
554-
command = cmd,
555-
container = container
556-
)
557-
)
558-
cmds
559542
}

man/posit_workbench_config.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/tests.R

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,6 @@ for (i in 0:4)
6060
test_null(register_serial("test_klass1", serialize, unserialize))
6161
test_null(register_serial(c("test_klass2", "test_klass3"), list(serialize, serialize), list(unserialize, unserialize)))
6262
test_equal(length(mirai:::.[["serial"]][[3L]]), 3L)
63-
# cloud launcher tests
64-
is.null(mirai:::posit_tools()) && {
65-
ns <- new.env(parent = emptyenv())
66-
`[[<-`(ns, ".rs.api.launcher.jobsFeatureAvailable", function() TRUE)
67-
`[[<-`(ns, ".rs.api.launcher.getInfo", function() list(clusters = list(list(name = "Kubernetes", defaultImage = "1.a.b.reg.prov.com/int-r-sess:ubuntu2204-20250609"))))
68-
`[[<-`(ns, ".rs.api.launcher.newContainer", function(image) image)
69-
`[[<-`(ns, ".rs.api.launcher.submitJob", function(...) NULL)
70-
attach(ns, name = "tools:rstudio")
71-
cfg <- posit_workbench_config()
72-
test_type("list", cfg)
73-
test_nzchar(daemons(url = local_url(), dispatcher = FALSE))
74-
test_class("miraiLaunchCmd", launch_remote(n = 2L, remote = cfg))
75-
test_null(daemons(0))
76-
detach()
77-
test_error(posit_workbench_config(), "can only be used from Posit Workbench")
78-
}
7963
# mirai and daemons tests
8064
connection && {
8165
Sys.sleep(1L)

0 commit comments

Comments
 (0)