1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#!/usr/bin/env -S deno run --allow-env --allow-net --allow-run --allow-read --allow-write
import { getRequiredEnv, getStdout, prependWith } from "@emprespresso/pengueno";
import { BitwardenSession, type SecureNote } from "@emprespresso/ci-utils";
import type { AnsiblePlaybookJobProps } from "@emprespresso/ci-model";
const args: AnsiblePlaybookJobProps = {
path: getRequiredEnv("path"),
playbooks: getRequiredEnv("playbooks"),
};
const logger = loggerWithPrefix(() => `[ansible_playbook."${args.playbooks}"]`);
const run = async () => {
logger.log(
"starting ansible playbook job~ (⑅˘꒳˘) let's configure all the things!",
);
const bitwardenSession = new BitwardenSession();
const secretFiles = await Promise.all(
["ansible_secrets", "ssh_key"]
.map((secretName) =>
bitwardenSession
.getItem<SecureNote>(secretName)
.then(async ({ notes: recoveredSecret }) => {
const tempFile = await Deno.makeTempFile();
await Deno.writeTextFile(tempFile, recoveredSecret);
logger.log(secretName, "safely tucked away at", tempFile, "(˘ω˘)");
return tempFile;
})
),
);
const [ansibleSecrets, sshKey] = secretFiles;
try {
const volumes = [
`${args.path}:/ansible`,
`${sshKey}:/root/id_rsa`,
`${ansibleSecrets}:/ansible/secrets.yml`,
];
const playbookCmd = `ansible-playbook -e @secrets.yml ${args.playbooks}`;
const deployCmd = [
"docker",
"run",
...prependWith(volumes, "-v"),
"willhallonline/ansible:latest",
...playbookCmd.split(" "),
];
logger.log("running ansible magic~ (◕ᴗ◕✿)", deployCmd);
await getStdout(deployCmd);
} finally {
await Promise.allSettled(
[bitwardenSession.close()].concat(
secretFiles.map((p) => {
logger.log(`tidying up`, p, "keeping things neat and tidy~");
return Deno.remove(p);
}),
),
);
}
logger.log("ansible playbook job all done! servers are happy now (。•̀ᴗ-)✧");
};
if (import.meta.main) {
try {
await run();
} catch (e) {
logger.error(
"oh nyo! ansible had a problem",
e,
"maybe next time? (´。﹏。`)",
);
throw e;
}
}
|