Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issues with legacy packages #2841

Merged
merged 5 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 54 additions & 45 deletions container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ExtendedVersion, types as T, utils } from "@start9labs/start-sdk"
import {
ExtendedVersion,
types as T,
utils,
VersionRange,
} from "@start9labs/start-sdk"
import * as fs from "fs/promises"

import { polyfillEffects } from "./polyfillEffects"
Expand Down Expand Up @@ -345,7 +350,8 @@ export class SystemForEmbassy implements System {
const previousVersion = await effects.getDataVersion()
if (previousVersion) {
if (
(await this.migration(effects, previousVersion, timeoutMs)).configured
(await this.migration(effects, { from: previousVersion }, timeoutMs))
.configured
) {
await effects.action.clearRequests({ only: ["needs-config"] })
}
Expand Down Expand Up @@ -542,7 +548,10 @@ export class SystemForEmbassy implements System {
nextVersion: Optional<string>,
timeoutMs: number | null,
): Promise<void> {
// TODO Do a migration down if the version exists
await this.currentRunning?.clean({ timeout: timeoutMs ?? undefined })
if (nextVersion) {
await this.migration(effects, { to: nextVersion }, timeoutMs)
}
await effects.setMainStatus({ status: "stopped" })
}

Expand Down Expand Up @@ -746,46 +755,37 @@ export class SystemForEmbassy implements System {

async migration(
effects: Effects,
fromVersion: string,
version: { from: string } | { to: string },
timeoutMs: number | null,
): Promise<{ configured: boolean }> {
const fromEmver = ExtendedVersion.parseEmver(fromVersion)
const currentEmver = ExtendedVersion.parseEmver(this.manifest.version)
if (!this.manifest.migrations) return { configured: true }
const fromMigration = Object.entries(this.manifest.migrations.from)
.map(
([version, procedure]) =>
[ExtendedVersion.parseEmver(version), procedure] as const,
)
.find(
([versionEmver, procedure]) =>
versionEmver.greaterThan(fromEmver) &&
versionEmver.lessThanOrEqual(currentEmver),
)
const toMigration = Object.entries(this.manifest.migrations.to)
.map(
([version, procedure]) =>
[ExtendedVersion.parseEmver(version), procedure] as const,
)
.find(
([versionEmver, procedure]) =>
versionEmver.greaterThan(fromEmver) &&
versionEmver.lessThanOrEqual(currentEmver),
)

// prettier-ignore
const migration = (
fromEmver.greaterThan(currentEmver) ? [toMigration, fromMigration] :
[fromMigration, toMigration]).filter(Boolean)[0]
let migration
let args: [string, ...string[]]
if ("from" in version) {
args = [version.from, "from"]
const fromExver = ExtendedVersion.parse(version.from)
if (!this.manifest.migrations) return { configured: true }
migration = Object.entries(this.manifest.migrations.from)
.map(
([version, procedure]) =>
[VersionRange.parseEmver(version), procedure] as const,
)
.find(([versionEmver, _]) => versionEmver.satisfiedBy(fromExver))
} else {
args = [version.to, "to"]
const toExver = ExtendedVersion.parse(version.to)
if (!this.manifest.migrations) return { configured: true }
migration = Object.entries(this.manifest.migrations.to)
.map(
([version, procedure]) =>
[VersionRange.parseEmver(version), procedure] as const,
)
.find(([versionEmver, _]) => versionEmver.satisfiedBy(toExver))
}

if (migration) {
const [version, procedure] = migration
const [_, procedure] = migration
if (procedure.type === "docker") {
const commands = [
procedure.entrypoint,
...procedure.args,
JSON.stringify(fromVersion),
]
const commands = [procedure.entrypoint, ...procedure.args]
const container = await DockerProcedureContainer.of(
effects,
this.manifest.id,
Expand All @@ -794,7 +794,11 @@ export class SystemForEmbassy implements System {
`Migration - ${commands.join(" ")}`,
)
return JSON.parse(
(await container.execFail(commands, timeoutMs)).stdout.toString(),
(
await container.execFail(commands, timeoutMs, {
input: JSON.stringify(args[0]),
})
).stdout.toString(),
)
} else if (procedure.type === "script") {
const moduleCode = await this.moduleCode
Expand All @@ -803,7 +807,7 @@ export class SystemForEmbassy implements System {
throw new Error("Expecting that the method migration exists")
return (await method(
polyfillEffects(effects, this.manifest),
fromVersion as string,
...args,
).then((x) => {
if ("result" in x) return x.result
if ("error" in x) throw new Error("Error getting config: " + x.error)
Expand Down Expand Up @@ -1177,9 +1181,14 @@ function extractServiceInterfaceId(manifest: Manifest, specInterface: string) {
return serviceInterfaceId
}
async function convertToNewConfig(value: OldGetConfigRes) {
const valueSpec: OldConfigSpec = matchOldConfigSpec.unsafeCast(value.spec)
const spec = transformConfigSpec(valueSpec)
if (!value.config) return { spec, config: null }
const config = transformOldConfigToNew(valueSpec, value.config)
return { spec, config }
try {
const valueSpec: OldConfigSpec = matchOldConfigSpec.unsafeCast(value.spec)
const spec = transformConfigSpec(valueSpec)
if (!value.config) return { spec, config: null }
const config = transformOldConfigToNew(valueSpec, value.config) ?? null
return { spec, config }
} catch (e) {
console.error(e)
throw e
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ function parseDfOutput(output: string): { used: number; total: number } {
.map((x) => x.split(/\s+/))
const index = lines.splice(0, 1)[0].map((x) => x.toLowerCase())
const usedIndex = index.indexOf("used")
const availableIndex = index.indexOf("available")
const sizeIndex = index.indexOf("size")
const used = lines.map((x) => Number.parseInt(x[usedIndex]))[0] || 0
const total = lines.map((x) => Number.parseInt(x[availableIndex]))[0] || 0
const total = lines.map((x) => Number.parseInt(x[sizeIndex]))[0] || 0
return { used, total }
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ export function transformOldConfigToNew(

delete config[key][val.tag.id]

if (!val.variants[selection]) return obj

newVal = {
selection,
value: transformOldConfigToNew(
Expand Down
Loading
Loading