Skip to content

Commit fff6c93

Browse files
chore: enhance role assignment and upload logic in copy_kb_files.sh
1 parent 9da5ab9 commit fff6c93

1 file changed

Lines changed: 83 additions & 31 deletions

File tree

infra/scripts/copy_kb_files.sh

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,41 @@ if ! az account show &> /dev/null; then
4545
az login --use-device-code
4646
fi
4747

48-
# Check and assign Storage Blob Data Contributor role to current user
49-
signed_user_id=$(az ad signed-in-user show --query id --output tsv 2>&1)
50-
if [ -z "$signed_user_id" ] || [[ "$signed_user_id" == *"ERROR"* ]] || [[ "$signed_user_id" == *"InteractionRequired"* ]]; then
51-
echo "✗ Failed to get signed-in user ID. Token may have expired. Re-authenticating..."
52-
az login --use-device-code
53-
signed_user_id=$(az ad signed-in-user show --query id --output tsv)
54-
if [ -z "$signed_user_id" ]; then
55-
echo "✗ Failed to get signed-in user ID after re-authentication"
48+
# Check and assign Storage Blob Data Contributor role to current identity (user or service principal)
49+
# First, determine if we're running as a user or service principal
50+
account_type=$(az account show --query user.type --output tsv 2>/dev/null)
51+
52+
if [ "$account_type" == "user" ]; then
53+
# Running as a user - get signed-in user ID
54+
signed_user_id=$(az ad signed-in-user show --query id --output tsv 2>&1)
55+
if [ -z "$signed_user_id" ] || [[ "$signed_user_id" == *"ERROR"* ]] || [[ "$signed_user_id" == *"InteractionRequired"* ]]; then
56+
echo "✗ Failed to get signed-in user ID. Token may have expired. Re-authenticating..."
57+
az login --use-device-code
58+
signed_user_id=$(az ad signed-in-user show --query id --output tsv)
59+
if [ -z "$signed_user_id" ]; then
60+
echo "✗ Failed to get signed-in user ID after re-authentication"
61+
exit 1
62+
fi
63+
fi
64+
echo "✓ Running as user: $signed_user_id"
65+
elif [ "$account_type" == "servicePrincipal" ]; then
66+
# Running as a service principal - get SP object ID
67+
client_id=$(az account show --query user.name --output tsv 2>/dev/null)
68+
if [ -z "$client_id" ]; then
69+
echo "✗ Failed to get service principal client ID"
70+
exit 1
71+
fi
72+
sp_show_output=$(az ad sp show --id "$client_id" --query id --output tsv 2>&1)
73+
if [ $? -ne 0 ] || [ -z "$sp_show_output" ]; then
74+
echo "✗ Failed to get service principal object ID using client ID '$client_id'. Azure CLI output:"
75+
echo "$sp_show_output"
5676
exit 1
5777
fi
78+
signed_user_id="$sp_show_output"
79+
echo "✓ Running as service principal: $signed_user_id"
80+
else
81+
echo "✗ Unknown account type: $account_type"
82+
exit 1
5883
fi
5984

6085
storage_resource_id=$(az storage account show --name "$storageAccountName" --resource-group "$resourceGroupName" --query id --output tsv)
@@ -71,39 +96,66 @@ if [ -z "$role_assignment" ]; then
7196
echo "✗ Failed to assign Storage Blob Data Contributor role"
7297
exit 1
7398
fi
99+
fi
100+
101+
# Wait for role assignment to propagate by testing storage access
102+
echo "⏳ Waiting for role assignment to propagate..."
103+
max_retries=30
104+
retry_count=0
105+
while [ $retry_count -lt $max_retries ]; do
106+
if az storage container list --account-name "$storageAccountName" --auth-mode login --output none 2>/dev/null; then
107+
echo "✓ Role assignment propagated successfully"
108+
break
109+
fi
110+
retry_count=$((retry_count + 1))
111+
echo " Attempt $retry_count/$max_retries - waiting 10 seconds..."
74112
sleep 10
113+
done
114+
115+
if [ $retry_count -eq $max_retries ]; then
116+
echo "✗ Role assignment did not propagate within expected time"
117+
exit 1
75118
fi
76119

77-
# Upload files to storage account
120+
# Upload files to storage account with retry logic
121+
upload_with_retry() {
122+
local source_folder="$1"
123+
local dest_path="$2"
124+
local description="$3"
125+
local upload_retries=5
126+
local upload_attempt=0
127+
128+
while [ $upload_attempt -lt $upload_retries ]; do
129+
if az storage blob upload-batch \
130+
--account-name "$storageAccountName" \
131+
--destination "$dest_path" \
132+
--source "$source_folder" \
133+
--auth-mode login \
134+
--pattern '*' \
135+
--overwrite \
136+
--output none 2>/dev/null; then
137+
echo "✓ Uploaded $description successfully"
138+
return 0
139+
fi
140+
upload_attempt=$((upload_attempt + 1))
141+
echo " Upload attempt $upload_attempt/$upload_retries failed - waiting 15 seconds..."
142+
sleep 15
143+
done
144+
echo "✗ Failed to upload $description after $upload_retries attempts"
145+
return 1
146+
}
147+
78148
if [ -d "$extractedFolder1" ]; then
79-
echo "✓ Uploading call transcripts"
80-
az storage blob upload-batch \
81-
--account-name "$storageAccountName" \
82-
--destination "$containerName/$extractedFolder1" \
83-
--source "$extractedFolder1" \
84-
--auth-mode login \
85-
--pattern '*' \
86-
--overwrite \
87-
--output none
88-
if [ $? -ne 0 ]; then
89-
echo "✗ Failed to upload call transcripts"
149+
echo "⏳ Uploading call transcripts..."
150+
if ! upload_with_retry "$extractedFolder1" "$containerName/$extractedFolder1" "call transcripts"; then
90151
exit 1
91152
fi
92153
fi
93154

94155
if [ "$usecase" == "telecom" ]; then
95156
if [ -d "$extractedFolder2" ]; then
96-
echo "✓ Uploading audio data"
97-
az storage blob upload-batch \
98-
--account-name "$storageAccountName" \
99-
--destination "$containerName/$extractedFolder2" \
100-
--source "$extractedFolder2" \
101-
--auth-mode login \
102-
--pattern '*' \
103-
--overwrite \
104-
--output none
105-
if [ $? -ne 0 ]; then
106-
echo "✗ Failed to upload audio data"
157+
echo "⏳ Uploading audio data..."
158+
if ! upload_with_retry "$extractedFolder2" "$containerName/$extractedFolder2" "audio data"; then
107159
exit 1
108160
fi
109161
fi

0 commit comments

Comments
 (0)