forked from endeveit/docker-jq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
54 lines (39 loc) · 1.67 KB
/
entrypoint.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
#!/bin/sh
VOLUME_NAME=$1
REGION=$2
VOLUME_COPY_NAME=${3:-$1-dev}
post() {
local JSON=$1
local ENDPOINT=$2
curl -sSX POST -H "Content-Type: application/json" -H "Authorization: Bearer $DO_TOKEN" -d $JSON "https://api.digitalocean.com/v2/$ENDPOINT"
}
get() {
local ENDPOINT=$1
curl -sSH "Authorization: Bearer $DO_TOKEN" "https://api.digitalocean.com/v2/$ENDPOINT"
}
delete() {
local ENDPOINT=$1
curl -sSX DELETE -H "Content-Type: application/json" -H "Authorization: Bearer $DO_TOKEN" "https://api.digitalocean.com/v2/$ENDPOINT"
}
# Get volume id to copy using it's name
VOLUME_ID=$(get "volumes?name=$VOLUME_NAME®ion=$REGION" | jq -r ".volumes[0].id")
echo "Got volume $VOLUME_NAME: $VOLUME_ID"
# Create snapshot and retreive its id
SNAPSHOT_ID=$(post "{\"name\":\"$VOLUME_COPY_NAME\"}" "volumes/$VOLUME_ID/snapshots" | jq -r ".snapshot.id")
echo "Created snapshot $SNAPSHOT_ID"
# Detach old staging volume
DROPLET_ATTACHED=$(get "volumes?name=$VOLUME_COPY_NAME®ion=$REGION" | jq ".volumes[0].droplet_ids[0]")
echo "volume: $VOLUME_COPY_NAME attached to: $DROPLET_ATTACHED"
post "{\"type\":\"detach\",\"droplet_id\":$DROPLET_ATTACHED,\"volume_name\":\"$VOLUME_COPY_NAME\",\"region\":\"fra1\"}" "volumes/actions"
sleep 20
echo "Volume detached from droplet."
# Delete old staging volume copy
delete "volumes?name=$VOLUME_COPY_NAME®ion=$REGION"
echo "Deleted old volume $VOLUME_COPY_NAME"
# Create staging volume from production snapshot
post "{\"name\":\"$VOLUME_COPY_NAME\",\"snapshot_id\":\"$SNAPSHOT_ID\"}" "volumes"
echo "Created copied volume from snapshot"
# Delete snapshot
delete "snapshots/$SNAPSHOT_ID"
echo "Deleted snapshot"
# TODO: Check that each stage was completed...