| 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 | set -euo pipefail |
| 57 | |
| 58 | HOST="steid" |
| 59 | DOMAIN="jpgill.dev" |
| 60 | TARGET="x86_64-unknown-linux-gnu" |
| 61 | SKIP_BUILD=0 |
| 62 | PUBLISH=0 |
| 63 | DEPLOY=1 |
| 64 | MIRROR=1 |
| 65 | VERSION="" |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | AWS_PROFILE_NAME="${STEID_AWS_PROFILE:-steid}" |
| 73 | RELEASES_BUCKET="${STEID_RELEASES_BUCKET:-steid-releases}" |
| 74 | RELEASES_HOST="${STEID_RELEASES_HOST:-releases.steid.dev}" |
| 75 | DISTRIBUTION="${STEID_RELEASES_DISTRIBUTION:-}" |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | DIST_DIR="/srv/steid-dist" |
| 84 | |
| 85 | die() { echo "deploy.sh: $*" >&2; exit 1; } |
| 86 | say() { echo "==> $*"; } |
| 87 | |
| 88 | while [ $# -gt 0 ]; do |
| 89 | case "$1" in |
| 90 | --host) HOST="${2:?--host needs a value}"; shift 2 ;; |
| 91 | --domain) DOMAIN="${2:?--domain needs a value}"; shift 2 ;; |
| 92 | --target) TARGET="${2:?--target needs a triple}"; shift 2 ;; |
| 93 | --version) VERSION="${2:?--version needs a value}"; shift 2 ;; |
| 94 | --skip-build) SKIP_BUILD=1; shift ;; |
| 95 | --publish) PUBLISH=1; shift ;; |
| 96 | |
| 97 | |
| 98 | |
| 99 | --publish-s3-only) PUBLISH=1; DEPLOY=0; MIRROR=0; shift ;; |
| 100 | --no-mirror) MIRROR=0; shift ;; |
| 101 | --distribution) DISTRIBUTION="${2:?--distribution needs an id}"; shift 2 ;; |
| 102 | -h|--help) sed -n '2,19p' "$0" | sed 's/^#\{1,2\} \{0,1\}//'; exit 0 ;; |
| 103 | *) die "unknown argument: $1" ;; |
| 104 | esac |
| 105 | done |
| 106 | |
| 107 | cd "$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)" |
| 108 | |
| 109 | [ -n "$VERSION" ] || VERSION="$(grep -m1 '^version *= *"' Cargo.toml | sed 's/.*"\(.*\)".*/\1/')" |
| 110 | NAME="steid-${VERSION}-${TARGET}" |
| 111 | TARBALL="dist/${NAME}.tar.gz" |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | if [ -n "$(git status --porcelain)" ]; then |
| 117 | die "working tree is dirty — commit first, so the deployed build has a commit to name" |
| 118 | fi |
| 119 | COMMIT="$(git rev-parse --short HEAD)" |
| 120 | |
| 121 | if [ "$SKIP_BUILD" = 0 ]; then |
| 122 | say "building ${NAME} from ${COMMIT}" |
| 123 | ./release.sh --target "$TARGET" --version "$VERSION" >/dev/null |
| 124 | else |
| 125 | say "skipping build (--skip-build)" |
| 126 | fi |
| 127 | [ -f "$TARBALL" ] || die "no ${TARBALL} — build it, or drop --skip-build" |
| 128 | |
| 129 | |
| 130 | |
| 131 | if [ "$DEPLOY" = 0 ]; then |
| 132 | say "not deploying (--publish-s3-only): this box is not touched" |
| 133 | else |
| 134 | say "checking the instance is reachable" |
| 135 | ssh -o BatchMode=yes "$HOST" true || die "cannot ssh to ${HOST}" |
| 136 | |
| 137 | |
| 138 | |
| 139 | STAMP="$(date +%Y%m%d-%H%M%S)" |
| 140 | say "snapshotting state and the current binary (${STAMP})" |
| 141 | ssh -o BatchMode=yes "$HOST" " |
| 142 | set -e |
| 143 | sudo mkdir -p /var/backups/steid |
| 144 | sudo tar czf /var/backups/steid/state-${STAMP}.tar.gz -C /var/lib steid |
| 145 | sudo tar czf /var/backups/steid/binary-${STAMP}.tar.gz -C /opt steid |
| 146 | sudo find /var/backups/steid -name '*.tar.gz' -mtime +14 -delete |
| 147 | " |
| 148 | |
| 149 | |
| 150 | |
| 151 | say "uploading" |
| 152 | scp -q -o BatchMode=yes "$TARBALL" "${TARBALL}.sha256" install.sh "${HOST}:" |
| 153 | |
| 154 | say "upgrading" |
| 155 | ssh -o BatchMode=yes "$HOST" "sudo sh ./install.sh --domain '${DOMAIN}' --tarball './${NAME}.tar.gz'" \ |
| 156 | | grep -E '^==>|is running' || true |
| 157 | |
| 158 | |
| 159 | |
| 160 | say "verifying https://${DOMAIN}" |
| 161 | OK=0 |
| 162 | for _ in $(seq 1 15); do |
| 163 | sleep 2 |
| 164 | if [ "$(curl -s -o /dev/null -m 10 -w '%{http_code}' "https://${DOMAIN}/healthz")" = "200" ]; then |
| 165 | OK=1; break |
| 166 | fi |
| 167 | done |
| 168 | |
| 169 | if [ "$OK" = 1 ]; then |
| 170 | say "healthy — ${VERSION} (${COMMIT}) is live at https://${DOMAIN}" |
| 171 | ssh -o BatchMode=yes "$HOST" "systemctl is-active steid caddy | paste -sd' ' -" | sed 's/^/ steid caddy: /' |
| 172 | else |
| 173 | echo "deploy.sh: the new build is NOT serving. Rolling back." >&2 |
| 174 | ssh -o BatchMode=yes "$HOST" " |
| 175 | set -e |
| 176 | sudo systemctl stop steid |
| 177 | sudo rm -rf /opt/steid |
| 178 | sudo tar xzf /var/backups/steid/binary-${STAMP}.tar.gz -C /opt |
| 179 | sudo systemctl start steid |
| 180 | " |
| 181 | sleep 4 |
| 182 | ROLLED="$(curl -s -o /dev/null -m 10 -w '%{http_code}' "https://${DOMAIN}/healthz")" |
| 183 | echo "deploy.sh: rolled back to the previous binary (healthz ${ROLLED})." >&2 |
| 184 | echo "deploy.sh: state was NOT restored — the database is untouched by an upgrade," >&2 |
| 185 | echo " and /var/backups/steid/state-${STAMP}.tar.gz holds it if you need it." >&2 |
| 186 | exit 1 |
| 187 | fi |
| 188 | fi |
| 189 | |
| 190 | |
| 191 | |
| 192 | if [ "$PUBLISH" = 1 ]; then |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | PUBLISH_TARGETS="x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu" |
| 200 | FILES="" |
| 201 | for t in $PUBLISH_TARGETS; do |
| 202 | f="dist/steid-${VERSION}-${t}.tar.gz" |
| 203 | if [ -f "$f" ] && [ -f "${f}.sha256" ]; then |
| 204 | FILES="${FILES} ${f} ${f}.sha256" |
| 205 | else |
| 206 | echo "deploy.sh: no ${f} — anyone whose 'uname -m' picks ${t} gets a 404." >&2 |
| 207 | echo " Build it first: ./release.sh --target ${t} --version ${VERSION}" >&2 |
| 208 | fi |
| 209 | done |
| 210 | [ -n "$FILES" ] || die "nothing to publish for ${VERSION}" |
| 211 | |
| 212 | |
| 213 | |
| 214 | command -v aws >/dev/null 2>&1 || die "no aws CLI, and the releases host is S3. |
| 215 | Install it, or pass --host/--no-mirror flags to publish only the mirror." |
| 216 | |
| 217 | say "publishing v${VERSION} to s3://${RELEASES_BUCKET} (profile ${AWS_PROFILE_NAME})" |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | for f in $FILES; do |
| 228 | case "$f" in |
| 229 | *.sha256) CT="text/plain" ;; |
| 230 | *) CT="application/gzip" ;; |
| 231 | esac |
| 232 | KEY="v${VERSION}/$(basename "$f")" |
| 233 | aws --profile "$AWS_PROFILE_NAME" s3 cp "$f" "s3://${RELEASES_BUCKET}/${KEY}" \ |
| 234 | --no-guess-mime-type --content-type "$CT" --only-show-errors \ |
| 235 | || die "upload failed: ${KEY}" |
| 236 | echo " s3://${RELEASES_BUCKET}/${KEY}" |
| 237 | done |
| 238 | |
| 239 | |
| 240 | |
| 241 | aws --profile "$AWS_PROFILE_NAME" s3 cp install.sh "s3://${RELEASES_BUCKET}/install.sh" \ |
| 242 | --no-guess-mime-type --content-type "text/x-shellscript" --only-show-errors \ |
| 243 | || die "upload failed: install.sh" |
| 244 | echo " s3://${RELEASES_BUCKET}/install.sh" |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | if [ -z "$DISTRIBUTION" ]; then |
| 251 | DISTRIBUTION="$(aws --profile "$AWS_PROFILE_NAME" cloudfront list-distributions \ |
| 252 | --query "DistributionList.Items[?contains(Aliases.Items, '${RELEASES_HOST}')].Id | [0]" \ |
| 253 | --output text 2>/dev/null || true)" |
| 254 | fi |
| 255 | [ -n "$DISTRIBUTION" ] && [ "$DISTRIBUTION" != "None" ] \ |
| 256 | || die "no CloudFront distribution has the alias ${RELEASES_HOST}. |
| 257 | Pass --distribution <id>, or set STEID_RELEASES_DISTRIBUTION." |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | say "invalidating /install.sh and /v${VERSION}/* on ${DISTRIBUTION}" |
| 266 | INVALIDATION="$(aws --profile "$AWS_PROFILE_NAME" cloudfront create-invalidation \ |
| 267 | --distribution-id "$DISTRIBUTION" \ |
| 268 | --paths "/install.sh" "/v${VERSION}/*" \ |
| 269 | --query 'Invalidation.Id' --output text)" || die "could not create an invalidation" |
| 270 | echo " ${INVALIDATION}" |
| 271 | |
| 272 | |
| 273 | aws --profile "$AWS_PROFILE_NAME" cloudfront wait invalidation-completed \ |
| 274 | --distribution-id "$DISTRIBUTION" --id "$INVALIDATION" \ |
| 275 | || die "invalidation ${INVALIDATION} did not complete" |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | say "verifying the published URLs" |
| 283 | for f in $FILES; do |
| 284 | URL="https://${RELEASES_HOST}/v${VERSION}/$(basename "$f")" |
| 285 | CODE="$(curl -s -o /dev/null -m 120 -w '%{http_code}' "$URL")" |
| 286 | [ "$CODE" = "200" ] || die "published but not served: ${URL} (HTTP ${CODE}). |
| 287 | Does the bucket policy let the distribution read this prefix?" |
| 288 | echo " ${CODE} ${URL}" |
| 289 | done |
| 290 | CODE="$(curl -s -o /dev/null -m 30 -w '%{http_code}' "https://${RELEASES_HOST}/install.sh")" |
| 291 | [ "$CODE" = "200" ] || die "published but not served: https://${RELEASES_HOST}/install.sh (HTTP ${CODE})" |
| 292 | echo " ${CODE} https://${RELEASES_HOST}/install.sh" |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | if [ "$MIRROR" = 1 ]; then |
| 300 | say "mirroring v${VERSION} to ${HOST}:${DIST_DIR}" |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | scp -q -o BatchMode=yes $FILES install.sh "${HOST}:" |
| 308 | REMOTE="set -e; sudo install -d -m 755 -o root -g root ${DIST_DIR}/v${VERSION};" |
| 309 | for f in $FILES; do |
| 310 | REMOTE="${REMOTE} sudo install -m 644 -o root -g root ./$(basename "$f") ${DIST_DIR}/v${VERSION}/;" |
| 311 | done |
| 312 | REMOTE="${REMOTE} sudo install -m 644 -o root -g root ./install.sh ${DIST_DIR}/install.sh" |
| 313 | ssh -o BatchMode=yes "$HOST" "$REMOTE" |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | say "verifying the mirrored URLs" |
| 319 | for f in $FILES; do |
| 320 | URL="https://${DOMAIN}/jamesgill/repos/steid/releases/v${VERSION}/$(basename "$f")" |
| 321 | CODE="$(curl -s -o /dev/null -m 60 -w '%{http_code}' "$URL")" |
| 322 | [ "$CODE" = "200" ] || die "mirrored but not served: ${URL} (HTTP ${CODE}). |
| 323 | Are the distribution handle_path blocks in /etc/caddy/Caddyfile? See deploy/Caddyfile." |
| 324 | echo " ${CODE} ${URL}" |
| 325 | done |
| 326 | CODE="$(curl -s -o /dev/null -m 30 -w '%{http_code}' "https://${DOMAIN}/install.sh")" |
| 327 | [ "$CODE" = "200" ] || die "mirrored but not served: https://${DOMAIN}/install.sh (HTTP ${CODE})" |
| 328 | echo " ${CODE} https://${DOMAIN}/install.sh" |
| 329 | else |
| 330 | say "skipping the ${DOMAIN} mirror" |
| 331 | fi |
| 332 | |
| 333 | say "published. A stranger now installs this version with:" |
| 334 | echo " curl -fsSL https://${RELEASES_HOST}/install.sh | sh -s -- --domain <their-host> --version ${VERSION}" |
| 335 | fi |