1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
# Discover the deployment target given the passed information.
#
# Returns a list containing a deployment record and the account details to use
# when performing the deployment.
#
# When appId is provided, it must identify an existing application. The
# application may have been created by some other user. That application may
# or may not have an existing deployment record on disk.
#
# When using appId, a search across all deployment records occurs, even when
# there is no local account+server referenced by the deployment record. This
# lets us identify on-disk deployment records created by some collaborator.
# When there is no on-disk deployment record, the configured account+server is
# queried for the appId.
#
# It is an error when appId does not identify an existing application.
#
# When appName is provided, it may identify an existing application owned by
# the calling user (e.g. associated with a locally known account).
#
# When using appName, the search across deployment records is restricted to
# the incoming account+server. When there is no incoming account+server, the
# search is restricted to deployments which have a corresponding local
# account.
#
# Without appId or appName to identify an existing deployment, deployment
# records associated with local accounts (possibly restricted by incoming
# account+server) are considered before falling back to a generated name.
#
# When the targeted name does not exist locally or on the targeted
# account+server, a deployment record with NULL appId is returned, which
# signals to the caller that an application should be created.
findDeploymentTarget <- function(
recordPath = ".",
appId = NULL,
appName = NULL,
appTitle = NULL,
envVars = NULL,
account = NULL,
server = NULL,
forceUpdate = FALSE,
error_call = caller_env()
) {
if (!is.null(appId)) {
return(findDeploymentTargetByAppId(
recordPath = recordPath,
appId = appId,
appName = appName,
appTitle = appTitle,
envVars = envVars,
account = account,
server = server,
error_call = error_call
))
}
if (!is.null(appName)) {
return(findDeploymentTargetByAppName(
recordPath = recordPath,
appName = appName,
appTitle = appTitle,
envVars = envVars,
account = account,
server = server,
forceUpdate = forceUpdate,
error_call = error_call
))
}
# No identifying appId or appName.
# When there are existing deployments, ask the user to select one and use
# it. Only deployments associated with locally configured account+server
# combinations are considered.
allDeployments <- deployments(
appPath = recordPath,
accountFilter = account,
serverFilter = server
)
if (nrow(allDeployments) > 0) {
deployment <- disambiguateDeployments(allDeployments, error_call = error_call)
deployment <- updateDeployment(deployment, appTitle, envVars)
accountDetails <- findAccountInfo(deployment$account, deployment$server, error_call = error_call)
return(list(
accountDetails = accountDetails,
deployment = deployment
))
}
# Otherwise, identify a target account (given just one available or prompted
# by the user), generate a name, and locate the deployment.
accountDetails <- findAccountInfo(account, server, error_call = error_call)
appName <- generateAppName(appTitle, recordPath, accountDetails$name, unique = FALSE)
return(findDeploymentTargetByAppName(
recordPath = recordPath,
appName = appName,
appTitle = appTitle,
envVars = envVars,
account = accountDetails$name,
server = accountDetails$server,
forceUpdate = forceUpdate,
error_call = error_call
))
}
# Discover the deployment target given appId.
#
# When appId is provided, all other information is secondary. An appId is an
# indication from the caller that the content has already been deployed
# elsewhere. If we cannot locate that content, deployment fails.
#
# Local deployment records are considered first before looking for the appId
# on the target server.
#
# The target content may have been created by some other user; the account for
# this session may differ from the account used when creating the content.
findDeploymentTargetByAppId <- function(
recordPath = ".",
appId = NULL,
appName = NULL,
appTitle = NULL,
envVars = NULL,
account = NULL,
server = NULL,
error_call = caller_env()
) {
# We must have a target account+server in order to use the appId.
# The selected account may not be the original creator of the content.
accountDetails <- findAccountInfo(account, server, error_call = error_call)
# Filtering is only by server and includes all deployments in case we have a deployment record
# from a collaborator.
appDeployments <- deployments(
appPath = recordPath,
serverFilter = server,
excludeOrphaned = FALSE
)
appDeployments <- appDeployments[appDeployments$appId == appId, ]
if (nrow(appDeployments) > 1) {
cli::cli_abort(
c(
"Supplied {.arg appId} ({appId}) identifies multiple deployments.",
i = "Manage obsolete deployments with rsconnect::forgetDeployment()."
),
call = error_call
)
}
# Existing local deployment record.
if (nrow(appDeployments) == 1) {
deployment <- appDeployments[1, ]
deployment <- updateDeployment(deployment, appTitle, envVars)
return(list(
accountDetails = accountDetails,
deployment = deployment
))
}
# No local deployment record. Get it from the server.
application <- getApplication(accountDetails$name, accountDetails$server, appId)
# Note: The account+server of this deployment record may
# not correspond to the original content creator.
deployment <- createDeploymentFromApplication(application, accountDetails)
deployment <- updateDeployment(deployment, appTitle, envVars)
return(list(
accountDetails = accountDetails,
deployment = deployment
))
}
# Discover the deployment target given appName.
#
# When appName is provided it identifies content previously created by a
# locally configured account.
#
# The account details from the deployment record identify the final
# credentials we will use, as account+server may not have been specified by
# the caller.
findDeploymentTargetByAppName <- function(
recordPath = ".",
appName = NULL,
appTitle = NULL,
envVars = NULL,
account = NULL,
server = NULL,
forceUpdate = FALSE,
error_call = caller_env()
) {
appDeployments <- deployments(
appPath = recordPath,
nameFilter = appName,
accountFilter = account,
serverFilter = server
)
# When the appName along with the (optional) account+server identifies
# exactly one previous deployment, use it.
if (nrow(appDeployments) == 1) {
deployment <- appDeployments[1, ]
deployment <- updateDeployment(deployment, appTitle, envVars)
accountDetails <- findAccountInfo(deployment$account, deployment$server, error_call = error_call)
return(list(
accountDetails = accountDetails,
deployment = deployment
))
}
# When the appName identifies multiple records, we may not have had an
# account+server constraint. Ask the user to choose.
if (nrow(appDeployments) > 1) {
deployment <- disambiguateDeployments(appDeployments, error_call = error_call)
deployment <- updateDeployment(deployment, appTitle, envVars)
accountDetails <- findAccountInfo(deployment$account, deployment$server, error_call = error_call)
return(list(
accountDetails = accountDetails,
deployment = deployment
))
}
# When the appName does not identify a record, see if it exists on the
# server. That content is conditionally used. A resolved account is
# required.
accountDetails <- findAccountInfo(account, server, error_call = error_call)
if (!isPositCloudServer(accountDetails$server)) {
client <- clientForAccount(accountDetails)
application <- tryCatch(
getAppByName(client, accountDetails, appName, error_call = error_call),
rsconnect_app_not_found = function(err) NULL
)
if (!is.null(application)) {
uniqueName <- findUnique(appName, application$name)
if (shouldUpdateApp(application, uniqueName, forceUpdate, error_call = error_call)) {
deployment <- createDeploymentFromApplication(application, accountDetails)
deployment <- updateDeployment(deployment, appTitle, envVars)
return(list(
accountDetails = accountDetails,
deployment = deployment
))
} else {
appName <- uniqueName
}
}
}
# No existing deployment, or the caller does not want to re-use that content.
deployment <- createDeployment(
appName = appName,
appTitle = appTitle,
appId = NULL,
envVars = envVars,
username = accountDetails$name,
account = accountDetails$name,
server = accountDetails$server
)
return(list(
accountDetails = accountDetails,
deployment = deployment
))
}
createDeployment <- function(appName,
appTitle,
appId,
envVars,
username,
account,
server,
version = deploymentRecordVersion) {
# Consider merging this object with the object returned by
# deploymentRecord().
#
# Field names are shared with deploymentRecord() objects to avoid lots of
# record rewriting. Objects returned by findDeploymentTargetByAppName may
# have fields from the on-disk records, which were created by
# deploymentRecord().
list(
name = appName,
title = appTitle %||% "",
envVars = envVars,
appId = appId,
username = username,
account = account,
server = server,
version = version
)
}
createDeploymentFromApplication <- function(application, accountDetails) {
createDeployment(
appName = application$name,
appTitle = application$title,
appId = application$id,
envVars = NULL,
username = application$owner_username %||% accountDetails$name,
account = accountDetails$name,
server = accountDetails$server
)
}
updateDeployment <- function(previous, appTitle = NULL, envVars = NULL) {
createDeployment(
appName = previous$name,
appTitle = appTitle %||% previous$title,
appId = previous$appId,
envVars = envVars %||% previous$envVars[[1]],
# if username not previously recorded, use current account
username = previous$username %||% previous$account,
account = previous$account,
server = previous$server,
version = previous$version
)
}
defaultAppName <- function(recordPath, server = NULL) {
if (isDocumentPath(recordPath)) {
name <- file_path_sans_ext(basename(recordPath))
if (name == "index") {
# parent directory will give more informative name
name <- basename(dirname(recordPath))
} else {
# deploying a document
}
} else {
# deploying a directory
name <- basename(recordPath)
}
if (isShinyappsServer(server)) {
# Replace non-alphanumerics with underscores, trim to length 64
name <- tolower(gsub("[^[:alnum:]_-]+", "_", name, perl = TRUE))
name <- gsub("_+", "_", name)
if (nchar(name) > 64) {
name <- substr(name, 1, 64)
}
}
name
}
shouldUpdateApp <- function(application,
uniqueName,
forceUpdate = FALSE,
error_call = caller_env()) {
if (forceUpdate) {
return(TRUE)
}
message <- c(
"Discovered a previously deployed app named {.str {application$name}}",
"(View it at {.url {application$url}})"
)
prompt <- "What do you want to do?"
choices <- c(
"Update the existing app.",
"Create a new app with automatically generated name ({.str {uniqueName}}).",
"Abort this deployment and supply a custom `appName`."
)
not_interactive <- c(
i = "Set `forceUpdate = TRUE` to update it.",
i = "Supply a unique `appName` to deploy a new application."
)
cli_menu(message, prompt, choices, not_interactive, quit = 3, error_call = error_call) == 1
}
findUnique <- function(x, existing) {
i <- 1
name <- paste0(x, "-", i)
while (name %in% existing) {
i <- i + 1
name <- paste0(x, "-", i)
}
name
}
|