Skip to content

Commit 5c7db5e

Browse files
Path fixes
1 parent 47630ee commit 5c7db5e

File tree

7 files changed

+98
-30
lines changed

7 files changed

+98
-30
lines changed

samples/_TEMPLATE/create.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
" utils.print_error(f'The specified infrastructure resource group and its resources must exist first. Please check that the user-defined parameters above are correctly referencing an existing infrastructure. If it does not yet exist, run the desired infrastructure in the /infra/ folder first.')\n",
7979
" raise SystemExit(1)\n",
8080
"\n",
81-
"# 3) Run the deployment\n",
82-
"output = utils.create_bicep_deployment_group(rg_name, rg_location, deployment, bicep_parameters)\n",
81+
"# 3) Run the deployment using the utility function that handles working directory management\n",
82+
"output = utils.create_bicep_deployment_group_for_sample('_TEMPLATE', rg_name, rg_location, bicep_parameters)\n",
8383
"\n",
8484
"# 4) Print a deployment summary, if successful; otherwise, exit with an error\n",
8585
"if not output.success:\n",
@@ -151,4 +151,4 @@
151151
},
152152
"nbformat": 4,
153153
"nbformat_minor": 2
154-
}
154+
}

samples/authX-pro/create.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@
129129
" utils.print_error(f'The specified infrastructure resource group and its resources must exist first. Please check that the user-defined parameters above are correctly referencing an existing infrastructure. If it does not yet exist, run the desired infrastructure in the /infra/ folder first.')\n",
130130
" raise SystemExit(1)\n",
131131
"\n",
132-
"# 3) Run the deployment\n",
133-
"output = utils.create_bicep_deployment_group(rg_name, rg_location, deployment, bicep_parameters)\n",
132+
"# 3) Run the deployment using the utility function that handles working directory management\n",
133+
"output = utils.create_bicep_deployment_group_for_sample('authX-pro', rg_name, rg_location, bicep_parameters)\n",
134134
"\n",
135135
"# 4) Print a deployment summary, if successful; otherwise, exit with an error\n",
136136
"if not output.success:\n",

samples/authX/create.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@
103103
" utils.print_error(f'The specified infrastructure resource group and its resources must exist first. Please check that the user-defined parameters above are correctly referencing an existing infrastructure. If it does not yet exist, run the desired infrastructure in the /infra/ folder first.')\n",
104104
" raise SystemExit(1)\n",
105105
"\n",
106-
"# 3) Run the deployment\n",
107-
"output = utils.create_bicep_deployment_group(rg_name, rg_location, deployment, bicep_parameters)\n",
106+
"# 3) Run the deployment using the utility function that handles working directory management\n",
107+
"output = utils.create_bicep_deployment_group_for_sample('authX', rg_name, rg_location, bicep_parameters)\n",
108108
"\n",
109109
"# 4) Print a deployment summary, if successful; otherwise, exit with an error\n",
110110
"if not output.success:\n",
@@ -193,4 +193,4 @@
193193
},
194194
"nbformat": 4,
195195
"nbformat_minor": 2
196-
}
196+
}

samples/general/create.ipynb

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,14 @@
6868
"source": [
6969
"import utils\n",
7070
"\n",
71-
"# 1) Define the Bicep parameters with serialized APIs\n",
71+
"# Define the Bicep parameters with serialized APIs\n",
7272
"bicep_parameters = {\n",
73-
" 'apis': {'value': [api.to_dict() for api in apis]}\n",
73+
" 'apis' : { 'value': [api.to_dict() for api in apis] },\n",
74+
" 'apimSku': { 'value': 'Developer' }\n",
7475
"}\n",
7576
"\n",
76-
"# 2) Infrastructure must be in place before samples can be layered on top\n",
77-
"if not utils.does_resource_group_exist(rg_name):\n",
78-
" utils.print_error(f'The specified infrastructure resource group and its resources must exist first. Please check that the user-defined parameters above are correctly referencing an existing infrastructure. If it does not yet exist, run the desired infrastructure in the /infra/ folder first.')\n",
79-
" raise SystemExit(1)\n",
80-
"\n",
81-
"# 3) Run the deployment\n",
82-
"output = utils.create_bicep_deployment_group(rg_name, rg_location, deployment, bicep_parameters)\n",
83-
"\n",
84-
"# 4) Print a deployment summary, if successful; otherwise, exit with an error\n",
85-
"if not output.success:\n",
86-
" raise SystemExit('Deployment failed')\n",
87-
"\n",
88-
"if output.success and output.json_data:\n",
89-
" apim_gateway_url = output.get('apimResourceGatewayURL', 'APIM API Gateway URL')\n",
90-
"\n",
91-
"utils.print_ok('Deployment completed')"
77+
"# Execute the deployment using the utility function that handles working directory management\n",
78+
"output = utils.create_bicep_deployment_group_for_sample('general', rg_name, rg_location, bicep_parameters)"
9279
]
9380
},
9481
{

samples/load-balancing/create.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@
8484
" utils.print_error(f'The specified infrastructure resource group and its resources must exist first. Please check that the user-defined parameters above are correctly referencing an existing infrastructure. If it does not yet exist, run the desired infrastructure in the /infra/ folder first.')\n",
8585
" raise SystemExit(1)\n",
8686
"\n",
87-
"# 3) Run the deployment\n",
88-
"output = utils.create_bicep_deployment_group(rg_name, rg_location, deployment, bicep_parameters)\n",
87+
"# 3) Run the deployment using the utility function that handles working directory management\n",
88+
"output = utils.create_bicep_deployment_group_for_sample('load-balancing', rg_name, rg_location, bicep_parameters)\n",
8989
"\n",
9090
"# 4) Print a deployment summary, if successful; otherwise, exit with an error\n",
9191
"if not output.success:\n",

samples/secure-blob-access/create.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@
131131
" utils.print_error(f'The specified infrastructure resource group and its resources must exist first. Please check that the user-defined parameters above are correctly referencing an existing infrastructure. If it does not yet exist, run the desired infrastructure in the /infra/ folder first.')\n",
132132
" raise SystemExit(1)\n",
133133
"\n",
134-
"# 3) Run the deployment\n",
135-
"output = utils.create_bicep_deployment_group(rg_name, rg_location, deployment, bicep_parameters)\n",
134+
"# 3) Run the deployment using the utility function that handles working directory management\n",
135+
"output = utils.create_bicep_deployment_group_for_sample('secure-blob-access', rg_name, rg_location, bicep_parameters)\n",
136136
"\n",
137137
"# 4) Print a deployment summary, if successful; otherwise, exit with an error\n",
138138
"if not output.success:\n",

shared/python/utils.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,87 @@ def create_bicep_deployment_group(rg_name: str, rg_location: str, deployment: st
417417
return run(f"az deployment group create --name {deployment_name} --resource-group {rg_name} --template-file \"{main_bicep_path}\" --parameters \"{params_file_path}\" --query \"properties.outputs\"",
418418
f"Deployment '{deployment_name}' succeeded", f"Deployment '{deployment_name}' failed.")
419419

420+
421+
def find_project_root() -> str:
422+
"""
423+
Find the project root directory by looking for specific marker files.
424+
425+
Returns:
426+
str: Path to the project root directory.
427+
428+
Raises:
429+
FileNotFoundError: If project root cannot be determined.
430+
"""
431+
current_dir = os.getcwd()
432+
433+
# Look for marker files that indicate the project root
434+
marker_files = ['requirements.txt', 'README.md', 'bicepconfig.json']
435+
436+
while current_dir != os.path.dirname(current_dir): # Stop at filesystem root
437+
if any(os.path.exists(os.path.join(current_dir, marker)) for marker in marker_files):
438+
# Additional check: verify this looks like our project by checking for samples directory
439+
if os.path.exists(os.path.join(current_dir, 'samples')):
440+
return current_dir
441+
current_dir = os.path.dirname(current_dir)
442+
443+
# If we can't find the project root, raise an error
444+
raise FileNotFoundError("Could not determine project root directory")
445+
446+
447+
def create_bicep_deployment_group_for_sample(sample_name: str, rg_name: str, rg_location: str, bicep_parameters: dict, bicep_parameters_file: str = 'params.json', rg_tags: dict | None = None) -> Output:
448+
"""
449+
Create a Bicep deployment for a sample, handling the working directory change automatically.
450+
This function ensures that the params.json file is written to the correct sample directory
451+
regardless of the current working directory (e.g., when running from VS Code).
452+
453+
Args:
454+
sample_name (str): Name of the sample (used for deployment name and directory).
455+
rg_name (str): Name of the resource group.
456+
rg_location (str): Azure region for the resource group.
457+
bicep_parameters: Parameters for the Bicep template.
458+
bicep_parameters_file (str, optional): File to write parameters to.
459+
rg_tags (dict, optional): Additional tags to apply to the resource group.
460+
461+
Returns:
462+
Output: The result of the deployment command.
463+
"""
464+
import os
465+
466+
# Get the current working directory
467+
original_cwd = os.getcwd()
468+
469+
try:
470+
# Determine the sample directory path
471+
# This handles both cases: running from project root or from sample directory
472+
if os.path.basename(original_cwd) == sample_name:
473+
# Already in the sample directory
474+
sample_dir = original_cwd
475+
else:
476+
# Assume we're in project root or elsewhere, navigate to sample directory
477+
project_root = find_project_root()
478+
sample_dir = os.path.join(project_root, 'samples', sample_name)
479+
480+
# Verify the sample directory exists and has main.bicep
481+
if not os.path.exists(sample_dir):
482+
raise FileNotFoundError(f"Sample directory not found: {sample_dir}")
483+
484+
main_bicep_path = os.path.join(sample_dir, 'main.bicep')
485+
if not os.path.exists(main_bicep_path):
486+
raise FileNotFoundError(f"main.bicep not found in sample directory: {sample_dir}")
487+
488+
# Change to the sample directory to ensure params.json is written there
489+
os.chdir(sample_dir)
490+
print(f"📁 Changed working directory to: {sample_dir}")
491+
492+
# Call the original deployment function
493+
return create_bicep_deployment_group(rg_name, rg_location, sample_name, bicep_parameters, bicep_parameters_file, rg_tags)
494+
495+
finally:
496+
# Always restore the original working directory
497+
os.chdir(original_cwd)
498+
print(f"📁 Restored working directory to: {original_cwd}")
499+
500+
420501
def create_resource_group(rg_name: str, resource_group_location: str | None = None, tags: dict | None = None) -> None:
421502
"""
422503
Create a resource group in Azure if it does not already exist.

0 commit comments

Comments
 (0)