Remove mirror command

This commit is contained in:
Ben Allfree 2024-09-06 03:23:14 -07:00
parent fc23568327
commit 98bf5e17b1
9 changed files with 1 additions and 207 deletions

View File

@ -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<WithCredentials>
export type MirrorInstanceFields = InstanceFields<WithUser<MirrorUserFields>>
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<UserFields>(`*`, (e) => {
const { action, record } = e
if ([`create`, `update`].includes(action)) {
client
.collection(`verified_users`)
.getOne<MirrorUserFields>(record.id)
.then((v) => {
updateUser(v)
})
.catch(error)
}
})
.catch((e) => {
error(`Failed to subscribe to users`, e)
})
client
.collection(INSTANCE_COLLECTION)
.subscribe<InstanceFields>(`*`, (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 }
})

View File

@ -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<InstanceFields_WithUser | null>,
)
return {
getInstanceByHost,
}
})

View File

@ -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)

View File

@ -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
}

View File

@ -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()}`)
})
}

View File

@ -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
}

View File

@ -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 */

View File

@ -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
}

View File

@ -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