feat(pockethost): gobot command updater/downloader

This commit is contained in:
Ben Allfree 2024-07-31 04:29:50 -04:00
parent d6e700bdd1
commit ad839eadf1
7 changed files with 54 additions and 33 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -56,8 +56,18 @@ export async function mothership(cfg: MothershipConfig) {
LS_WEBHOOK_SECRET: LS_WEBHOOK_SECRET(),
}
dbg(env)
const options: Partial<GobotOptions> = {
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<GobotOptions> = {
version: MOTHERSHIP_SEMVER(),
env,
}
dbg(`args`, args)
dbg(`options`, options)
const { gobot } = GobotService()
const bot = await gobot(`pocketbase`, options)
bot.run(args, { env })
}

View File

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

View File

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