mimir/source/main.ts

158 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-10-13 15:53:50 +02:00
/**
*/
function get_stamp(): string
{
let date: Date = (new Date(Date.now()));
return lib_plankton.string.coin(
// "{{year}}{{month}}{{day}}T{{hour}}{{minute}}{{second}}",
"{{year}}-{{month}}-{{day}}",
{
"year": date.getFullYear().toFixed(0).padStart(4, "0"),
"month": (date.getMonth() + 1).toFixed(0).padStart(2, "0"),
"day": date.getDate().toFixed(0).padStart(2, "0"),
"hour": date.getHours().toFixed(0).padStart(2, "0"),
"minute": date.getMinutes().toFixed(0).padStart(2, "0"),
"second": date.getSeconds().toFixed(0).padStart(2, "0"),
}
);
}
/**
*/
async function main(): Promise<void>
{
const conf = lib_plankton.json.decode(await lib_plankton.file.read("conf.json"));
const stamp: string = get_stamp();
const target_directory = (conf.target.directory + "/" + stamp);
let commands: Array<string> = [];
const commands_add : (command: string) => void = (command) => {
commands.push(command);
};
const commands_apply : () => void = () => {
// TODO
process.stdout.write(commands.join("\n") + "\n");
};
commands_add(
lib_plankton.string.coin(
"mkdir --parents {{directory}}",
{
"directory": target_directory,
}
)
);
commands_add(
""
);
for await (const concern of conf.concerns) {
if (! concern.active) {
// do nothing
}
else {
commands_add(
lib_plankton.string.coin(
"# {{name}}",
{
"name": concern.name,
"kind": concern.kind,
}
)
);
commands_add(
lib_plankton.string.coin(
"echo '-- {{name}}' > /dev/stderr",
{
"name": concern.name,
"kind": concern.kind,
}
)
);
switch (concern.kind) {
case "postgresql_dump": {
const password_file_path: string = "${HOME}/.pgpass";
commands_add(
lib_plankton.string.coin(
"echo '{{host}}:{{port}}:{{schema}}:{{username}}:{{password}}' > {{path}} && chmod 0600 {{path}}",
{
"path": password_file_path,
"host": concern.parameters.credentials.host,
"port": concern.parameters.credentials.port.toFixed(0),
"username": concern.parameters.credentials.username,
"password": concern.parameters.credentials.password,
"schema": concern.parameters.credentials.schema,
}
)
);
commands_add(
lib_plankton.string.coin(
"pg_dump --host={{host}} --port={{port}} --username={{username}} {{schema}} > {{target_path}}",
{
"host": concern.parameters.credentials.host,
"port": concern.parameters.credentials.port.toFixed(0),
"username": concern.parameters.credentials.username,
"schema": concern.parameters.credentials.schema,
"target_path": lib_plankton.string.coin(
"{{directory}}/{{name}}.sql",
{
"directory": target_directory,
"name": concern.parameters.name,
}
),
}
)
);
commands_add(
lib_plankton.string.coin(
"rm {{path}}",
{
"path": password_file_path,
}
)
);
break;
}
case "files": {
commands_add(
lib_plankton.string.coin(
"tar --create --directory={{path}} . > {{target_path}}",
{
"path": concern.parameters.path,
"target_path": lib_plankton.string.coin(
"{{directory}}/{{name}}.tar",
{
"directory": target_directory,
"name": concern.parameters.name,
}
),
}
)
);
break;
}
default: {
throw (new Error("unhandled kind: " + concern.kind));
break;
}
}
commands_add(
""
);
}
}
commands_add(
lib_plankton.string.coin(
"echo '{{directory}}'",
{
"directory": target_directory,
}
)
);
commands_apply();
return Promise.resolve<void>(undefined);
}
main();