feat: initial lunarfront-manager app
This commit is contained in:
76
src/services/git.ts
Normal file
76
src/services/git.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { execSync } from "child_process";
|
||||
import { writeFileSync, mkdirSync, rmSync } from "fs";
|
||||
import { tmpdir } from "os";
|
||||
import { join } from "path";
|
||||
import { config } from "../lib/config";
|
||||
|
||||
function withRepo<T>(fn: (dir: string) => T): T {
|
||||
const keyPath = join(tmpdir(), `manager-ssh-key-${Date.now()}`);
|
||||
const dir = join(tmpdir(), `lunarfront-charts-${Date.now()}`);
|
||||
|
||||
writeFileSync(keyPath, config.gitSshKey, { mode: 0o600 });
|
||||
|
||||
const env = {
|
||||
...process.env,
|
||||
GIT_SSH_COMMAND: `ssh -i ${keyPath} -o StrictHostKeyChecking=no`,
|
||||
};
|
||||
|
||||
try {
|
||||
execSync(`git clone ${config.gitRepoUrl} ${dir}`, { env, stdio: "pipe" });
|
||||
execSync(`git -C ${dir} config user.email "manager@lunarfront.tech"`, { env });
|
||||
execSync(`git -C ${dir} config user.name "lunarfront-manager"`, { env });
|
||||
const result = fn(dir);
|
||||
execSync(`git -C ${dir} push origin main`, { env, stdio: "pipe" });
|
||||
return result;
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
rmSync(keyPath, { force: true });
|
||||
}
|
||||
}
|
||||
|
||||
export function addCustomerChart(slug: string, appVersion: string) {
|
||||
withRepo((dir) => {
|
||||
const manifest = buildArgoCDApp(slug, appVersion);
|
||||
writeFileSync(join(dir, "customers", `${slug}.yaml`), manifest);
|
||||
execSync(`git -C ${dir} add customers/${slug}.yaml`);
|
||||
execSync(`git -C ${dir} commit -m "feat: provision customer ${slug}"`, { env: process.env });
|
||||
});
|
||||
}
|
||||
|
||||
export function removeCustomerChart(slug: string) {
|
||||
withRepo((dir) => {
|
||||
rmSync(join(dir, "customers", `${slug}.yaml`), { force: true });
|
||||
execSync(`git -C ${dir} add customers/${slug}.yaml`);
|
||||
execSync(`git -C ${dir} commit -m "chore: deprovision customer ${slug}"`, { env: process.env });
|
||||
});
|
||||
}
|
||||
|
||||
function buildArgoCDApp(slug: string, appVersion: string): string {
|
||||
return `apiVersion: argoproj.io/v1alpha1
|
||||
kind: Application
|
||||
metadata:
|
||||
name: customer-${slug}
|
||||
namespace: argocd
|
||||
spec:
|
||||
project: default
|
||||
sources:
|
||||
- repoURL: git.lunarfront.tech/ryan/lunarfront-app
|
||||
chart: lunarfront
|
||||
targetRevision: "${appVersion}"
|
||||
helm:
|
||||
valueFiles:
|
||||
- $values/customers/${slug}.yaml
|
||||
- repoURL: ssh://git@git-ssh.lunarfront.tech/ryan/lunarfront-charts.git
|
||||
targetRevision: main
|
||||
ref: values
|
||||
destination:
|
||||
server: https://kubernetes.default.svc
|
||||
namespace: customer-${slug}
|
||||
syncPolicy:
|
||||
automated:
|
||||
prune: true
|
||||
selfHeal: true
|
||||
syncOptions:
|
||||
- CreateNamespace=true
|
||||
`;
|
||||
}
|
||||
Reference in New Issue
Block a user