Created
July 11, 2018 22:14
-
-
Save sellorm/dd6431ed3501bab96f85ea9617817ec3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env Rscript | |
library(rvest) | |
futile.logger::flog.info('starting...') | |
futile.logger::flog.info('checking website') | |
ubuntu_dailies <- read_html("https://dailies.rstudio.com/rstudioserver/oss/ubuntu/amd64/") | |
futile.logger::flog.info('extracting url') | |
ubuntu_latest <- ubuntu_dailies %>% | |
html_nodes(".filename a") %>% | |
html_attr("href") | |
futile.logger::flog.info(paste0('URL: ', ubuntu_latest[1])) | |
futile.logger::flog.info('building filename and filepath') | |
url_split <- unlist(strsplit(ubuntu_latest[1], "/")) | |
filename <- url_split[length(url_split)] | |
filepath <- paste0("/tmp/", filename) | |
futile.logger::flog.info(paste0('downloading file: ', filepath)) | |
download.file(ubuntu_latest[1], filepath, quiet = TRUE) | |
futile.logger::flog.info('installing') | |
install_info <- system2("/usr/bin/gdebi", args = c("-n", filepath), stdout = TRUE, stderr = TRUE) | |
futile.logger::flog.info('getting installed rstudio version') | |
rstudio_version <- system2("/usr/sbin/rstudio-server", args="version", stdout = TRUE) | |
futile.logger::flog.info(paste0('removing: ', filepath)) | |
unlink(filepath) | |
# posting to slack | |
futile.logger::flog.info("Posting to slack...") | |
if ( Sys.getenv("SLACK_WEBHOOK_URL")[1] == ""){ | |
futile.logger::flog.error("SLACK_WEBHOOK_URL environment variable not set") | |
quit() | |
} else { | |
slack_webhook_url <- Sys.getenv("SLACK_WEBHOOK_URL")[1] | |
} | |
if ( Sys.getenv("RSTUDIO_SERVER_URL")[1] == ""){ | |
futile.logger::flog.error("RSTUDIO_SERVER_URL environment variable not set") | |
quit() | |
} else { | |
rstudio_server_url <- Sys.getenv("RSTUDIO_SERVER_URL")[1] | |
} | |
slack_message <- paste0("The RStudio Server updater has run. <", rstudio_server_url, "|Now at v", rstudio_version, "> and the current daily is: ", filename) | |
payload <- list(text = slack_message , username = "RStudio Server Updater") | |
r <- httr::POST(slack_webhook_url, body = payload, encode = "json") | |
# the end | |
futile.logger::flog.info('finished') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It desperately needs some actual error handling, but this script gets the URL of the latest RStudio Server Daily for ubuntu, downloads it and installs it.
It needs two environment variables setting for the post to slack (via a webhook) thing at the end to work.
SLACK_WEBHOOK_URL - the URL of your slack webhook
RSTUDIO_SERVER_URL - the URL of the RStudio Server instance we're updating (this is used as a link in the message)
You'll need the rvest, futile.logger, and httr R packages installed. You'll also need the 'gdebi' command line tool
I have this set up as a cronjob to update RSS a couple of times a week. I'm running it as root, but i probably shouldn't be. (I could perhaps improve that by running the script as a non-privileged user and doing a 'sudo gdebi...' in the system2 call, but I haven't spent a great deal of time thinking that through).