Skip to content
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
33 changes: 33 additions & 0 deletions examples/local-op-quickstart/ops/azure-deploy.op.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Op, phase, activity, azGroupEnsure, build, azGroupDelete } from "@intentius/chant-lexicon-temporal";

/**
* Deploy an ARM template to Azure: ensure the resource group, build the template,
* apply it via `az deployment group create`, then delete the group (issue #707).
*
* `chant run azure-deploy` executes this in-process via the local executor.
* Requires the `az` CLI logged in (`az login`). chant emits the ARM template; the
* `arm` target of `nativeApply` runs `az deployment group create`, which needs the
* resource group to exist first — hence the `azGroupEnsure` phase.
*
* This is the real-Azure path. Local emulation against floci-az awaits an upstream
* `Microsoft.Resources/deployments` provider (#705).
*/
export default Op({
name: "azure-deploy",
overview: "az group ensure → build → arm deploy → group delete: a first-class Azure deploy",
taskQueue: "azure-deploy",
phases: [
phase("Group", [
azGroupEnsure("chant-rg", { location: "eastus" }),
]),
phase("Build", [
build("."),
]),
phase("Deploy", [
activity("nativeApply", { target: "arm", env: "chant-rg", output: "dist/stack.json" }),
]),
phase("Teardown", [
azGroupDelete("chant-rg"),
]),
],
});
2 changes: 2 additions & 0 deletions lexicons/temporal/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export {
k3dDown,
flociUp,
flociDown,
azGroupEnsure,
azGroupDelete,
policyGate,
} from "@intentius/chant/op";
export type { OpConfig, PhaseDefinition, StepDefinition, ActivityStep, GateStep } from "@intentius/chant/op";
22 changes: 22 additions & 0 deletions lexicons/temporal/src/op/activities/azure.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, test, expect } from "vitest";
import { azGroupEnsureCommand, azGroupDeleteCommand } from "./azure";

describe("azGroupEnsureCommand (#707)", () => {
test("creates the resource group in the given location, quietly", () => {
expect(azGroupEnsureCommand("chant-rg", "eastus")).toBe(
"az group create --name chant-rg --location eastus --output none",
);
});

test("honors a non-default location", () => {
expect(azGroupEnsureCommand("rg", "westeurope")).toContain("--location westeurope");
});
});

describe("azGroupDeleteCommand (#707)", () => {
test("force-deletes the group without blocking", () => {
expect(azGroupDeleteCommand("chant-rg")).toBe(
"az group delete --name chant-rg --yes --no-wait",
);
});
});
66 changes: 66 additions & 0 deletions lexicons/temporal/src/op/activities/azure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { exec } from "node:child_process";
import { promisify } from "node:util";

const execAsync = promisify(exec);

const DEFAULT_LOCATION = "eastus";

export interface AzGroupEnsureArgs {
/** Resource group name — the same value passed as `env` to a `arm` `nativeApply`. */
resourceGroup: string;
/** Azure region for the group. Default: `eastus`. */
location?: string;
}

export interface AzGroupDeleteArgs {
/** Resource group to delete. */
resourceGroup: string;
}

/**
* Build the `az group create` command. `az group create` is idempotent — a group
* that already exists is returned unchanged — so no existence check is needed.
* Pure — exported for testing.
*/
export function azGroupEnsureCommand(resourceGroup: string, location: string): string {
return `az group create --name ${resourceGroup} --location ${location} --output none`;
}

/** Build the `az group delete` command (non-blocking). Pure — exported for testing. */
export function azGroupDeleteCommand(resourceGroup: string): string {
return `az group delete --name ${resourceGroup} --yes --no-wait`;
}

/**
* Ensure an Azure resource group exists before an ARM deployment.
*
* `az deployment group create` (the `arm` target of `nativeApply`) fails if its
* resource group is absent — unlike CloudFormation, which creates its own stack.
* Run this before the apply. Uses fastIdempotent profile — 5m timeout.
*
* This targets real Azure (via the logged-in `az` context). Local emulation
* against floci-az awaits an upstream `Microsoft.Resources/deployments` provider
* (see #705); when that lands, an endpoint/custom-cloud override slots in here.
*/
export async function azGroupEnsure(args: AzGroupEnsureArgs, signal?: AbortSignal): Promise<void> {
const location = args.location ?? DEFAULT_LOCATION;
const { stdout, stderr } = await execAsync(
azGroupEnsureCommand(args.resourceGroup, location),
{ signal },
);
if (stdout) console.log(stdout);
if (stderr) console.error(stderr);
}

/**
* Delete an Azure resource group and everything in it (non-blocking). A no-op
* success when the group is already gone. Uses fastIdempotent profile — 5m
* timeout (`--no-wait` returns immediately).
*/
export async function azGroupDelete(args: AzGroupDeleteArgs, signal?: AbortSignal): Promise<void> {
try {
await execAsync(azGroupDeleteCommand(args.resourceGroup), { signal });
} catch {
// Group already absent — treat as success.
}
}
3 changes: 3 additions & 0 deletions lexicons/temporal/src/op/activities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export {
} from "./floci";
export type { FlociUpArgs, FlociDownArgs } from "./floci";

export { azGroupEnsure, azGroupDelete, azGroupEnsureCommand, azGroupDeleteCommand } from "./azure";
export type { AzGroupEnsureArgs, AzGroupDeleteArgs } from "./azure";

export { reconcilePr } from "./reconcile";
export type { ReconcilePrArgs, ReconcileResult, ReconcileMode, ReconcileEntry } from "./reconcile";

Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/op/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,23 @@ export const flociDown = (opts?: Record<string, unknown>): ActivityStep => {
return activity("flociDown", args, profile ?? "fastIdempotent");
};

/**
* Ensure an Azure resource group exists before an ARM apply. `az deployment
* group create` (the `arm` apply target) fails without its group, so place this
* before the deploy phase. Idempotent. Defaults to the `fastIdempotent` profile
* (override via `opts.profile`). `opts` accepts `location` (default `eastus`).
*/
export const azGroupEnsure = (resourceGroup: string, opts?: Record<string, unknown>): ActivityStep => {
const { args, profile } = takeProfile(opts);
return activity("azGroupEnsure", { resourceGroup, ...args }, profile ?? "fastIdempotent");
};

/** Delete an Azure resource group and its contents (non-blocking). Defaults to the `fastIdempotent` profile (override via `opts.profile`). */
export const azGroupDelete = (resourceGroup: string, opts?: Record<string, unknown>): ActivityStep => {
const { args, profile } = takeProfile(opts);
return activity("azGroupDelete", { resourceGroup, ...args }, profile ?? "fastIdempotent");
};

/**
* Gate an apply on organizational policy: build the project and run its
* `lint.policies` over the resolved resources, blocking the workflow on any
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/op/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { Op, phase, activity, gate, build, kubectlApply, helmInstall, waitForStack,
gitlabPipeline, lifecycleSnapshot, shell, teardown, k3dUp, k3dDown, flociUp, flociDown, policyGate } from "./builders";
gitlabPipeline, lifecycleSnapshot, shell, teardown, k3dUp, k3dDown, flociUp, flociDown,
azGroupEnsure, azGroupDelete, policyGate } from "./builders";
export { OpResource } from "./resource";
export type { OpConfig, PhaseDefinition, StepDefinition, ActivityStep, GateStep } from "./types";
export { discoverOps } from "./discover";
Expand Down