-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsnapshot.sh
executable file
·51 lines (40 loc) · 1.73 KB
/
snapshot.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
#!/usr/bin/env bash
set -eu
set -o pipefail
CHANNEL=${CHANNEL:=""}
DELETE_OLD=${DELETE_OLD:=""}
delete_snapshots() {
local CHAN=$1
SNAPSHOT_IDS=$(aws ec2 describe-snapshots --filters Name=tag:Name,Values=zpool-snapshot-$CHAN-* | jq .Snapshots[].SnapshotId --raw-output)
xargs --no-run-if-empty -L1 aws ec2 delete-snapshot --snapshot-id <<< $SNAPSHOT_IDS
}
if [ -z "$CHANNEL" ]; then
#CHANNEL=$(git show --no-commit-id --pretty="" --name-only HEAD)
CHANNEL=$(git diff-tree --no-commit-id --name-only HEAD)
CHANGED_FILES=$(wc -l <<< "$CHANNEL")
if [[ $CHANGED_FILES -ne 1 ]]; then
echo "$CHANGED_FILES files have changed."
echo $CHANNEL
echo "Exitting."
exit 0
fi
if ! grep -q '\.json$' <<< $CHANNEL; then
echo "Changed file isn't a JSON. Exitting."
exit 0
fi
fi
trap 'vagrant destroy -f' EXIT
vagrant up --provider=aws
cat $CHANNEL | jq '.[0].images | keys[] as $k | $k + ":" + .[$k]' --raw-output | vagrant ssh -c 'xargs -L1 docker pull'
BUILD_NO=$(cat $CHANNEL | jq .[0].build)
vagrant ssh -c 'docker images'
vagrant ssh -c 'zpool status'
vagrant ssh -c 'sync'
vagrant halt
CHANNEL_WITHOUT_EXT=$(sed 's/\.json$//' <<< $CHANNEL)
if [ "$DELETE_OLD" == "true" ]; then
delete_snapshots $CHANNEL_WITHOUT_EXT
fi
VOLUME_ID=$(aws ec2 describe-instances --instance-ids $(<.vagrant/machines/*/aws/id) | jq '.Reservations[0].Instances[0].BlockDeviceMappings[] | select(.DeviceName == "/dev/xvdb") | .Ebs.VolumeId' --raw-output)
SNAPSHOT_ID=$(aws ec2 create-snapshot --volume-id $VOLUME_ID --description "SoP zpool snapshot, channel $CHANNEL_WITHOUT_EXT, build $BUILD_NO" | jq .SnapshotId --raw-output)
aws ec2 create-tags --resources $SNAPSHOT_ID --tags "Key=Name,Value=zpool-snapshot-$CHANNEL_WITHOUT_EXT-$BUILD_NO" "Key=Channel,Value=$CHANNEL"