-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_scale.sh
executable file
·69 lines (62 loc) · 1.95 KB
/
auto_scale.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
#!/bin/bash
## Local system enviroments
API_URL="https://api.digitalocean.com/v2"
TOKEN="N/A"
LBAPI="N/A"
LBDB="N/A"
TAG="ARIOT"
TYPE="API"
TOTAL_DROPLETS=$(curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" "$API_URL/droplets?tag_name=${TAG}${TYPE}" | jq .meta.total)
## DigitalOcean enviroments
DROPLET_NAME="ariot-api-"
DROPLET_REGION="lon1"
DROPLET_SIZE="512mb"
DROPLET_API_IMAGE="N/A"
DROPLET_DB_IMAGE="N/A"
## Calculations
CPU=$(grep -c "^processor" /proc/cpuinfo)
CURRENT_LOAD=$(cat /proc/loadavg | awk '{print $1}')
AVERAGE_LOAD=$(($(echo ${CURRENT_LOAD} | awk '{print 100 * $1}') / ${CPU}))
## Logic
if [ ${TOTAL_DROPLETS} -le 5 ] ; then
if [ ${AVERAGE_LOAD} -ge 60 ] ; then
if [ ${TYPE} == "API" ] ; then
add_droplet_to_lbapi $(create_api_droplet_from_snapshot | jq .droplet.id)
else
add_droplet_to_lbapi $(create_db_droplet_from_snapshot | jq .droplet.id)
fi
fi
fi
## API droplet creation
create_api_droplet_from_snapshot {
curl -X POST "$API_URL/droplets" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d "{\"name\":\"${DROPLET_NAME}${TOTAL_DROPLETS}\",
\"region\":\"${DROPLET_REGION}\",
\"size\":\"${DROPLET_SIZE}\",
\"tags\":\"${TAG}\",
\"private_networking\":\"true\",
\"monitoring\":\"true\",
\"image\": \"${DROPLET_API_IMAGE}\"}"
}
## DB droplet creation
create_db_droplet_from_snapshot {
curl -X POST "$API_URL/droplets" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d "{\"name\":\"${DROPLET_NAME}${TOTAL_DROPLETS}\",
\"region\":\"${DROPLET_REGION}\",
\"size\":\"${DROPLET_SIZE}\",
\"tags\":\"${TAG}\",
\"private_networking\":\"true\",
\"monitoring\":\"true\",
\"image\": \"${DROPLET_DBIMAGE}\"}"
}
## LB
add_droplet_to_lbapi {
curl -X POST "$API_URL/load_balancers/$LBAPI/droplets" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d '{"droplet_ids": [$1]}'
}