forked from kosolabs/koso
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·127 lines (107 loc) · 4.34 KB
/
deploy.sh
File metadata and controls
executable file
·127 lines (107 loc) · 4.34 KB
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
124
125
126
127
#!/usr/bin/env zsh
set -eo pipefail
if [ -z "${KOSO_IMAGE_DIGEST}" ]; then
echo "KOSO_IMAGE_DIGEST variable must be set."
exit 1
fi
if [ -z "${GITHUB_SHA}" ]; then
echo "GITHUB_SHA variable must be set."
exit 1
fi
echo "Deploying commit ${GITHUB_SHA}, image digest ${KOSO_IMAGE_DIGEST}"
cd /root/koso
# Checkout the repo at the given commit.
git status
git reset --hard
git clean -f -d
git fetch
git checkout $GITHUB_SHA
git status
# Cleanup old images and containers
echo "Cleaning up stale images and containers..."
docker image prune -a --force --filter "until=32h"
docker container prune --force --filter "until=32h"
echo "Cleaned up stale images and containers."
echo "Deploying image ghcr.io/kosolabs/koso@$KOSO_IMAGE_DIGEST"
# Pull the new image
if [ -n "${GHCR_USER}" ]; then
echo $GHCR_TOKEN | docker login ghcr.io -u $GHCR_USER --password-stdin
fi
docker pull ghcr.io/kosolabs/koso@$KOSO_IMAGE_DIGEST
# Run DB migrations.
echo "Running database migrations..."
docker run \
--env DATABASE_URL=postgresql://koso:koso@localhost/koso \
--network=host \
--rm \
ghcr.io/kosolabs/koso@$KOSO_IMAGE_DIGEST \
"./sqlx" migrate run
echo "Finished database migrations."
# Bind a failure handler that will trigger rollbacks if things go wrong.
mkdir -p /root/rollouts
touch /root/rollouts/koso_deployed_sha /root/rollouts/koso_rollback_sha /root/rollouts/koso_deployed_image /root/rollouts/koso_rollback_image
if [ "${DISABLE_ROLLBACK}" != "true" ]; then
ROLLBACK_SHA=$(cat /root/rollouts/koso_deployed_sha|tr -d '\n')
ROLLBACK_IMAGE=$(cat /root/rollouts/koso_deployed_image|tr -d '\n')
echo "If something goes wrong, will rollback to commit ${ROLLBACK_SHA}, image digest ${ROLLBACK_IMAGE}"
function _on_fail {
echo "Deploy failed, trying to rollback."
if [ -z "${ROLLBACK_SHA}" ]; then
echo "No rollback sha (/root/rollouts/koso_deployed_sha) present. Will not rollback"
elif [ -z "${ROLLBACK_IMAGE}" ]; then
echo "No rollback image (/root/rollouts/koso_deployed_image) present. Will not rollback"
elif [[ "${ROLLBACK_SHA}" == "${GITHUB_SHA}" ]] && [[ "${ROLLBACK_IMAGE}" == "${KOSO_IMAGE_DIGEST}" ]]; then
echo "Rollback target is the same as deployed. Will not rollback"
else
echo "Rolling back to commit ${ROLLBACK_SHA} and image ${ROLLBACK_IMAGE}"
# First checkout the rollback target in case this script was changed.
git reset --hard
git clean -f -d
git fetch
git checkout $ROLLBACK_SHA
git status
systemctl --no-pager -l --lines 20 status koso.service
echo "Running ./deploy.sh in rollback mode..."
DISABLE_ROLLBACK="true" GITHUB_SHA="${ROLLBACK_SHA}" KOSO_IMAGE_DIGEST="${ROLLBACK_IMAGE}" ./deploy.sh
echo "Rollback complete."
fi
exit 1
}
trap _on_fail ZERR
fi
# Setup backups
mkdir -p /root/koso-psql-backups
cp -r /root/koso/backend/psql_backups/. /root/koso-psql-backups/
crontab -u root /root/koso-psql-backups/psql_backup_cron.txt
# Copy over the latest systemctl unit file.
cp backend/koso.service /etc/systemd/system/koso.service
# Set the image label in the systemctl override file.
mkdir -p /etc/systemd/system/koso.service.d/
cat >/etc/systemd/system/koso.service.d/override.conf <<EOL
[Service]
Environment="KOSO_IMAGE_DIGEST=$KOSO_IMAGE_DIGEST"
EOL
# Load the updated koso.service file and restart on the new version.
echo "Restarting service..."
systemctl daemon-reload
systemctl restart koso.service
systemctl is-active koso.service && echo Koso service is running
systemctl enable koso.service
systemctl --no-pager -l --lines 20 status koso.service
echo "Restarted service."
# Wait for the server to respond healthy.
echo "Health checking service..."
curl -sS --verbose --fail \
--retry 30 \
--retry-connrefused \
--retry-delay 1 \
--retry-max-time 120 \
--max-time 15 \
http://localhost:3000/healthz
echo "\nHealth check passed."
# Finally, after things are healthy, write the deployed state.
cp /root/rollouts/koso_deployed_sha /root/rollouts/koso_rollback_sha
cp /root/rollouts/koso_deployed_image /root/rollouts/koso_rollback_image
echo -n "${GITHUB_SHA}" > /root/rollouts/koso_deployed_sha
echo -n "${KOSO_IMAGE_DIGEST}" > /root/rollouts/koso_deployed_image
echo "Deployment complete"