#!/usr/bin/env bash # Set the VM up from the hand-over, and change variables later. # From https://exe.dev/deploy/migrate-netlify # # netlify/import setup variables, Blobs, submissions, the database, git, the service, the plan # netlify/import set KEY [VALUE] set a variable (VALUE from stdin if left out), restart # netlify/import set --preview KEY ... the same, for branch deploys # netlify/import unset [--preview] KEY # netlify/import live the move: the newest data, scheduled functions on set -euo pipefail kit=~/netlify site=~/site ho=~/netlify/handover umask 077 restart() { sudo systemctl try-restart "netlify@$1"; } # Netlify Database: Postgres here, the same major version, with a copy of # Netlify's production branch in database "site" (and its migrations recorded). database() { # HAND-OVER-DIR local d=$1/db url major pg [ -f $d/url ] || return 0 url=$(cat $d/url) if [ ! -d /usr/share/postgresql-common/pgdg ]; then sudo apt-get install -y -qq postgresql-common >/dev/null 2>&1 fi if [ ! -f /etc/apt/sources.list.d/pgdg.sources ] && [ ! -f /etc/apt/sources.list.d/pgdg.list ]; then sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y >/dev/null 2>&1 fi ls /usr/lib/postgresql/*/bin/psql >/dev/null 2>&1 || sudo apt-get install -y -qq postgresql-client >/dev/null 2>&1 major=$("$(ls -d /usr/lib/postgresql/*/bin | sort -V | tail -1)/psql" -XAtc 'show server_version_num' "$url" | cut -c1-2) [ -d /usr/lib/postgresql/$major/bin ] && [ -n "$(ls /etc/postgresql/$major 2>/dev/null)" ] || sudo apt-get install -y -qq postgresql-$major >/dev/null 2>&1 pg=/usr/lib/postgresql/$major/bin sudo -u postgres psql -XAtqc "select 1 from pg_roles where rolname = '$USER'" | grep -q 1 || sudo -u postgres createuser --createdb "$USER" echo "database: copying Netlify's production branch (Postgres $major)" # The read-only role can't read sequences' positions: skip them, and set them from the data. $pg/pg_dump --no-owner --no-privileges --exclude-schema=netlify -f $d/dump.sql \ $($pg/psql -XAtc "select format('--exclude-table-data=%I.%I', schemaname, sequencename) from pg_sequences" "$url") "$url" sudo systemctl stop netlify@production netlify@preview 2>/dev/null || true dropdb --if-exists --force site createdb site psql -X -q -v ON_ERROR_STOP=1 -d site -f $d/dump.sql >/dev/null { echo 'CREATE SCHEMA IF NOT EXISTS netlify;' echo 'CREATE TABLE IF NOT EXISTS netlify.migrations (name TEXT PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL DEFAULT now());' echo "DO \$\$ DECLARE r record; BEGIN FOR r IN SELECT s.oid::regclass AS seq, a.attname, t.oid::regclass AS tbl FROM pg_class s JOIN pg_depend d ON d.objid = s.oid AND d.classid = 'pg_class'::regclass AND d.refclassid = 'pg_class'::regclass AND d.deptype IN ('a', 'i') JOIN pg_class t ON t.oid = d.refobjid JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = d.refobjsubid WHERE s.relkind = 'S' LOOP EXECUTE format('SELECT setval(%L, COALESCE((SELECT max(%I) FROM %s), 0) + 1, false)', r.seq, r.attname, r.tbl); END LOOP; END \$\$;" jq -r --arg q "'" '.[] | "INSERT INTO netlify.migrations (name) VALUES (\($q)\(gsub($q; $q + $q))\($q)) ON CONFLICT DO NOTHING;"' $d/migrations.json } | psql -X -q -v ON_ERROR_STOP=1 -d site rm -f $d/dump.sql $d/url touch $site/postgres echo "database: $(psql -XAt -d site -c "select count(*) from pg_tables where schemaname not in ('pg_catalog','information_schema','netlify')") tables, $(jq length $d/migrations.json) migrations applied" # The deploy here may have migrations that Netlify's doesn't yet. [ ! -e $site/production ] || $kit/deploy migrate sudo systemctl start netlify@production 2>/dev/null || true [ ! -e $site/preview ] || sudo systemctl start netlify@preview 2>/dev/null || true } case ${1:-} in setup) [ -f $ho/site.json ] || { echo "no hand-over yet: run handover on your computer first" >&2; exit 1; } # Node.js, jq, and the kit's own packages. if ! command -v node >/dev/null || [ "$(node -v | sed 's/^v//; s/\..*//')" -lt 22 ]; then curl -fsSL https://deb.nodesource.com/setup_24.x | sudo -E bash - >/dev/null 2>&1 sudo apt-get install -y -qq nodejs >/dev/null 2>&1 fi command -v jq >/dev/null || sudo apt-get install -y -qq jq >/dev/null 2>&1 ( cd $kit && [ -f package.json ] || echo '{"private":true}' > package.json npm install --silent --no-audit --no-fund esbuild@0.28 smol-toml@1 sharp@0.35 \ @netlify/blobs@11 @netlify/redirect-parser@16 @netlify/headers-parser@10 ) chmod +x $kit/deploy $kit/import $kit/backup mkdir -p $site/data $site/releases ~/git ~/backups cp $ho/site.json $kit/site.json jq -r '.build_settings.repo_branch // "main"' $kit/site.json > $kit/branch # Values set here with `set` (secrets) survive a second hand-over. for pair in production:env.json branch-deploy:env.preview.json; do src=$ho/env.${pair%%:*}.json dst=$site/${pair#*:} [ -f "$dst" ] || echo '{}' > "$dst" jq -s '.[0] * .[1]' "$dst" "$src" > "$dst.new" && mv "$dst.new" "$dst" done node $kit/site.mjs import-data $ho database $ho test -d ~/git/site.git || git init -q --bare ~/git/site.git ln -sfn $kit/deploy ~/git/site.git/hooks/pre-receive sudo tee /etc/systemd/system/netlify@.service >/dev/null </dev/null | grep -v 'netlify/backup' || true echo "30 3 * * * $kit/backup 2>&1 | systemd-cat -t netlify-backup"; } | crontab - # The plan: what came over, and what needs a person. { echo "# $(jq -r .name $kit/site.json)" echo echo "- Netlify URL: $(jq -r '.ssl_url // .url' $kit/site.json)" echo "- Domains: $(jq -r '[.custom_domain // empty] + (.domain_aliases // []) | join(", ") | if . == "" then "none" else . end' $kit/site.json)" echo "- Production branch: $(cat $kit/branch); build: $(jq -r '.build_settings.cmd // "(netlify.toml)"' $kit/site.json); publish: $(jq -r '.build_settings.dir // "(netlify.toml)"' $kit/site.json)" echo "- Variables: $(jq length $site/env.json) for production, $(jq length $site/env.preview.json) for branch deploys" echo "- Secret variables to set with netlify/import set: $(jq -r 'join(" ") | if . == "" then "none" else . end' $ho/secrets.json)" echo "- Likely API keys (could move to integrations): $(jq -r 'keys | map(select(test("KEY|SECRET|TOKEN|PASSWORD"; "i"))) | join(" ") | if . == "" then "none" else . end' $site/env.json)" echo "- Forms: $(cd $ho && ls forms 2>/dev/null | sed 's/\.json$//' | tr '\n' ' ' | grep . || echo none)" echo "- Blobs stores: $(cd $ho && ls blobs 2>/dev/null | tr '\n' ' ' | grep . || echo none)" [ -d $ho/dns ] && echo "- Netlify DNS zones (copy the records before you leave): $(ls $ho/dns | sed 's/\.json$//' | tr '\n' ' ')" [ -f $site/postgres ] && echo "- Netlify Database: copied into Postgres here (database site; site_preview for branch deploys)" jq -r 'if .sso_login and (.sso_login_context // "all") != "none" then "- Netlify team login protects the site (\(.sso_login_context)): the VM is private to you and those you share it with, the same thing; leave it private at the move" else empty end' $kit/site.json jq -r 'if .has_password then "- A site password protects the site (\(.password_context // "all")): share the VM with people instead (ssh exe.dev share add VM EMAIL), or put Basic-Auth in _headers" else empty end' $kit/site.json jq -r 'if (.identity_instance_id // "") != "" then "- Netlify Identity is on: it has no equivalent here" else empty end' $kit/site.json true } > $kit/plan.md cat $kit/plan.md ;; set|unset) op=$1; shift f=$site/env.json unit=production if [ "${1:-}" = --preview ]; then f=$site/env.preview.json unit=preview; shift; fi key=${1:?usage: netlify/import set [--preview] KEY [VALUE]} if [ $op = unset ]; then jq --arg k "$key" 'del(.[$k])' "$f" > "$f.new" else if [ $# -ge 2 ]; then val=$2; else val=$(cat); fi jq --arg k "$key" --arg v "$val" '.[$k] = $v' "$f" > "$f.new" fi mv "$f.new" "$f" restart $unit echo "$op $key; the functions see it now. If the build uses it, redeploy: git commit --allow-empty -m redeploy && git push exe" ;; live) node $kit/site.mjs import-data $kit/handover-move database $kit/handover-move d=$(jq -r '.custom_domain // empty' $kit/site.json) if [ -n "$d" ]; then echo "$d" > $site/domain; fi touch $site/live $kit/deploy cron sudo systemctl restart netlify@production for _ in $(seq 20); do curl -s -o /dev/null --max-time 2 localhost:8000/ && break; sleep 0.5; done echo echo "Live on the VM, with the newest data from Netlify, and scheduled functions on." echo "Now point ${d:-your domain} at $(hostname).exe.xyz and run, on your computer:" echo " ssh exe.dev domain add $(hostname) ${d:-www.example.com}" if jq -e '.sso_login or .has_password' $kit/site.json >/dev/null; then echo " ssh exe.dev share add $(hostname) EMAIL (Netlify kept the site private; so does exe.dev)" else echo " ssh exe.dev share set-public $(hostname)" fi ;; *) sed -n '2,10p' "$0" | sed 's/^# \{0,1\}//' exit 2 ;; esac