Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Terraform #512

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ on:
- created

jobs:
terraform:
runs-on: ubuntu-18.04

env:
ARM_CLIENT_ID: ${{secrets.SP_CLIENT}}
ARM_CLIENT_SECRET: ${{secrets.SP_PASSWORD}}
ARM_SUBSCRIPTION_ID: ${{secrets.SUBSCRIPTION_ID}}
ARM_TENANT_ID: ${{secrets.SP_TENANT}}
AZURE_STORAGE_ACCOUNT: "tstate31414"
TF_ACTION_WORKING_DIR: "./docker/setup/terraform"

steps:
- uses: actions/checkout@v2

- name: Setup Terraform
uses: hashicorp/setup-terraform@v1

- name: Terraform Init
run: terraform init

- name: Terraform Apply
run: terraform apply -auto-approve


release:
runs-on: ubuntu-18.04

Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,28 @@ jobs:
ci build verify-build
- run: bash <(curl -s https://codecov.io/bash)
if: ${{ success() }}

terraform-plan:
runs-on: ubuntu-18.04

env:
ARM_CLIENT_ID: ${{secrets.SP_CLIENT}}
ARM_CLIENT_SECRET: ${{secrets.SP_PASSWORD}}
ARM_SUBSCRIPTION_ID: ${{secrets.SUBSCRIPTION_ID}}
ARM_TENANT_ID: ${{secrets.SP_TENANT}}

defaults:
run:
working-directory: "./docker/setup/terraform"

steps:
- uses: actions/checkout@v2

- name: Setup Terraform
uses: hashicorp/setup-terraform@v1

- name: Terraform Init
run: terraform init

- name: Terraform Plan
run: terraform plan
11 changes: 11 additions & 0 deletions docker/ci/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ RUN wget -q https://github.com/mvdan/sh/releases/download/v${SHFMT_VERSION}/shfm
&& chmod +x /usr/local/bin/shfmt \
&& shfmt -version

ARG TERRAFORM_VERSION=0.13.5
RUN wget -q https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
&& unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
&& cp terraform /usr/local/bin \
&& rm terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
&& terraform --version

COPY docker/ci/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

Expand Down Expand Up @@ -70,3 +77,7 @@ RUN helm lint --strict ./helm/opwen_cloudserver \
&& rm helm.yaml

RUN shfmt -d -i 2 -ci .

RUN terraform fmt -check

RUN terraform validate -no-color
17 changes: 17 additions & 0 deletions docker/setup/terraform/01_provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Configure Azure storage to manage Terraform State
# ref: https://docs.microsoft.com/en-us/azure/developer/terraform/store-state-in-azure-storage
terraform {
backend "azurerm" {
resource_group_name = "tstate"
storage_account_name = "tstate31414"
container_name = "tstate"
key = "terraform.tfstate"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on making the key dynamic, e.g. based on branch name via the GITHUB_REF environment variable, so that we can run this potentially for multiple deployments (e.g. for work-in-progress code) without risking to override the production resources.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure can! That makes a lot of sense.

}
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
version = "=2.23.0"

features {}
}
166 changes: 166 additions & 0 deletions docker/setup/terraform/02_data_rg.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# ------------------------------
# Data Resource Group
# ------------------------------

# Create a data resource group if it doesn't exist
resource "azurerm_resource_group" "data" {
name = "${var.RESOURCE_GROUP_NAME}data"
location = var.location

tags = {}
}

# Generate random text for a unique storage account name
resource "random_id" "randomId" {
keepers = {
# Generate a new ID only when a new resource group is defined
resource_group = azurerm_resource_group.vm.name
}

byte_length = 3
}

# Create server table storage account if it doesn't exist
resource "azurerm_storage_account" "serverTablesName" {
name = "${var.serverTablesName}${random_id.randomId.hex}"
resource_group_name = azurerm_resource_group.data.name
location = azurerm_resource_group.data.location
account_kind = "Storage"
account_tier = "Standard"
account_replication_type = "GRS"
}

# Create server blob storage account if it doesn't exist
resource "azurerm_storage_account" "serverBlobsName" {
name = "${var.serverBlobsName}${random_id.randomId.hex}"
resource_group_name = azurerm_resource_group.data.name
location = azurerm_resource_group.data.location
account_kind = "Storage"
account_tier = "Standard"
account_replication_type = "GRS"
}

# Create client blob storage account if it doesn't exist
resource "azurerm_storage_account" "clientBlobsName" {
name = "${var.clientBlobsName}${random_id.randomId.hex}"
resource_group_name = azurerm_resource_group.data.name
location = azurerm_resource_group.data.location
account_kind = "Storage"
account_tier = "Standard"
account_replication_type = "GRS"
}

resource "azurerm_storage_container" "clientsauth" {
name = "clientsauth"
storage_account_name = azurerm_storage_account.serverTablesName.name
container_access_type = "private"
}

resource "azurerm_storage_container" "pendingemails" {
name = "pendingemails"
storage_account_name = azurerm_storage_account.serverTablesName.name
container_access_type = "private"
}

resource "azurerm_storage_container" "users" {
name = "users"
storage_account_name = azurerm_storage_account.serverTablesName.name
container_access_type = "private"
}

resource "azurerm_storage_container" "emails" {
name = "emails"
storage_account_name = azurerm_storage_account.serverBlobsName.name
container_access_type = "private"
}

resource "azurerm_storage_container" "mailbox" {
name = "mailbox"
storage_account_name = azurerm_storage_account.serverBlobsName.name
container_access_type = "private"
}

resource "azurerm_storage_container" "secretsopwencluster" {
name = "secretsopwencluster"
storage_account_name = azurerm_storage_account.serverBlobsName.name
container_access_type = "private"
}

resource "azurerm_storage_container" "sendgridinboundemails" {
name = "sendgridinboundemails"
storage_account_name = azurerm_storage_account.serverBlobsName.name
container_access_type = "private"
}

resource "azurerm_storage_container" "compressedpackages" {
name = "compressedpackages"
storage_account_name = azurerm_storage_account.clientBlobsName.name
container_access_type = "private"
}

# Create a server queue if it doesn't exist
resource "azurerm_servicebus_namespace" "data" {
name = var.serverQueuesName
location = azurerm_resource_group.data.location
resource_group_name = azurerm_resource_group.data.name
sku = "Standard"
zone_redundant = false
}

resource "azurerm_servicebus_namespace_authorization_rule" "data" {
name = var.serverQueuesSasName
namespace_name = azurerm_servicebus_namespace.data.name
resource_group_name = azurerm_resource_group.data.name
send = true
listen = true
manage = true
}

resource "azurerm_servicebus_queue" "serverQueueSendgridMime" {
name = var.serverQueueSendgridMime
resource_group_name = azurerm_resource_group.data.name
namespace_name = azurerm_servicebus_namespace.data.name
max_size_in_megabytes = 512
}

resource "azurerm_servicebus_queue" "serverQueueEmailSend" {
name = var.serverQueueEmailSend
resource_group_name = azurerm_resource_group.data.name
namespace_name = azurerm_servicebus_namespace.data.name
max_size_in_megabytes = 5120
}

resource "azurerm_servicebus_queue" "serverQueueClientPackage" {
name = var.serverQueueClientPackage
resource_group_name = azurerm_resource_group.data.name
namespace_name = azurerm_servicebus_namespace.data.name
max_size_in_megabytes = 5120
}

resource "azurerm_servicebus_queue" "mailboxreceived" {
name = "mailboxreceived"
resource_group_name = azurerm_resource_group.data.name
namespace_name = azurerm_servicebus_namespace.data.name
max_size_in_megabytes = 1024
}

resource "azurerm_servicebus_queue" "mailboxsent" {
name = "mailboxsent"
resource_group_name = azurerm_resource_group.data.name
namespace_name = azurerm_servicebus_namespace.data.name
max_size_in_megabytes = 1024
}

resource "azurerm_servicebus_queue" "register" {
name = "register"
resource_group_name = azurerm_resource_group.data.name
namespace_name = azurerm_servicebus_namespace.data.name
max_size_in_megabytes = 1024
}

resource "azurerm_servicebus_queue" "service" {
name = "service"
resource_group_name = azurerm_resource_group.data.name
namespace_name = azurerm_servicebus_namespace.data.name
max_size_in_megabytes = 1024
}
11 changes: 11 additions & 0 deletions docker/setup/terraform/03_server_rg.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ------------------------------
# Server Resource Group
# ------------------------------

# Create the server resource group if it doesn't exist
resource "azurerm_resource_group" "server" {
name = "${var.RESOURCE_GROUP_NAME}server"
location = var.location

tags = {}
}
Loading