R/selenium.R

Defines functions selenium

Documented in selenium

#' Selenium Server
#'
#' This function starts the selenium server, at the present moment only works in
#' Ubuntu and you need to provide your password in the file ~/.Renviron using
#' the follow arg: PASSWORD=yourpassword.
#'
#' @param port selenium (Default: 4445)
#' @param port2 VPN (Default: 5901)
#' @param auto detect the ports that are not being used and start the selenium
#'     server using them.
#' 
#' @export

selenium <- function(port = 4445L, port2 = 5901L, auto = TRUE) {
    
    if (Sys.getenv("PASSWORD") == "") {
        stop("You must provide your password in your ~/.Renviron\n",
             "\tEx: PASSWORD=12345")
    }

    # --------------------------------------------
    # Ubuntu
    # --------------------------------------------
    x <- paste0("echo ", Sys.getenv("PASSWORD"), " | sudo -S docker ps")
    x <- invisible(system(x, intern = TRUE))
    
    if (isTRUE(auto)) {
        port <- stringr::str_extract(x, "[0-9]{4}(?=->4444)")[-1]
        port2 <- stringr::str_extract(x, "[0-9]{4}(?=->5900)")[-1]

        port <- as.integer(port)
        port2 <- as.integer(port2)

        new_ports <- 4490:4445
        new_ports2 <- 5990:5901

        port <- new_ports[which(port != new_ports)]
        port2 <- new_ports2[which(port2 != new_ports2)]

        port <- sample(port, 1)
        port2 <- sample(port2, 1)

        system(
            paste0(
                "echo ", Sys.getenv("PASSWORD"),
                " | sudo -S docker run -d -p ", port, ":4444 -p ",
                port2, ":5900 selenium/standalone-firefox-debug:2.53.0"
            )
        )
        Sys.sleep(10)
        return(c(port = port, port2 = port2))
    } else {
        if (!any(stringr::str_detect(x, paste0(port, "->4444"))) &
            !any(stringr::str_detect(x, paste0(port2, "->5900")))) {
            system(
                paste0(
                    "echo ", Sys.getenv("PASSWORD"),
                    " | sudo -S docker run -d -p ", port, ":4444 -p ",
                    port2, ":5900 selenium/standalone-firefox-debug:2.53.0"
                )
            )
            Sys.sleep(10)
            return(c(port = port, port2 = port2))
        } else {
            message("There is already a docker in progress in these ports.")
        }
    }
    
    
}
Andryas/WEBDATA documentation built on Jan. 2, 2020, 1:31 p.m.