|
| 1 | +package nl.codestar.azurefunctions |
| 2 | + |
| 3 | +import sbt.File |
| 4 | +import sbt.internal.util.ManagedLogger |
| 5 | + |
| 6 | +class FunctionDeployFailedException(msg: String, cause: Throwable = None.orNull) extends sbt.FeedbackProvidedException |
| 7 | + |
| 8 | +object DeployFunctionHelper { |
| 9 | + import scala.language.postfixOps |
| 10 | + import scala.sys.process._ |
| 11 | + |
| 12 | + def createResourceGroup(rgName: String, location: String, log: ManagedLogger): Unit = { |
| 13 | + log.info(s"Checking if group ${rgName} exists...") |
| 14 | + |
| 15 | + val output = (s"az group exists -n ${rgName}" !!) |
| 16 | + val groupExists = output.startsWith("true") |
| 17 | + if (!groupExists) { |
| 18 | + log.info(s"Group ${rgName} does not yet exist, creating it") |
| 19 | + val result = (s"az group create -n ${rgName} -l ${location}" !) |
| 20 | + if (result != 0) { |
| 21 | + log.error("Failed to create resource group") |
| 22 | + throw new FunctionDeployFailedException("Failed to create resource group") |
| 23 | + } else { |
| 24 | + log.info("Resource group created") |
| 25 | + } |
| 26 | + } else { |
| 27 | + log.info(s"Group ${rgName} is present") |
| 28 | + } |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + def createStorageAccount( |
| 33 | + saName: String, |
| 34 | + rgName: String, |
| 35 | + location: String, |
| 36 | + skuValue: String, |
| 37 | + log: ManagedLogger |
| 38 | + ): Unit = { |
| 39 | + log.info(s"Checking if storage account ${saName} exists...") |
| 40 | + |
| 41 | + val output = (s"az storage account check-name -n ${saName} --query nameAvailable" !!) |
| 42 | + val saExists = output.startsWith("false") |
| 43 | + if (!saExists) { |
| 44 | + log.info(s"Storage Account ${saName} does not yet exist, creating it") |
| 45 | + |
| 46 | + val result = (s"az storage account create -n ${saName} -l ${location} -g ${rgName} --sku ${skuValue}" !) |
| 47 | + if (result != 0) { |
| 48 | + log.error("Failed to create storage account") |
| 49 | + throw new FunctionDeployFailedException("Failed to create storage account") |
| 50 | + } else { |
| 51 | + log.info("Storage account created") |
| 52 | + } |
| 53 | + } else { |
| 54 | + log.info(s"Storage account ${saName} is present") |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + def createAppInsightsInstance( |
| 60 | + aiName: String, |
| 61 | + rgName: String, |
| 62 | + location: String, |
| 63 | + workspace: Option[String], |
| 64 | + log: ManagedLogger |
| 65 | + ): Unit = { |
| 66 | + log.info(s"Checking if app insights ${aiName} exists...") |
| 67 | + val output = (s"az monitor app-insights component show --app ${aiName} -g ${rgName}" !) |
| 68 | + if (output != 0) { |
| 69 | + log.info(s"App Insights ${aiName} does not yet exist, creating it") |
| 70 | + |
| 71 | + val workspacePart = workspace match { |
| 72 | + case None => "" |
| 73 | + case Some(ws) => s"--workspace $ws" |
| 74 | + } |
| 75 | + val result = (s""" |
| 76 | + |az monitor app-insights component create |
| 77 | + | --app $aiName |
| 78 | + | --location $location |
| 79 | + | --resource-group $rgName |
| 80 | + | --application-type web |
| 81 | + | $workspacePart |
| 82 | + |""".stripMargin !) |
| 83 | + if (result != 0) { |
| 84 | + log.error("Failed to create App Insights") |
| 85 | + throw new FunctionDeployFailedException("Failed to create App Insights") |
| 86 | + } else { |
| 87 | + log.info("App Insights created") |
| 88 | + } |
| 89 | + } else { |
| 90 | + log.info(s"App Insights $aiName is present") |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + def createFunctionApp( |
| 95 | + appName: String, |
| 96 | + rgName: String, |
| 97 | + location: String, |
| 98 | + saName: String, |
| 99 | + aiName: String, |
| 100 | + log: ManagedLogger |
| 101 | + ): Unit = { |
| 102 | + log.info(s"Checking if function app $appName exists...") |
| 103 | + val output = (s"az functionapp show -g $rgName --name $appName" !) |
| 104 | + if (output != 0) { |
| 105 | + log.info(s"Function App $appName does not yet exist, creating it") |
| 106 | + val result = (s""" |
| 107 | + |az functionapp create |
| 108 | + | --name $appName |
| 109 | + | --resource-group $rgName |
| 110 | + | --storage-account $saName |
| 111 | + | --consumption-plan-location $location |
| 112 | + | --app-insights $aiName |
| 113 | + | --runtime java |
| 114 | + |""".stripMargin !) |
| 115 | + if (result != 0) { |
| 116 | + log.error("Failed to create Function App") |
| 117 | + throw new FunctionDeployFailedException("Failed to create Function App") |
| 118 | + } else { |
| 119 | + log.info("Function App created") |
| 120 | + } |
| 121 | + } else { |
| 122 | + log.info(s"Function App $appName exists") |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + def deployZipFile(zipFile: File, appName: String, rgName: String, log: ManagedLogger): Unit = { |
| 127 | + val zipLocation = zipFile.getAbsolutePath |
| 128 | + log.info(s"Uploading $zipLocation into $appName") |
| 129 | + val result = (s"az functionapp deployment source config-zip -g $rgName -n $appName --src $zipLocation" !) |
| 130 | + if (result != 0) { |
| 131 | + log.error("Failed to upload zip file") |
| 132 | + throw new FunctionDeployFailedException("Failed to upload zip file") |
| 133 | + } else { |
| 134 | + log.info("Upload done") |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments