init_mega.py is a standalone Mega initialization / sync script. After the Mega service is up, it:
- Buckal Bundles sync (default): clones the
toolchainsrepo, replacesbuckal-bundleswith the latest from GitHub, commits if there are changes, then uses the Mega API to find and auto-merge the corresponding CL (merge-no-auth, no human review).
This script is extracted from the server-side initialization logic so you can run initialization manually (locally or in CI) without relying on the server’s internal startup flow. It is also used by the mega-init Kubernetes Job (re-run when the mega-init image tag changes on terraform apply).
Dockerfile: scripts/init_mega/Dockerfile (python3 + git + these scripts).
- CI:
.github/workflows/mega-init-deploy.ymlpushesmega/mega-initto Harbor on changes underscripts/init_mega/. - Local:
./scripts/demo/build-demo-images-local.sh mega-init(optional--push).
K8s Job command overrides the image entrypoint and runs python3 scripts/init_mega/init_mega.py --base-url ....
- Python 3
- Git (available on the command line)
- A reachable Mega service (HTTP/HTTPS)
--base-url BASE_URL- Mega service base URL (default:
https://git.gitmega.com). Use this to point to a local/dev server, for example:http://127.0.0.1:8000
- Mega service base URL (default:
--skip-buckal- Skip the Buckal Bundles sync
--init-secret SECRET- Shared secret for
POST /api/v1/bots/bootstrap-init(must match mono-engineMEGA_INIT_BOOTSTRAP_SECRET, ≥32 chars). Defaults to envMEGA_INIT_BOOTSTRAP_SECRET.
- Shared secret for
Sync buckal-bundles only (default; use default base URL):
export MEGA_INIT_BOOTSTRAP_SECRET='your-long-shared-secret-at-least-32-chars'
python3 scripts/init_mega/init_mega.pySync buckal-bundles (override base URL):
python3 scripts/init_mega/init_mega.py \
--base-url http://127.0.0.1:8000 \
--init-secret "$MEGA_INIT_BOOTSTRAP_SECRET"Show help:
python3 scripts/init_mega/init_mega.py --helpThe script polls:
GET {base_url}/api/v1/status
When it returns HTTP 2xx, the service is considered ready and the script proceeds.
- Bootstrap a push credential (shared-secret gated):
POST {base_url}/api/v1/bots/bootstrap-initwith headerX-Mega-Init-Secret→{ bot_id, bot_name, token }(bot_token)
- Clone
toolchains:
git clone {base_url}/toolchains.git
- Configure the commit identity (repo-local):
git config user.email mega-bot@example.comgit config user.name Mega Botgit config commit.gpgsign false(andgit commit --no-gpg-sign) so a globalcommit.gpgsign=truedoes not fail without a Mega Bot GPG key
- Remove any existing
toolchains/buckal-bundles(idempotent re-sync). - Clone latest
buckal-bundles:
git clone --depth 1 https://github.com/buck2hub/buckal-bundles.git- Record upstream short SHA for the commit message
- Remove
buckal-bundles/.gitso it becomes a regular directory tracked bytoolchains(vendoring). - Commit and push only if there are changes:
git add .git commit --no-gpg-sign -m "bot: sync buckal-bundles <sha>"(skipped if already up to date)git -c http.extraHeader="Authorization: Bearer <bot_token>" push
- Use Mega APIs to find and auto-merge the corresponding CL:
POST {base_url}/api/v1/cl/list(paginate open CLs and matchtitle == "bot: sync buckal-bundles <sha>")POST {base_url}/api/v1/cl/{link}/merge-no-auth
- Git HTTP push requires auth; this script uses the mega-init bot token from
bootstrap-init. bootstrap-initis gated byMEGA_INIT_BOOTSTRAP_SECRET(headerX-Mega-Init-Secret); without a matching secret the endpoint returns 401.- Receive-pack accepts the init bot only with Write/Admin scope plus an Enabled installation (bootstrap ensures a system Organization install).
- Mega monorepo pushes create a CL; this script auto-merges it via
merge-no-authso no human review is required. - CL discovery depends on an exact title match:
bot: sync buckal-bundles <sha>. - Re-running with unchanged upstream content is a no-op (no commit/push/CL).
- This script does not start the Mega service. Start the service first, then run the script.
- Mono must have
MEGA_BOT_TOKEN_HMAC_SECRET(≥32 chars) set for bot token issue/verify. - Mono must have
MEGA_INIT_BOOTSTRAP_SECRET(≥32 chars) set to accept bootstrap-init.