Skip to content

Commit 5735a70

Browse files
fix: added subscription selection in scripts, added retry logic for uploading documents to blob
1 parent f518a5d commit 5735a70

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

infra/scripts/copy_kb_files.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ if [ -z "$role_assignment" ]; then
4646
MSYS_NO_PATHCONV=1 az role assignment create --assignee $signed_user_id --role "Storage Blob Data Contributor" --scope $storage_account_resource_id --output none
4747
if [ $? -eq 0 ]; then
4848
echo "Role assignment completed successfully."
49+
sleep 5
4950
retries=3
5051
while [ $retries -gt 0 ]; do
5152
# Check if the role assignment was successful
5253
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)
5354
if [ -n "$role_assignment_check" ]; then
5455
echo "Role assignment verified successfully."
56+
sleep 5
5557
break
5658
else
5759
echo "Role assignment not found, retrying..."
@@ -93,6 +95,27 @@ unzip -o $zipUrl1 -d infra/data/"$extractedFolder1"
9395
# unzip /mnt/azscripts/azscriptinput/"$zipFileName2" -d /mnt/azscripts/azscriptinput/"$extractedFolder2"
9496

9597
# 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.
96-
echo "Uploading files to Azure Storage"
98+
echo "Uploading files to Azure Blob Storage"
9799
az storage blob upload-batch --account-name "$storageAccount" --destination "$fileSystem"/"$extractedFolder1" --source infra/data/"$extractedFolder1" --auth-mode login --pattern '*' --overwrite --output none
100+
if [ $? -ne 0 ]; then
101+
retries=3
102+
sleepTime=10
103+
echo "Error: Failed to upload files to Azure Blob Storage. Retrying upload...($((4 - retries)) of 3)"
104+
while [ $retries -gt 0 ]; do
105+
sleep $sleepTime
106+
az storage blob upload-batch --account-name "$storageAccount" --destination "$fileSystem"/"$extractedFolder1" --source infra/data/"$extractedFolder1" --auth-mode login --pattern '*' --overwrite --output none
107+
if [ $? -eq 0 ]; then
108+
echo "Files uploaded successfully to Azure Blob Storage."
109+
break
110+
else
111+
((retries--))
112+
echo "Retrying upload... ($((4 - retries)) of 3)"
113+
sleepTime=$((sleepTime * 2))
114+
sleep $sleepTime
115+
fi
116+
done
117+
exit 1
118+
else
119+
echo "Files uploaded successfully to Azure Blob Storage."
120+
fi
98121
# az storage blob upload-batch --account-name "$storageAccount" --destination data/"$extractedFolder2" --source /mnt/azscripts/azscriptinput/"$extractedFolder2" --auth-mode login --pattern '*' --overwrite

infra/scripts/process_sample_data.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,72 @@ if [ -z "$aiFoundryName" ]; then
3434
aiFoundryName=$(azd env get-value AI_FOUNDRY_NAME)
3535
fi
3636

37+
azSubscriptionId=$(azd env get-value AZURE_SUBSCRIPTION_ID)
38+
3739
# Check if all required arguments are provided
3840
if [ -z "$storageAccount" ] || [ -z "$fileSystem" ] || [ -z "$keyvaultName" ] || [ -z "$cosmosDbAccountName" ] || [ -z "$resourceGroupName" ] || [ -z "$aiFoundryName" ]; then
3941
echo "Usage: $0 <storageAccount> <storageContainerName> <keyvaultName> <cosmosDbAccountName> <resourceGroupName> <aiFoundryName>"
4042
exit 1
4143
fi
4244

45+
# Authenticate with Azure
46+
if az account show &> /dev/null; then
47+
echo "Already authenticated with Azure."
48+
else
49+
if [ -n "$managedIdentityClientId" ]; then
50+
# Use managed identity if running in Azure
51+
echo "Authenticating with Managed Identity..."
52+
az login --identity --client-id ${managedIdentityClientId}
53+
else
54+
# Use Azure CLI login if running locally
55+
echo "Authenticating with Azure CLI..."
56+
az login
57+
fi
58+
echo "Not authenticated with Azure. Attempting to authenticate..."
59+
fi
60+
61+
#check if user has selected the correct subscription
62+
currentSubscriptionId=$(az account show --query id -o tsv)
63+
currentSubscriptionName=$(az account show --query name -o tsv)
64+
if [ "$currentSubscriptionId" != "$azSubscriptionId" ]; then
65+
echo "Current selected subscription is $currentSubscriptionName ( $currentSubscriptionId )."
66+
read -rp "Do you want to continue with this subscription?(y/n): " confirmation
67+
if [[ "$confirmation" != "y" && "$confirmation" != "Y" ]]; then
68+
echo "Fetching available subscriptions..."
69+
availableSubscriptions=$(az account list --query "[?state=='Enabled'].[name,id]" --output tsv)
70+
while true; do
71+
echo ""
72+
echo "Available Subscriptions:"
73+
echo "========================"
74+
echo "$availableSubscriptions" | awk '{printf "%d. %s ( %s )\n", NR, $1, $2}'
75+
echo "========================"
76+
echo ""
77+
read -rp "Enter the number of the subscription (1-$(echo "$availableSubscriptions" | wc -l)) to use: " subscriptionIndex
78+
if [[ "$subscriptionIndex" =~ ^[0-9]+$ ]] && [ "$subscriptionIndex" -ge 1 ] && [ "$subscriptionIndex" -le $(echo "$availableSubscriptions" | wc -l) ]; then
79+
selectedSubscription=$(echo "$availableSubscriptions" | sed -n "${subscriptionIndex}p")
80+
selectedSubscriptionName=$(echo "$selectedSubscription" | cut -f1)
81+
selectedSubscriptionId=$(echo "$selectedSubscription" | cut -f2)
82+
83+
# Set the selected subscription
84+
if az account set --subscription "$selectedSubscriptionId"; then
85+
echo "Switched to subscription: $selectedSubscriptionName ( $selectedSubscriptionId )"
86+
break
87+
else
88+
echo "Failed to switch to subscription: $selectedSubscriptionName ( $selectedSubscriptionId )."
89+
fi
90+
else
91+
echo "Invalid selection. Please try again."
92+
fi
93+
done
94+
else
95+
echo "Proceeding with the current subscription: $currentSubscriptionName ( $currentSubscriptionId )"
96+
az account set --subscription "$currentSubscriptionId"
97+
fi
98+
else
99+
echo "Proceeding with the subscription: $currentSubscriptionName ( $currentSubscriptionId )"
100+
az account set --subscription "$currentSubscriptionId"
101+
fi
102+
43103
# Call add_cosmosdb_access.sh
44104
echo "Running add_cosmosdb_access.sh"
45105
bash infra/scripts/add_cosmosdb_access.sh "$resourceGroupName" "$cosmosDbAccountName" "$managedIdentityClientId"

0 commit comments

Comments
 (0)