Skip to content

Commit

Permalink
New: Add terraform script
Browse files Browse the repository at this point in the history
  • Loading branch information
sarvaje committed Oct 1, 2019
1 parent 4c43d08 commit fb2e3db
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ local.settings.json
# Autogenerated files
packages/services/src/scanner/categories.json
packages/services/src/scanner/hint-extends.json

# Terraform
.terraform
*.tfstate
*.tfstate.backup
38 changes: 19 additions & 19 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ steps:
displayName: 'Install Node.js'
- script: |
echo "##vso[task.setvariable variable=dockerTag]$(date '+%Y%m%d%H%M%S')"
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
displayName: 'Calculating docker image tag'
- script: |
echo Docker tag calculated $(dockerTag)
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
displayName: 'Docker tag check'
- script: |
yarn --ignore-engines
Expand All @@ -36,10 +36,10 @@ steps:
displayName: 'Test'
- script: |
npm install -g azure-functions-core-tools
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
displayName: 'Install Azure functions core tools'
- task: CmdLine@2
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
inputs:
script: |
cd $(Build.SourcesDirectory)/packages/services
Expand All @@ -50,19 +50,19 @@ steps:
failOnStderr: true
displayName: 'Install Azure Function extensions'
- task: CmdLine@2
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
inputs:
script: |
cd $(Build.SourcesDirectory)/packages/utils
rm -rf node_modules/
npm install
npm install --only=prod
cd ../services
rm -rf node_modules/
npm install
npm install --only=prod
workingDirectory: '$(Build.SourcesDirectory)'
displayName: 'Install dependecies for utils and services functions'
- task: ArchiveFiles@2
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
inputs:
rootFolderOrFile: '$(Build.SourcesDirectory)/packages/services'
includeRootFolder: false
Expand All @@ -72,13 +72,13 @@ steps:
verbose: true
displayName: Create deployment zip file for service functions
- task: Docker@2
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
displayName: Login to ACR
inputs:
command: login
containerRegistry: DockerACRConnection
containerRegistry: DockerTerraformACRConnection
- task: Docker@2
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
displayName: Build worker image
inputs:
command: build
Expand All @@ -87,27 +87,27 @@ steps:
Dockerfile: $(Build.SourcesDirectory)/packages/worker/Dockerfile
buildContext: $(Build.SourcesDirectory)
- task: Docker@2
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
displayName: Push worker image
inputs:
command: push
containerRegistry: DockerACRConnection
containerRegistry: DockerTerraformACRConnection
repository: $(dockerImage)
tags: $(dockerTag)
- task: AzureFunctionApp@1
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
inputs:
azureSubscription: 'webhint-staging'
azureSubscription: 'webhint-staging-terraform'
appType: 'functionAppLinux'
appName: 'webhint-staging-functions'
appName: 'service-functions-staging'
package: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
runtimeStack: 'DoCKER|microsoft/azure-functions-node8:2.0'
appSettings: '-APPINSIGHTS_INSTRUMENTATIONKEY $(APPINSIGHTS_INSTRUMENTATIONKEY) -AzureWebJobsStorage $(AzureWebJobsStorage) -DatabaseConnection $(DatabaseConnection) -FUNCTIONS_EXTENSION_VERSION $(FUNCTIONS_EXTENSION_VERSION) -FUNCTIONS_WORKER_RUNTIME $(FUNCTIONS_WORKER_RUNTIME) -QueueConnection $(QueueConnection) -WEBSITE_ENABLE_SYNC_UPDATE_SITE $(WEBSITE_ENABLE_SYNC_UPDATE_SITE) -WEBSITE_NODE_DEFAULT_VERSION $(WEBSITE_NODE_DEFAULT_VERSION) -WEBSITES_ENABLE_APP_SERVICE_STORAGE $(WEBSITES_ENABLE_APP_SERVICE_STORAGE) -WEBSITE_RUN_FROM_PACKAGE $(WEBSITE_RUN_FROM_PACKAGE)'
displayName: Deploy service functions
- task: AzureFunctionAppContainer@1
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
# condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
displayName: Deploy worker function
inputs:
azureSubscription: 'webhint-staging'
appName: workerstagingchrome
azureSubscription: 'webhint-staging-terraform'
appName: worker-functions
imageName: $(DOCKER_REPOSITORY)/$(dockerImage):$(dockerTag)
155 changes: 155 additions & 0 deletions scripts/deploy/azure/azure.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
provider "azurerm" {
subscription_id = "7e37cd1e-ed76-41c7-bc84-4a3a6bfbe9cb"
client_id = "63215898-e9be-49b8-9e1e-e69dc4284aa9"
client_secret = "792a748d-7196-4a61-923e-2958f6d604c7"
tenant_id = "72f988bf-86f1-41af-91ab-2d7cd011db47"
}

resource "azurerm_resource_group" "rg" {
name = "test-terraform-staging"
location = "eastus"
}

resource "azurerm_app_service_plan" "linuxConsumptionPlan" {
name = "ConsumptionPlan"
location = "westus"
resource_group_name = "${azurerm_resource_group.rg.name}"
kind = "Linux"
reserved = true

sku {
tier = "Dynamic"
size = "Y1"
}
}

resource "azurerm_app_service_plan" "linuxAppServicePlan" {
name = "AppServicePlan"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
kind = "Linux"
reserved = true

sku {
tier = "PremiumV2"
size = "P2v2"
capacity = 3
}
}

resource "azurerm_container_registry" "acr" {
name = "terraformwebhintstagingregistry"
resource_group_name = "${azurerm_resource_group.rg.name}"
location = "${azurerm_resource_group.rg.location}"
sku = "Basic"
admin_enabled = true
}

resource "azurerm_servicebus_namespace" "servicebus" {
name = "test-webhint-staging"
location = "${azurerm_resource_group.rg.location}"
resource_group_name = "${azurerm_resource_group.rg.name}"
sku = "Standard"
}

resource "azurerm_servicebus_queue" "jobsqueue" {
name = "webhint-jobs"
resource_group_name = "${azurerm_resource_group.rg.name}"
namespace_name = "${azurerm_servicebus_namespace.servicebus.name}"

dead_lettering_on_message_expiration = true
default_message_ttl = "P14D"
duplicate_detection_history_time_window = "PT30S"
enable_partitioning = false
lock_duration = "PT5M"
max_delivery_count = 30
max_size_in_megabytes = 1024
}

resource "azurerm_servicebus_queue" "resultsqueue" {
name = "webhint-results"
resource_group_name = "${azurerm_resource_group.rg.name}"
namespace_name = "${azurerm_servicebus_namespace.servicebus.name}"

dead_lettering_on_message_expiration = true
default_message_ttl = "P14D"
duplicate_detection_history_time_window = "PT30S"
enable_partitioning = false
lock_duration = "PT30S"
max_delivery_count = 10
max_size_in_megabytes = 1024
}

resource "azurerm_cosmosdb_account" "cosmosdb" {
name = "webhint-staging-db"
resource_group_name = "${azurerm_resource_group.rg.name}"
location = "${azurerm_resource_group.rg.location}"
offer_type = "standard"
kind = "MongoDB"

consistency_policy {
consistency_level = "session"
}

geo_location {
location = "${azurerm_resource_group.rg.location}"
failover_priority = 0
}
}

resource "azurerm_application_insights" "appInsight" {
name = "webhint-appinsight"
resource_group_name = "${azurerm_resource_group.rg.name}"
location = "${azurerm_resource_group.rg.location}"

application_type = "web"
}

resource "azurerm_storage_account" "functionsStorage" {
name = "webhintstorage"
resource_group_name = "${azurerm_resource_group.rg.name}"
location = "${azurerm_resource_group.rg.location}"

account_tier = "Standard"
account_replication_type = "LRS"
account_kind = "Storage"
}

resource "azurerm_function_app" "functionsServices" {
name = "service-functions-staging"
resource_group_name = "${azurerm_resource_group.rg.name}"
location = "westus"

app_service_plan_id = "${azurerm_app_service_plan.linuxConsumptionPlan.id}"
storage_connection_string = "${azurerm_storage_account.functionsStorage.primary_connection_string}"
version = "~2"

app_settings = {
"APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.appInsight.instrumentation_key}"
"DatabaseConnection" = "${azurerm_cosmosdb_account.cosmosdb.connection_strings[0]}"
"FUNCTIONS_WORKER_RUNTIME" = "node"
"QueueConnection" = "${azurerm_servicebus_namespace.servicebus.default_primary_connection_string}"
"WEBSITE_NODE_DEFAULT_VERSION" = "10.14.1"
}
}

resource "azurerm_function_app" "functionsWorker" {
name = "worker-functions"
resource_group_name = "${azurerm_resource_group.rg.name}"
location = "${azurerm_resource_group.rg.location}"

app_service_plan_id = "${azurerm_app_service_plan.linuxAppServicePlan.id}"
storage_connection_string = "${azurerm_storage_account.functionsStorage.primary_connection_string}"
version = "~2"

site_config {
always_on = true
linux_fx_version = "DOCKER|mcr.microsoft.com/azure-functions/node:2.0-node8-appservice"
}

app_settings = {
"APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.appInsight.instrumentation_key}"
"QueueConnection" = "${azurerm_servicebus_namespace.servicebus.default_primary_connection_string}"
"WEBSITE_NODE_DEFAULT_VERSION" = "10.14.1"
}
}

0 comments on commit fb2e3db

Please sign in to comment.