219 lines
6.9 KiB
TypeScript
219 lines
6.9 KiB
TypeScript
import { defineCommand } from "citty";
|
|
import { Listr } from "listr2";
|
|
import type { ComposeSpecification } from "../schema/docker.d";
|
|
import { ctx } from "../lib/context";
|
|
import {
|
|
assertManagedNamespace,
|
|
applyResource,
|
|
deleteResource,
|
|
getStaleResources,
|
|
sortResources,
|
|
} from "../lib/apply";
|
|
import { buildServices } from "../lib/build";
|
|
import { composeToKubernetes, type KubernetesResource } from "../lib/convert";
|
|
import {
|
|
getComposePostgresClaims,
|
|
reconcilePostgresClaims,
|
|
} from "../lib/database";
|
|
import { getComposeS3Claims, reconcileS3Claims } from "../lib/storage";
|
|
import { restartDeployment, waitForDeploymentRollout } from "../lib/shared";
|
|
|
|
type UpContext = {
|
|
compose?: ComposeSpecification;
|
|
serviceEnv?: Record<string, Record<string, string>>;
|
|
resources?: KubernetesResource[];
|
|
staleResources?: KubernetesResource[];
|
|
};
|
|
|
|
function mergeServiceEnv(
|
|
current: Record<string, Record<string, string>> | undefined,
|
|
next: Record<string, Record<string, string>>,
|
|
): Record<string, Record<string, string>> {
|
|
const merged = { ...current };
|
|
for (const [service, environment] of Object.entries(next)) {
|
|
merged[service] = { ...merged[service], ...environment };
|
|
}
|
|
return merged;
|
|
}
|
|
|
|
function getDeploymentNames(resources: KubernetesResource[]): string[] {
|
|
return resources
|
|
.filter((resource) => resource.kind === "Deployment")
|
|
.map((resource) => resource.metadata?.name)
|
|
.filter((name): name is string => Boolean(name));
|
|
}
|
|
|
|
export async function runUp(build: boolean) {
|
|
const { project, compose, cwd } = ctx();
|
|
await assertManagedNamespace(project);
|
|
|
|
await new Listr<UpContext>(
|
|
[
|
|
{
|
|
title: "Read compose",
|
|
task: async (taskCtx, task) => {
|
|
taskCtx.compose = await compose();
|
|
task.output = `${Object.keys(taskCtx.compose.services ?? {}).length} services`;
|
|
},
|
|
},
|
|
{
|
|
title: "Build images",
|
|
rendererOptions: {
|
|
outputBar: 10,
|
|
persistentOutput: true,
|
|
},
|
|
enabled: () => build,
|
|
skip: (taskCtx) =>
|
|
Object.values(taskCtx.compose?.services ?? {}).some(
|
|
(service) => service.build,
|
|
)
|
|
? false
|
|
: "No buildable services",
|
|
task: async (taskCtx, task) => {
|
|
const built = await buildServices(project, taskCtx.compose!, cwd, {
|
|
progress: (message) => {
|
|
task.output = message;
|
|
},
|
|
stream: task.stdout(),
|
|
});
|
|
task.output = `Built ${built} image${built === 1 ? "" : "s"}`;
|
|
},
|
|
},
|
|
{
|
|
title: "Reconcile databases",
|
|
skip: (taskCtx) =>
|
|
getComposePostgresClaims(taskCtx.compose!).length > 0
|
|
? false
|
|
: "No managed postgres volumes",
|
|
task: async (taskCtx, task) => {
|
|
taskCtx.serviceEnv = mergeServiceEnv(
|
|
taskCtx.serviceEnv,
|
|
await reconcilePostgresClaims(project, taskCtx.compose!),
|
|
);
|
|
task.output = `${Object.keys(taskCtx.serviceEnv).length} service${Object.keys(taskCtx.serviceEnv).length === 1 ? "" : "s"}`;
|
|
},
|
|
},
|
|
{
|
|
title: "Reconcile S3 storage",
|
|
skip: (taskCtx) =>
|
|
getComposeS3Claims(taskCtx.compose!).length > 0
|
|
? false
|
|
: "No managed S3 volumes",
|
|
task: async (taskCtx, task) => {
|
|
taskCtx.serviceEnv = mergeServiceEnv(
|
|
taskCtx.serviceEnv,
|
|
await reconcileS3Claims(project, taskCtx.compose!),
|
|
);
|
|
task.output = `${Object.keys(taskCtx.serviceEnv).length} service${Object.keys(taskCtx.serviceEnv).length === 1 ? "" : "s"}`;
|
|
},
|
|
},
|
|
{
|
|
title: "Render manifests",
|
|
task: async (taskCtx, task) => {
|
|
taskCtx.resources = await composeToKubernetes(
|
|
project,
|
|
taskCtx.compose!,
|
|
cwd,
|
|
taskCtx.serviceEnv,
|
|
);
|
|
task.output = `${taskCtx.resources.length} resources`;
|
|
},
|
|
},
|
|
{
|
|
title: "Plan reconciliation",
|
|
task: async (taskCtx, task) => {
|
|
taskCtx.staleResources = (await getStaleResources(
|
|
project,
|
|
taskCtx.resources!,
|
|
)) as KubernetesResource[];
|
|
task.output = `${taskCtx.staleResources.length} stale resource${taskCtx.staleResources.length === 1 ? "" : "s"}`;
|
|
},
|
|
},
|
|
{
|
|
title: "Apply resources",
|
|
task: (taskCtx, task) => {
|
|
const resources = sortResources(taskCtx.resources!);
|
|
|
|
task.output = `${resources.length} resources queued`;
|
|
return task.newListr(
|
|
resources.map((resource) => ({
|
|
title: `${resource.kind} ${resource.metadata?.name}`,
|
|
task: () => applyResource(resource),
|
|
})),
|
|
{ concurrent: false, exitOnError: true },
|
|
);
|
|
},
|
|
},
|
|
{
|
|
title: "Restart deployments",
|
|
task: (taskCtx, task) => {
|
|
const deployments = getDeploymentNames(taskCtx.resources!);
|
|
|
|
task.output = `${deployments.length} deployments queued`;
|
|
return task.newListr(
|
|
deployments.map((name) => ({
|
|
title: `Deployment ${name}`,
|
|
task: () => restartDeployment(name),
|
|
})),
|
|
{ concurrent: false, exitOnError: true },
|
|
);
|
|
},
|
|
},
|
|
{
|
|
title: "Wait for rollout",
|
|
task: (taskCtx, task) => {
|
|
const deployments = getDeploymentNames(taskCtx.resources!);
|
|
|
|
task.output = `${deployments.length} deployments queued`;
|
|
return task.newListr(
|
|
deployments.map((name) => ({
|
|
title: `Deployment ${name}`,
|
|
task: () => waitForDeploymentRollout(name),
|
|
})),
|
|
{ concurrent: false, exitOnError: true },
|
|
);
|
|
},
|
|
},
|
|
{
|
|
title: "Delete stale resources",
|
|
skip: (taskCtx) =>
|
|
taskCtx.staleResources && taskCtx.staleResources.length > 0
|
|
? false
|
|
: "No stale resources",
|
|
task: (taskCtx, task) => {
|
|
const resources = sortResources(taskCtx.staleResources!).reverse();
|
|
|
|
task.output = `${resources.length} resources queued`;
|
|
return task.newListr(
|
|
resources.map((resource) => ({
|
|
title: `${resource.kind} ${resource.metadata?.name}`,
|
|
task: () => deleteResource(resource),
|
|
})),
|
|
{ concurrent: false, exitOnError: true },
|
|
);
|
|
},
|
|
},
|
|
],
|
|
{ rendererOptions: { collapseErrors: false } },
|
|
).run();
|
|
}
|
|
|
|
export const up = defineCommand({
|
|
meta: {
|
|
name: "up",
|
|
description: "Create and start deployments",
|
|
},
|
|
args: {
|
|
build: {
|
|
type: "boolean",
|
|
default: true,
|
|
alias: "b",
|
|
description: "Build images before starting deployments",
|
|
negativeDescription: "Don't build an image, even if it's policy",
|
|
},
|
|
},
|
|
async run({ args }) {
|
|
await runUp(args.build);
|
|
},
|
|
});
|