Skip to content

Commit

Permalink
Make SQL provisioning scripts fail on error
Browse files Browse the repository at this point in the history
  • Loading branch information
algattik committed Jul 24, 2019
1 parent a60f86b commit 047eefb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
23 changes: 21 additions & 2 deletions components/azure-sql/create-sql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ az storage file upload-batch --source ../components/azure-sql/provision \
-o tsv >> log.txt

echo 'running provisioning scripts in container instance'
az container create -g $RESOURCE_GROUP -n sqlprovision \
instanceName="sqlprovision-$(uuidgen | tr A-Z a-z)"
az container create -g $RESOURCE_GROUP -n "$instanceName" \
--image mcr.microsoft.com/mssql-tools:v1 \
--azure-file-volume-account-name $AZURE_STORAGE_ACCOUNT --azure-file-volume-account-key $AZURE_STORAGE_KEY \
--azure-file-volume-share-name sqlprovision --azure-file-volume-mount-path /sqlprovision \
Expand All @@ -56,6 +57,24 @@ az container create -g $RESOURCE_GROUP -n sqlprovision \
--restart-policy Never \
-o tsv >> log.txt

TIMEOUT=60
for i in $(seq 1 $TIMEOUT); do
containerState=$(az container show -g $RESOURCE_GROUP -n "$instanceName" --query instanceView.state -o tsv)
case "state_$containerState" in
state_Pending|state_Running) : ;;
*) break;;
esac
done

if [ "$containerState" != "Succeeded" ]; then
az container logs -g $RESOURCE_GROUP -n "$instanceName"
fi

echo 'deleting container instance'
az container delete -g $RESOURCE_GROUP -n sqlprovision --yes \
echo az container delete -g $RESOURCE_GROUP -n "$instanceName" --yes \
-o tsv >> log.txt

if [ "$containerState" != "Succeeded" ]; then
echo "SQL provisioning FAILED"
exit 1
fi
16 changes: 15 additions & 1 deletion components/azure-sql/provision/provision.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
#!/bin/bash
/opt/mssql-tools/bin/sqlcmd -S "$SQL_SERVER_NAME.database.windows.net" -d "$SQL_DATABASE_NAME" -U serveradmin -P "$SQL_ADMIN_PASS" -i /sqlprovision/provision.sql
function run_sqlcmd {
/opt/mssql-tools/bin/sqlcmd -e -b -S "$SQL_SERVER_NAME.database.windows.net" -d "$SQL_DATABASE_NAME" -U serveradmin -P "$SQL_ADMIN_PASS" "$@"
}

run_sqlcmd -Q "CREATE TABLE IF NOT EXISTS sqlprovision(script nvarchar(4000))"
for provisioning_script in /sqlprovision/*.sql; do
echo -n "[$provisioning_script] "
already_run=$(run_sqlcmd -Q "SELECT count(*) FROM sqlprovision WHERE script='$(basename $provisioning_script)'")
if [ "$already_run" == "0" ]; then
echo "running sqlcmd"
run_sqlcmd -i /sqlprovision/provision.sql
else
echo "already run"
fi
done

0 comments on commit 047eefb

Please sign in to comment.