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

SecretsOp migration runbook #8

Merged
merged 3 commits into from
Sep 8, 2024
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
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
65 changes: 65 additions & 0 deletions src/books/secrets-operator-destruction/generic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { password } from '@inquirer/prompts';
import { $ } from 'bun';
import type { CloudTreeCluster, ServiceTreeService } from '../../lib/service-tree-def.ts';
import type { TaskRunner } from '../../tasks/tasks.ts';

class GenericSecretOperatorDestructor {
constructor(
private task: TaskRunner,
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);
},
]);

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

await this.task.Run([
'Destroy general (database and ingress) Tofu',
async () => {
await $`pls general:destroy`.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([
'Init Compute',
async () => {
await $`pls ${{ raw: compute }}:init`.cwd(i_path);
},
]);

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

export { GenericSecretOperatorDestructor };
59 changes: 4 additions & 55 deletions src/books/secrets-operator-destruction/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +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 { password } from '@inquirer/prompts';
import type { RunBook } from '../run-book.ts';
import type { GenericSecretOperatorDestructor } from './generic.ts';

class SecretsOperatorDestructor implements RunBook {
constructor(
private task: TaskRunner,
private destructor: GenericSecretOperatorDestructor,
private stp: ServiceTreePrompter,
private sulfoxide_infisical: ServiceTreeService,
) {}

name: string = 'Destroy Secrets Operator';
Expand All @@ -21,55 +18,7 @@ class SecretsOperatorDestructor 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);
},
]);

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

await this.task.Run([
'Destroy general (database and ingress) Tofu',
async () => {
await $`pls general:destroy`.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([
'Init Compute',
async () => {
await $`pls ${{ raw: compute }}:init`.cwd(i_path);
},
]);

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

Expand Down
Loading
Loading