Skip to content

Commit d5ae230

Browse files
Merge pull request #413 from microsoft/psl-hb-fixes-28apr
fix: fixed dev container, start.sh and refactored post deployment scripts
2 parents 72019d0 + 90864f1 commit d5ae230

11 files changed

+155
-50
lines changed

.devcontainer/devcontainer.json

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@
33
"image": "mcr.microsoft.com/devcontainers/python:3.11-bullseye",
44
"forwardPorts": [50505],
55
"features": {
6-
"ghcr.io/azure/azure-dev/azd:latest": {}
6+
"ghcr.io/devcontainers/features/node:1": {
7+
"nodeGypDependencies": true,
8+
"installYarnUsingApt": true,
9+
"version": "lts",
10+
"pnpmVersion": "latest",
11+
"nvmVersion": "latest"
12+
},
13+
"ghcr.io/devcontainers/features/azure-cli:1": {
14+
"installBicep": true,
15+
"version": "latest",
16+
"bicepVersion": "latest"
17+
},
18+
"ghcr.io/azure/azure-dev/azd:0": {
19+
"version": "stable"
20+
}
721
},
822
"customizations": {
923
"vscode": {
@@ -16,7 +30,7 @@
1630
]
1731
}
1832
},
19-
"postStartCommand": "git pull origin main && python3 -m pip install -r infra/scripts/index_scripts/requirements.txt && curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash && chmod +x ./scripts/quota_check_params.sh",
33+
"postCreateCommand": "bash ./.devcontainer/setup_env.sh",
2034
"remoteUser": "vscode",
2135
"hostRequirements": {
2236
"memory": "4gb"

.devcontainer/setup_env.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
git fetch
4+
git pull
5+
6+
# provide execute permission to quotacheck script
7+
sudo chmod +x ./scripts/quota_check_params.sh

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ myenv
1111

1212
scriptsenv/
1313

14-
scriptenv
14+
scriptenv
15+
pdf

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "python",
77
"request": "launch",
88
"module": "quart",
9-
"cwd": "${workspaceFolder}",
9+
"cwd": "${workspaceFolder}/src",
1010
"env": {
1111
"QUART_APP": "app:app",
1212
"QUART_ENV": "development",

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ To change the azd parameters from the default values, follow the steps [here](./
193193
* This deployment will take *7-10 minutes* to provision the resources in your account and set up the solution with sample data.
194194
* If you get an error or timeout with deployment, changing the location can help, as there may be availability constraints for the resources.
195195
196-
5. Once the deployment has completed successfully and you would like to use the sample data, run the bash command printed in the terminal. The bash command will look like the following:
196+
5. Once the deployment has completed successfully and you would like to use the sample data, run the bash command printed in the terminal. The bash command will look like the following:
197+
```shell
198+
bash ./infra/scripts/process_sample_data.sh
199+
```
200+
if you don't have azd env then you need to pass parameters along with the command. Then the command will look like the following:
197201
```shell
198202
bash ./infra/scripts/process_sample_data.sh <Storage-Account-name> <Storgae-Account-container-name> <Key-Vault-name> <CosmosDB-Account-name> <Resource-Group-name>
199203
```

azure.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ hooks:
3232
Write-Host "Web app URL: "
3333
Write-Host "$env:WEB_APP_URL" -ForegroundColor Cyan
3434
Write-Host "`nIf you want to use the Sample Data, run the following command in the Bash terminal to process it:"
35-
Write-Host "bash ./infra/scripts/process_sample_data.sh $env:STORAGE_ACCOUNT_NAME $env:STORAGE_CONTAINER_NAME $env:KEY_VAULT_NAME $env:COSMOSDB_ACCOUNT_NAME $env:RESOURCE_GROUP_NAME" -ForegroundColor Cyan
35+
Write-Host "bash ./infra/scripts/process_sample_data.sh" -ForegroundColor Cyan
3636
shell: pwsh
3737
continueOnError: false
3838
interactive: true
@@ -41,8 +41,8 @@ hooks:
4141
echo "Web app URL: "
4242
echo $WEB_APP_URL
4343
echo ""
44-
echo "If you want to use the Sample Data, run the following command in the terminal to process it:"
45-
echo "bash ./infra/scripts/process_sample_data.sh $STORAGE_ACCOUNT_NAME $STORAGE_CONTAINER_NAME $KEY_VAULT_NAME $COSMOSDB_ACCOUNT_NAME $RESOURCE_GROUP_NAME"
44+
echo "If you want to use the Sample Data, run the following command in the bash terminal to process it:"
45+
echo "bash ./infra/scripts/process_sample_data.sh"
4646
shell: sh
4747
continueOnError: false
4848
interactive: true

docs/README_LOCAL.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
### Deploy from your local machine
22

33
#### Local Setup: Basic Chat Experience
4-
1. Copy `.env.sample` to a new file called `.env` and configure the settings as described in the [Environment variables](#environment-variables) section.
4+
1. Copy `.env.sample` present in `src` folder to a new file called `.env` and configure the settings as described in the [Environment variables](#environment-variables) section.
55

66
These variables are required:
77
- `AZURE_OPENAI_RESOURCE`
@@ -17,7 +17,7 @@
1717

1818
See the [documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#example-response-2) for more information on these parameters.
1919

20-
2. Start the app with `start.cmd`. This will build the frontend, install backend dependencies, and then start the app. Or, just run the backend in debug mode using the VSCode debug configuration in `.vscode/launch.json`.
20+
2. Start the app with `start.cmd` or `start.sh`. This will build the frontend, install backend dependencies, and then start the app. Or, just run the backend in debug mode using the VSCode debug configuration in `.vscode/launch.json`.
2121

2222
3. You can see the local running app at http://127.0.0.1:50505. If you experience a port conflict and the app does not load, stop the application in the terminal (CTRL-C on Windows), edit the `start.cmd` file and change the port to a value not in use (i.e., 5000).
2323

@@ -50,7 +50,7 @@ NOTE: You may find you need to set: MacOS: `export NODE_OPTIONS="--max-old-space
5050
- `AZURE_SEARCH_STRICTNESS`
5151
- `AZURE_OPENAI_EMBEDDING_NAME`
5252

53-
3. Start the app with `start.cmd`. This will build the frontend, install backend dependencies, and then start the app. Or, just run the backend in debug mode using the VSCode debug configuration in `.vscode/launch.json`.
53+
3. Start the app with `start.cmd` or `start.sh`. This will build the frontend, install backend dependencies, and then start the app. Or, just run the backend in debug mode using the VSCode debug configuration in `.vscode/launch.json`.
5454
4. You can see the local running app at http://127.0.0.1:50505. If you experience a port conflict and the app does not load, stop the application in the terminal (CTRL-C on Windows), edit the `start.cmd` file and change the port to a value not in use (i.e., 5000).
5555

5656
NOTE: You may find you need to set: MacOS: `export NODE_OPTIONS="--max-old-space-size=8192"` or Windows: `set NODE_OPTIONS=--max-old-space-size=8192` to avoid running out of memory when building the frontend.
@@ -62,7 +62,7 @@ To enable chat history, you will need to set up CosmosDB resources. The ARM temp
6262
- `AZURE_COSMOSDB_CONVERSATIONS_CONTAINER`
6363
- `AZURE_COSMOSDB_ACCOUNT_KEY`
6464

65-
As above, start the app with `start.cmd`, then visit the local running app at http://127.0.0.1:50505. Or, just run the backend in debug mode using the VSCode debug configuration in `.vscode/launch.json`. If you experience a port conflict and the app does not load, stop the application in the terminal (CTRL-C on Windows), edit the `start.cmd` file and change the port to a value not in use (i.e., 5000).
65+
As above, start the app with `start.cmd` or `start.sh`, then visit the local running app at http://127.0.0.1:50505. Or, just run the backend in debug mode using the VSCode debug configuration in `.vscode/launch.json`. If you experience a port conflict and the app does not load, stop the application in the terminal (CTRL-C on Windows), edit the `start.cmd` file and change the port to a value not in use (i.e., 5000).
6666

6767
#### Local Setup: Enable Message Feedback
6868
To enable message feedback, you will need to set up CosmosDB resources. Then specify these additional environment variable:
@@ -75,10 +75,10 @@ To enable message feedback, you will need to set up CosmosDB resources. Then spe
7575

7676
You can use the [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) to deploy the app from your local machine. Make sure you have version 2.48.1 or later.
7777

78-
If this is your first time deploying the app, you can use [az webapp up](https://learn.microsoft.com/en-us/cli/azure/webapp?view=azure-cli-latest#az-webapp-up). Run the following two commands from the root folder of the repo, updating the placeholder values to your desired app name, resource group, location, and subscription. You can also change the SKU if desired.
78+
If this is your first time deploying the app, you can use [az webapp up](https://learn.microsoft.com/en-us/cli/azure/webapp?view=azure-cli-latest#az-webapp-up). Run the following two commands from the `src` folder of the repo, updating the placeholder values to your desired app name, resource group, location, and subscription. You can also change the SKU if desired.
7979

8080
1. `az webapp up --runtime PYTHON:3.11 --sku B1 --name <new-app-name> --resource-group <resource-group-name> --location <azure-region> --subscription <subscription-name>`
81-
1. `az webapp config set --startup-file "python3 -m gunicorn app:app" --name <new-app-name>`
81+
1. `az webapp config set --startup-file "python3 -m gunicorn app:app" --name <new-app-name> --resource-group <resource-group-name>`
8282

8383
If you've deployed the app previously, first run this command to update the appsettings to allow local code deployment:
8484

@@ -88,10 +88,10 @@ Check the runtime stack for your app by viewing the app service resource in the
8888

8989
Check the SKU in the same way. Use the abbreviated SKU name in the argument below, e.g. for "Basic (B1)" the SKU is `B1`.
9090

91-
Then, use these commands to deploy your local code to the existing app:
91+
Then, use these commands from `src` folder to deploy your local code to the existing app:
9292

9393
1. `az webapp up --runtime <runtime-stack> --sku <sku> --name <existing-app-name> --resource-group <resource-group-name>`
94-
1. `az webapp config set --startup-file "python3 -m gunicorn app:app" --name <existing-app-name>`
94+
1. `az webapp config set --startup-file "python3 -m gunicorn app:app" --name <existing-app-name> --resource-group <resource-group-name>`
9595

9696
Make sure that the app name and resource group match exactly for the app that was previously deployed.
9797

infra/scripts/copy_kb_files.sh

+38-20
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,6 @@ fileSystem="$2"
66
# baseUrl="$3"
77
managedIdentityClientId="$3"
88

9-
zipFileName1="pdfdata.zip"
10-
extractedFolder1="pdf"
11-
zipUrl1=${baseUrl}"infra/data/pdfdata.zip"
12-
13-
# zipFileName2="audio_data.zip"
14-
# extractedFolder2="audiodata"
15-
# zipUrl2=${baseUrl}"infra/data/audio_data.zip"
16-
17-
# Create folders if they do not exist
18-
# mkdir -p "/mnt/azscripts/azscriptinput/$extractedFolder1"
19-
# mkdir -p "/mnt/azscripts/azscriptinput/$extractedFolder2"
20-
21-
# Download the zip file
22-
# curl --output /mnt/azscripts/azscriptinput/"$zipFileName1" "$zipUrl1"
23-
# curl --output /mnt/azscripts/azscriptinput/"$zipFileName2" "$zipUrl2"
24-
25-
# Extract the zip file
26-
unzip infra/data/"$zipFileName1" -d infra/data/"$extractedFolder1"
27-
# unzip /mnt/azscripts/azscriptinput/"$zipFileName2" -d /mnt/azscripts/azscriptinput/"$extractedFolder2"
28-
299
echo "Script Started"
3010

3111
# Authenticate with Azure
@@ -58,6 +38,23 @@ if [ -z "$role_assignment" ]; then
5838
MSYS_NO_PATHCONV=1 az role assignment create --assignee $signed_user_id --role "Storage Blob Data Contributor" --scope $storage_account_resource_id --output none
5939
if [ $? -eq 0 ]; then
6040
echo "Role assignment completed successfully."
41+
retries=3
42+
while [ $retries -gt 0 ]; do
43+
# Check if the role assignment was successful
44+
role_assignment_check=$(MSYS_NO_PATHCONV=1 az role assignment list --assignee $signed_user_id --role "Storage Blob Data Contributor" --scope $storage_account_resource_id --query "[].roleDefinitionId" -o tsv)
45+
if [ -n "$role_assignment_check" ]; then
46+
echo "Role assignment verified successfully."
47+
break
48+
else
49+
echo "Role assignment not found, retrying..."
50+
((retries--))
51+
sleep 10
52+
fi
53+
done
54+
if [ $retries -eq 0 ]; then
55+
echo "Error: Role assignment verification failed after multiple attempts. Try rerunning the script."
56+
exit 1
57+
fi
6158
else
6259
echo "Error: Role assignment failed."
6360
exit 1
@@ -66,6 +63,27 @@ else
6663
echo "User already has the Storage Blob Data Contributor role."
6764
fi
6865

66+
zipFileName1="pdfdata.zip"
67+
extractedFolder1="pdf"
68+
zipUrl1="infra/data/$zipFileName1"
69+
# zipUrl1=${baseUrl}"infra/data/$zipFileName1"
70+
71+
# zipFileName2="audio_data.zip"
72+
# extractedFolder2="audiodata"
73+
# zipUrl2=${baseUrl}"infra/data/audio_data.zip"
74+
75+
# Create folders if they do not exist
76+
# mkdir -p "/mnt/azscripts/azscriptinput/$extractedFolder1"
77+
# mkdir -p "/mnt/azscripts/azscriptinput/$extractedFolder2"
78+
79+
# Download the zip file
80+
# curl --output /mnt/azscripts/azscriptinput/"$zipFileName1" "$zipUrl1"
81+
# curl --output /mnt/azscripts/azscriptinput/"$zipFileName2" "$zipUrl2"
82+
83+
# Extract the zip file
84+
unzip -o $zipUrl1 -d infra/data/"$extractedFolder1"
85+
# unzip /mnt/azscripts/azscriptinput/"$zipFileName2" -d /mnt/azscripts/azscriptinput/"$extractedFolder2"
86+
6987
# Using az storage blob upload-batch to upload files with managed identity authentication, as the az storage fs directory upload command is not working with managed identity authentication.
7088
echo "Uploading files to Azure Storage"
7189
az storage blob upload-batch --account-name "$storageAccount" --destination "$fileSystem"/"$extractedFolder1" --source infra/data/"$extractedFolder1" --auth-mode login --pattern '*' --overwrite --output none

infra/scripts/process_sample_data.sh

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,31 @@ cosmosDbAccountName="$4"
88
resourceGroupName="$5"
99
managedIdentityClientId="$6"
1010

11+
# get parameters from azd env, if not provided
12+
if [ -z "$resourceGroupName" ]; then
13+
resourceGroupName=$(azd env get-value RESOURCE_GROUP_NAME)
14+
fi
15+
16+
if [ -z "$cosmosDbAccountName" ]; then
17+
cosmosDbAccountName=$(azd env get-value COSMOSDB_ACCOUNT_NAME)
18+
fi
19+
20+
if [ -z "$storageAccount" ]; then
21+
storageAccount=$(azd env get-value STORAGE_ACCOUNT_NAME)
22+
fi
23+
24+
if [ -z "$fileSystem" ]; then
25+
fileSystem=$(azd env get-value STORAGE_CONTAINER_NAME)
26+
fi
27+
28+
if [ -z "$keyvaultName" ]; then
29+
keyvaultName=$(azd env get-value KEY_VAULT_NAME)
30+
fi
31+
32+
1133
# Check if all required arguments are provided
1234
if [ -z "$storageAccount" ] || [ -z "$fileSystem" ] || [ -z "$keyvaultName" ] || [ -z "$cosmosDbAccountName" ] || [ -z "$resourceGroupName" ]; then
13-
echo "Usage: $0 <storageAccount> <fileSystem> <keyvaultName> <cosmosDbAccountName> <resourceGroupName> [managedIdentityClientId]"
35+
echo "Usage: $0 <storageAccount> <storageContainerName> <keyvaultName> <cosmosDbAccountName> <resourceGroupName>"
1436
exit 1
1537
fi
1638

infra/scripts/run_create_index_scripts.sh

+41-10
Original file line numberDiff line numberDiff line change
@@ -72,40 +72,71 @@ if [ -n "$managedIdentityClientId" ]; then
7272
sed -i "s/mici_to-be-replaced/${managedIdentityClientId}/g" "infra/scripts/index_scripts/02_process_data.py"
7373
fi
7474

75+
# Determine the correct Python command
76+
if command -v python3 &> /dev/null; then
77+
PYTHON_CMD="python3"
78+
elif command -v python &> /dev/null; then
79+
PYTHON_CMD="python"
80+
else
81+
echo "Python is not installed on this system. Or it is not added in the PATH."
82+
exit 1
83+
fi
7584

7685
# create virtual environment
7786
# Check if the virtual environment already exists
7887
if [ -d "infra/scripts/scriptenv" ]; then
7988
echo "Virtual environment already exists. Skipping creation."
8089
else
8190
echo "Creating virtual environment"
82-
python3 -m venv infra/scripts/scriptenv
91+
$PYTHON_CMD -m venv infra/scripts/scriptenv
8392
fi
8493

85-
# handling virtual environment activation for different OS
86-
activate_env_output=$(source infra/scripts/scriptenv/bin/activate 2>&1)
87-
if [ -n "$activate_env_output" ]; then
88-
source infra/scripts/scriptenv/Scripts/activate
94+
# Activate the virtual environment
95+
if [ -f "infra/scripts/scriptenv/bin/activate" ]; then
96+
echo "Activating virtual environment (Linux/macOS)"
97+
source "infra/scripts/scriptenv/bin/activate"
98+
elif [ -f "infra/scripts/scriptenv/Scripts/activate" ]; then
99+
echo "Activating virtual environment (Windows)"
100+
source "infra/scripts/scriptenv/Scripts/activate"
101+
else
102+
echo "Error activating virtual environment. Requirements may be installed globally."
89103
fi
90104

91105
# Install the requirements
92106
echo "Installing requirements"
93107
pip install --quiet -r infra/scripts/index_scripts/requirements.txt
94108
echo "Requirements installed"
95109

110+
error_flag=false
96111
# Run the scripts
97112
echo "Running the python scripts"
98113
echo "Creating the search index"
99114
python infra/scripts/index_scripts/01_create_search_index.py
100115
if [ $? -ne 0 ]; then
101116
echo "Error: 01_create_search_index.py failed."
102-
exit 1
117+
error_flag=true
103118
fi
104119

105-
echo "Processing the data"
106-
python infra/scripts/index_scripts/02_process_data.py
107-
if [ $? -ne 0 ]; then
108-
echo "Error: 02_process_data.py failed."
120+
if [ "$error_flag" = false ]; then
121+
echo "Processing the data"
122+
python infra/scripts/index_scripts/02_process_data.py
123+
if [ $? -ne 0 ]; then
124+
echo "Error: 02_process_data.py failed."
125+
error_flag=true
126+
fi
127+
fi
128+
129+
# revert the key vault name and managed identity client id in the python files
130+
sed -i "s/${keyvaultName}/kv_to-be-replaced/g" "infra/scripts/index_scripts/01_create_search_index.py"
131+
sed -i "s/${keyvaultName}/kv_to-be-replaced/g" "infra/scripts/index_scripts/02_process_data.py"
132+
if [ -n "$managedIdentityClientId" ]; then
133+
sed -i "s/${managedIdentityClientId}/mici_to-be-replaced/g" "infra/scripts/index_scripts/01_create_search_index.py"
134+
sed -i "s/${managedIdentityClientId}/mici_to-be-replaced/g" "infra/scripts/index_scripts/02_process_data.py"
135+
fi
136+
137+
if [ "$error_flag" = true ]; then
138+
echo "Error: One or more scripts failed during the script execution."
109139
exit 1
110140
fi
141+
111142
echo "Scripts completed"

src/start.sh

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
export NODE_OPTIONS=--max_old_space_size=8192
44

5+
# Restoring backend python packages
6+
echo ""
7+
echo "Restoring backend python packages"
8+
echo ""
9+
python3 -m pip install -r requirements.txt
10+
if [ $? -ne 0 ]; then
11+
echo "Failed to restore backend python packages"
12+
exit $?
13+
fi
14+
515
echo ""
616
echo "Restoring frontend npm packages"
717
echo ""
@@ -22,12 +32,10 @@ if [ $? -ne 0 ]; then
2232
fi
2333

2434
cd ..
25-
. ./scripts/loadenv.sh
26-
2735
echo ""
2836
echo "Starting backend"
2937
echo ""
30-
./.venv/bin/python -m quart run --port=50505 --host=127.0.0.1 --reload
38+
python3 -m quart run --port=50505 --host=127.0.0.1 --reload
3139
if [ $? -ne 0 ]; then
3240
echo "Failed to start backend"
3341
exit $?

0 commit comments

Comments
 (0)