Skip to content

Commit afe1788

Browse files
committed
adding preprovision hook
1 parent 8535b2b commit afe1788

File tree

15 files changed

+374
-156
lines changed

15 files changed

+374
-156
lines changed

devcontainer.json renamed to .devcontainer/devcontainer.json

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,31 @@
77
"features": {
88
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
99
"ghcr.io/azure/azure-dev/azd:latest": {},
10-
"ghcr.io/devcontainers/features/azure-cli:1.0.8": {}
10+
"ghcr.io/devcontainers/features/azure-cli:latest": {},
11+
"ghcr.io/devcontainers/features/github-cli:latest": {}
1112
},
1213

1314
// Configure tool-specific properties.
1415
"customizations": {
1516
// Configure properties specific to VS Code.
1617
"vscode": {
18+
"files.watcherExclude": {
19+
"**/venv/**": true,
20+
"**/venv_old": true,
21+
"**/staticfiles": true,
22+
"**/package-lock*": true,
23+
"**/dist": true,
24+
"**/dev_media": true,
25+
"**/local_junk": true,
26+
"**/coverage": true,
27+
"**/htmlcov": true,
28+
"**/__pycache__": true,
29+
"**/rust/**/target/**": true,
30+
"**/.vscode/**": true,
31+
"**/.mypy_cache/**": true,
32+
"**/.git/**": true,
33+
"**/tmp/**": true
34+
},
1735
// Add the IDs of extensions you want installed when the container is created.
1836
"extensions": [
1937
"ms-python.python",
@@ -31,6 +49,7 @@
3149
"python.linting.pylintArgs": [
3250
"--disable=C0114,C0115,C0116"
3351
],
52+
"python.languageServer": "Pylance",
3453
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
3554
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
3655
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
@@ -46,7 +65,6 @@
4665
}
4766
}
4867
},
49-
5068
// Use 'forwardPorts' to make a list of ports inside the container available locally.
5169
"forwardPorts": [5000],
5270

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ repos:
1515
hooks:
1616
- id: black
1717
args: ['--config=./src/pyproject.toml']
18+
- repo: local
19+
hooks:
20+
- id: trufflehog
21+
name: TruffleHog
22+
description: Detect secrets in your data.
23+
entry: bash -c 'docker run -v "$(pwd):/workdir" -it --rm us-docker.pkg.dev/thog-artifacts/public/scanner:latest git main HEAD /workdir'
24+
language: system
25+
stages: ["commit", "push"]

azure.yaml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
# yaml-language-server: $schema=https://raw.githubusercontent.com/Azure/azure-dev/main/schemas/v1.0/azure.yaml.json
22

3-
name: simple-flask-container-app
3+
name: simple-bigbang-istio-service
44
metadata:
5-
5+
6+
hooks:
7+
preprovision:
8+
interactive: true
9+
shell: sh
10+
run: python3 src/hooks/preprovision.py
611
services:
712
api:
813
project: ./src
914
language: py
10-
host: containerapp
15+
host: aks
16+
hooks:
17+
postdeploy:
18+
shell: sh
19+
interactive: true
20+
run: python3 src/hooks/postdeploy.py

infra/hooks/preprov.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

infra/hooks/preprovision.py

Lines changed: 0 additions & 90 deletions
This file was deleted.

infra/hooks/wrappers/azure.py

Lines changed: 0 additions & 41 deletions
This file was deleted.
File renamed without changes.

src/hooks/__init__.py

Whitespace-only changes.

src/hooks/preprovision.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import os
2+
import subprocess
3+
from pick import pick
4+
from wrappers.azure import AzureClient, AZD_ENVIRONMENT_NAME_RESOURCE_TAG
5+
from wrappers.github import GithubClient
6+
7+
def prompt_user_for_target_clusters(clusters):
8+
"""
9+
Prompts the user to select a cluster from a list of clusters.
10+
11+
Args:
12+
clusters (List[ManagedCluster]): A list of AKS clusters.
13+
14+
Returns:
15+
ManagedCluster: The selected cluster.
16+
"""
17+
title = "Select a Big Bang cluster for your deployment environment"
18+
19+
options = [f"Environment: {cluster.tags[AZD_ENVIRONMENT_NAME_RESOURCE_TAG]} \
20+
Cluster: {cluster.name}" for cluster in clusters]
21+
22+
_, index = pick(options, title)
23+
24+
return clusters[index - 1]
25+
26+
def set_azd_env_variable(name, value, export=False):
27+
"""
28+
Sets an Azure Developer CLI environment variable with the given name and value.
29+
30+
Args:
31+
name (str): The name of the environment variable to set.
32+
value (str): The value to set for the environment variable.
33+
34+
Returns:
35+
None
36+
"""
37+
print(f"Setting {name} environment variable...")
38+
try:
39+
standard_out = subprocess.check_output(["azd", "env", "set", name, value],\
40+
universal_newlines=True)
41+
print(standard_out)
42+
43+
if export:
44+
os.environ[name] = value
45+
46+
except subprocess.CalledProcessError as ex:
47+
print(f" Error: {ex.output}")
48+
49+
if __name__ == "__main__":
50+
print("Pre-provisioning hook running...")
51+
52+
# verify that the .azure folder exists
53+
azure_dir = os.path.join(os.getcwd(), ".azure")
54+
if not os.path.exists(azure_dir):
55+
raise ValueError("The .azure folder does not exist. Please run 'azd init' \
56+
to setup your environment.")
57+
58+
print("Getting active subscription ID...")
59+
client = AzureClient()
60+
subscription_id = client.get_active_subscription_id()
61+
62+
if subscription_id is None:
63+
raise ValueError\
64+
("No active subscription found. Please run 'az login' to log in to Azure.")
65+
66+
print(f"Active subscription ID: {subscription_id}")
67+
print("Getting AKS clusters...")
68+
clusters = client.get_aks_clusters(subscription_id)
69+
70+
# Check if any clusters were found
71+
if len(clusters) == 0:
72+
raise ValueError(f"No AKS clusters found. Please create an AKS cluster and set the \
73+
{AZD_ENVIRONMENT_NAME_RESOURCE_TAG} tag.")
74+
75+
target_cluster = prompt_user_for_target_clusters(clusters)
76+
resource_group_name = target_cluster.id.split("/")[4]
77+
github_pat_token = client.get_gitops_repo_pat_token(\
78+
target_cluster.tags[AZD_ENVIRONMENT_NAME_RESOURCE_TAG], \
79+
subscription_id, resource_group_name)
80+
81+
registry = client.get_container_registry(subscription_id, resource_group_name)
82+
83+
if registry is None:
84+
raise ValueError("No container registry found. Please create a container registry \
85+
and set the azd-container-registry-name tag.")
86+
87+
if github_pat_token is not None:
88+
set_azd_env_variable("GITHUB_TOKEN", github_pat_token, True)
89+
90+
set_azd_env_variable("AZURE_AKS_CLUSTER_NAME", target_cluster.name)
91+
set_azd_env_variable("AZURE_RESOURCE_GROUP", resource_group_name)
92+
set_azd_env_variable("AZURE_CONTAINER_REGISTRY_ENDPOINT", registry.login_server)
93+
set_azd_env_variable("GITOPS_REPO_RELEASE_BRANCH", \
94+
target_cluster.tags["gitops-release-branch"], True)
95+
set_azd_env_variable("GITOPS_REPO", target_cluster.tags["gitops-repo"], True)
96+
97+
print("Pre-provisioning hook complete.")

src/hooks/wrappers/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)