From d6e700bdd1efd6c10777a5f3a86cd380a947680c Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Wed, 31 Jul 2024 04:27:54 -0400 Subject: [PATCH 1/4] feat(pockethost): mothership schema dump --- .../MothershipCommand/SchemaCommand/index.ts | 11 +++++ .../MothershipCommand/SchemaCommand/schema.ts | 40 +++++++++++++++++++ .../cli/commands/MothershipCommand/index.ts | 4 +- 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 packages/pockethost/src/cli/commands/MothershipCommand/SchemaCommand/index.ts create mode 100644 packages/pockethost/src/cli/commands/MothershipCommand/SchemaCommand/schema.ts diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/SchemaCommand/index.ts b/packages/pockethost/src/cli/commands/MothershipCommand/SchemaCommand/index.ts new file mode 100644 index 00000000..bee89ce3 --- /dev/null +++ b/packages/pockethost/src/cli/commands/MothershipCommand/SchemaCommand/index.ts @@ -0,0 +1,11 @@ +import { Command } from 'commander' +import { schema } from './schema' + +export const SchemaCommand = () => { + const cmd = new Command(`schema`) + .description(`Create snapshot of the current PocketHost mothership schema`) + .action(async (options) => { + await schema() + }) + return cmd +} diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/SchemaCommand/schema.ts b/packages/pockethost/src/cli/commands/MothershipCommand/SchemaCommand/schema.ts new file mode 100644 index 00000000..465b6888 --- /dev/null +++ b/packages/pockethost/src/cli/commands/MothershipCommand/SchemaCommand/schema.ts @@ -0,0 +1,40 @@ +import { GobotOptions } from 'gobot' +import { + IS_DEV, + LoggerService, + MOTHERSHIP_DATA_ROOT, + MOTHERSHIP_MIGRATIONS_DIR, + MOTHERSHIP_SEMVER, +} from '../../../../../core' +import { GobotService } from '../../../../services/GobotService' + +export async function schema() { + const logger = LoggerService().create(`MothershipSchema`) + const { dbg, error, info, warn } = logger + info(`Starting`) + + const options: Partial = { + version: MOTHERSHIP_SEMVER(), + } + dbg(`gobot options`, options) + const { gobot } = GobotService() + const bot = await gobot(`pocketbase`, options) + + const args = [ + `migrate`, + `collections`, + `--automigrate`, + `0`, + `--hooksDir`, + `foo`, + `--dir`, + MOTHERSHIP_DATA_ROOT(`pb_data`), + `--migrationsDir`, + MOTHERSHIP_MIGRATIONS_DIR(), + ] + if (IS_DEV()) { + args.push(`--dev`) + } + dbg(`args`, args) + bot.run(args) +} diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/index.ts b/packages/pockethost/src/cli/commands/MothershipCommand/index.ts index f80f18ae..4019eff6 100644 --- a/packages/pockethost/src/cli/commands/MothershipCommand/index.ts +++ b/packages/pockethost/src/cli/commands/MothershipCommand/index.ts @@ -1,6 +1,6 @@ import { Command } from 'commander' +import { SchemaCommand } from './SchemaCommand' import { ServeCommand } from './ServeCommand' -import { UpdateCommand } from './UpdateCommand' type Options = { debug: boolean @@ -10,6 +10,6 @@ export const MothershipCommand = () => { const cmd = new Command(`mothership`) .description(`Mothership commands`) .addCommand(ServeCommand()) - .addCommand(UpdateCommand()) + .addCommand(SchemaCommand()) return cmd } From ad839eadf11dcd13c82b954845ef64b1b906d39b Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Wed, 31 Jul 2024 04:29:50 -0400 Subject: [PATCH 2/4] feat(pockethost): gobot command updater/downloader --- ecosystem.config.cjs | 9 ++----- .../freshenPocketbaseVersions.ts | 25 +++++++++++++++---- .../GobotCommand/UpdateCommand/index.ts | 11 ++++++++ .../src/cli/commands/GobotCommand/index.ts | 9 +++++++ .../ServeCommand/mothership.ts | 17 +++++++------ .../MothershipCommand/UpdateCommand/index.ts | 14 ----------- packages/pockethost/src/cli/index.ts | 2 ++ 7 files changed, 54 insertions(+), 33 deletions(-) rename packages/pockethost/src/cli/commands/{MothershipCommand => GobotCommand/UpdateCommand}/freshenPocketbaseVersions.ts (78%) create mode 100644 packages/pockethost/src/cli/commands/GobotCommand/UpdateCommand/index.ts create mode 100644 packages/pockethost/src/cli/commands/GobotCommand/index.ts delete mode 100644 packages/pockethost/src/cli/commands/MothershipCommand/UpdateCommand/index.ts diff --git a/ecosystem.config.cjs b/ecosystem.config.cjs index 631625f1..bf4f596b 100644 --- a/ecosystem.config.cjs +++ b/ecosystem.config.cjs @@ -22,14 +22,9 @@ module.exports = { script: 'pnpm prod:cli mothership serve', }, { - name: `updater`, + name: `gobot`, restart_delay: 60 * 60 * 1000, // 1 hour - script: 'pnpm prod:cli mothership update', - }, - { - name: `downloader`, - restart_delay: 60 * 60 * 1000, // 1 hour - script: 'pnpm gobot:download', + script: 'pnpm prod:cli gobot update', }, { name: `health`, diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/freshenPocketbaseVersions.ts b/packages/pockethost/src/cli/commands/GobotCommand/UpdateCommand/freshenPocketbaseVersions.ts similarity index 78% rename from packages/pockethost/src/cli/commands/MothershipCommand/freshenPocketbaseVersions.ts rename to packages/pockethost/src/cli/commands/GobotCommand/UpdateCommand/freshenPocketbaseVersions.ts index 3f295d19..2f68fd97 100644 --- a/packages/pockethost/src/cli/commands/MothershipCommand/freshenPocketbaseVersions.ts +++ b/packages/pockethost/src/cli/commands/GobotCommand/UpdateCommand/freshenPocketbaseVersions.ts @@ -1,10 +1,10 @@ import { writeFileSync } from 'fs' -import { gobot } from 'gobot' import { + LoggerService, MOTHERSHIP_DATA_ROOT, - MOTHERSHIP_HOOKS_DIR, stringify, -} from '../../../../core' +} from '../../../../../core' +import { GobotService } from '../../../../services/GobotService' function compareSemVer(a: string, b: string): number { // Consider wildcards as higher than any version number, hence represented by a large number for comparison @@ -54,12 +54,27 @@ function expandAndSortSemVers(semvers: string[]): string[] { } export async function freshenPocketbaseVersions() { + const { info } = LoggerService().create(`freshenPocketbaseVersions`) + + const { gobot } = await GobotService() + + info(`Updating pocketbase`) const bot = await gobot(`pocketbase`) await bot.update() + await bot.download() const rawVersions = await bot.versions() const versions = expandAndSortSemVers(rawVersions) const cjs = `module.exports = ${stringify(versions, null, 2)}` - writeFileSync(MOTHERSHIP_DATA_ROOT(`pb_hooks`, `versions.cjs`), cjs) - writeFileSync(MOTHERSHIP_HOOKS_DIR(`versions.cjs`), cjs) + + { + const path = MOTHERSHIP_DATA_ROOT(`pb_hooks`, `versions.cjs`) + info(`Writing to ${path}`) + writeFileSync(path, cjs) + } + { + const path = bot.cachePath(`versions.cjs`) + info(`Writing to ${path}`) + writeFileSync(path, cjs) + } return cjs } diff --git a/packages/pockethost/src/cli/commands/GobotCommand/UpdateCommand/index.ts b/packages/pockethost/src/cli/commands/GobotCommand/UpdateCommand/index.ts new file mode 100644 index 00000000..ca44f44f --- /dev/null +++ b/packages/pockethost/src/cli/commands/GobotCommand/UpdateCommand/index.ts @@ -0,0 +1,11 @@ +import { Command } from 'commander' +import { freshenPocketbaseVersions } from './freshenPocketbaseVersions' + +export const UpdateCommand = () => { + const cmd = new Command(`update`) + .description(`Update all Gobot dependencies`) + .action(async (options) => { + await freshenPocketbaseVersions() + }) + return cmd +} diff --git a/packages/pockethost/src/cli/commands/GobotCommand/index.ts b/packages/pockethost/src/cli/commands/GobotCommand/index.ts new file mode 100644 index 00000000..f631a62f --- /dev/null +++ b/packages/pockethost/src/cli/commands/GobotCommand/index.ts @@ -0,0 +1,9 @@ +import { Command } from 'commander' +import { UpdateCommand } from './UpdateCommand' + +export const GobotCommand = () => { + const cmd = new Command(`gobot`).description(`Gobot commands`) + + cmd.addCommand(UpdateCommand()) + 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 d646dd54..67763570 100644 --- a/packages/pockethost/src/cli/commands/MothershipCommand/ServeCommand/mothership.ts +++ b/packages/pockethost/src/cli/commands/MothershipCommand/ServeCommand/mothership.ts @@ -56,8 +56,18 @@ export async function mothership(cfg: MothershipConfig) { LS_WEBHOOK_SECRET: LS_WEBHOOK_SECRET(), } dbg(env) + + const options: Partial = { + version: MOTHERSHIP_SEMVER(), + env, + } + dbg(`options`, options) + const { gobot } = GobotService() + const bot = await gobot(`pocketbase`, options) + await rimraf(MOTHERSHIP_DATA_ROOT(`pb_hooks`)) await _copy(MOTHERSHIP_HOOKS_DIR(`**/*`), MOTHERSHIP_DATA_ROOT(`pb_hooks`)) + await _copy(bot.cachePath(`versions.cjs`), MOTHERSHIP_DATA_ROOT(`pb_hooks`)) await rimraf(MOTHERSHIP_DATA_ROOT(`pb_migrations`)) await _copy( MOTHERSHIP_MIGRATIONS_DIR(`**/*`), @@ -80,13 +90,6 @@ export async function mothership(cfg: MothershipConfig) { if (IS_DEV()) { args.push(`--dev`) } - const options: Partial = { - version: MOTHERSHIP_SEMVER(), - env, - } dbg(`args`, args) - dbg(`options`, options) - const { gobot } = GobotService() - const bot = await gobot(`pocketbase`, options) bot.run(args, { env }) } diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/UpdateCommand/index.ts b/packages/pockethost/src/cli/commands/MothershipCommand/UpdateCommand/index.ts deleted file mode 100644 index ffc824f9..00000000 --- a/packages/pockethost/src/cli/commands/MothershipCommand/UpdateCommand/index.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Command } from 'commander' -import { freshenPocketbaseVersions } from '../freshenPocketbaseVersions' - -type Options = {} - -export const UpdateCommand = () => { - const cmd = new Command(`update`) - .description(`Update known PocketBase versions`) - .action(async (options: Options) => { - const cjs = await freshenPocketbaseVersions() - console.log(cjs) - }) - return cmd -} diff --git a/packages/pockethost/src/cli/index.ts b/packages/pockethost/src/cli/index.ts index d0d68b61..7c2f03af 100755 --- a/packages/pockethost/src/cli/index.ts +++ b/packages/pockethost/src/cli/index.ts @@ -14,6 +14,7 @@ import { import { GobotService } from '../services/GobotService' import { EdgeCommand } from './commands/EdgeCommand' import { FirewallCommand } from './commands/FirewallCommand' +import { GobotCommand } from './commands/GobotCommand' import { HealthCommand } from './commands/HealthCommand' import { MothershipCommand } from './commands/MothershipCommand' import { SendMailCommand } from './commands/SendMailCommand' @@ -43,6 +44,7 @@ export const main = async () => { .addCommand(FirewallCommand()) .addCommand(SendMailCommand()) .addCommand(ServeCommand()) + .addCommand(GobotCommand()) await program.parseAsync() } From 7d5f82b46506bbe4208a5f0021df7d1c2c66a1e8 Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Wed, 31 Jul 2024 04:30:10 -0400 Subject: [PATCH 3/4] fix(pockethost): hoist disabled hooks in mothership --- .../disabled/instances-migrate-regions.pb.js | 0 .../disabled/instances-migrate-versions.pb.js | 0 .../mothership-app/pb_hooks/src/versions.cjs | 166 ------------------ 3 files changed, 166 deletions(-) rename packages/pockethost/src/mothership-app/pb_hooks/{src => }/disabled/instances-migrate-regions.pb.js (100%) rename packages/pockethost/src/mothership-app/pb_hooks/{src => }/disabled/instances-migrate-versions.pb.js (100%) delete mode 100644 packages/pockethost/src/mothership-app/pb_hooks/src/versions.cjs diff --git a/packages/pockethost/src/mothership-app/pb_hooks/src/disabled/instances-migrate-regions.pb.js b/packages/pockethost/src/mothership-app/pb_hooks/disabled/instances-migrate-regions.pb.js similarity index 100% rename from packages/pockethost/src/mothership-app/pb_hooks/src/disabled/instances-migrate-regions.pb.js rename to packages/pockethost/src/mothership-app/pb_hooks/disabled/instances-migrate-regions.pb.js diff --git a/packages/pockethost/src/mothership-app/pb_hooks/src/disabled/instances-migrate-versions.pb.js b/packages/pockethost/src/mothership-app/pb_hooks/disabled/instances-migrate-versions.pb.js similarity index 100% rename from packages/pockethost/src/mothership-app/pb_hooks/src/disabled/instances-migrate-versions.pb.js rename to packages/pockethost/src/mothership-app/pb_hooks/disabled/instances-migrate-versions.pb.js diff --git a/packages/pockethost/src/mothership-app/pb_hooks/src/versions.cjs b/packages/pockethost/src/mothership-app/pb_hooks/src/versions.cjs deleted file mode 100644 index e5042989..00000000 --- a/packages/pockethost/src/mothership-app/pb_hooks/src/versions.cjs +++ /dev/null @@ -1,166 +0,0 @@ -module.exports = [ - "0.22.*", - "0.22.18", - "0.22.17", - "0.22.16", - "0.22.15", - "0.22.14", - "0.22.14-rc", - "0.22.13", - "0.22.12", - "0.22.11", - "0.22.10", - "0.22.9", - "0.22.8", - "0.22.7", - "0.22.6", - "0.22.5", - "0.22.4", - "0.22.3", - "0.22.2", - "0.22.1", - "0.22.0", - "0.21.*", - "0.21.3", - "0.21.2", - "0.21.1", - "0.21.0", - "0.20.*", - "0.20.7", - "0.20.6", - "0.20.5", - "0.20.4", - "0.20.3", - "0.20.2", - "0.20.1", - "0.20.0", - "0.20.0-rc3", - "0.20.0-rc2", - "0.20.0-rc", - "0.19.*", - "0.19.4", - "0.19.3", - "0.19.2", - "0.19.1", - "0.19.0", - "0.18.*", - "0.18.10", - "0.18.9", - "0.18.8", - "0.18.7", - "0.18.6", - "0.18.5", - "0.18.4", - "0.18.3", - "0.18.2", - "0.18.1", - "0.18.0", - "0.17.*", - "0.17.7", - "0.17.6", - "0.17.5", - "0.17.4", - "0.17.3", - "0.17.2", - "0.17.1", - "0.17.0", - "0.16.*", - "0.16.10", - "0.16.9", - "0.16.8", - "0.16.7", - "0.16.6", - "0.16.5", - "0.16.4", - "0.16.3", - "0.16.2", - "0.16.1", - "0.16.0", - "0.15.*", - "0.15.3", - "0.15.2", - "0.15.1", - "0.15.0", - "0.14.*", - "0.14.5", - "0.14.4", - "0.14.3", - "0.14.2", - "0.14.1", - "0.14.0", - "0.13.*", - "0.13.4", - "0.13.3", - "0.13.2", - "0.13.1", - "0.13.0", - "0.12.*", - "0.12.3", - "0.12.2", - "0.12.1", - "0.12.0", - "0.11.*", - "0.11.4", - "0.11.3", - "0.11.2", - "0.11.1", - "0.11.0", - "0.10.*", - "0.10.4", - "0.10.3", - "0.10.2", - "0.10.1", - "0.10.0", - "0.9.*", - "0.9.2", - "0.9.1", - "0.9.0", - "0.8.*", - "0.8.0", - "0.8.0-rc4", - "0.8.0-rc3", - "0.8.0-rc2", - "0.8.0-rc1", - "0.7.*", - "0.7.10", - "0.7.9", - "0.7.8", - "0.7.7", - "0.7.6", - "0.7.5", - "0.7.4", - "0.7.3", - "0.7.2", - "0.7.1", - "0.7.0", - "0.6.*", - "0.6.0", - "0.5.*", - "0.5.2", - "0.5.1", - "0.5.0", - "0.4.*", - "0.4.2", - "0.4.1", - "0.4.0", - "0.3.*", - "0.3.4", - "0.3.3", - "0.3.2", - "0.3.1", - "0.3.0", - "0.2.*", - "0.2.8", - "0.2.7", - "0.2.6", - "0.2.5", - "0.2.4", - "0.2.3", - "0.2.2", - "0.2.1", - "0.2.0", - "0.1.*", - "0.1.2", - "0.1.1", - "0.1.0" -] \ No newline at end of file From d2b5f21e9db2f2e74d4dcf6137db2d4cfd57ee1d Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Wed, 31 Jul 2024 04:30:34 -0400 Subject: [PATCH 4/4] feat(pockethost): gobot verbosity --- packages/pockethost/src/services/GobotService.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/pockethost/src/services/GobotService.ts b/packages/pockethost/src/services/GobotService.ts index 85a5ec0d..777342ea 100644 --- a/packages/pockethost/src/services/GobotService.ts +++ b/packages/pockethost/src/services/GobotService.ts @@ -1,10 +1,19 @@ +import { default as env } from 'env-var' import { GobotOptions, gobot } from 'gobot' import { mkSingleton } from '../common' import { PH_GOBOT_ROOT } from '../constants' +export const PH_GOBOT_VERBOSITY = () => + env.get(`PH_GOBOT_VERBOSITY`).default(1).asIntPositive() + export const GobotService = mkSingleton(() => { return { - gobot: (name: string, options?: Partial) => - gobot(name, { ...options, cachePath: PH_GOBOT_ROOT(`cache`, name) }), + gobot: (name: string, options?: Partial) => { + // verbosity(PH_GOBOT_VERBOSITY()) + return gobot(name, { + ...options, + cachePath: PH_GOBOT_ROOT(`cache`, name), + }) + }, } })