From c68acebe312ff786224c25b46b4beaaf99885688 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Tue, 13 May 2025 13:11:27 -0700 Subject: [PATCH] Add db migrations to CV2 db --- ext/central-controller-docker/Dockerfile | 11 +++-- .../Dockerfile.builder | 5 +- .../Dockerfile.run_base | 12 +++-- ext/central-controller-docker/main.sh | 7 ++- .../migrations/0001_init.down.sql | 3 ++ .../migrations/0001_init.up.sql | 47 +++++++++++++++++++ 6 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 ext/central-controller-docker/migrations/0001_init.down.sql create mode 100644 ext/central-controller-docker/migrations/0001_init.up.sql diff --git a/ext/central-controller-docker/Dockerfile b/ext/central-controller-docker/Dockerfile index c134cbdcf..6d9a1b3ae 100644 --- a/ext/central-controller-docker/Dockerfile +++ b/ext/central-controller-docker/Dockerfile @@ -1,11 +1,16 @@ # Dockerfile for ZeroTier Central Controllers -FROM registry.zerotier.com/zerotier/ctlbuild:latest as builder -MAINTAINER Adam Ierymekno , Grant Limberg +FROM registry.zerotier.com/zerotier/ctlbuild:2025-05-13-01 AS builder ADD . /ZeroTierOne RUN export PATH=$PATH:~/.cargo/bin && cd ZeroTierOne && make clean && make central-controller -j8 -FROM registry.zerotier.com/zerotier/ctlrun:latest +FROM golang:bookworm AS go_base +RUN go install github.com/golang-migrate/migrate/v4/cmd/migrate@latest + +FROM registry.zerotier.com/zerotier/ctlrun:2025-05-13-01 COPY --from=builder /ZeroTierOne/zerotier-one /usr/local/bin/zerotier-one +COPY --from=go_base /go/bin/migrate /usr/local/bin/migrate +COPY ext/central-controller-docker/migrations /migrations + RUN chmod a+x /usr/local/bin/zerotier-one RUN echo "/usr/local/lib64" > /etc/ld.so.conf.d/usr-local-lib64.conf && ldconfig diff --git a/ext/central-controller-docker/Dockerfile.builder b/ext/central-controller-docker/Dockerfile.builder index 29356b8e4..e96a61995 100644 --- a/ext/central-controller-docker/Dockerfile.builder +++ b/ext/central-controller-docker/Dockerfile.builder @@ -1,8 +1,5 @@ # Dockerfile for building ZeroTier Central Controllers -FROM ubuntu:jammy as builder -MAINTAINER Adam Ierymekno , Grant Limberg - -ARG git_branch=master +FROM debian:bookworm RUN apt update && apt upgrade -y RUN apt -y install \ diff --git a/ext/central-controller-docker/Dockerfile.run_base b/ext/central-controller-docker/Dockerfile.run_base index a581d015f..606ebfcfb 100644 --- a/ext/central-controller-docker/Dockerfile.run_base +++ b/ext/central-controller-docker/Dockerfile.run_base @@ -1,15 +1,17 @@ -FROM ubuntu:jammy +FROM debian:bookworm + + RUN apt update && apt upgrade -y - RUN apt -y install \ - netcat \ + netcat-traditional \ postgresql-client \ postgresql-client-common \ libjemalloc2 \ libpq5 \ curl \ binutils \ - linux-tools-gke \ perf-tools-unstable \ - google-perftools + google-perftools \ + gnupg + diff --git a/ext/central-controller-docker/main.sh b/ext/central-controller-docker/main.sh index 154cc07cf..2ccf1f2ce 100755 --- a/ext/central-controller-docker/main.sh +++ b/ext/central-controller-docker/main.sh @@ -21,7 +21,7 @@ if [ -z "$ZT_DB_PASSWORD" ]; then exit 1 fi if [ -z "$ZT_DB_TYPE" ]; then - ZT_DB="postgres" + ZT_DB_TYPE="postgres" fi REDIS="" @@ -103,6 +103,11 @@ else done fi +if [ "$ZT_DB_TYPE" == "cv2" ]; then + echo "Migrating database (if needed)..." + /usr/local/bin/migrate -source /migrations -database "postgres://$ZT_DB_USER:$ZT_DB_PASSWORD@$ZT_DB_HOST:$ZT_DB_PORT/$ZT_DB_NAME?x-migrations-table=controller_migrations" up +fi + if [ -n "$ZT_TEMPORAL_HOST" ] && [ -n "$ZT_TEMPORAL_PORT" ]; then echo "waiting for temporal..." while ! nc -z ${ZT_TEMPORAL_HOST} ${ZT_TEMPORAL_PORT}; do diff --git a/ext/central-controller-docker/migrations/0001_init.down.sql b/ext/central-controller-docker/migrations/0001_init.down.sql new file mode 100644 index 000000000..03dc63c81 --- /dev/null +++ b/ext/central-controller-docker/migrations/0001_init.down.sql @@ -0,0 +1,3 @@ +DROP TABLE IF EXISTS network_memberships_ctl; +DROP TABLE IF EXISTS networks_ctl; +DROP TABLE IF EXISTS controllers_ctl; \ No newline at end of file diff --git a/ext/central-controller-docker/migrations/0001_init.up.sql b/ext/central-controller-docker/migrations/0001_init.up.sql new file mode 100644 index 000000000..90d29e889 --- /dev/null +++ b/ext/central-controller-docker/migrations/0001_init.up.sql @@ -0,0 +1,47 @@ +-- inits controller db schema + +CREATE TABLE IF NOT EXISTS controllers_ctl ( + id text NOT NULL PRIMARY KEY, + hostname text, + last_heartbeat timestamp with time zone, + public_identity text NOT NULL, + version text +); + +CREATE TABLE IF NOT EXISTS networks_ctl ( + id character varying(22) NOT NULL PRIMARY KEY, + name text NOT NULL, + configuration jsonb DEFAULT '{}'::jsonb NOT NULL, + controller_id text REFERENCES controllers_ctl(id), + revision integer DEFAULT 0 NOT NULL, + last_modified timestamp with time zone DEFAULT now(), + creation_time timestamp with time zone DEFAULT now() +); + +CREATE TABLE IF NOT EXISTS network_memberships_ctl ( + device_id character varying(22) NOT NULL, + network_id character varying(22) NOT NULL REFERENCES networks_ctl(id), + authorized boolean, + active_bridge boolean, + ip_assignments text[], + no_auto_assign_ips boolean, + sso_exempt boolean, + authentication_expiry_time timestamp with time zone, + capabilities jsonb, + creation_time timestamp with time zone DEFAULT now(), + last_modified timestamp with time zone DEFAULT now(), + identity text DEFAULT ''::text, + last_authorized_credential text, + last_authorized_time timestamp with time zone, + last_deauthorized_time timestamp with time zone, + last_seen jsonb DEFAULT '{}'::jsonb NOT NULL, -- in the context of the network + remote_trace_level integer DEFAULT 0 NOT NULL, + remote_trace_target text DEFAULT ''::text NOT NULL, + revision integer DEFAULT 0 NOT NULL, + tags jsonb, + version_major integer DEFAULT 0 NOT NULL, + version_minor integer DEFAULT 0 NOT NULL, + version_revision integer DEFAULT 0 NOT NULL, + version_protocol integer DEFAULT 0 NOT NULL, + PRIMARY KEY (device_id, network_id) +);