Skip to content

Commit

Permalink
add slots #4
Browse files Browse the repository at this point in the history
  • Loading branch information
maxheld83 committed Aug 31, 2020
1 parent 551e261 commit 4f62d2a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
39 changes: 35 additions & 4 deletions R/azure.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
#' Defaults to `NULL` for public registries.
#' Do not expose your credentials in public code; it's best to use secret environment variables.
#'
#' @param slot
#' The name of the [deployment slot](https://docs.microsoft.com/en-us/azure/app-service/deploy-staging-slots).
#' Defaults to the production slot if not specified.
#' Only available for higher app service plan tiers.
#'
#' @param restart whether to restart the web app.
#'
#' @example tests/testthat/setup-azure.R
Expand All @@ -60,12 +65,14 @@ az_webapp_config <- function(name,
docker_registry_server_url = NULL,
docker_registry_server_user = NULL,
docker_registry_server_password = NULL,
slot = NULL,
restart = FALSE) {
checkmate::assert_string(name)
checkmate::assert_string(deployment_container_image_name)
checkmate::assert_string(startup_file, null.ok = TRUE)
checkmate::assert_string(subscription)
checkmate::assert_string(resource_group)
checkmate::assert_string(slot, null.ok = TRUE)
checkmate::assert_string(docker_registry_server_url, null.ok = TRUE)
checkmate::assert_string(docker_registry_server_user, null.ok = TRUE)
checkmate::assert_string(docker_registry_server_password, null.ok = TRUE)
Expand Down Expand Up @@ -104,6 +111,15 @@ az_webapp_config <- function(name,
# todo also pass on tags #25
))

# create deployment slot
if (!is.null(slot)) {
az_cli_run(args = c(
"webapp", "deployment", "slot", "create",
"--name", name,
"--slot", slot
))
}

cli::cli_alert_info("Setting web app container settings")
az_cli_run(args = c(
"webapp", "config", "container", "set",
Expand All @@ -118,6 +134,9 @@ az_webapp_config <- function(name,
},
if (!is.null(docker_registry_server_password)) {
c("--docker-registry-server-password", docker_registry_server_password)
},
if (!is.null(slot)) {
c("--slot", slot)
}
))

Expand All @@ -126,7 +145,10 @@ az_webapp_config <- function(name,
az_cli_run(args = c(
"webapp", "update",
"--client-affinity-enabled", "true", # send traffic to same machine
"--https-only", "true"
"--https-only", "true",
if (!is.null(slot)) {
c("--slot", slot)
}
))

cli::cli_alert_info("Setting web app configuration ...")
Expand All @@ -136,19 +158,28 @@ az_webapp_config <- function(name,
"--ftps-state", "disabled", # not needed
"--web-sockets-enabled", "true", # needed to serve shiny
"--http20-enabled", "true",
"--min-tls-version", "1.2"
"--min-tls-version", "1.2",
if (!is.null(slot)) {
c("--slot", slot)
}
))

# weirdly this cannot be set in the above
az_cli_run(args = c(
"webapp", "config", "appsettings", "set",
"--settings", "DOCKER_ENABLE_CI=false"
"--settings", "DOCKER_ENABLE_CI=false",
if (!is.null(slot)) {
c("--slot", slot)
}
))

cli::cli_alert_info("Restaring web app ...")
if (restart) {
az_cli_run(args = c(
"webapp", "restart"
"webapp", "restart",
if (!is.null(slot)) {
c("--slot", slot)
}
))
}

Expand Down
29 changes: 29 additions & 0 deletions man/az_webapp_config.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/testthat/setup-azure.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ az_webapp_config(
subscription = subscription
)

# deploy shiny app to slot
az_webapp_config(
name = "hello-shiny",
# this image actually includes *more* than necessary
# for example, it includes shinyserver, but just shiny would suffice
deployment_container_image_name = "rocker/shiny:4.0.2",
# above image has no `ENTRYPOINT` and/or `CMD` to start shiny by default.
# so this `[COMMAND]` must be appended to `docker run`
startup_file = paste(
"Rscript",
# setting shiny options for azure manually
# equivalent to running shinycaas::az_webapp_shiny_opts()
"-e options(shiny.host='0.0.0.0',shiny.port=as.integer(Sys.getenv('PORT')))",
# remove getOption call https://github.com/subugoe/shinycaas/issues/37
"-e shiny::runExample('04_mpg',port=getOption('shiny.port'))"
),
# replace below with your own credentials
plan = plan,
resource_group = resource_group,
subscription = subscription,
slot = "mpg"
)


# below env vars and secrets are only available on github actions
if (is_github_actions()) {
# deploy shiny app using muggle image
Expand Down

0 comments on commit 4f62d2a

Please sign in to comment.