diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/EdgeMirror.ts b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/EdgeMirror.ts deleted file mode 100644 index 00ab4ba6..00000000 --- a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/EdgeMirror.ts +++ /dev/null @@ -1,123 +0,0 @@ -import { forEach, reduce } from '@s-libs/micro-dash' -import { - INSTANCE_COLLECTION, - InstanceFields, - InstanceId, - LoggerService, - UserFields, - UserId, - WithCredentials, - WithUser, - mkInstanceCanonicalHostname, - mkInstanceHostname, - mkSingleton, -} from '../../../../../../core' -import { MothershipAdminClientService } from '../../../../../services' - -export type MirrorUserFields = UserFields -export type MirrorInstanceFields = InstanceFields> - -export const MirrorService = mkSingleton(async () => { - const { dbg, info, error } = LoggerService().create(`EdgeMirror`) - - info(`Initializing edge mirror`) - const adminSvc = await MothershipAdminClientService() - const { client } = adminSvc.client - - const instanceCleanupsById: { [_: InstanceId]: () => void } = {} - const instancesById: { [_: InstanceId]: InstanceFields | undefined } = {} - const instancesByHostName: { [_: string]: InstanceFields | undefined } = {} - const usersById: { - [_: UserId]: MirrorUserFields - } = {} - - client - .collection(`users`) - .subscribe(`*`, (e) => { - const { action, record } = e - if ([`create`, `update`].includes(action)) { - client - .collection(`verified_users`) - .getOne(record.id) - .then((v) => { - updateUser(v) - }) - .catch(error) - } - }) - .catch((e) => { - error(`Failed to subscribe to users`, e) - }) - - client - .collection(INSTANCE_COLLECTION) - .subscribe(`*`, (e) => { - const { action, record } = e - if ([`create`, `update`].includes(action)) { - setItem(record) - } - }) - .catch((e) => { - error(`Failed to subscribe to instances`, e) - }) - - info(`Loading mirror data`) - await client - .send(`/api/mirror`, { method: `GET` }) - .then(({ instances, users }) => { - const usersById: { [_: UserId]: MirrorUserFields } = reduce( - users, - (acc, user) => ({ ...acc, [user.id]: user }), - {}, - ) - forEach(instances, (record) => { - record.expand = { uid: usersById[record.uid] } - setItem(record, true) - }) - info(`Mirror data loaded`) - }) - .catch(error) - - function updateUser(record: MirrorUserFields) { - dbg(`Updating user ${record.email} (${record.id})`, { record }) - usersById[record.id] = record - } - - function setItem(record: InstanceFields, safe = false) { - if (safe && instancesById[record.id]) { - dbg(`Skipping instance update ${record.subdomain} (${record.id})`) - return - } - instanceCleanupsById[record.id]?.() - instancesById[record.id] = record - if (record.cname) { - instancesByHostName[record.cname] = record - } - instancesByHostName[mkInstanceHostname(record)] = record - instancesByHostName[mkInstanceCanonicalHostname(record)] = record - instanceCleanupsById[record.id] = () => { - dbg(`Cleaning up instance ${record.subdomain} (${record.id})`) - delete instancesById[record.id] - delete instancesByHostName[mkInstanceHostname(record)] - delete instancesByHostName[mkInstanceCanonicalHostname(record)] - if (record.cname) { - delete instancesByHostName[record.cname] - } - } - dbg(`Updating instance ${record.subdomain} (${record.id})`) - } - - function getInstanceByHost(host: string): MirrorInstanceFields | null { - const instance = instancesByHostName[host] - if (!instance) return null - const user = usersById[instance.uid] - if (!user) { - throw new Error( - `User ${instance.uid} not found for instance ${instance.subdomain} (${instance.uid})`, - ) - } - return { ...instance, expand: { uid: user } } - } - - return { getInstanceByHost } -}) diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/client.ts b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/client.ts deleted file mode 100644 index e0066d85..00000000 --- a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/client.ts +++ /dev/null @@ -1,14 +0,0 @@ -import fetch from 'node-fetch' -import { InstanceFields_WithUser, mkSingleton } from '../../../../../common' -import { mkMothershipMirrorUrl } from './helpers' - -export const EdgeMirrorClient = mkSingleton(() => { - const getInstanceByHost = (host: string) => - fetch(mkMothershipMirrorUrl(`instance`, `byHost`, host)).then( - (res) => res.json() as Promise, - ) - - return { - getInstanceByHost, - } -}) diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/helpers.ts b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/helpers.ts deleted file mode 100644 index 3a23790c..00000000 --- a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/helpers.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { join } from 'path' -import { PH_MOTHERSHIP_MIRROR_PORT } from '../../../../../constants' - -export const mkMothershipMirrorUrl = (...paths: string[]) => - join(`http://localhost:${PH_MOTHERSHIP_MIRROR_PORT()}`, ...paths) diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/index.ts b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/index.ts deleted file mode 100644 index 0aa85253..00000000 --- a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/index.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Command } from 'commander' -import { startMothershipMirrorServer } from './server' - -type Options = { - debug: boolean -} - -export const ServeCommand = () => { - const cmd = new Command(`serve`) - .description(`Run a mothership mirror`) - .action(async (options: Options) => { - await startMothershipMirrorServer() - }) - return cmd -} diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/server.ts b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/server.ts deleted file mode 100644 index 7cafef6f..00000000 --- a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/server.ts +++ /dev/null @@ -1,30 +0,0 @@ -import express from 'express' -import { LoggerService } from '../../../../../common' -import { PH_MOTHERSHIP_MIRROR_PORT } from '../../../../../constants' -import { MothershipAdminClientService } from '../../../../../services' -import { MirrorService } from './EdgeMirror' - -export async function startMothershipMirrorServer() { - const logger = LoggerService().create(`MothershipMirrorServer`) - const { dbg, error, info, warn } = logger - info(`Starting`) - - await MothershipAdminClientService({}) - const cache = await MirrorService({}) - - const app = express() - - app.get(`/instance/byHost/:host`, (req, res) => { - const { host } = req.params - const cached = cache.getInstanceByHost(host) - if (!cached) { - info(`Cache miss for ${host}`) - return res.status(404).json(null) - } - res.json(cached) - }) - - app.listen(PH_MOTHERSHIP_MIRROR_PORT(), () => { - info(`Listening on port ${PH_MOTHERSHIP_MIRROR_PORT()}`) - }) -} diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/index.ts b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/index.ts deleted file mode 100644 index 9781e15a..00000000 --- a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/index.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Command } from 'commander' -import { ServeCommand } from './ServeCommand' - -type Options = { - debug: boolean -} - -export const MirrorCommand = () => { - const cmd = new Command(`mirror`) - .description(`Instance mirror commands`) - .addCommand(ServeCommand()) - return cmd -} diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/ServeCommand/mothership.ts b/packages/pockethost/src/cli/commands/MothershipCommand/ServeCommand/mothership.ts index 67763570..d40952cd 100644 --- a/packages/pockethost/src/cli/commands/MothershipCommand/ServeCommand/mothership.ts +++ b/packages/pockethost/src/cli/commands/MothershipCommand/ServeCommand/mothership.ts @@ -16,7 +16,7 @@ import { import { PortService } from '../../../../services' import { GobotService } from '../../../../services/GobotService' -export type MothershipConfig = { isolate: boolean } +export type MothershipConfig = {} const _copy = (src: string, dst: string) => { const { error } = LoggerService().create(`copy`) @@ -40,13 +40,10 @@ const _copy = (src: string, dst: string) => { } export async function mothership(cfg: MothershipConfig) { - const { isolate } = cfg const logger = LoggerService().create(`Mothership`) const { dbg, error, info, warn } = logger info(`Starting`) - dbg(`Isolation mode:`, { isolate }) - await PortService({}) /** Launch central database */ diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/index.ts b/packages/pockethost/src/cli/commands/MothershipCommand/index.ts index bedde468..79f148de 100644 --- a/packages/pockethost/src/cli/commands/MothershipCommand/index.ts +++ b/packages/pockethost/src/cli/commands/MothershipCommand/index.ts @@ -1,5 +1,4 @@ import { Command } from 'commander' -import { MirrorCommand } from './MirrorCommand' import { SchemaCommand } from './SchemaCommand' import { ServeCommand } from './ServeCommand' @@ -12,7 +11,6 @@ export const MothershipCommand = () => { .description(`Mothership commands`) .addCommand(ServeCommand()) .addCommand(SchemaCommand()) - .addCommand(MirrorCommand()) return cmd } diff --git a/packages/pockethost/src/cli/commands/ServeCommand/index.ts b/packages/pockethost/src/cli/commands/ServeCommand/index.ts index 1c904276..ea8f78f5 100644 --- a/packages/pockethost/src/cli/commands/ServeCommand/index.ts +++ b/packages/pockethost/src/cli/commands/ServeCommand/index.ts @@ -12,7 +12,6 @@ type Options = { export const ServeCommand = () => { const cmd = new Command(`serve`) .description(`Run the entire PocketHost stack`) - .option(`--isolate`, `Use Docker for process isolation.`, false) .action(async (options: Options) => { const logger = LoggerService().create(`ServeCommand`) const { dbg, error, info, warn } = logger