|
11 | 11 | pico_username <- local({ |
12 | 12 | username <- NULL |
13 | 13 |
|
14 | | - function(host = "pipe.pico.sh", ssh_args = NULL, timeout = 10.0) { |
| 14 | + function(host = "pico.sh", ssh_args = NULL, timeout = 10.0) { |
15 | 15 | if (is.null(username)) { |
16 | | - ## (1) Launch a temporary pico pipe with a random name |
17 | | - topic <- session_uuid() |
18 | | - p_pipe <- pico_pipe(topic, command = "pipe", args = "-r", host = host, ssh_args = ssh_args) |
19 | | - on.exit(tryCatch(pico_terminate(p_pipe), error = identity)) |
20 | | - p <- p_pipe$process |
21 | | - msg <- sprintf("msg=%s", topic) |
22 | | - |
23 | | - ## (2) Wait for pico pipe to be up |
24 | | - t_max <- proc.time()[3] + timeout |
25 | | - pico_send_message(p_pipe, message = msg) |
26 | | - repeat { |
27 | | - bfr <- pico_receive_message(p_pipe) |
28 | | - if (length(bfr) > 0L) { |
29 | | - if (any(bfr == msg)) break |
30 | | - } |
31 | | - if (proc.time()[3] > t_max) { |
32 | | - stop("Failed to identity pico.sh username - echo failed") |
33 | | - } |
34 | | - Sys.sleep(0.1) |
| 16 | + debug <- isTRUE(getOption("future.p2p.debug")) |
| 17 | + if (debug) { |
| 18 | + mdebug_push("pico_username() ...") |
| 19 | + mdebugf("Host: %s", sQuote(host)) |
| 20 | + mdebugf("Timeout: %s seconds", timeout) |
| 21 | + mstr(list(ssh_args = ssh_args)) |
| 22 | + mstr(list(ssh_args = ssh_args)) |
| 23 | + on.exit({ |
| 24 | + mdebug_pop() |
| 25 | + }) |
35 | 26 | } |
36 | | - |
37 | | - ## (3) Get info pico pipe |
38 | | - t_max <- proc.time()[3] + timeout |
39 | | - pattern <- sprintf(".*[[:blank:]]([^/]+)/%s:.*", topic) |
40 | | - while (is.null(username)) local({ |
41 | | - p_ls <- pico_pipe(command = "ls", host = host, ssh_args = ssh_args) |
42 | | - on.exit(tryCatch(pico_terminate(p_ls), error = identity)) |
43 | | - p <- p_ls$process |
44 | | - if (length(bfr <- p$read_all_output_lines()) > 1L) { |
45 | | - line <- grep(pattern, bfr, value = TRUE) |
46 | | - if (length(line) == 1L) { |
47 | | - username <<- gsub(pattern, "\\1", line) |
48 | | - return(username) |
49 | | - } |
50 | | - } |
51 | | - if (proc.time()[3] > t_max) { |
52 | | - stop("Failed to identity pico.sh username - ls failed") |
53 | | - } |
54 | | - Sys.sleep(0.1) |
55 | | - }) |
| 27 | + |
| 28 | + ssh_config <- list(options = ssh_args, host = host) |
| 29 | + args <- c(ssh_config[["options"]], ssh_config[["host"]], "user") |
| 30 | + if (debug) { |
| 31 | + mdebug("SSH call:") |
| 32 | + mstr(list(args = args)) |
| 33 | + } |
| 34 | + |
| 35 | + out <- system2("ssh", args = args, stdout = TRUE, stderr = TRUE, timeout = timeout) |
| 36 | + if (debug) { |
| 37 | + mdebug("SSH result:") |
| 38 | + mstr(list(out = out)) |
| 39 | + } |
| 40 | + status <- attr(out, "status") |
| 41 | + if (!is.null(status)) { |
| 42 | + stop(sprintf("Failed to infer pico.sh username. Exit code %s", status)) |
| 43 | + } |
| 44 | + stopifnot(length(out) >= 3) |
| 45 | + username <<- structure(out[1], id = out[2], created_on = as.POSIXct(sub("T", " ", out[3])), class = "pico_username") |
56 | 46 | } |
57 | 47 |
|
58 | | - structure(username, class = "pico_username") |
| 48 | + username |
59 | 49 | } |
60 | 50 | }) |
61 | 51 |
|
|
0 commit comments