Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
admin-self-host.tsx16.8 KB · 499 lines
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/**
 * BLOCK W — `/admin/self-host` site-admin self-host dashboard.
 *
 * One-page status of the Gluecron self-host migration:
 *   - is the Gluecron.com repo mirrored to Gluecron itself?
 *   - is the post-receive hook installed on the bare repo on disk?
 *   - is SELF_HOST_REPO set in the running process's env?
 *   - last 10 self-deploys (read from platform_deploys where source='self-deploy')
 *
 * POST /admin/self-host/bootstrap kicks off the bootstrap script. Same
 * security model as /admin/ops: requireAuth + isSiteAdmin gate + audit log.
 */
/* eslint-disable @typescript-eslint/no-explicit-any */

import { Hono } from "hono";
import { and, desc, eq } from "drizzle-orm";
import { existsSync } from "fs";
import { join } from "path";
import { db } from "../db";
import { repositories, users } from "../db/schema";
import { platformDeploys } from "../db/schema-deploys";
import { Layout } from "../views/layout";
import { softAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import { isSiteAdmin } from "../lib/admin";
import { audit as realAudit } from "../lib/notify";
import { config } from "../lib/config";
import { relativeTime, shortSha } from "./admin-deploys-page";

// ---------------------------------------------------------------------------
// DI seam — tests inject fakes so we never spawn the real bootstrap.
// ---------------------------------------------------------------------------

type AuditFn = typeof realAudit;
type BootstrapSpawnFn = (
  cmd: string[],
  opts: { detached?: boolean }
) => unknown;
type FsExistsFn = (p: string) => boolean;

interface Deps {
  audit: AuditFn;
  spawn: BootstrapSpawnFn;
  fsExists: FsExistsFn;
  getEnv: () => Record<string, string | undefined>;
}

const REAL_DEPS: Deps = {
  audit: realAudit,
  spawn: (cmd, _opts) =>
    Bun.spawn(cmd, {
      stdout: "ignore",
      stderr: "ignore",
      stdin: "ignore",
    }),
  fsExists: existsSync,
  getEnv: () => process.env as Record<string, string | undefined>,
};

let _deps: Deps = REAL_DEPS;

/** Test-only: replace one or more collaborators. Pass `null` to reset. */
export function __setSelfHostDepsForTests(d: Partial<Deps> | null): void {
  _deps = d ? { ...REAL_DEPS, ..._deps, ...d } : REAL_DEPS;
}

// ---------------------------------------------------------------------------
// Constants — the repo we self-host.
// ---------------------------------------------------------------------------

const SELF_HOST_OWNER = "ccantynz";
const SELF_HOST_NAME = "Gluecron.com";
const SELF_HOST_FULL = `${SELF_HOST_OWNER}/${SELF_HOST_NAME}`;
const SELF_DEPLOY_SOURCE = "self-deploy";

// ---------------------------------------------------------------------------
// Status reads — every probe wrapped so a single failure doesn't 500 the page.
// ---------------------------------------------------------------------------

interface RepoState {
  exists: boolean;
  diskPath: string | null;
}

async function readRepoState(): Promise<RepoState> {
  try {
    const [ownerRow] = await db
      .select({ id: users.id })
      .from(users)
      .where(eq(users.username, SELF_HOST_OWNER))
      .limit(1);
    if (!ownerRow) return { exists: false, diskPath: null };
    const [repo] = await db
      .select({ id: repositories.id, diskPath: repositories.diskPath })
      .from(repositories)
      .where(
        and(
          eq(repositories.ownerId, ownerRow.id),
          eq(repositories.name, SELF_HOST_NAME)
        )
      )
      .limit(1);
    if (!repo) return { exists: false, diskPath: null };
    return { exists: true, diskPath: repo.diskPath };
  } catch (err) {
    console.error("[admin-self-host] readRepoState:", err);
    return { exists: false, diskPath: null };
  }
}

interface HookState {
  installed: boolean;
  path: string;
}

function readHookState(diskPath: string | null): HookState {
  // Where the bootstrap installs the hook. If we can't resolve the repo
  // row we fall back to the conventional path so the operator can still
  // see the expected location.
  const base =
    diskPath ||
    join(config.gitReposPath, SELF_HOST_OWNER, `${SELF_HOST_NAME}.git`);
  const path = join(base, "hooks", "post-receive");
  try {
    return { installed: _deps.fsExists(path), path };
  } catch {
    return { installed: false, path };
  }
}

interface EnvState {
  selfHostRepoSet: boolean;
  selfHostRepo: string | null;
  matchesExpected: boolean;
}

function readEnvState(): EnvState {
  const env = _deps.getEnv();
  const v = env.SELF_HOST_REPO || null;
  return {
    selfHostRepoSet: !!v,
    selfHostRepo: v,
    matchesExpected: v === SELF_HOST_FULL,
  };
}

interface RecentDeploy {
  id: string;
  sha: string;
  status: string;
  startedAt: Date;
  finishedAt: Date | null;
  durationMs: number | null;
}

async function readRecentDeploys(): Promise<RecentDeploy[]> {
  try {
    const rows = await db
      .select({
        id: platformDeploys.id,
        sha: platformDeploys.sha,
        status: platformDeploys.status,
        startedAt: platformDeploys.startedAt,
        finishedAt: platformDeploys.finishedAt,
        durationMs: platformDeploys.durationMs,
      })
      .from(platformDeploys)
      .where(eq(platformDeploys.source, SELF_DEPLOY_SOURCE))
      .orderBy(desc(platformDeploys.startedAt))
      .limit(10);
    return rows;
  } catch (err) {
    console.error("[admin-self-host] readRecentDeploys:", err);
    return [];
  }
}

// ---------------------------------------------------------------------------
// Render helpers
// ---------------------------------------------------------------------------

function Card({ title, children }: { title: string; children: any }) {
  return (
    <div style="background:var(--bg-elevated);border:1px solid var(--border);border-radius:8px;padding:var(--space-4);margin-bottom:var(--space-4)">
      <h3 style="margin:0 0 12px 0;font-size:14px;letter-spacing:0.04em;text-transform:uppercase;color:var(--text-muted)">
        {title}
      </h3>
      {children}
    </div>
  );
}

function Pill({ ok, label }: { ok: boolean; label: string }) {
  return (
    <span
      style={`display:inline-flex;align-items:center;gap:6px;padding:2px 10px;border-radius:999px;font-size:12px;font-weight:600;background:${
        ok ? "rgba(52, 211, 153, 0.16)" : "rgba(248, 113, 113, 0.16)"
      };color:${ok ? "#34d399" : "#f87171"}`}
    >
      <span aria-hidden="true">{ok ? "v" : "x"}</span>
      <span>{label}</span>
    </span>
  );
}

// ---------------------------------------------------------------------------
// Gating
// ---------------------------------------------------------------------------

const selfHost = new Hono<AuthEnv>();
selfHost.use("*", softAuth);

async function gate(c: any): Promise<{ user: any } | Response> {
  const user = c.get("user");
  if (!user) return c.redirect("/login?next=/admin/self-host");
  if (!(await isSiteAdmin(user.id))) {
    return c.html(
      <Layout title="Forbidden" user={user}>
        <div class="empty-state">
          <h2>403 — Not a site admin</h2>
          <p>You don't have permission to view this page.</p>
        </div>
      </Layout>,
      403
    );
  }
  return { user };
}

function redirectWith(c: any, kind: "success" | "error", msg: string): Response {
  return c.redirect(`/admin/self-host?${kind}=${encodeURIComponent(msg)}`);
}

// ---------------------------------------------------------------------------
// GET /admin/self-host
// ---------------------------------------------------------------------------

selfHost.get("/admin/self-host", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;

  const success = c.req.query("success");
  const error = c.req.query("error");

  const [repoState, recent] = await Promise.all([
    readRepoState(),
    readRecentDeploys(),
  ]);
  const hookState = readHookState(repoState.diskPath);
  const envState = readEnvState();

  const allGreen =
    repoState.exists && hookState.installed && envState.matchesExpected;

  return c.html(
    <Layout title="Self-host — admin" user={user}>
      <div style="max-width:880px;margin:0 auto;padding:var(--space-6) var(--space-4)">
        <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:18px">
          <h1 style="margin:0">Self-host</h1>
          <a href="/admin" class="btn btn-sm">
            Back to admin
          </a>
        </div>
        <p style="color:var(--text-muted);margin-bottom:20px">
          Status of the BLOCK W migration — Gluecron's own source hosted
          on Gluecron itself. Once all three cards are green, every push
          to <code>{SELF_HOST_FULL}</code> deploys via the local
          post-receive hook in ~25 seconds.
        </p>

        {success && (
          <div class="auth-success" style="margin-bottom:16px">
            {decodeURIComponent(success)}
          </div>
        )}
        {error && (
          <div class="auth-error" style="margin-bottom:16px">
            {decodeURIComponent(error)}
          </div>
        )}

        <Card title="Status">
          <ul style="list-style:none;padding:0;margin:0">
            <li style="display:flex;align-items:center;gap:10px;padding:6px 0;font-size:13px">
              <Pill
                ok={repoState.exists}
                label={repoState.exists ? "Mirrored" : "Not mirrored"}
              />
              <span>
                Gluecron repo row exists ({SELF_HOST_FULL})
                {repoState.diskPath && (
                  <code style="margin-left:8px;color:var(--text-muted);font-size:12px">
                    {repoState.diskPath}
                  </code>
                )}
              </span>
            </li>
            <li style="display:flex;align-items:center;gap:10px;padding:6px 0;font-size:13px">
              <Pill
                ok={hookState.installed}
                label={hookState.installed ? "Installed" : "Missing"}
              />
              <span>
                Bare-repo post-receive hook
                <code style="margin-left:8px;color:var(--text-muted);font-size:12px">
                  {hookState.path}
                </code>
              </span>
            </li>
            <li style="display:flex;align-items:center;gap:10px;padding:6px 0;font-size:13px">
              <Pill
                ok={envState.matchesExpected}
                label={
                  envState.matchesExpected
                    ? "Set"
                    : envState.selfHostRepoSet
                    ? "Mismatch"
                    : "Unset"
                }
              />
              <span>
                <code>SELF_HOST_REPO</code> env
                {envState.selfHostRepo && (
                  <code style="margin-left:8px;color:var(--text-muted);font-size:12px">
                    = {envState.selfHostRepo}
                  </code>
                )}
                {!envState.selfHostRepoSet && (
                  <span style="margin-left:8px;color:var(--text-muted);font-size:12px">
                    expected <code>{SELF_HOST_FULL}</code>
                  </span>
                )}
              </span>
            </li>
          </ul>
          <div style="margin-top:14px;font-size:12px;color:var(--text-muted)">
            Overall:{" "}
            <Pill
              ok={allGreen}
              label={allGreen ? "Self-host ready" : "Setup incomplete"}
            />
          </div>
        </Card>

        <Card title="Bootstrap">
          <p style="font-size:13px;color:var(--text-muted);margin:0 0 10px 0">
            Mirror Gluecron's source from GitHub onto this Gluecron
            instance. Idempotent — safe to re-run. See{" "}
            <a href="/ccantynz/Gluecron.com/blob/main/docs/SELF_HOST.md">
              docs/SELF_HOST.md
            </a>{" "}
            for the full runbook.
          </p>
          <div style="display:flex;gap:var(--space-2);align-items:center">
            <form
              method="post"
              action="/admin/self-host/bootstrap"
              style="margin:0"
              onsubmit="return confirm('Run the self-host bootstrap on this box? Safe to re-run, but it will spawn a child process.')"
            >
              <button
                type="submit"
                class="btn btn-sm btn-primary"
                disabled={repoState.exists && hookState.installed}
                title={
                  repoState.exists && hookState.installed
                    ? "Bootstrap already applied"
                    : "Run scripts/self-host-bootstrap.ts"
                }
              >
                {repoState.exists && hookState.installed
                  ? "Already bootstrapped"
                  : "Run bootstrap"}
              </button>
            </form>
            <span style="font-size:12px;color:var(--text-muted)">
              Runs <code>bun run scripts/self-host-bootstrap.ts</code>{" "}
              detached. Watch the systemd journal or{" "}
              <code>/var/log/gluecron-self-deploy.log</code>.
            </span>
          </div>
        </Card>

        <Card title="Last 10 self-deploys">
          {recent.length === 0 ? (
            <p style="color:var(--text-muted);font-size:13px;margin:0">
              No self-deploys recorded yet. Push a commit to{" "}
              <code>{SELF_HOST_FULL}</code> after completing the bootstrap.
            </p>
          ) : (
            <table style="width:100%;border-collapse:collapse;font-size:13px">
              <thead>
                <tr style="text-align:left;color:var(--text-muted);font-size:12px;text-transform:uppercase;letter-spacing:0.04em">
                  <th style="padding:6px 4px;border-bottom:1px solid var(--border)">
                    SHA
                  </th>
                  <th style="padding:6px 4px;border-bottom:1px solid var(--border)">
                    Status
                  </th>
                  <th style="padding:6px 4px;border-bottom:1px solid var(--border)">
                    Started
                  </th>
                  <th style="padding:6px 4px;border-bottom:1px solid var(--border);text-align:right">
                    Duration
                  </th>
                </tr>
              </thead>
              <tbody>
                {recent.map((d) => (
                  <tr>
                    <td style="padding:6px 4px;border-bottom:1px solid var(--border)">
                      <code class="meta-mono">{shortSha(d.sha)}</code>
                    </td>
                    <td style="padding:6px 4px;border-bottom:1px solid var(--border)">
                      <Pill
                        ok={d.status === "succeeded"}
                        label={d.status}
                      />
                    </td>
                    <td
                      style="padding:6px 4px;border-bottom:1px solid var(--border)"
                      title={d.startedAt.toISOString()}
                    >
                      {relativeTime(d.startedAt)}
                    </td>
                    <td style="padding:6px 4px;border-bottom:1px solid var(--border);text-align:right">
                      {d.durationMs != null
                        ? `${(d.durationMs / 1000).toFixed(1)}s`
                        : "—"}
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          )}
        </Card>
      </div>
    </Layout>
  );
});

// ---------------------------------------------------------------------------
// POST /admin/self-host/bootstrap
//
// Spawns scripts/self-host-bootstrap.ts with the default args. Detached
// so the request returns immediately. The operator watches the systemd
// journal / log file for progress.
// ---------------------------------------------------------------------------

selfHost.post("/admin/self-host/bootstrap", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;

  try {
    const bunCmd =
      _deps.getEnv().GLUECRON_BUN_PATH || "/root/.bun/bin/bun";
    const scriptPath =
      _deps.getEnv().GLUECRON_BOOTSTRAP_SCRIPT ||
      "/opt/gluecron/scripts/self-host-bootstrap.ts";
    const child = _deps.spawn([bunCmd, "run", scriptPath], { detached: true });
    try {
      (child as any)?.unref?.();
    } catch {
      /* ignore */
    }
    try {
      await _deps.audit({
        userId: user.id,
        action: "admin.self_host.bootstrap_triggered",
        targetType: "repository",
        metadata: { repo: SELF_HOST_FULL },
      });
    } catch {
      /* audit failure is non-fatal */
    }
    return redirectWith(
      c,
      "success",
      "Bootstrap dispatched — watch the system journal for progress."
    );
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    return redirectWith(c, "error", `Bootstrap failed to spawn: ${message}`);
  }
});

export const __test = {
  readRepoState,
  readHookState,
  readEnvState,
  readRecentDeploys,
  SELF_HOST_OWNER,
  SELF_HOST_NAME,
  SELF_HOST_FULL,
};

export default selfHost;