forked from Azure-Samples/azure-sql-db-rest-graphql-directus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure-deploy.sh
executable file
·123 lines (105 loc) · 4.45 KB
/
azure-deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
set -euo pipefail
# Load values from .env file or create it if it doesn't exists
FILE=".env"
if [[ -f $FILE ]]; then
echo "Loading from $FILE"
export $(egrep "^[^#;]" $FILE | xargs -n1)
else
cat << EOF > .env
# Azure-Specific Configuration
resourceGroup=""
location=""
# Directus-Specific Configuration
directusAdminEmail='[email protected]'
directusAdminPassword='directuS_PAzzw0rd!'
EOF
echo "Enviroment file not detected."
echo "Please configure values for your environment in the created .env file"
echo "and run the script again."
exit 1
fi
echo "Creating Resource Group...";
az group create \
-n $resourceGroup \
-l $location
echo "Deploying Resources...";
uid=`az deployment group create \
--name AzureSQL_Directus \
--resource-group "$resourceGroup" \
--template-file azuredeploy.json \
--parameters \
location="$location" \
directusAdministratorEmail="$directusAdminEmail" \
directusAdministratorPassword="$directusAdminPassword" \
--query "properties.outputs.uniqueId.value" \
--output tsv`
echo "Generated Unique Id: $uid"
echo "Writing client/directus-address.json..."
file="./client/directus-address.json"
site="https://directus-${uid}.azurewebsites.net"
rm -f -- $file
cat << EOF >> $file
[
"$site"
]
EOF
echo "Getting access to created storage account..."
storage="directus${uid}"
AZURE_STORAGE_CONNECTION_STRING=`az storage account show-connection-string -g "$resourceGroup" -n "$storage" --query "connectionString" -o tsv`
echo "Creating container..."
az storage container create \
--connection-string "$AZURE_STORAGE_CONNECTION_STRING" \
-n '$web'
echo "Uploading files..."
az storage blob upload-batch \
--connection-string "$AZURE_STORAGE_CONNECTION_STRING" \
-d '$web' \
-s ./client
echo "Enabling static website..."
az storage blob service-properties update \
--connection-string "$AZURE_STORAGE_CONNECTION_STRING" \
--account-name $storage \
--static-website \
--index-document index.html
echo "Getting static website address..."
website=`az storage account show -g $resourceGroup -n $storage --query "primaryEndpoints.web" -o tsv`
echo "Website available at: $website"
echo "Waiting for Directus to be fully deployed (it might take a minute)..."
curl -s -X POST "$site/auth/login" -H "Content-Type: application/json" -d "{}" > /dev/null 2>&1
sleep 30
curl -s -X POST "$site/auth/login" -H "Content-Type: application/json" -d "{}" > /dev/null 2>&1
echo "Logging in into Directus..."
payload="{\"email\":\"$directusAdminEmail\", \"password\":\"$directusAdminPassword\"}"
result=`curl -s -X POST "$site/auth/login" -H "Content-Type: application/json" -d "$payload"`
token=`echo $result | jq -r .data.access_token`
echo "Creating Todo collection..."
curl -s -X POST "$site/collections" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $token" \
-d '{"collection": "todo", "fields": [ { "field": "id", "type": "integer", "schema": { "is_primary_key": true, "has_auto_increment": true } }, { "field": "title", "type": "string" }, { "field": "completed", "type": "boolean" } ], "schema": {}, "meta": { "singleton": false } }'
echo "Setting permissions on Todo collection..."
curl -s -X POST "$site/permissions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $token" \
-d '{"role":null, "collection":"todo", "action":"create", "fields":"*", "permissions":{}, "validation":{}}'
curl -s -X POST "$site/permissions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $token" \
-d '{"role":null, "collection":"todo", "action":"read", "fields":"*", "permissions":{}, "validation":{}}'
curl -s -X POST "$site/permissions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $token" \
-d '{"role":null, "collection":"todo", "action":"update", "fields":"*", "permissions":{}, "validation":{}}'
curl -s -X POST "$site/permissions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $token" \
-d '{"role":null, "collection":"todo", "action":"delete", "fields":"*", "permissions":{}, "validation":{}}'
echo "Creating sample Todo items..."
curl -s -X POST "$site/items/todo" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $token" \
-d '[{"title": "Hello world item", "completed": true},{"title": "Another item", "completed": false},{"title": "Last one"}]'
echo "Done."
echo "Directus available at: $site"
echo "Sample static website at: $website"