Skip to content

Commit

Permalink
feat: extract reusable component in secrets operator creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinnee committed Sep 8, 2024
1 parent fccb35c commit 89b28f9
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 103 deletions.
111 changes: 111 additions & 0 deletions src/books/secrets-operator-creation/generic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import type { CloudTreeCluster, ServiceTreeService } from '../../lib/service-tree-def.ts';
import { input, password } from '@inquirer/prompts';
import { $ } from 'bun';
import path from 'node:path';
import type { TaskRunner } from '../../tasks/tasks.ts';
import type { UtilPrompter } from '../../lib/prompts/util-prompter.ts';
import type { YamlManipulator } from '../../lib/utility/yaml-manipulator.ts';

class GenericSecretOperatorCreator {
constructor(
private task: TaskRunner,
private up: UtilPrompter,
private y: YamlManipulator,
private sulfoxide_infisical: ServiceTreeService,
) {}

async Run(cluster: CloudTreeCluster): Promise<void> {
const infisical = this.sulfoxide_infisical;

const i_path = `./platforms/${infisical.platform.slug}/${infisical.principal.slug}`;

await this.task.Run([
'Setup infisical',
async () => {
const pw = await password({ message: 'Enter your Bitwarden password' });

await $`echo ${pw} | nix develop -c pls setup`.cwd(i_path);
},
]);

// prompt to check if we want new secrets
const newSecrets = await this.up.YesNo(`Do you want to inject new secrets for ${cluster.principal.name}?`);
if (newSecrets) {
await this.task.Run([
'Inject new secrets',
async () => {
const token = await input({ message: `Enter your ${cluster.cloud.name} token` });

const yamlPath = path.join(i_path, 'bw.secrets.yaml');
await this.y.Mutate(yamlPath, [[['Tokens', cluster.cloud.name, cluster.principal.name], token]]);

console.log('✅ Secrets modified. Remember to update Bitwarden the new secrets');

let updated = false;

while (!updated) {
updated = await this.up.YesNo('Have you updated Bitwarden with the new secrets?');
}
},
]);
}

// synchronize secrets
await this.task.Run([
'Synchronize secrets',
async () => {
await $`nix develop -c pls sync`.cwd(i_path);
},
]);

await this.task.Run([
'Initialize general (database and ingress) Tofu',
async () => {
await $`pls general:init`.cwd(i_path);
},
]);

await this.task.Run([
'Apply general (database and ingress) Tofu',
async () => {
await $`pls general:apply -- -auto-approve`.cwd(i_path);
},
]);

await this.task.Run([
'Generate .env',
async () => {
await $`nix develop -c pls generate:env`.cwd(i_path);
},
]);

// provision compute
const compute = cluster.principal.slug;
await this.task.Run([
'Provision compute',
async () => {
await $`pls ${{ raw: compute }}:init`.cwd(i_path);
},
]);

await this.task.Run([
'Apply compute',
async () => {
await $`pls ${{ raw: compute }}:apply -- -auto-approve`.cwd(i_path);
},
]);

// deploy secrets operator
const c = cluster.principal.slug;
await this.task.Run([
'Deploy secrets operator',
async () => {
await $`nix develop -c pls deploy -- ${{ raw: c }}`.cwd(i_path).env({
ANSIBLE_HOST_KEY_CHECKING: 'False',
});
},
]);
}
}

export { GenericSecretOperatorCreator };
105 changes: 4 additions & 101 deletions src/books/secrets-operator-creation/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import type { CloudTreeCluster, ServiceTreeService } from '../../lib/service-tree-def.ts';
import type { TaskRunner } from '../../tasks/tasks.ts';
import type { CloudTreeCluster } from '../../lib/service-tree-def.ts';
import type { ServiceTreePrompter } from '../../lib/prompts/landscape.ts';
import { $ } from 'bun';
import type { UtilPrompter } from '../../lib/prompts/util-prompter.ts';
import { input, password } from '@inquirer/prompts';
import type { YamlManipulator } from '../../lib/utility/yaml-manipulator.ts';
import path from 'node:path';
import type { RunBook } from '../run-book.ts';
import { GenericSecretOperatorCreator } from './generic.ts';

class SecretsOperatorCreator implements RunBook {
constructor(
private task: TaskRunner,
private creator: GenericSecretOperatorCreator,
private stp: ServiceTreePrompter,
private up: UtilPrompter,
private y: YamlManipulator,
private sulfoxide_infisical: ServiceTreeService,
) {}

name: string = 'Create Secrets Operator';
Expand All @@ -26,96 +18,7 @@ class SecretsOperatorCreator implements RunBook {
'Which cluster do you want to create infisical in?',
);

const infisical = this.sulfoxide_infisical;

const i_path = `./platforms/${infisical.platform.slug}/${infisical.principal.slug}`;

await this.task.Run([
'Setup infisical',
async () => {
const pw = await password({ message: 'Enter your Bitwarden password' });

await $`echo ${pw} | nix develop -c pls setup`.cwd(i_path);
},
]);

// prompt to check if we want new secrets
const newSecrets = await this.up.YesNo(`Do you want to inject new secrets for ${cluster.principal.name}?`);
if (newSecrets) {
await this.task.Run([
'Inject new secrets',
async () => {
const token = await input({ message: `Enter your ${cluster.cloud.name} token` });

const yamlPath = path.join(i_path, 'bw.secrets.yaml');
await this.y.Mutate(yamlPath, [[['Tokens', cluster.cloud.name, cluster.principal.name], token]]);

console.log('✅ Secrets modified. Remember to update Bitwarden the new secrets');

let updated = false;

while (!updated) {
updated = await this.up.YesNo('Have you updated Bitwarden with the new secrets?');
}
},
]);
}

// synchronize secrets
await this.task.Run([
'Synchronize secrets',
async () => {
await $`nix develop -c pls sync`.cwd(i_path);
},
]);

await this.task.Run([
'Initialize general (database and ingress) Tofu',
async () => {
await $`pls general:init`.cwd(i_path);
},
]);

await this.task.Run([
'Apply general (database and ingress) Tofu',
async () => {
await $`pls general:apply -- -auto-approve`.cwd(i_path);
},
]);

await this.task.Run([
'Generate .env',
async () => {
await $`nix develop -c pls generate:env`.cwd(i_path);
},
]);

// provision compute
const compute = cluster.principal.slug;
await this.task.Run([
'Provision compute',
async () => {
await $`pls ${{ raw: compute }}:init`.cwd(i_path);
},
]);

await this.task.Run([
'Apply compute',
async () => {
await $`pls ${{ raw: compute }}:apply -- -auto-approve`.cwd(i_path);
},
]);

// deploy secrets operator
const c = cluster.principal.slug;
await this.task.Run([
'Deploy secrets operator',
async () => {
await $`nix develop -c pls deploy -- ${{ raw: c }}`.cwd(i_path).env({
ANSIBLE_HOST_KEY_CHECKING: 'False',
});
},
]);
await this.creator.Run(cluster);
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/books/secrets-operator-migration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { RunBook } from "../run-book.ts";

class SecretsOperatorMigrator implements RunBook {
constructor() {
}

name: string = "Migrate Secrets Operator";
desc: string = "Migrate the secrets operator to from one cloud-cluster to another";

async Run(): Promise<void> {

}
}

export { SecretsOperatorMigrator };
8 changes: 6 additions & 2 deletions src/init/runbooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { VultrPhysicalClusterCreator } from "../books/physical-cluster-creation/
import { VultrGracefulPhysicalClusterDestructor } from "../books/graceful-physical-cluster-destruction/vultr.ts";
import { SecretsOperatorDestructor } from "../books/secrets-operator-destruction";
import { SecretsOperatorCreator } from "../books/secrets-operator-creation";
import { GenericSecretOperatorCreator } from "../books/secrets-operator-creation/generic.ts";

function initRunBooks(d: Dependencies, t: TaskGenerator): RunBook[] {
const sulfoxide = SERVICE_TREE.sulfoxide;
Expand Down Expand Up @@ -180,13 +181,16 @@ function initRunBooks(d: Dependencies, t: TaskGenerator): RunBook[] {
);

// create secrets operator
const secretsOperatorCreator = new SecretsOperatorCreator(
const genericSecretOperatorCreator = new GenericSecretOperatorCreator(
d.taskRunner,
d.stp,
d.utilPrompter,
d.yamlManipulator,
sulfoxide.services.infisical
);
const secretsOperatorCreator = new SecretsOperatorCreator(
genericSecretOperatorCreator,
d.stp,
);

const secretsOperatorDestructor = new SecretsOperatorDestructor(
d.taskRunner,
Expand Down

0 comments on commit 89b28f9

Please sign in to comment.