Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
split of aca and appservice.bicep
Browse files Browse the repository at this point in the history
  • Loading branch information
kjaymiller committed Sep 1, 2023
1 parent 6995008 commit 2250238
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 201 deletions.
16 changes: 15 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,19 @@
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
"workbench.colorCustomizations": {
"commandCenter.border": "#e7e7e799",
"sideBar.border": "#6204bd",
"statusBar.background": "#48038b",
"statusBar.foreground": "#e7e7e7",
"statusBarItem.hoverBackground": "#6204bd",
"statusBarItem.remoteBackground": "#48038b",
"statusBarItem.remoteForeground": "#e7e7e7",
"titleBar.activeBackground": "#48038b",
"titleBar.activeForeground": "#e7e7e7",
"titleBar.inactiveBackground": "#48038b99",
"titleBar.inactiveForeground": "#e7e7e799"
},
"peacock.color": "#48038b"
}
Empty file removed hooks/__init__.py
Empty file.
28 changes: 0 additions & 28 deletions hooks/linters.py

This file was deleted.

118 changes: 0 additions & 118 deletions hooks/movers.py

This file was deleted.

150 changes: 144 additions & 6 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,151 @@
import linters
import movers

import rich
# Steps to finalize the cookiecutter build
import shutil
import os
import pathlib
import importlib.util
import logging
import subprocess

def move_db_files(db_resource: str):
"""
Moves the correct db files to the correct location
Delete the remaining files in the db folder and the db folder itself
"""

if "postgres" in db_resource:
shutil.move(
"src/db/postgres_models.py",
"src/models.py"
)
shutil.move(
"src/db/postgres_seeder.py",
"src/flask/flaskapp/seeder.py",
)
if "mongo" in db_resource:
shutil.move(
"src/db/mongo_models.py",
"src/models.py"
)
shutil.move(
"src/db/mongo_seeder.py",
"src/flask/flaskapp/seeder.py",
)

def remove_aca_files() -> None:
"""Removes unneeded files if aca is not selected"""
file_names = (
"src/Dockerfile",
)

for file_name in file_names:
os.remove(file_name)

def remove_flask_migration_files() -> None:
"""
Removes the flask migration files if postgres is not selected
This only applies to flask projects
"""
if "{{ cookiecutter.project_backend }}" == "flask" and "mongo" in "{{cookiecutter.db_resource }}":
shutil.rmtree("src/flask/flaskapp/migrations")
else:
pass

def rename_backend_files():
"""
Rename the selected backend folder corresponding to the selected option.
remove the project_backend folders that are not selected
"""

selected_backend = "{{cookiecutter.project_backend}}"

project_backends = ["django", "fastapi", "flask"]
project_backends.remove(selected_backend)

src = pathlib.Path('src')

for unused_backend in project_backends:
shutil.rmtree(src / pathlib.Path(unused_backend))

shutil.copytree(
src / pathlib.Path(selected_backend),
pathlib.Path.cwd() / src,
dirs_exist_ok=True,
)
shutil.rmtree(src / pathlib.Path(selected_backend))


def choose_web_bicep():
"""Selects the correct web.bicep file"""
bicep_files = {
"aca": "infra/aca.bicep",
"appservice": "infra/appservice.bicep",
}

shutil.move(
bicep_files.pop("{{cookiecutter.project_host}}"),
"infra/web.bicep",
)

for file_name in bicep_files.values():
os.remove(file_name)


def check_for_files() -> None:
"""
Iterate through the cookiecutter options.
remove the files corresponding to the results
TODO: Add task progress
"""

# DB Options
# The chosen db option is stored in src.
move_db_files("{{cookiecutter.db_resource}}")
shutil.rmtree("src/db") # Clean up remaining db folder

# Backend Options
remove_flask_migration_files()

# Azure Host Options
choose_web_bicep()
if "{{cookiecutter.project_host }}" != "aca":
remove_aca_files()

if "{{cookiecutter.project_host}}" == "appservice":
pass


rename_backend_files()


def error_msg(pkg: str) -> str:
return f"`{pkg}` is not installed. Run `pip install {pkg}` to install it."

def run_ruff_fix_and_black() -> None:
"""checks if ruff and black are installed and runs them on the project"""
if importlib.util.find_spec("ruff"):
subprocess.run(["python3", "-m" "ruff", "--fix", "src"])
else:
logging.warning(error_msg("ruff"))

if importlib.util.find_spec("black"):
subprocess.run(["python3", "-m", "black", "-q", "src"])
else:
logging.warning(error_msg("black"))

def run_bicep_format() -> None:
"""formats your bicep files"""
subprocess.run(["az", "bicep", "format", "--file", "infra/main.bicep"])


def lint() -> None:
"""Runs all linters"""
run_ruff_fix_and_black()
run_bicep_format()

if __name__ == "__main__":
# It's import to remove the unecessary files before moving the db files

rich.print("Removing unecessary files")
movers.check_for_files()
check_for_files()
rich.print("Linting files")
linters.lint()
lint()
24 changes: 14 additions & 10 deletions {{cookiecutter.__src_folder_name}}/infra/aca.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ param name string
param location string = resourceGroup().location
param tags object = {}

param applicationInsightsName string
param containerAppsEnvironmentName string
param containerRegistryName string
param exists bool
param identityName string
param serviceName string = 'web'
param keyVaultUrl string
param keyVaultName string
{# The dbserver values do not exist in the postgres aca add-on #}
{% if cookiecutter.db_resource in ("postgres-flexible", "cosmos-postgres") %}
param dbserverDomainName string
Expand All @@ -18,10 +19,6 @@ param dbserverUser string
@secure()
param dbserverPassword string
{% endif %}
{% if cookiecutter.project_backend in ("django", "flask") %}
@secure()
param secretKey string
{% endif %}
{% if cookiecutter.db_resource == "postgres-addon" %}
param postgresServiceId string
{% endif %}
Expand All @@ -31,6 +28,11 @@ resource webIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-3
location: location
}

resource keyVault 'Microsoft.KeyVault/vaults@2023-02-01' existing = {
name: keyVaultName
}


{% if cookiecutter.project_host == "aca" %}
module app 'core/host/container-app-upsert.bicep' = {
name: '${serviceName}-container-app-module'
Expand Down Expand Up @@ -88,14 +90,15 @@ module app 'core/host/container-app-upsert.bicep' = {
{% if cookiecutter.project_backend in ("django", "flask") %}
{
name: 'secret-key'
value: secretKey
keyVaultUrl: '${keyVault.properties.vaultUri}/secrets/SECRETKEY'
identity: webIdentity.id
}
{% endif %}
{% if "mongodb" in cookiecutter.project_backend%}
{% if "mongodb" in cookiecutter.db_resource %}
{
name: 'azure-cosmos-connection-string'
keyVaultUrl: '${keyVaultURI}/secrets/azure-cosmos-connection-string'
identity:
keyVaultUrl: '${keyVault.properties.vaultUri}/secrets/AZURE-COSMOS-CONNECTION-STRING'
identity: webIdentity.id
}
{% endif %}
]
Expand All @@ -109,8 +112,9 @@ module app 'core/host/container-app-upsert.bicep' = {

resource applicationInsights 'Microsoft.Insights/components@2020-02-02' existing = {
name: applicationInsightsName
}

output SERVICE_WEB_IDENTITY_PRINCIPAL_ID string = webIdentity.properties.principalId
output SERVICE_WEB_IDENTITY_PRINCIPAL_ID string = webIdentity.properties.principalId
output SERVICE_WEB_NAME string = app.outputs.name
output SERVICE_WEB_URI string = app.outputs.uri
output SERVICE_WEB_IMAGE_NAME string = app.outputs.imageName
Loading

0 comments on commit 2250238

Please sign in to comment.