#!/usr/bin/env node // Hand a Netlify site over to an exe.dev VM. // From https://exe.dev/deploy/migrate-netlify // // node handover.mjs my-site.exe.xyz [SITE] copy settings, variables, Blobs, form submissions, the database // node handover.mjs my-site.exe.xyz [SITE] --move copy the data again, and go live on the VM // // Run it on your computer with NETLIFY_AUTH_TOKEN set to a personal access // token, in the site's checkout (after `netlify link`) or with the site's // name. It only reads from Netlify. Everything goes straight to the VM over // SSH; nothing is written on this computer outside a temporary directory. import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { execFileSync, spawnSync } from 'node:child_process'; const args = process.argv.slice(2).filter(a => !a.startsWith('--')); const move = process.argv.includes('--move'); const [vm, siteArg] = args; const token = process.env.NETLIFY_AUTH_TOKEN; const API = process.env.NETLIFY_API_URL || 'https://api.netlify.com'; const die = m => { console.error(`handover: ${m}`); process.exit(1); }; if (!vm) die('usage: node handover.mjs my-site.exe.xyz [SITE] [--move]'); if (!token) die('set NETLIFY_AUTH_TOKEN to a personal access token (Netlify: User settings, Applications)'); if (spawnSync('ssh', ['-n', vm, 'true'], { stdio: 'inherit' }).status !== 0) die(`can't reach ${vm} over SSH`); const api = async (p, ok404) => { const r = await fetch(`${API}/api/v1${p}`, { headers: { authorization: `Bearer ${token}` } }); if (ok404 && r.status === 404) return null; if (!r.ok) die(`GET ${p}: ${r.status} ${await r.text()}`); return r.json(); }; const pages = async p => { const out = []; for (let page = 1; ; page++) { const items = await api(`${p}${p.includes('?') ? '&' : '?'}per_page=100&page=${page}`); out.push(...items); if (items.length < 100) return out; } }; let siteID = siteArg; if (!siteID) { try { siteID = JSON.parse(fs.readFileSync('.netlify/state.json', 'utf8')).siteId; } catch { die('run this in the site\'s checkout after `netlify link`, or give the site\'s name: node handover.mjs my-site.exe.xyz SITE'); } } let site = await api(`/sites/${encodeURIComponent(siteID)}`, true); if (!site) site = (await api(`/sites?name=${encodeURIComponent(siteID)}&filter=all`)).find(s => s.name === siteID); if (!site) die(`no site ${siteID}; give its name as the second argument`); console.log(`site: ${site.name} (${site.ssl_url || site.url})`); const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'handover-')); process.on('exit', () => fs.rmSync(tmp, { recursive: true, force: true })); const save = (f, data) => { fs.mkdirSync(path.dirname(path.join(tmp, f)), { recursive: true, mode: 0o700 }); fs.writeFileSync(path.join(tmp, f), typeof data === 'string' || Buffer.isBuffer(data) ? data : JSON.stringify(data, null, 2), { mode: 0o600 }); }; if (!move) { save('site.json', site); // Variables, as each deploy context sees them. Secret values can't be read back. const vars = await api(`/accounts/${site.account_slug}/env?site_id=${site.id}`); const secrets = []; for (const ctx of ['production', 'branch-deploy']) { const env = {}; for (const v of vars) { const val = v.values.find(x => x.context === ctx) || v.values.find(x => x.context === 'all'); if (!val) continue; if (v.is_secret) { if (!secrets.includes(v.key)) secrets.push(v.key); continue; } env[v.key] = val.value; } save(`env.${ctx}.json`, env); console.log(`variables (${ctx}): ${Object.keys(env).length}`); } save('secrets.json', secrets); if (secrets.length) console.log(`secret variables, which you set on the VM yourself: ${secrets.join(' ')}`); const zones = (await api(`/sites/${site.id}/dns`, true)) || []; for (const z of zones) save(`dns/${z.name}.json`, await api(`/dns_zones/${z.id}/dns_records`)); if (zones.length) console.log(`Netlify DNS zones: ${zones.map(z => z.name).join(' ')}`); } // Netlify Database: its read-only connection string, which the VM uses once // to copy the production branch and then deletes, and the migrations applied. const db = await api(`/sites/${site.id}/database`, true); if (db?.connection_string) { save('db/url', db.connection_string); const { migrations = [] } = await api(`/sites/${site.id}/database/migrations?branch=production`); save('db/migrations.json', migrations.filter(x => x.applied).map(x => x.name)); console.log(`database: yes, ${migrations.filter(x => x.applied).length} migrations applied; the VM copies it`); } // Form submissions, every page. for (const f of await api(`/sites/${site.id}/forms`)) { const subs = await pages(`/forms/${f.id}/submissions`); save(`forms/${f.name}.json`, subs); console.log(`form ${f.name}: ${subs.length} submissions`); } // Blobs: every site-wide store, with metadata, through Netlify's own client. console.log('blobs: installing @netlify/blobs in a temporary directory'); execFileSync('npm', ['install', '--silent', '--no-audit', '--no-fund', '--prefix', path.join(tmp, 'npm'), '@netlify/blobs@11'], { stdio: 'inherit' }); const { listStores, getStore } = await import(path.join(tmp, 'npm/node_modules/@netlify/blobs/dist/main.js')); const { stores } = await listStores({ siteID: site.id, token, apiURL: API }); for (const name of stores) { const store = getStore({ name, siteID: site.id, token, apiURL: API }); const { blobs } = await store.list(); for (const { key } of blobs) { const b = await store.getWithMetadata(key, { type: 'arrayBuffer' }); if (!b) continue; const f = `blobs/${encodeURIComponent(name)}/${encodeURIComponent(key)}`; save(`${f}.data`, Buffer.from(b.data)); save(`${f}.meta.json`, b.metadata || {}); } console.log(`blobs ${name}: ${blobs.length} entries`); } fs.rmSync(path.join(tmp, 'npm'), { recursive: true, force: true }); // To the VM. const dest = move ? 'netlify/handover-move' : 'netlify/handover'; const tar = spawnSync('bash', ['-c', `tar -C "$1" -cf - . | ssh "$2" "umask 077 && rm -rf $3 && mkdir -p $3 && tar -x -C $3"`, '-', tmp, vm, dest], { stdio: 'inherit' }); if (tar.status !== 0) die('copying to the VM failed'); if (move) { const r = spawnSync('ssh', [vm, 'netlify/import', 'live'], { stdio: 'inherit' }); process.exit(r.status ?? 1); } execFileSync('ssh', ['-n', vm, 'test -d git/site.git || git init -q --bare git/site.git; date -u +%FT%TZ > netlify/handed-over']); const branch = site.build_settings?.repo_branch || 'main'; console.log(` Handed over. Now push the code Netlify builds, from the site's checkout: git remote add exe ${vm}:git/site.git git push exe ${branch} `);