@opencrvs/toolkit 2.2.0-rc.1a111f2 → 2.2.0-rc.25c3c81
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build.sh +15 -2
- package/dist/application-config/index.js +16 -5
- package/dist/authentication/index.d.ts +1 -1
- package/dist/authentication/index.js +4 -8
- package/dist/cli.js +1109 -25
- package/dist/commons/api/router.d.ts +13 -0
- package/dist/commons/application-config/index.d.ts +1 -1
- package/dist/commons/conditionals/conditionals.d.ts +4 -4
- package/dist/commons/conditionals/validate.d.ts +1 -1
- package/dist/commons/events/ActionStatus.d.ts +2 -2
- package/dist/commons/events/EventConfig.d.ts +66 -5
- package/dist/commons/events/EventDocument.d.ts +2 -0
- package/dist/commons/events/EventIndex.d.ts +1 -0
- package/dist/commons/events/EventInput.d.ts +1 -0
- package/dist/commons/events/EventMetadata.d.ts +1 -0
- package/dist/commons/events/FieldTypeMapping.d.ts +19 -19
- package/dist/commons/events/PlainDate.d.ts +5 -0
- package/dist/commons/events/defineConfig.d.ts +1 -1
- package/dist/commons/events/locations.d.ts +1 -1
- package/dist/commons/events/scopes.d.ts +1 -1
- package/dist/commons/events/state/index.d.ts +1 -0
- package/dist/commons/events/test.utils.d.ts +2 -1
- package/dist/commons/events/utils.d.ts +15 -2
- package/dist/commons/users/Role.d.ts +23 -0
- package/dist/commons/users/User.d.ts +263 -0
- package/dist/commons/users/authentication.d.ts +67 -0
- package/dist/commons/users/index.d.ts +5 -0
- package/dist/commons/users/roleValidations.d.ts +15 -0
- package/dist/commons/users/scopes.d.ts +1402 -0
- package/dist/commons/users/userReferences.d.ts +176 -0
- package/dist/conditionals/index.js +3 -3
- package/dist/configurator/index.d.ts +24 -0
- package/dist/configurator/index.d.ts.map +1 -0
- package/dist/configurator/index.js +161 -0
- package/dist/configurator/layout.d.ts +28 -0
- package/dist/configurator/layout.d.ts.map +1 -0
- package/dist/configurator/layout.js +78 -0
- package/dist/configurator/paths.d.ts +13 -0
- package/dist/configurator/paths.d.ts.map +1 -0
- package/dist/configurator/paths.js +48 -0
- package/dist/configurator/server/environments.d.ts +20 -0
- package/dist/configurator/server/environments.d.ts.map +1 -0
- package/dist/configurator/server/environments.js +143 -0
- package/dist/configurator/server/files.d.ts +11 -0
- package/dist/configurator/server/files.d.ts.map +1 -0
- package/dist/configurator/server/files.js +67 -0
- package/dist/configurator/server/github.d.ts +56 -0
- package/dist/configurator/server/github.d.ts.map +1 -0
- package/dist/configurator/server/github.js +239 -0
- package/dist/configurator/server/plugin.d.ts +39 -0
- package/dist/configurator/server/plugin.d.ts.map +1 -0
- package/dist/configurator/server/plugin.js +298 -0
- package/dist/configurator/server/seal.d.ts +2 -0
- package/dist/configurator/server/seal.d.ts.map +1 -0
- package/dist/configurator/server/seal.js +36 -0
- package/dist/configurator/server/sshKeys.d.ts +5 -0
- package/dist/configurator/server/sshKeys.d.ts.map +1 -0
- package/dist/configurator/server/sshKeys.js +28 -0
- package/dist/configurator/server/start.d.ts +12 -0
- package/dist/configurator/server/start.d.ts.map +1 -0
- package/dist/configurator/server/start.js +91 -0
- package/dist/configurator/testing.d.ts +11 -0
- package/dist/configurator/testing.d.ts.map +1 -0
- package/dist/configurator/testing.js +74 -0
- package/dist/configurator/ui/bindings/bindings.d.ts +35 -0
- package/dist/configurator/ui/bindings/bindings.d.ts.map +1 -0
- package/dist/configurator/ui/bindings/bindings.js +5 -0
- package/dist/configurator/ui/bindings/parsers.d.ts +7 -0
- package/dist/configurator/ui/bindings/parsers.d.ts.map +1 -0
- package/dist/configurator/ui/bindings/parsers.js +85 -0
- package/dist/configurator/ui/form/users.d.ts +11 -0
- package/dist/configurator/ui/form/users.d.ts.map +1 -0
- package/dist/configurator/ui/form/users.js +33 -0
- package/dist/configurator/ui/plan/dependencyHosts.d.ts +2 -0
- package/dist/configurator/ui/plan/dependencyHosts.d.ts.map +1 -0
- package/dist/configurator/ui/plan/dependencyHosts.js +36 -0
- package/dist/configurator/ui/plan/templateValues.d.ts +11 -0
- package/dist/configurator/ui/plan/templateValues.d.ts.map +1 -0
- package/dist/configurator/ui/plan/templateValues.js +70 -0
- package/dist/configurator/workflows.d.ts +53 -0
- package/dist/configurator/workflows.d.ts.map +1 -0
- package/dist/configurator/workflows.js +161 -0
- package/dist/configurator-assets/forms/bindings.yml +263 -0
- package/dist/configurator-assets/templates/README.md +18 -0
- package/dist/configurator-assets/templates/charts-values/dependencies/values.override.yaml +4 -0
- package/dist/configurator-assets/templates/charts-values/dependencies/values.yaml +99 -0
- package/dist/configurator-assets/templates/charts-values/opencrvs-services/values.override.yaml +4 -0
- package/dist/configurator-assets/templates/charts-values/opencrvs-services/values.yaml +83 -0
- package/dist/configurator-assets/templates/charts-values/opentelemetry/values.yaml +102 -0
- package/dist/configurator-assets/templates/charts-values/traefik/values.override.yaml +4 -0
- package/dist/configurator-assets/templates/charts-values/traefik/values.yaml +130 -0
- package/dist/configurator-assets/templates/inventory/inventory.template.yml +98 -0
- package/dist/configurator-assets/ui/apple-touch-icon.png +0 -0
- package/dist/configurator-assets/ui/assets/index-Bf3eC7uO.js +169 -0
- package/dist/configurator-assets/ui/assets/index-CaqYFJUk.css +6 -0
- package/dist/configurator-assets/ui/assets/opencrvs-logo-sIjz08HT.svg +1 -0
- package/dist/configurator-assets/ui/assets/poppins-400-BwTUGeWf.woff2 +0 -0
- package/dist/configurator-assets/ui/assets/poppins-500-CWICHMj6.woff2 +0 -0
- package/dist/configurator-assets/ui/assets/poppins-600-DJVo7c07.woff2 +0 -0
- package/dist/configurator-assets/ui/assets/poppins-700-g9MkRaG0.woff2 +0 -0
- package/dist/configurator-assets/ui/favicon-32x32.png +0 -0
- package/dist/configurator-assets/ui/index.html +25 -0
- package/dist/environment/setup-environment.d.ts.map +1 -1
- package/dist/environment/setup-environment.js +6 -0
- package/dist/events/index.js +188 -65
- package/dist/notification/index.js +124 -48
- package/dist/roles/index.d.ts +2 -0
- package/dist/roles/index.d.ts.map +1 -0
- package/dist/roles/index.js +730 -0
- package/dist/scopes/index.d.ts +0 -10
- package/dist/scopes/index.js +3 -7
- package/dist/templates/charts-values/dependencies/values.yaml +2 -0
- package/opencrvs-toolkit-2.2.0-rc.25c3c81.tgz +0 -0
- package/package.json +15 -3
- package/tsconfig.build.json +11 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.json +15 -6
- package/opencrvs-toolkit-2.2.0-rc.1a111f2.tgz +0 -0
package/dist/cli.js
CHANGED
|
@@ -2396,6 +2396,7 @@ async function runSetupEnvironment() {
|
|
|
2396
2396
|
existingValues
|
|
2397
2397
|
);
|
|
2398
2398
|
const rustfs_host_data_path = existingRustfsHostDataPath?.value || (environment_exists ? "/data/minio" : "/data/rustfs");
|
|
2399
|
+
const rustfs_pvc_name = rustfs_host_data_path.endsWith("/minio") ? "minio" : "rustfs";
|
|
2399
2400
|
let backupHost = findExistingValue("BACKUP_HOST", "VARIABLE", "ENVIRONMENT", existingValues)?.value || "";
|
|
2400
2401
|
let backupType = "";
|
|
2401
2402
|
let restoreEnvironmentName = findExistingValue(
|
|
@@ -3145,6 +3146,7 @@ async function runSetupEnvironment() {
|
|
|
3145
3146
|
two_fa_enabled,
|
|
3146
3147
|
monitoring_enabled,
|
|
3147
3148
|
rustfs_host_data_path,
|
|
3149
|
+
rustfs_pvc_name,
|
|
3148
3150
|
traefik_mode: traefikConfOption,
|
|
3149
3151
|
restore_enabled: restoreEnvironmentName ? true : false,
|
|
3150
3152
|
restore_environment_name: restoreEnvironmentName || "",
|
|
@@ -3816,24 +3818,24 @@ function isAction(literal, actionType) {
|
|
|
3816
3818
|
return import_ts_morph.Node.isPropertyAssignment(type) && type.getInitializerOrThrow().getType().getLiteralValue() === actionType;
|
|
3817
3819
|
}
|
|
3818
3820
|
function indentationOf(node) {
|
|
3819
|
-
const
|
|
3820
|
-
const lineStart =
|
|
3821
|
-
return /^[ \t]*/.exec(
|
|
3821
|
+
const text2 = node.getSourceFile().getFullText();
|
|
3822
|
+
const lineStart = text2.lastIndexOf("\n", node.getStart() - 1) + 1;
|
|
3823
|
+
return /^[ \t]*/.exec(text2.slice(lineStart))?.[0] ?? "";
|
|
3822
3824
|
}
|
|
3823
|
-
function restOfLine(
|
|
3824
|
-
const lineEnd =
|
|
3825
|
-
return
|
|
3825
|
+
function restOfLine(text2, position) {
|
|
3826
|
+
const lineEnd = text2.indexOf("\n", position);
|
|
3827
|
+
return text2.slice(position, lineEnd === -1 ? text2.length : lineEnd);
|
|
3826
3828
|
}
|
|
3827
|
-
function isOnlyComments(
|
|
3828
|
-
return /^\s*(?:\/\/.*|\/\*.*?\*\/\s*)*$/.test(
|
|
3829
|
+
function isOnlyComments(text2) {
|
|
3830
|
+
return /^\s*(?:\/\/.*|\/\*.*?\*\/\s*)*$/.test(text2);
|
|
3829
3831
|
}
|
|
3830
3832
|
function layoutOf(property) {
|
|
3831
|
-
const
|
|
3833
|
+
const text2 = property.getSourceFile().getFullText();
|
|
3832
3834
|
const next = property.getNextSibling();
|
|
3833
3835
|
const hasComma = next?.getKind() === import_ts_morph.SyntaxKind.CommaToken;
|
|
3834
3836
|
const end = next && hasComma ? next.getEnd() : property.getEnd();
|
|
3835
|
-
const trailing = restOfLine(
|
|
3836
|
-
const lineBreak =
|
|
3837
|
+
const trailing = restOfLine(text2, end);
|
|
3838
|
+
const lineBreak = text2.slice(property.getFullStart(), property.getStart()).indexOf("\n");
|
|
3837
3839
|
if (lineBreak === -1 || !isOnlyComments(trailing)) {
|
|
3838
3840
|
return {
|
|
3839
3841
|
ownsLines: false,
|
|
@@ -3846,21 +3848,21 @@ function layoutOf(property) {
|
|
|
3846
3848
|
const firstLine = property.getFullStart() + lineBreak + 1;
|
|
3847
3849
|
return {
|
|
3848
3850
|
ownsLines: true,
|
|
3849
|
-
start: firstLine + (/^\s*/.exec(
|
|
3851
|
+
start: firstLine + (/^\s*/.exec(text2.slice(firstLine))?.[0].length ?? 0),
|
|
3850
3852
|
end,
|
|
3851
3853
|
hasComma,
|
|
3852
3854
|
trailingComment: trailing.trimEnd()
|
|
3853
3855
|
};
|
|
3854
3856
|
}
|
|
3855
|
-
function insertAt(position,
|
|
3856
|
-
return { start: position, end: position, text };
|
|
3857
|
+
function insertAt(position, text2) {
|
|
3858
|
+
return { start: position, end: position, text: text2 };
|
|
3857
3859
|
}
|
|
3858
3860
|
function applyTextEdits(sourceFile, edits) {
|
|
3859
|
-
const
|
|
3861
|
+
const text2 = [...edits].sort((a, b) => b.start - a.start).reduce(
|
|
3860
3862
|
(result, edit) => result.slice(0, edit.start) + edit.text + result.slice(edit.end),
|
|
3861
3863
|
sourceFile.getFullText()
|
|
3862
3864
|
);
|
|
3863
|
-
sourceFile.replaceWithText(
|
|
3865
|
+
sourceFile.replaceWithText(text2);
|
|
3864
3866
|
}
|
|
3865
3867
|
function findToolkitImport(sourceFile) {
|
|
3866
3868
|
return sourceFile.getImportDeclaration(
|
|
@@ -3941,7 +3943,7 @@ function findDeclareAction(config) {
|
|
|
3941
3943
|
}
|
|
3942
3944
|
function insertIntoDeclareAction(declareAction, property) {
|
|
3943
3945
|
const anchor = declareAction.getProperty("label") ?? declareAction.getPropertyOrThrow("type");
|
|
3944
|
-
const
|
|
3946
|
+
const text2 = property.getSourceFile().getFullText();
|
|
3945
3947
|
const anchorLayout = layoutOf(anchor);
|
|
3946
3948
|
const layout = layoutOf(property);
|
|
3947
3949
|
const indent = indentationOf(anchor);
|
|
@@ -3950,8 +3952,8 @@ function insertIntoDeclareAction(declareAction, property) {
|
|
|
3950
3952
|
(line2, index) => index > 0 && line2.startsWith(oldIndent) ? indent + line2.slice(oldIndent.length) : line2
|
|
3951
3953
|
).join("\n");
|
|
3952
3954
|
if (anchorLayout.ownsLines) {
|
|
3953
|
-
const moved = reindent(
|
|
3954
|
-
const position = anchorLayout.end + restOfLine(
|
|
3955
|
+
const moved = reindent(text2.slice(layout.start, property.getEnd()));
|
|
3956
|
+
const position = anchorLayout.end + restOfLine(text2, anchorLayout.end).length;
|
|
3955
3957
|
const insertion = `
|
|
3956
3958
|
${indent}${moved}${anchorLayout.hasComma ? "," : ""}${layout.trailingComment}`;
|
|
3957
3959
|
if (anchorLayout.hasComma) {
|
|
@@ -3959,7 +3961,7 @@ ${indent}${moved}${anchorLayout.hasComma ? "," : ""}${layout.trailingComment}`;
|
|
|
3959
3961
|
}
|
|
3960
3962
|
return position === anchor.getEnd() ? [insertAt(position, `,${insertion}`)] : [insertAt(anchor.getEnd(), ","), insertAt(position, insertion)];
|
|
3961
3963
|
}
|
|
3962
|
-
const droppedComments =
|
|
3964
|
+
const droppedComments = text2.slice(layout.start, property.getStart(true)).trim() !== "" || layout.trailingComment !== "";
|
|
3963
3965
|
if (droppedComments) {
|
|
3964
3966
|
console.warn(
|
|
3965
3967
|
` [${location(property)}] The comments next to \`${DECLARATION_PROPERTY_NAME}\` were not moved with it. Move them onto the ${DECLARE_ACTION_TYPE} action by hand.`
|
|
@@ -3973,13 +3975,13 @@ ${indent}` : ", ";
|
|
|
3973
3975
|
];
|
|
3974
3976
|
}
|
|
3975
3977
|
function removeFromConfig(property) {
|
|
3976
|
-
const
|
|
3978
|
+
const text2 = property.getSourceFile().getFullText();
|
|
3977
3979
|
const layout = layoutOf(property);
|
|
3978
3980
|
const previousComma = property.getPreviousSiblings().find((sibling) => !isOnlyComments(sibling.getText()))?.asKind(import_ts_morph2.SyntaxKind.CommaToken);
|
|
3979
3981
|
if (layout.ownsLines) {
|
|
3980
3982
|
const lines = {
|
|
3981
|
-
start:
|
|
3982
|
-
end: layout.end + restOfLine(
|
|
3983
|
+
start: text2.lastIndexOf("\n", layout.start - 1),
|
|
3984
|
+
end: layout.end + restOfLine(text2, layout.end).length,
|
|
3983
3985
|
text: ""
|
|
3984
3986
|
};
|
|
3985
3987
|
return !layout.hasComma && previousComma ? [
|
|
@@ -4140,11 +4142,11 @@ function findNotifyAction(config, actions) {
|
|
|
4140
4142
|
}
|
|
4141
4143
|
function insertIntoAction(action, properties) {
|
|
4142
4144
|
const anchor = action.getProperty("label") ?? action.getPropertyOrThrow("type");
|
|
4143
|
-
const
|
|
4145
|
+
const text2 = action.getSourceFile().getFullText();
|
|
4144
4146
|
const anchorLayout = layoutOf(anchor);
|
|
4145
4147
|
const indent = indentationOf(anchor);
|
|
4146
4148
|
if (anchorLayout.ownsLines) {
|
|
4147
|
-
const position = anchorLayout.end + restOfLine(
|
|
4149
|
+
const position = anchorLayout.end + restOfLine(text2, anchorLayout.end).length;
|
|
4148
4150
|
const lines = properties.map((property) => `
|
|
4149
4151
|
${indent}${property}`);
|
|
4150
4152
|
if (anchorLayout.hasComma) {
|
|
@@ -4903,6 +4905,1028 @@ async function runEnvironmentSwarmToK8s() {
|
|
|
4903
4905
|
await Promise.resolve().then(() => (init_swarm_to_k8s(), swarm_to_k8s_exports));
|
|
4904
4906
|
}
|
|
4905
4907
|
|
|
4908
|
+
// src/configurator/index.ts
|
|
4909
|
+
var import_node_fs6 = require("node:fs");
|
|
4910
|
+
var import_node_path8 = require("node:path");
|
|
4911
|
+
var import_node_util = require("node:util");
|
|
4912
|
+
|
|
4913
|
+
// src/configurator/layout.ts
|
|
4914
|
+
var import_node_fs2 = require("node:fs");
|
|
4915
|
+
var import_node_path2 = require("node:path");
|
|
4916
|
+
|
|
4917
|
+
// src/configurator/paths.ts
|
|
4918
|
+
var import_node_fs = require("node:fs");
|
|
4919
|
+
var import_node_path = require("node:path");
|
|
4920
|
+
var OLD_INVENTORY_DIR2 = (0, import_node_path.join)(
|
|
4921
|
+
"infrastructure",
|
|
4922
|
+
"server-setup",
|
|
4923
|
+
"inventory"
|
|
4924
|
+
);
|
|
4925
|
+
var ENVIRONMENTS_DIR = "environments";
|
|
4926
|
+
var WORKFLOWS_DIR = (0, import_node_path.join)(".github", "workflows");
|
|
4927
|
+
var INVENTORY_FILE_NAME2 = "inventory.yml";
|
|
4928
|
+
function environmentInventoryPath(environment) {
|
|
4929
|
+
return (0, import_node_path.join)(ENVIRONMENTS_DIR, environment, INVENTORY_FILE_NAME2);
|
|
4930
|
+
}
|
|
4931
|
+
function isDirectory2(path15) {
|
|
4932
|
+
try {
|
|
4933
|
+
return (0, import_node_fs.statSync)(path15).isDirectory();
|
|
4934
|
+
} catch {
|
|
4935
|
+
return false;
|
|
4936
|
+
}
|
|
4937
|
+
}
|
|
4938
|
+
function assertInfrastructureRepository(root, required = [
|
|
4939
|
+
WORKFLOWS_DIR,
|
|
4940
|
+
ENVIRONMENTS_DIR,
|
|
4941
|
+
"infrastructure"
|
|
4942
|
+
]) {
|
|
4943
|
+
const missing = required.filter(
|
|
4944
|
+
(directory) => !isDirectory2((0, import_node_path.resolve)(root, directory))
|
|
4945
|
+
);
|
|
4946
|
+
if (missing.length > 0) {
|
|
4947
|
+
throw new Error(
|
|
4948
|
+
`The current directory is not a valid OpenCRVS infrastructure repository. Missing required director${missing.length === 1 ? "y" : "ies"}: ${missing.join(", ")}`
|
|
4949
|
+
);
|
|
4950
|
+
}
|
|
4951
|
+
}
|
|
4952
|
+
|
|
4953
|
+
// src/configurator/layout.ts
|
|
4954
|
+
function findOldInventoryFiles2(root) {
|
|
4955
|
+
const directory = (0, import_node_path2.join)(root, OLD_INVENTORY_DIR2);
|
|
4956
|
+
if (!isDirectory2(directory)) return [];
|
|
4957
|
+
return (0, import_node_fs2.readdirSync)(directory, { withFileTypes: true }).filter((entry) => entry.isFile() && entry.name.endsWith(".yml")).map((entry) => (0, import_node_path2.join)(OLD_INVENTORY_DIR2, entry.name)).sort();
|
|
4958
|
+
}
|
|
4959
|
+
function migrateLayout(root) {
|
|
4960
|
+
const oldFiles = findOldInventoryFiles2(root);
|
|
4961
|
+
const result = {
|
|
4962
|
+
moved: [],
|
|
4963
|
+
skipped: [],
|
|
4964
|
+
oldDirectoryRemoved: false
|
|
4965
|
+
};
|
|
4966
|
+
if (oldFiles.length === 0) return result;
|
|
4967
|
+
for (const from of oldFiles) {
|
|
4968
|
+
const environment = (0, import_node_path2.basename)(from, ".yml");
|
|
4969
|
+
const move = {
|
|
4970
|
+
environment,
|
|
4971
|
+
from,
|
|
4972
|
+
to: environmentInventoryPath(environment)
|
|
4973
|
+
};
|
|
4974
|
+
if ((0, import_node_fs2.existsSync)((0, import_node_path2.join)(root, move.to))) {
|
|
4975
|
+
result.skipped.push(move);
|
|
4976
|
+
continue;
|
|
4977
|
+
}
|
|
4978
|
+
(0, import_node_fs2.mkdirSync)((0, import_node_path2.dirname)((0, import_node_path2.join)(root, move.to)), { recursive: true });
|
|
4979
|
+
(0, import_node_fs2.renameSync)((0, import_node_path2.join)(root, move.from), (0, import_node_path2.join)(root, move.to));
|
|
4980
|
+
result.moved.push(move);
|
|
4981
|
+
}
|
|
4982
|
+
const oldDirectory = (0, import_node_path2.join)(root, OLD_INVENTORY_DIR2);
|
|
4983
|
+
try {
|
|
4984
|
+
if ((0, import_node_fs2.readdirSync)(oldDirectory).length === 0) {
|
|
4985
|
+
(0, import_node_fs2.rmdirSync)(oldDirectory);
|
|
4986
|
+
result.oldDirectoryRemoved = true;
|
|
4987
|
+
}
|
|
4988
|
+
} catch {
|
|
4989
|
+
}
|
|
4990
|
+
return result;
|
|
4991
|
+
}
|
|
4992
|
+
function assertNoOldInventoryLayout2(root) {
|
|
4993
|
+
if (findOldInventoryFiles2(root).length === 0) return;
|
|
4994
|
+
throw new Error(
|
|
4995
|
+
`Old inventory layout detected at ${OLD_INVENTORY_DIR2}. Please run \`opencrvs configurator upgrade\` before \`opencrvs configurator init\`.`
|
|
4996
|
+
);
|
|
4997
|
+
}
|
|
4998
|
+
|
|
4999
|
+
// src/configurator/server/start.ts
|
|
5000
|
+
var import_node_http = require("node:http");
|
|
5001
|
+
var import_node_fs4 = require("node:fs");
|
|
5002
|
+
var import_promises4 = require("node:fs/promises");
|
|
5003
|
+
var import_node_path6 = require("node:path");
|
|
5004
|
+
var import_yaml = require("yaml");
|
|
5005
|
+
|
|
5006
|
+
// src/configurator/server/plugin.ts
|
|
5007
|
+
var import_node_fs3 = require("node:fs");
|
|
5008
|
+
var import_node_path5 = require("node:path");
|
|
5009
|
+
|
|
5010
|
+
// src/configurator/server/environments.ts
|
|
5011
|
+
var import_promises2 = require("node:fs/promises");
|
|
5012
|
+
var import_node_path3 = require("node:path");
|
|
5013
|
+
var ENVIRONMENT_FILES = ["inventory.yml", "dependencies/values.yaml", "traefik/values.yaml", "opencrvs-services/values.yaml"];
|
|
5014
|
+
var INVENTORY = ENVIRONMENT_FILES[0];
|
|
5015
|
+
var YAML_FILE = /\.ya?ml$/;
|
|
5016
|
+
var SAFE_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
5017
|
+
async function listEnvironmentNames(dir) {
|
|
5018
|
+
let entries;
|
|
5019
|
+
try {
|
|
5020
|
+
entries = await (0, import_promises2.readdir)(dir, { withFileTypes: true });
|
|
5021
|
+
} catch {
|
|
5022
|
+
return { names: [], layout: "none" };
|
|
5023
|
+
}
|
|
5024
|
+
const folders = entries.filter((entry) => entry.isDirectory() && !entry.name.startsWith("."));
|
|
5025
|
+
if (folders.length > 0) {
|
|
5026
|
+
return { names: folders.map((entry) => entry.name).sort(), layout: "folders" };
|
|
5027
|
+
}
|
|
5028
|
+
const files = entries.filter((entry) => entry.isFile() && YAML_FILE.test(entry.name));
|
|
5029
|
+
if (files.length > 0) {
|
|
5030
|
+
return { names: files.map((entry) => entry.name.replace(YAML_FILE, "")).sort(), layout: "files" };
|
|
5031
|
+
}
|
|
5032
|
+
return { names: [], layout: "none" };
|
|
5033
|
+
}
|
|
5034
|
+
async function readIfPresent(path15) {
|
|
5035
|
+
try {
|
|
5036
|
+
return await (0, import_promises2.readFile)(path15, "utf8");
|
|
5037
|
+
} catch {
|
|
5038
|
+
return void 0;
|
|
5039
|
+
}
|
|
5040
|
+
}
|
|
5041
|
+
async function readEnvironmentFiles(dir, name, flatFile = INVENTORY) {
|
|
5042
|
+
if (!SAFE_NAME.test(name)) return void 0;
|
|
5043
|
+
const { names, layout } = await listEnvironmentNames(dir);
|
|
5044
|
+
if (!names.includes(name)) return void 0;
|
|
5045
|
+
const files = {};
|
|
5046
|
+
if (layout === "files") {
|
|
5047
|
+
for (const extension of [".yml", ".yaml"]) {
|
|
5048
|
+
const text2 = await readIfPresent((0, import_node_path3.join)(dir, `${name}${extension}`));
|
|
5049
|
+
if (text2 !== void 0) files[flatFile] = text2;
|
|
5050
|
+
}
|
|
5051
|
+
return files;
|
|
5052
|
+
}
|
|
5053
|
+
for (const file of ENVIRONMENT_FILES) {
|
|
5054
|
+
const text2 = await readIfPresent((0, import_node_path3.join)(dir, name, file));
|
|
5055
|
+
if (text2 !== void 0) files[file] = text2;
|
|
5056
|
+
}
|
|
5057
|
+
return files;
|
|
5058
|
+
}
|
|
5059
|
+
async function fileTargets(dir, name, flatFile = INVENTORY) {
|
|
5060
|
+
const { layout } = await listEnvironmentNames(dir);
|
|
5061
|
+
const targets = {};
|
|
5062
|
+
if (layout === "files") {
|
|
5063
|
+
const hasYaml = await readIfPresent((0, import_node_path3.join)(dir, `${name}.yaml`)) !== void 0;
|
|
5064
|
+
const hasYml = await readIfPresent((0, import_node_path3.join)(dir, `${name}.yml`)) !== void 0;
|
|
5065
|
+
const extension = hasYml && !hasYaml ? ".yml" : ".yaml";
|
|
5066
|
+
for (const path15 of ENVIRONMENT_FILES) targets[path15] = path15 === flatFile ? `${name}${extension}` : null;
|
|
5067
|
+
} else {
|
|
5068
|
+
for (const path15 of ENVIRONMENT_FILES) targets[path15] = `${name}/${path15}`;
|
|
5069
|
+
}
|
|
5070
|
+
return { layout, targets };
|
|
5071
|
+
}
|
|
5072
|
+
function parseGitRemote(url) {
|
|
5073
|
+
const match = url.trim().match(/github\.com[:/]([^/\s]+)\/([^/\s]+?)(?:\.git)?\/?$/);
|
|
5074
|
+
return match ? { owner: match[1], repo: match[2] } : null;
|
|
5075
|
+
}
|
|
5076
|
+
async function readGitRemote(cwd) {
|
|
5077
|
+
const config = await readIfPresent((0, import_node_path3.join)(cwd, ".git", "config"));
|
|
5078
|
+
if (config === void 0) return void 0;
|
|
5079
|
+
let inOrigin = false;
|
|
5080
|
+
for (const line2 of config.split(/\r?\n/)) {
|
|
5081
|
+
const section = line2.match(/^\s*\[(.*)\]\s*$/);
|
|
5082
|
+
if (section) {
|
|
5083
|
+
inOrigin = /^remote\s+"origin"$/.test(section[1].trim());
|
|
5084
|
+
continue;
|
|
5085
|
+
}
|
|
5086
|
+
const url = inOrigin ? line2.match(/^\s*url\s*=\s*(.+?)\s*$/) : null;
|
|
5087
|
+
if (url) return url[1];
|
|
5088
|
+
}
|
|
5089
|
+
return void 0;
|
|
5090
|
+
}
|
|
5091
|
+
|
|
5092
|
+
// src/configurator/server/files.ts
|
|
5093
|
+
var import_handlebars2 = __toESM(require("handlebars"));
|
|
5094
|
+
var import_promises3 = require("node:fs/promises");
|
|
5095
|
+
var import_node_path4 = require("node:path");
|
|
5096
|
+
|
|
5097
|
+
// src/configurator/ui/form/users.ts
|
|
5098
|
+
function parseKeys(text2) {
|
|
5099
|
+
if (typeof text2 !== "string") return [];
|
|
5100
|
+
return text2.split("\n").map((line2) => line2.trim()).filter((line2) => line2 && !line2.startsWith("#"));
|
|
5101
|
+
}
|
|
5102
|
+
function rowsToUsers(rows) {
|
|
5103
|
+
if (!Array.isArray(rows)) return [];
|
|
5104
|
+
return rows.filter((row) => row?.name?.trim()).map((row) => ({
|
|
5105
|
+
name: row.name.trim(),
|
|
5106
|
+
ssh_keys: parseKeys(row.ssh_keys),
|
|
5107
|
+
state: row.state ?? "present",
|
|
5108
|
+
role: row.role ?? "operator"
|
|
5109
|
+
}));
|
|
5110
|
+
}
|
|
5111
|
+
|
|
5112
|
+
// src/configurator/ui/bindings/parsers.ts
|
|
5113
|
+
var text = (raw) => typeof raw === "string" || typeof raw === "number" || typeof raw === "boolean" ? String(raw).trim() : "";
|
|
5114
|
+
var FORMATTERS = {
|
|
5115
|
+
string: (value) => typeof value === "string" ? value.trim() : String(value),
|
|
5116
|
+
boolean: (value) => value === true || value === "true",
|
|
5117
|
+
// table rows -> "10.0.0.1,10.0.0.2" (a GitHub variable)
|
|
5118
|
+
"csv-to-ip-rows": (value) => ipsOf(value).join(","),
|
|
5119
|
+
// table rows -> ["10.0.0.1", "10.0.0.2"] (a template list)
|
|
5120
|
+
"ip-rows": (value) => ipsOf(value),
|
|
5121
|
+
// table rows -> the users the inventory template expects
|
|
5122
|
+
users: (value) => rowsToUsers(value)
|
|
5123
|
+
};
|
|
5124
|
+
function ipsOf(rows) {
|
|
5125
|
+
if (!Array.isArray(rows)) return [];
|
|
5126
|
+
return rows.map((row) => text(row?.ip)).filter(Boolean);
|
|
5127
|
+
}
|
|
5128
|
+
function formatWith(name, value) {
|
|
5129
|
+
const formatter = FORMATTERS[name ?? "string"];
|
|
5130
|
+
if (!formatter) throw new Error(`Unknown parser "${name}"`);
|
|
5131
|
+
return formatter(value);
|
|
5132
|
+
}
|
|
5133
|
+
|
|
5134
|
+
// src/configurator/ui/plan/dependencyHosts.ts
|
|
5135
|
+
var DEFAULT_HOSTS = {
|
|
5136
|
+
ELASTICSEARCH_HOST: (environment) => `elasticsearch.opencrvs-deps-${environment}.svc.cluster.local`,
|
|
5137
|
+
S3_HOST: (environment) => `rustfs-0.rustfs.opencrvs-deps-${environment}.svc.cluster.local`,
|
|
5138
|
+
POSTGRES_HOST: (environment) => `postgres-0.postgres.opencrvs-deps-${environment}.svc.cluster.local`,
|
|
5139
|
+
REDIS_HOST: (environment) => `redis-0.redis.opencrvs-deps-${environment}.svc.cluster.local`,
|
|
5140
|
+
OTEL_COLLECTOR_ENDPOINT: (environment) => `http://opentelemetry-collector.opencrvs-deps-${environment}.svc.cluster.local:4317`
|
|
5141
|
+
};
|
|
5142
|
+
|
|
5143
|
+
// src/configurator/ui/plan/templateValues.ts
|
|
5144
|
+
var DERIVED = {
|
|
5145
|
+
env: ({ environment }) => environment,
|
|
5146
|
+
// A cluster is a single node unless it has workers or a backup server (the CLI passes a string).
|
|
5147
|
+
single_node: ({ fromQuestions }) => fromQuestions.kube_worker_nodes?.length || fromQuestions.backup_host ? "false" : "true",
|
|
5148
|
+
backup_enabled: ({ answers }) => answers.BACKUP_OR_RESTORE === "backup",
|
|
5149
|
+
restore_enabled: ({ answers }) => answers.BACKUP_OR_RESTORE === "restore",
|
|
5150
|
+
opencrvs_environment: ({ environment }) => environment,
|
|
5151
|
+
// On unless the question says otherwise (the template reads it only to switch tracing on).
|
|
5152
|
+
monitoring_enabled: ({ answers }) => answers.MONITORING_ENABLED !== false,
|
|
5153
|
+
lets_encrypt: ({ answers }) => answers.TRAEFIK_MODE === "lets_encrypt",
|
|
5154
|
+
static_ssl: ({ answers }) => answers.TRAEFIK_MODE === "static_ssl"
|
|
5155
|
+
};
|
|
5156
|
+
var variableOf = (target) => target.collect === "container" ? target.var.split(".")[0] : target.var;
|
|
5157
|
+
function* fileTargets2(template, bindings) {
|
|
5158
|
+
for (const [question, binding] of Object.entries(bindings.bindings)) {
|
|
5159
|
+
for (const target of binding.targets) {
|
|
5160
|
+
if (target.source === "file" && target.template === template) yield { question, target, variable: variableOf(target) };
|
|
5161
|
+
}
|
|
5162
|
+
}
|
|
5163
|
+
}
|
|
5164
|
+
function builtInValues(template, environment, bindings) {
|
|
5165
|
+
const values = {};
|
|
5166
|
+
for (const { question, variable } of fileTargets2(template, bindings)) {
|
|
5167
|
+
const address = DEFAULT_HOSTS[question];
|
|
5168
|
+
if (address) values[variable] = address(environment);
|
|
5169
|
+
}
|
|
5170
|
+
return values;
|
|
5171
|
+
}
|
|
5172
|
+
function templateValues(template, environment, answers, bindings) {
|
|
5173
|
+
const fromQuestions = {};
|
|
5174
|
+
for (const { question, target, variable } of fileTargets2(template, bindings)) {
|
|
5175
|
+
if (answers[question] !== void 0) fromQuestions[variable] = formatWith(target.parse, answers[question]);
|
|
5176
|
+
}
|
|
5177
|
+
const config = bindings.templates?.[template];
|
|
5178
|
+
const values = { ...config?.defaults ?? {}, ...fromQuestions };
|
|
5179
|
+
for (const [variable, address] of Object.entries(builtInValues(template, environment, bindings))) {
|
|
5180
|
+
if (!values[variable]) values[variable] = address;
|
|
5181
|
+
}
|
|
5182
|
+
for (const name of config?.derived ?? []) {
|
|
5183
|
+
const derive = DERIVED[name];
|
|
5184
|
+
if (!derive) throw new Error(`${template}: no derived value named "${name}"`);
|
|
5185
|
+
values[name] = derive({ environment, answers, fromQuestions });
|
|
5186
|
+
}
|
|
5187
|
+
return values;
|
|
5188
|
+
}
|
|
5189
|
+
|
|
5190
|
+
// src/configurator/server/files.ts
|
|
5191
|
+
function renderEnvironmentFiles({ environment, values, bindings, readTemplate: readTemplate2 }) {
|
|
5192
|
+
const files = {};
|
|
5193
|
+
for (const [template, output] of Object.entries(bindings.files)) {
|
|
5194
|
+
const handlebars = import_handlebars2.default.create();
|
|
5195
|
+
handlebars.registerHelper("data_label_idx", (value) => Number.parseInt(String(value), 10) + 2);
|
|
5196
|
+
files[output] = handlebars.compile(readTemplate2(template))(templateValues(template, environment, values, bindings));
|
|
5197
|
+
}
|
|
5198
|
+
return files;
|
|
5199
|
+
}
|
|
5200
|
+
async function writeEnvironmentFiles(dir, environment, files, flatFile) {
|
|
5201
|
+
if (!SAFE_NAME.test(environment)) throw new Error("Invalid environment name.");
|
|
5202
|
+
for (const path15 of Object.keys(files)) {
|
|
5203
|
+
if (!ENVIRONMENT_FILES.includes(path15)) throw new Error(`Refusing to write ${path15}.`);
|
|
5204
|
+
}
|
|
5205
|
+
const { targets } = await fileTargets(dir, environment, flatFile);
|
|
5206
|
+
const written = [];
|
|
5207
|
+
for (const [path15, text2] of Object.entries(files)) {
|
|
5208
|
+
const target = targets[path15];
|
|
5209
|
+
if (target === null || target === void 0) continue;
|
|
5210
|
+
await (0, import_promises3.mkdir)((0, import_node_path4.dirname)((0, import_node_path4.join)(dir, target)), { recursive: true });
|
|
5211
|
+
await (0, import_promises3.writeFile)((0, import_node_path4.join)(dir, target), text2);
|
|
5212
|
+
written.push(target);
|
|
5213
|
+
}
|
|
5214
|
+
return written;
|
|
5215
|
+
}
|
|
5216
|
+
|
|
5217
|
+
// src/configurator/server/seal.ts
|
|
5218
|
+
var import_libsodium_wrappers2 = __toESM(require("libsodium-wrappers"));
|
|
5219
|
+
async function sealSecret(publicKeyBase64, value) {
|
|
5220
|
+
await import_libsodium_wrappers2.default.ready;
|
|
5221
|
+
const key = import_libsodium_wrappers2.default.from_base64(publicKeyBase64, import_libsodium_wrappers2.default.base64_variants.ORIGINAL);
|
|
5222
|
+
const sealed = import_libsodium_wrappers2.default.crypto_box_seal(import_libsodium_wrappers2.default.from_string(value), key);
|
|
5223
|
+
return import_libsodium_wrappers2.default.to_base64(sealed, import_libsodium_wrappers2.default.base64_variants.ORIGINAL);
|
|
5224
|
+
}
|
|
5225
|
+
|
|
5226
|
+
// src/configurator/server/github.ts
|
|
5227
|
+
var GitHubError = class extends Error {
|
|
5228
|
+
status;
|
|
5229
|
+
constructor(message, status) {
|
|
5230
|
+
super(message);
|
|
5231
|
+
this.status = status;
|
|
5232
|
+
}
|
|
5233
|
+
};
|
|
5234
|
+
var API_VERSION = "2022-11-28";
|
|
5235
|
+
var PER_PAGE = 100;
|
|
5236
|
+
var GitHubClient = class {
|
|
5237
|
+
owner;
|
|
5238
|
+
repo;
|
|
5239
|
+
token;
|
|
5240
|
+
baseUrl;
|
|
5241
|
+
fetchImpl;
|
|
5242
|
+
constructor(options) {
|
|
5243
|
+
this.owner = options.owner;
|
|
5244
|
+
this.repo = options.repo;
|
|
5245
|
+
this.token = options.token;
|
|
5246
|
+
this.baseUrl = new URL(options.baseUrl ?? "https://api.github.com");
|
|
5247
|
+
this.fetchImpl = options.fetch ?? fetch;
|
|
5248
|
+
}
|
|
5249
|
+
repoPath(suffix = "") {
|
|
5250
|
+
return `/repos/${encodeURIComponent(this.owner)}/${encodeURIComponent(this.repo)}${suffix}`;
|
|
5251
|
+
}
|
|
5252
|
+
// Messages never contain the token, a value that was sent, or anything GitHub sent back.
|
|
5253
|
+
async request(method, url, body) {
|
|
5254
|
+
let response;
|
|
5255
|
+
try {
|
|
5256
|
+
response = await this.fetchImpl(url, {
|
|
5257
|
+
method,
|
|
5258
|
+
headers: {
|
|
5259
|
+
Authorization: `Bearer ${this.token}`,
|
|
5260
|
+
Accept: "application/vnd.github+json",
|
|
5261
|
+
"X-GitHub-Api-Version": API_VERSION,
|
|
5262
|
+
"User-Agent": "env-init-ui",
|
|
5263
|
+
...body === void 0 ? {} : { "Content-Type": "application/json" }
|
|
5264
|
+
},
|
|
5265
|
+
body: body === void 0 ? void 0 : JSON.stringify(body)
|
|
5266
|
+
});
|
|
5267
|
+
} catch {
|
|
5268
|
+
throw new GitHubError("Could not reach GitHub.");
|
|
5269
|
+
}
|
|
5270
|
+
if (response.ok) return response;
|
|
5271
|
+
if (response.status === 401) throw new GitHubError("GitHub rejected the token (bad credentials).", 401);
|
|
5272
|
+
if (response.status === 403 && response.headers.get("x-ratelimit-remaining") === "0") {
|
|
5273
|
+
throw new GitHubError("GitHub rate limit reached. Try again later.", 403);
|
|
5274
|
+
}
|
|
5275
|
+
if (response.status === 403) {
|
|
5276
|
+
throw new GitHubError(
|
|
5277
|
+
method === "GET" ? "The token is not allowed to read this (HTTP 403)." : "The token is not allowed to change this (HTTP 403). Writing needs access to the repository, and creating environments needs admin access.",
|
|
5278
|
+
403
|
|
5279
|
+
);
|
|
5280
|
+
}
|
|
5281
|
+
if (response.status === 404) {
|
|
5282
|
+
throw new GitHubError(`Not found: ${this.owner}/${this.repo}, or the token cannot see it.`, 404);
|
|
5283
|
+
}
|
|
5284
|
+
if (response.status === 409) throw new GitHubError("GitHub reports a conflict (HTTP 409).", 409);
|
|
5285
|
+
if (response.status === 422) throw new GitHubError("GitHub refused the request (HTTP 422).", 422);
|
|
5286
|
+
throw new GitHubError(`GitHub answered with HTTP ${response.status}.`, response.status);
|
|
5287
|
+
}
|
|
5288
|
+
get(url) {
|
|
5289
|
+
return this.request("GET", url);
|
|
5290
|
+
}
|
|
5291
|
+
url(path15) {
|
|
5292
|
+
return new URL(path15, this.baseUrl);
|
|
5293
|
+
}
|
|
5294
|
+
// Follows rel="next" links, but only ones on the same host: the token must never be sent elsewhere.
|
|
5295
|
+
async paginate(path15, pick) {
|
|
5296
|
+
const items = [];
|
|
5297
|
+
let next = this.url(path15);
|
|
5298
|
+
next.searchParams.set("per_page", String(PER_PAGE));
|
|
5299
|
+
for (let page = 0; next && page < 100; page++) {
|
|
5300
|
+
const response = await this.get(next);
|
|
5301
|
+
items.push(...pick(await response.json()));
|
|
5302
|
+
const link = response.headers.get("link") ?? "";
|
|
5303
|
+
const target = link.match(/<([^>]+)>;\s*rel="next"/)?.[1];
|
|
5304
|
+
const candidate = target ? new URL(target, this.baseUrl) : void 0;
|
|
5305
|
+
next = candidate && candidate.origin === this.baseUrl.origin ? candidate : void 0;
|
|
5306
|
+
}
|
|
5307
|
+
return items;
|
|
5308
|
+
}
|
|
5309
|
+
// Checks the token and what it can do. Classic tokens report their scopes in a header;
|
|
5310
|
+
// fine-grained tokens do not, so for those only the repository permissions can be checked.
|
|
5311
|
+
async connect() {
|
|
5312
|
+
try {
|
|
5313
|
+
const user = await this.get(this.url("/user"));
|
|
5314
|
+
const scopesHeader = user.headers.get("x-oauth-scopes");
|
|
5315
|
+
const login = String((await user.json()).login ?? this.owner);
|
|
5316
|
+
const repository = await this.get(this.url(this.repoPath()));
|
|
5317
|
+
const permissions = (await repository.json()).permissions ?? {};
|
|
5318
|
+
const missing = [];
|
|
5319
|
+
if (scopesHeader !== null) {
|
|
5320
|
+
const scopes = scopesHeader.split(",").map((scope) => scope.trim());
|
|
5321
|
+
if (!scopes.includes("repo")) missing.push('manage environments, secrets and variables or trigger workflows (classic token scope "repo")');
|
|
5322
|
+
if (!scopes.includes("workflow")) missing.push('update workflow files (classic token scope "workflow")');
|
|
5323
|
+
}
|
|
5324
|
+
if (!permissions.admin) missing.push("create environments (needs admin access to the repository)");
|
|
5325
|
+
return { ok: true, login, missingPermissions: missing };
|
|
5326
|
+
} catch (error2) {
|
|
5327
|
+
return { ok: false, error: error2 instanceof GitHubError ? error2.message : "Could not reach GitHub." };
|
|
5328
|
+
}
|
|
5329
|
+
}
|
|
5330
|
+
async listEnvironments() {
|
|
5331
|
+
const environments = await this.paginate(this.repoPath("/environments"), (body) => body.environments ?? []);
|
|
5332
|
+
return environments.map((environment) => environment.name);
|
|
5333
|
+
}
|
|
5334
|
+
async readScope(base) {
|
|
5335
|
+
const [variables, secrets] = await Promise.all([
|
|
5336
|
+
this.paginate(`${base}/variables`, (body) => body.variables ?? []),
|
|
5337
|
+
this.paginate(`${base}/secrets`, (body) => body.secrets ?? [])
|
|
5338
|
+
]);
|
|
5339
|
+
return {
|
|
5340
|
+
variables: Object.fromEntries(variables.map((variable) => [variable.name, variable.value])),
|
|
5341
|
+
// GitHub returns names (and dates) for secrets, never values.
|
|
5342
|
+
secretNames: secrets.map((secret) => secret.name)
|
|
5343
|
+
};
|
|
5344
|
+
}
|
|
5345
|
+
readRepository() {
|
|
5346
|
+
return this.readScope(this.repoPath("/actions"));
|
|
5347
|
+
}
|
|
5348
|
+
async readEnvironment(name) {
|
|
5349
|
+
try {
|
|
5350
|
+
return await this.readScope(this.repoPath(`/environments/${encodeURIComponent(name)}`));
|
|
5351
|
+
} catch (error2) {
|
|
5352
|
+
if (error2 instanceof GitHubError && error2.status === 404) return { variables: {}, secretNames: [] };
|
|
5353
|
+
throw error2;
|
|
5354
|
+
}
|
|
5355
|
+
}
|
|
5356
|
+
// --- writing ---------------------------------------------------------------------------
|
|
5357
|
+
scopeBase(target) {
|
|
5358
|
+
return target.scope === "environment" ? this.repoPath(`/environments/${encodeURIComponent(target.environment ?? "")}`) : this.repoPath("/actions");
|
|
5359
|
+
}
|
|
5360
|
+
// Creates the environment, or leaves it as it is if it exists.
|
|
5361
|
+
async createEnvironment(name) {
|
|
5362
|
+
await this.request("PUT", this.url(this.repoPath(`/environments/${encodeURIComponent(name)}`)), {});
|
|
5363
|
+
}
|
|
5364
|
+
// A 404 on an environment's variables or secrets means the environment is missing,
|
|
5365
|
+
// not that the repository is: say so.
|
|
5366
|
+
async inEnvironment(target, action) {
|
|
5367
|
+
try {
|
|
5368
|
+
await action();
|
|
5369
|
+
} catch (error2) {
|
|
5370
|
+
if (error2 instanceof GitHubError && error2.status === 404 && target.scope === "environment") {
|
|
5371
|
+
throw new GitHubError(`The environment \u201C${target.environment}\u201D does not exist yet.`, 404);
|
|
5372
|
+
}
|
|
5373
|
+
throw error2;
|
|
5374
|
+
}
|
|
5375
|
+
}
|
|
5376
|
+
putVariable(target, exists) {
|
|
5377
|
+
return this.inEnvironment(target, () => this.putVariableUnchecked(target, exists));
|
|
5378
|
+
}
|
|
5379
|
+
putSecret(target) {
|
|
5380
|
+
return this.inEnvironment(target, () => this.putSecretUnchecked(target));
|
|
5381
|
+
}
|
|
5382
|
+
// `exists` is what the plan found out; if it was wrong, the other call is tried.
|
|
5383
|
+
async putVariableUnchecked(target, exists) {
|
|
5384
|
+
const base = `${this.scopeBase(target)}/variables`;
|
|
5385
|
+
const create = () => this.request("POST", this.url(base), { name: target.name, value: target.value });
|
|
5386
|
+
const update = () => this.request("PATCH", this.url(`${base}/${encodeURIComponent(target.name)}`), { name: target.name, value: target.value });
|
|
5387
|
+
try {
|
|
5388
|
+
await (exists ? update() : create());
|
|
5389
|
+
} catch (error2) {
|
|
5390
|
+
if (!(error2 instanceof GitHubError)) throw error2;
|
|
5391
|
+
if (!exists && error2.status === 409) await update();
|
|
5392
|
+
else if (exists && error2.status === 404) await create();
|
|
5393
|
+
else throw error2;
|
|
5394
|
+
}
|
|
5395
|
+
}
|
|
5396
|
+
async putSecretUnchecked(target) {
|
|
5397
|
+
const base = `${this.scopeBase(target)}/secrets`;
|
|
5398
|
+
const key = await (await this.get(this.url(`${base}/public-key`))).json();
|
|
5399
|
+
const encrypted = await sealSecret(key.key, target.value);
|
|
5400
|
+
await this.request("PUT", this.url(`${base}/${encodeURIComponent(target.name)}`), {
|
|
5401
|
+
encrypted_value: encrypted,
|
|
5402
|
+
key_id: key.key_id
|
|
5403
|
+
});
|
|
5404
|
+
}
|
|
5405
|
+
};
|
|
5406
|
+
|
|
5407
|
+
// src/configurator/server/sshKeys.ts
|
|
5408
|
+
var import_ssh22 = __toESM(require("ssh2"));
|
|
5409
|
+
var { utils: utils2 } = import_ssh22.default;
|
|
5410
|
+
function generateSSHKeyPair2() {
|
|
5411
|
+
const keys = utils2.generateKeyPairSync("ed25519");
|
|
5412
|
+
return {
|
|
5413
|
+
publicKey: keys.public.toString(),
|
|
5414
|
+
privateKey: keys.private.toString()
|
|
5415
|
+
};
|
|
5416
|
+
}
|
|
5417
|
+
|
|
5418
|
+
// src/configurator/server/plugin.ts
|
|
5419
|
+
var GitHubSession = class {
|
|
5420
|
+
client;
|
|
5421
|
+
baseUrl;
|
|
5422
|
+
fetchImpl;
|
|
5423
|
+
constructor(options = {}) {
|
|
5424
|
+
this.baseUrl = options.baseUrl;
|
|
5425
|
+
this.fetchImpl = options.fetch;
|
|
5426
|
+
}
|
|
5427
|
+
async connect(token, owner, repo) {
|
|
5428
|
+
const client = new GitHubClient({ token, owner, repo, baseUrl: this.baseUrl, fetch: this.fetchImpl });
|
|
5429
|
+
const result = await client.connect();
|
|
5430
|
+
this.client = result.ok ? client : void 0;
|
|
5431
|
+
return result;
|
|
5432
|
+
}
|
|
5433
|
+
disconnect() {
|
|
5434
|
+
this.client = void 0;
|
|
5435
|
+
}
|
|
5436
|
+
get current() {
|
|
5437
|
+
return this.client;
|
|
5438
|
+
}
|
|
5439
|
+
};
|
|
5440
|
+
var LOCAL_HOST = /^(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/;
|
|
5441
|
+
var NAME = /^[A-Za-z0-9_.-]{1,100}$/;
|
|
5442
|
+
var VARIABLE_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,99}$/;
|
|
5443
|
+
var MAX_VALUE = 49152;
|
|
5444
|
+
var fail = (status, error2) => ({ status, body: { error: error2 } });
|
|
5445
|
+
function crossSiteProblem(request2) {
|
|
5446
|
+
const host = request2.host ?? "localhost";
|
|
5447
|
+
if (!LOCAL_HOST.test(host)) return "Only local requests are accepted.";
|
|
5448
|
+
if (request2.origin !== void 0 && request2.origin !== `http://${host}`) return "Requests from other sites are not accepted.";
|
|
5449
|
+
return void 0;
|
|
5450
|
+
}
|
|
5451
|
+
var mapGitHubError = (error2) => error2 instanceof GitHubError ? fail(error2.status === 401 ? 401 : 502, error2.message) : fail(502, "Could not reach GitHub.");
|
|
5452
|
+
async function githubWrite(path15, body, session) {
|
|
5453
|
+
const client = session.current;
|
|
5454
|
+
if (!client) return fail(409, "Not connected to GitHub.");
|
|
5455
|
+
const input4 = body ?? {};
|
|
5456
|
+
try {
|
|
5457
|
+
if (path15 === "/api/github/environment") {
|
|
5458
|
+
if (typeof input4.name !== "string" || !NAME.test(input4.name)) return fail(400, "Invalid environment name.");
|
|
5459
|
+
await client.createEnvironment(input4.name);
|
|
5460
|
+
return { status: 200, body: { ok: true } };
|
|
5461
|
+
}
|
|
5462
|
+
const { scope, environment, name, value } = input4;
|
|
5463
|
+
if (scope !== "environment" && scope !== "repository") return fail(400, "Scope must be environment or repository.");
|
|
5464
|
+
if (scope === "environment" && (typeof environment !== "string" || !NAME.test(environment))) return fail(400, "Invalid environment name.");
|
|
5465
|
+
if (typeof name !== "string" || !VARIABLE_NAME.test(name)) return fail(400, "Invalid variable or secret name.");
|
|
5466
|
+
if (typeof value !== "string" || value === "" || value.length > MAX_VALUE) return fail(400, "The value must be 1 to 49152 characters.");
|
|
5467
|
+
const target = { scope, name, value, ...scope === "environment" ? { environment } : {} };
|
|
5468
|
+
if (path15 === "/api/github/variable") await client.putVariable(target, typeof input4.exists === "boolean" ? input4.exists : void 0);
|
|
5469
|
+
else await client.putSecret(target);
|
|
5470
|
+
return { status: 200, body: { ok: true } };
|
|
5471
|
+
} catch (error2) {
|
|
5472
|
+
return mapGitHubError(error2);
|
|
5473
|
+
}
|
|
5474
|
+
}
|
|
5475
|
+
async function githubRoute(path15, method, body, options) {
|
|
5476
|
+
const session = options.github;
|
|
5477
|
+
if (path15 === "/api/github/status" && method === "GET") {
|
|
5478
|
+
const client = session.current;
|
|
5479
|
+
return { status: 200, body: client ? { connected: true, owner: client.owner, repo: client.repo } : { connected: false } };
|
|
5480
|
+
}
|
|
5481
|
+
if (path15 === "/api/github/connect" && method === "POST") {
|
|
5482
|
+
const { token, owner, repo } = body ?? {};
|
|
5483
|
+
if (typeof token !== "string" || !token.trim() || token.length > 500) return fail(400, "Enter a token.");
|
|
5484
|
+
if (typeof owner !== "string" || !NAME.test(owner) || typeof repo !== "string" || !NAME.test(repo)) {
|
|
5485
|
+
return fail(400, "Enter the repository owner and name.");
|
|
5486
|
+
}
|
|
5487
|
+
return { status: 200, body: await session.connect(token.trim(), owner, repo) };
|
|
5488
|
+
}
|
|
5489
|
+
if (path15 === "/api/github/disconnect" && method === "POST") {
|
|
5490
|
+
session.disconnect();
|
|
5491
|
+
return { status: 200, body: { ok: true } };
|
|
5492
|
+
}
|
|
5493
|
+
const writeRoutes = ["/api/github/environment", "/api/github/variable", "/api/github/secret"];
|
|
5494
|
+
if (method === "POST" && writeRoutes.includes(path15)) return githubWrite(path15, body, session);
|
|
5495
|
+
const reads = ["/api/github/environments", "/api/github/repository"];
|
|
5496
|
+
const environment = path15.match(/^\/api\/github\/environments\/([^/]+)$/);
|
|
5497
|
+
if (method === "GET" && (reads.includes(path15) || environment)) {
|
|
5498
|
+
const client = session.current;
|
|
5499
|
+
if (!client) return fail(409, "Not connected to GitHub.");
|
|
5500
|
+
try {
|
|
5501
|
+
if (path15 === "/api/github/environments") return { status: 200, body: { names: await client.listEnvironments() } };
|
|
5502
|
+
if (path15 === "/api/github/repository") return { status: 200, body: await client.readRepository() };
|
|
5503
|
+
return { status: 200, body: await client.readEnvironment(decodeURIComponent(environment[1])) };
|
|
5504
|
+
} catch (error2) {
|
|
5505
|
+
return mapGitHubError(error2);
|
|
5506
|
+
}
|
|
5507
|
+
}
|
|
5508
|
+
return null;
|
|
5509
|
+
}
|
|
5510
|
+
async function filesRoute(name, action, method, body, options) {
|
|
5511
|
+
const wanted = action === "render" ? "POST" : "PUT";
|
|
5512
|
+
if (method !== wanted) return null;
|
|
5513
|
+
if (!options.files) return fail(501, "File rendering is not configured.");
|
|
5514
|
+
if (!SAFE_NAME.test(name)) return fail(400, "Invalid environment name.");
|
|
5515
|
+
const values = body?.values;
|
|
5516
|
+
if (!values || typeof values !== "object" || Array.isArray(values)) return fail(400, "Send the answers as { values }.");
|
|
5517
|
+
let rendered;
|
|
5518
|
+
try {
|
|
5519
|
+
const { bindings, templatesDir } = options.files;
|
|
5520
|
+
rendered = renderEnvironmentFiles({
|
|
5521
|
+
environment: name,
|
|
5522
|
+
values,
|
|
5523
|
+
bindings,
|
|
5524
|
+
readTemplate: (template) => (0, import_node_fs3.readFileSync)((0, import_node_path5.join)(templatesDir, template), "utf8")
|
|
5525
|
+
});
|
|
5526
|
+
} catch {
|
|
5527
|
+
return fail(500, "Could not render the files.");
|
|
5528
|
+
}
|
|
5529
|
+
const flat = options.files.bindings.flat;
|
|
5530
|
+
if (action === "render") {
|
|
5531
|
+
const existing = await readEnvironmentFiles(options.environmentsDir, name, flat) ?? {};
|
|
5532
|
+
const { layout, targets } = await fileTargets(options.environmentsDir, name, flat);
|
|
5533
|
+
return {
|
|
5534
|
+
status: 200,
|
|
5535
|
+
body: {
|
|
5536
|
+
layout,
|
|
5537
|
+
// `target` is where the file would be written, or null when the layout has no place for it.
|
|
5538
|
+
files: Object.entries(rendered).map(([path15, text2]) => ({
|
|
5539
|
+
path: path15,
|
|
5540
|
+
text: text2,
|
|
5541
|
+
existing: targets[path15] === null ? null : existing[path15] ?? null,
|
|
5542
|
+
target: targets[path15] ?? null
|
|
5543
|
+
}))
|
|
5544
|
+
}
|
|
5545
|
+
};
|
|
5546
|
+
}
|
|
5547
|
+
try {
|
|
5548
|
+
return { status: 200, body: { written: await writeEnvironmentFiles(options.environmentsDir, name, rendered, flat) } };
|
|
5549
|
+
} catch (error2) {
|
|
5550
|
+
return fail(409, error2 instanceof Error ? error2.message : "Could not write the files.");
|
|
5551
|
+
}
|
|
5552
|
+
}
|
|
5553
|
+
async function handleApiRequest(path15, options, request2 = {}) {
|
|
5554
|
+
const method = request2.method ?? "GET";
|
|
5555
|
+
const problem = crossSiteProblem(request2);
|
|
5556
|
+
if (problem) return fail(403, problem);
|
|
5557
|
+
if (path15.startsWith("/api/github/")) return githubRoute(path15, method, request2.body, options);
|
|
5558
|
+
if (path15 === "/api/ssh-keypair" && method === "POST") return { status: 200, body: generateSSHKeyPair2() };
|
|
5559
|
+
const fileRoute = path15.match(/^\/api\/environments\/([^/]+)\/(render|files)$/);
|
|
5560
|
+
if (fileRoute) return filesRoute(decodeURIComponent(fileRoute[1]), fileRoute[2], method, request2.body, options);
|
|
5561
|
+
if (method !== "GET") return null;
|
|
5562
|
+
if (path15 === "/api/context") {
|
|
5563
|
+
const remote = await (options.gitRemote ?? (() => readGitRemote(options.projectDir)))();
|
|
5564
|
+
return { status: 200, body: { repository: remote ? parseGitRemote(remote) : null } };
|
|
5565
|
+
}
|
|
5566
|
+
if (path15 === "/api/environments") {
|
|
5567
|
+
return { status: 200, body: await listEnvironmentNames(options.environmentsDir) };
|
|
5568
|
+
}
|
|
5569
|
+
const match = path15.match(/^\/api\/environments\/([^/]+)$/);
|
|
5570
|
+
if (match) {
|
|
5571
|
+
const files = await readEnvironmentFiles(options.environmentsDir, decodeURIComponent(match[1]));
|
|
5572
|
+
return files ? { status: 200, body: { files } } : fail(404, "Unknown environment");
|
|
5573
|
+
}
|
|
5574
|
+
return null;
|
|
5575
|
+
}
|
|
5576
|
+
async function readJson(request2) {
|
|
5577
|
+
const chunks = [];
|
|
5578
|
+
let size = 0;
|
|
5579
|
+
for await (const chunk of request2) {
|
|
5580
|
+
size += chunk.length;
|
|
5581
|
+
if (size > 3e5) throw new Error("too large");
|
|
5582
|
+
chunks.push(chunk);
|
|
5583
|
+
}
|
|
5584
|
+
return chunks.length ? JSON.parse(Buffer.concat(chunks).toString("utf8")) : void 0;
|
|
5585
|
+
}
|
|
5586
|
+
function middleware(options) {
|
|
5587
|
+
return async (request2, response, next) => {
|
|
5588
|
+
const path15 = new URL(request2.url ?? "/", "http://localhost").pathname;
|
|
5589
|
+
if (!path15.startsWith("/api/")) return next();
|
|
5590
|
+
let result;
|
|
5591
|
+
try {
|
|
5592
|
+
const method = request2.method ?? "GET";
|
|
5593
|
+
const json = (request2.headers["content-type"] ?? "").startsWith("application/json");
|
|
5594
|
+
const hasBody = method === "POST" || method === "PUT";
|
|
5595
|
+
if (hasBody && !json) {
|
|
5596
|
+
result = fail(415, "Send JSON.");
|
|
5597
|
+
} else {
|
|
5598
|
+
result = await handleApiRequest(path15, options, {
|
|
5599
|
+
method,
|
|
5600
|
+
body: hasBody ? await readJson(request2) : void 0,
|
|
5601
|
+
host: request2.headers.host,
|
|
5602
|
+
origin: request2.headers.origin
|
|
5603
|
+
}) ?? fail(404, "Unknown API path");
|
|
5604
|
+
}
|
|
5605
|
+
} catch {
|
|
5606
|
+
result = fail(400, "Bad request.");
|
|
5607
|
+
}
|
|
5608
|
+
response.statusCode = result.status;
|
|
5609
|
+
response.setHeader("Content-Type", "application/json");
|
|
5610
|
+
response.setHeader("Cache-Control", "no-store");
|
|
5611
|
+
response.end(JSON.stringify(result.body));
|
|
5612
|
+
};
|
|
5613
|
+
}
|
|
5614
|
+
|
|
5615
|
+
// src/configurator/server/start.ts
|
|
5616
|
+
var TYPES = {
|
|
5617
|
+
".html": "text/html; charset=utf-8",
|
|
5618
|
+
".js": "text/javascript; charset=utf-8",
|
|
5619
|
+
".css": "text/css; charset=utf-8",
|
|
5620
|
+
".svg": "image/svg+xml",
|
|
5621
|
+
".png": "image/png",
|
|
5622
|
+
".woff2": "font/woff2",
|
|
5623
|
+
".json": "application/json"
|
|
5624
|
+
};
|
|
5625
|
+
async function serveStatic(staticDir, urlPath, response) {
|
|
5626
|
+
const relative = (0, import_node_path6.normalize)(decodeURIComponent(urlPath)).replace(/^([/\\])+/, "");
|
|
5627
|
+
let file = (0, import_node_path6.join)(staticDir, relative);
|
|
5628
|
+
if (!file.startsWith(staticDir)) file = (0, import_node_path6.join)(staticDir, "index.html");
|
|
5629
|
+
let body;
|
|
5630
|
+
try {
|
|
5631
|
+
body = await (0, import_promises4.readFile)(file);
|
|
5632
|
+
} catch {
|
|
5633
|
+
file = (0, import_node_path6.join)(staticDir, "index.html");
|
|
5634
|
+
body = await (0, import_promises4.readFile)(file);
|
|
5635
|
+
}
|
|
5636
|
+
response.setHeader("Content-Type", TYPES[(0, import_node_path6.extname)(file)] ?? "application/octet-stream");
|
|
5637
|
+
response.end(body);
|
|
5638
|
+
}
|
|
5639
|
+
function startServer(options) {
|
|
5640
|
+
const projectDir = (0, import_node_path6.resolve)(options.projectDir);
|
|
5641
|
+
const assetsDir = (0, import_node_path6.resolve)(options.assetsDir);
|
|
5642
|
+
const staticDir = (0, import_node_path6.join)(assetsDir, "ui");
|
|
5643
|
+
const api = middleware({
|
|
5644
|
+
environmentsDir: process.env.ENVIRONMENTS_DIR ?? (0, import_node_path6.join)(projectDir, "environments"),
|
|
5645
|
+
projectDir,
|
|
5646
|
+
github: new GitHubSession({ baseUrl: process.env.GITHUB_API_URL }),
|
|
5647
|
+
files: {
|
|
5648
|
+
bindings: (0, import_yaml.parse)((0, import_node_fs4.readFileSync)((0, import_node_path6.join)(assetsDir, "forms", "bindings.yml"), "utf8")),
|
|
5649
|
+
templatesDir: (0, import_node_path6.join)(assetsDir, "templates")
|
|
5650
|
+
}
|
|
5651
|
+
});
|
|
5652
|
+
const server = (0, import_node_http.createServer)((request2, response) => {
|
|
5653
|
+
api(request2, response, () => {
|
|
5654
|
+
const path15 = new URL(request2.url ?? "/", "http://localhost").pathname;
|
|
5655
|
+
serveStatic(staticDir, path15, response).catch(() => {
|
|
5656
|
+
response.statusCode = 500;
|
|
5657
|
+
response.end("The page is not built. Build the toolkit first.");
|
|
5658
|
+
});
|
|
5659
|
+
}).catch(() => {
|
|
5660
|
+
response.statusCode = 500;
|
|
5661
|
+
response.end();
|
|
5662
|
+
});
|
|
5663
|
+
});
|
|
5664
|
+
return new Promise((resolveListening, reject) => {
|
|
5665
|
+
server.once("error", reject);
|
|
5666
|
+
server.listen(options.port ?? 4173, options.host ?? "127.0.0.1", () => resolveListening(server));
|
|
5667
|
+
});
|
|
5668
|
+
}
|
|
5669
|
+
|
|
5670
|
+
// src/configurator/workflows.ts
|
|
5671
|
+
var import_node_fs5 = require("node:fs");
|
|
5672
|
+
var import_node_path7 = require("node:path");
|
|
5673
|
+
var import_yaml2 = require("yaml");
|
|
5674
|
+
var INFRASTRUCTURE_WORKFLOWS = [
|
|
5675
|
+
"provision.yml",
|
|
5676
|
+
"reset-2fa.yml"
|
|
5677
|
+
];
|
|
5678
|
+
var APPLICATION_WORKFLOWS = [
|
|
5679
|
+
"deploy-dependencies.yml",
|
|
5680
|
+
"deploy-opencrvs.yml",
|
|
5681
|
+
"clear-all-data.yml",
|
|
5682
|
+
"seed-data.yml",
|
|
5683
|
+
"reindex.yml",
|
|
5684
|
+
"github-to-k8s-sync-env.yml"
|
|
5685
|
+
];
|
|
5686
|
+
var ENVIRONMENT_INPUT = [
|
|
5687
|
+
"on",
|
|
5688
|
+
"workflow_dispatch",
|
|
5689
|
+
"inputs",
|
|
5690
|
+
"environment"
|
|
5691
|
+
];
|
|
5692
|
+
var WorkflowFormatError = class extends Error {
|
|
5693
|
+
};
|
|
5694
|
+
function listEnvironments(root) {
|
|
5695
|
+
const directory = (0, import_node_path7.join)(root, ENVIRONMENTS_DIR);
|
|
5696
|
+
const names = isDirectory2(directory) ? (0, import_node_fs5.readdirSync)(directory).filter((name) => isDirectory2((0, import_node_path7.join)(directory, name))).sort() : [];
|
|
5697
|
+
return {
|
|
5698
|
+
infrastructure: names.filter(
|
|
5699
|
+
(name) => (0, import_node_fs5.existsSync)((0, import_node_path7.join)(directory, name, INVENTORY_FILE_NAME2))
|
|
5700
|
+
),
|
|
5701
|
+
application: names.filter(
|
|
5702
|
+
(name) => isDirectory2((0, import_node_path7.join)(directory, name, "opencrvs-services"))
|
|
5703
|
+
)
|
|
5704
|
+
};
|
|
5705
|
+
}
|
|
5706
|
+
function updateEnvironmentOptions(content, environments) {
|
|
5707
|
+
const document = (0, import_yaml2.parseDocument)(content);
|
|
5708
|
+
if (document.errors.length > 0) {
|
|
5709
|
+
throw new WorkflowFormatError(
|
|
5710
|
+
`Invalid YAML: ${document.errors[0].message}`
|
|
5711
|
+
);
|
|
5712
|
+
}
|
|
5713
|
+
const options = document.getIn([...ENVIRONMENT_INPUT, "options"], true);
|
|
5714
|
+
if (!(0, import_yaml2.isSeq)(options)) {
|
|
5715
|
+
throw new WorkflowFormatError(
|
|
5716
|
+
"Expected a list at on.workflow_dispatch.inputs.environment.options"
|
|
5717
|
+
);
|
|
5718
|
+
}
|
|
5719
|
+
if (options.flow || !options.range) {
|
|
5720
|
+
throw new WorkflowFormatError(
|
|
5721
|
+
'on.workflow_dispatch.inputs.environment.options has to be a block list, one "- name" per line'
|
|
5722
|
+
);
|
|
5723
|
+
}
|
|
5724
|
+
const warnings = [];
|
|
5725
|
+
const fallback = document.getIn([...ENVIRONMENT_INPUT, "default"]);
|
|
5726
|
+
if (typeof fallback === "string" && !environments.includes(fallback)) {
|
|
5727
|
+
warnings.push(
|
|
5728
|
+
`the default environment "${fallback}" is not one of the options`
|
|
5729
|
+
);
|
|
5730
|
+
}
|
|
5731
|
+
const current = options.items.map(
|
|
5732
|
+
(item) => String((0, import_yaml2.isScalar)(item) ? item.value : item)
|
|
5733
|
+
);
|
|
5734
|
+
const target = [
|
|
5735
|
+
...current.filter(
|
|
5736
|
+
(name, i) => environments.includes(name) && current.indexOf(name) === i
|
|
5737
|
+
),
|
|
5738
|
+
...environments.filter((name) => !current.includes(name))
|
|
5739
|
+
];
|
|
5740
|
+
if (target.length === current.length && target.every((name, i) => name === current[i])) {
|
|
5741
|
+
return { content, warnings };
|
|
5742
|
+
}
|
|
5743
|
+
const [start, end] = options.range;
|
|
5744
|
+
const indentation = content.slice(
|
|
5745
|
+
content.lastIndexOf("\n", start - 1) + 1,
|
|
5746
|
+
start
|
|
5747
|
+
);
|
|
5748
|
+
if (indentation.trim() !== "") {
|
|
5749
|
+
throw new WorkflowFormatError(
|
|
5750
|
+
"on.workflow_dispatch.inputs.environment.options is not on its own lines"
|
|
5751
|
+
);
|
|
5752
|
+
}
|
|
5753
|
+
const eol = content.includes("\r\n") ? "\r\n" : "\n";
|
|
5754
|
+
const endsWithLineBreak = content.slice(start, end).endsWith("\n");
|
|
5755
|
+
const items = target.map((name) => `- ${(0, import_yaml2.stringify)(name, { lineWidth: 0 }).trimEnd()}`).join(eol + indentation);
|
|
5756
|
+
return {
|
|
5757
|
+
content: content.slice(0, start) + items + (endsWithLineBreak ? eol : "") + content.slice(end),
|
|
5758
|
+
warnings
|
|
5759
|
+
};
|
|
5760
|
+
}
|
|
5761
|
+
function planWorkflow(root, workflow, environments) {
|
|
5762
|
+
const file = (0, import_node_path7.join)(WORKFLOWS_DIR, workflow);
|
|
5763
|
+
const path15 = (0, import_node_path7.join)(root, file);
|
|
5764
|
+
const change = {
|
|
5765
|
+
file,
|
|
5766
|
+
environments,
|
|
5767
|
+
before: "",
|
|
5768
|
+
after: "",
|
|
5769
|
+
warnings: []
|
|
5770
|
+
};
|
|
5771
|
+
if (!(0, import_node_fs5.existsSync)(path15)) return { ...change, status: "not-found" };
|
|
5772
|
+
const before = (0, import_node_fs5.readFileSync)(path15, "utf8");
|
|
5773
|
+
if (environments.length === 0)
|
|
5774
|
+
return { ...change, status: "no-environments", before, after: before };
|
|
5775
|
+
try {
|
|
5776
|
+
const { content, warnings } = updateEnvironmentOptions(before, environments);
|
|
5777
|
+
return {
|
|
5778
|
+
...change,
|
|
5779
|
+
status: content === before ? "unchanged" : "updated",
|
|
5780
|
+
before,
|
|
5781
|
+
after: content,
|
|
5782
|
+
warnings
|
|
5783
|
+
};
|
|
5784
|
+
} catch (error2) {
|
|
5785
|
+
if (error2 instanceof WorkflowFormatError) {
|
|
5786
|
+
throw new WorkflowFormatError(`${file}: ${error2.message}`);
|
|
5787
|
+
}
|
|
5788
|
+
throw error2;
|
|
5789
|
+
}
|
|
5790
|
+
}
|
|
5791
|
+
function planWorkflowUpdates(root) {
|
|
5792
|
+
const { infrastructure, application } = listEnvironments(root);
|
|
5793
|
+
return [
|
|
5794
|
+
...INFRASTRUCTURE_WORKFLOWS.map(
|
|
5795
|
+
(workflow) => planWorkflow(root, workflow, infrastructure)
|
|
5796
|
+
),
|
|
5797
|
+
...APPLICATION_WORKFLOWS.map(
|
|
5798
|
+
(workflow) => planWorkflow(root, workflow, application)
|
|
5799
|
+
)
|
|
5800
|
+
];
|
|
5801
|
+
}
|
|
5802
|
+
function applyWorkflowUpdates(root, changes) {
|
|
5803
|
+
const updated = changes.filter((change) => change.status === "updated");
|
|
5804
|
+
for (const change of updated) {
|
|
5805
|
+
(0, import_node_fs5.writeFileSync)((0, import_node_path7.join)(root, change.file), change.after, "utf8");
|
|
5806
|
+
}
|
|
5807
|
+
return updated.map((change) => change.file);
|
|
5808
|
+
}
|
|
5809
|
+
|
|
5810
|
+
// src/configurator/index.ts
|
|
5811
|
+
var processContext = () => ({
|
|
5812
|
+
root: process.cwd(),
|
|
5813
|
+
log: console.log
|
|
5814
|
+
});
|
|
5815
|
+
var defaultAssetsDir = () => (0, import_node_path8.join)(__dirname, "configurator-assets");
|
|
5816
|
+
function startConfigurator(context, { port = 4173, host, assetsDir = defaultAssetsDir() } = {}) {
|
|
5817
|
+
return startServer({ projectDir: context.root, assetsDir, port, host });
|
|
5818
|
+
}
|
|
5819
|
+
async function runConfiguratorInit(args2 = [], context = processContext()) {
|
|
5820
|
+
const { values } = (0, import_node_util.parseArgs)({
|
|
5821
|
+
args: args2,
|
|
5822
|
+
options: { port: { type: "string" }, host: { type: "string" } }
|
|
5823
|
+
});
|
|
5824
|
+
const port = values.port === void 0 ? 4173 : Number(values.port);
|
|
5825
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535) {
|
|
5826
|
+
throw new Error(`Invalid port: ${values.port}`);
|
|
5827
|
+
}
|
|
5828
|
+
assertInfrastructureRepository(context.root);
|
|
5829
|
+
assertNoOldInventoryLayout2(context.root);
|
|
5830
|
+
const assetsDir = defaultAssetsDir();
|
|
5831
|
+
if (!(0, import_node_fs6.existsSync)((0, import_node_path8.join)(assetsDir, "ui", "index.html"))) {
|
|
5832
|
+
throw new Error(
|
|
5833
|
+
"The configurator page is not built. Build the toolkit first (pnpm build in packages/toolkit)."
|
|
5834
|
+
);
|
|
5835
|
+
}
|
|
5836
|
+
const server = await startConfigurator(context, {
|
|
5837
|
+
port,
|
|
5838
|
+
host: values.host,
|
|
5839
|
+
assetsDir
|
|
5840
|
+
});
|
|
5841
|
+
const { port: listening } = server.address();
|
|
5842
|
+
context.log(`OpenCRVS configurator: http://localhost:${listening}`);
|
|
5843
|
+
context.log(`Repository: ${context.root}`);
|
|
5844
|
+
context.log("Press Ctrl+C to stop.");
|
|
5845
|
+
await new Promise((resolve4) => {
|
|
5846
|
+
const stop = () => {
|
|
5847
|
+
server.close(() => resolve4());
|
|
5848
|
+
server.closeAllConnections();
|
|
5849
|
+
};
|
|
5850
|
+
process.once("SIGINT", stop);
|
|
5851
|
+
process.once("SIGTERM", stop);
|
|
5852
|
+
});
|
|
5853
|
+
}
|
|
5854
|
+
var describe = (change) => `${change.file} (${change.environments.join(", ")})`;
|
|
5855
|
+
function reportWorkflows(changes, context, dryRun) {
|
|
5856
|
+
for (const change of changes) {
|
|
5857
|
+
switch (change.status) {
|
|
5858
|
+
case "updated":
|
|
5859
|
+
context.log(
|
|
5860
|
+
` ${dryRun ? "would update" : "\u2713 updated"} ${describe(change)}`
|
|
5861
|
+
);
|
|
5862
|
+
break;
|
|
5863
|
+
case "unchanged":
|
|
5864
|
+
context.log(` = ${change.file} is up to date`);
|
|
5865
|
+
break;
|
|
5866
|
+
case "not-found":
|
|
5867
|
+
context.log(
|
|
5868
|
+
` \u21B7 skipped ${change.file} (not found in this repository)`
|
|
5869
|
+
);
|
|
5870
|
+
break;
|
|
5871
|
+
case "no-environments":
|
|
5872
|
+
context.log(` \u21B7 skipped ${change.file} (no environments to list)`);
|
|
5873
|
+
break;
|
|
5874
|
+
}
|
|
5875
|
+
for (const warning of change.warnings) {
|
|
5876
|
+
context.log(` \u26A0 ${change.file}: ${warning}`);
|
|
5877
|
+
}
|
|
5878
|
+
}
|
|
5879
|
+
}
|
|
5880
|
+
function updateWorkflows2(context, dryRun) {
|
|
5881
|
+
const { infrastructure, application } = listEnvironments(context.root);
|
|
5882
|
+
context.log(
|
|
5883
|
+
`Servers (inventory.yml): ${infrastructure.join(", ") || "none"}`
|
|
5884
|
+
);
|
|
5885
|
+
context.log(
|
|
5886
|
+
`Clusters (opencrvs-services): ${application.join(", ") || "none"}`
|
|
5887
|
+
);
|
|
5888
|
+
const changes = planWorkflowUpdates(context.root);
|
|
5889
|
+
context.log(`
|
|
5890
|
+
Workflows in ${WORKFLOWS_DIR}:`);
|
|
5891
|
+
if (!dryRun) applyWorkflowUpdates(context.root, changes);
|
|
5892
|
+
reportWorkflows(changes, context, dryRun);
|
|
5893
|
+
}
|
|
5894
|
+
async function runConfiguratorUpdateWorkflows(args2 = [], context = processContext()) {
|
|
5895
|
+
const { values } = (0, import_node_util.parseArgs)({
|
|
5896
|
+
args: args2,
|
|
5897
|
+
options: { "dry-run": { type: "boolean" } }
|
|
5898
|
+
});
|
|
5899
|
+
assertInfrastructureRepository(context.root);
|
|
5900
|
+
updateWorkflows2(context, values["dry-run"] ?? false);
|
|
5901
|
+
}
|
|
5902
|
+
async function runConfiguratorUpgrade(context = processContext()) {
|
|
5903
|
+
assertInfrastructureRepository(context.root);
|
|
5904
|
+
const migration = migrateLayout(context.root);
|
|
5905
|
+
if (migration.moved.length + migration.skipped.length === 0) {
|
|
5906
|
+
context.log(`No old inventory files found at ${OLD_INVENTORY_DIR2}`);
|
|
5907
|
+
} else {
|
|
5908
|
+
context.log("Migrating Ansible inventory files");
|
|
5909
|
+
for (const { from, to } of migration.moved) {
|
|
5910
|
+
context.log(` \u2713 moved ${from} -> ${to}`);
|
|
5911
|
+
}
|
|
5912
|
+
for (const { from, to } of migration.skipped) {
|
|
5913
|
+
context.log(` \u21B7 skipped ${from}: ${to} already exists`);
|
|
5914
|
+
}
|
|
5915
|
+
if (migration.oldDirectoryRemoved) {
|
|
5916
|
+
context.log(` \u2713 removed empty ${OLD_INVENTORY_DIR2}`);
|
|
5917
|
+
} else if ((0, import_node_fs6.existsSync)((0, import_node_path8.join)(context.root, OLD_INVENTORY_DIR2))) {
|
|
5918
|
+
context.log(
|
|
5919
|
+
` \u26A0 ${OLD_INVENTORY_DIR2} still has files in it, review it manually`
|
|
5920
|
+
);
|
|
5921
|
+
}
|
|
5922
|
+
}
|
|
5923
|
+
context.log("");
|
|
5924
|
+
updateWorkflows2(context, false);
|
|
5925
|
+
context.log(
|
|
5926
|
+
migration.moved.length > 0 ? "\n\u2713 Environment layout upgraded. Commit the changes into git." : "\n\u2713 Environment layout already uses the current structure."
|
|
5927
|
+
);
|
|
5928
|
+
}
|
|
5929
|
+
|
|
4906
5930
|
// src/verify/endpoints.ts
|
|
4907
5931
|
var import_colors = require("kleur/colors");
|
|
4908
5932
|
var z = __toESM(require("zod/v4"));
|
|
@@ -5290,6 +6314,7 @@ Usage: opencrvs <command>
|
|
|
5290
6314
|
|
|
5291
6315
|
Commands:
|
|
5292
6316
|
environment Manage deployment environments
|
|
6317
|
+
configurator Manage deployment environments in the browser
|
|
5293
6318
|
upgrade Upgrade an existing environment
|
|
5294
6319
|
verify-endpoints Verify the locally-running country config exposes the
|
|
5295
6320
|
expected endpoints, keeps secured ones locked down and
|
|
@@ -5347,6 +6372,8 @@ function main8() {
|
|
|
5347
6372
|
switch (command) {
|
|
5348
6373
|
case "environment":
|
|
5349
6374
|
return handleEnvironment();
|
|
6375
|
+
case "configurator":
|
|
6376
|
+
return handleConfigurator();
|
|
5350
6377
|
case "upgrade":
|
|
5351
6378
|
return handleUpgrade();
|
|
5352
6379
|
case "verify-endpoints":
|
|
@@ -5408,6 +6435,63 @@ async function runEnvironmentCommand(operation, command) {
|
|
|
5408
6435
|
process.exit(1);
|
|
5409
6436
|
}
|
|
5410
6437
|
}
|
|
6438
|
+
async function handleConfigurator() {
|
|
6439
|
+
const subcommand = args[1];
|
|
6440
|
+
const subcommandArgs = args.slice(2);
|
|
6441
|
+
if (!subcommand || subcommand === "--help" || subcommand === "-h") {
|
|
6442
|
+
console.log(
|
|
6443
|
+
`
|
|
6444
|
+
Usage: opencrvs configurator <subcommand>
|
|
6445
|
+
|
|
6446
|
+
Run it from the root of an OpenCRVS infrastructure repository.
|
|
6447
|
+
|
|
6448
|
+
Subcommands:
|
|
6449
|
+
init [--port <port>] [--host <host>]
|
|
6450
|
+
Create or update environments in the browser. Serves the
|
|
6451
|
+
configurator on http://localhost:4173 until stopped.
|
|
6452
|
+
In a container pass --host 0.0.0.0 and publish the port on
|
|
6453
|
+
the host's loopback only.
|
|
6454
|
+
upgrade Move the inventories of an old repository layout to
|
|
6455
|
+
environments/<name>/inventory.yml, then update the workflows
|
|
6456
|
+
update-workflows [--dry-run]
|
|
6457
|
+
Set the environments the GitHub workflows offer to the ones
|
|
6458
|
+
found in environments/
|
|
6459
|
+
`.trim()
|
|
6460
|
+
);
|
|
6461
|
+
process.exit(0);
|
|
6462
|
+
}
|
|
6463
|
+
const runConfiguratorCommand = async (operation, command) => {
|
|
6464
|
+
try {
|
|
6465
|
+
await command();
|
|
6466
|
+
} catch (error2) {
|
|
6467
|
+
console.error(
|
|
6468
|
+
`Configurator ${operation} failed:`,
|
|
6469
|
+
error2 instanceof Error ? error2.message : error2
|
|
6470
|
+
);
|
|
6471
|
+
process.exit(1);
|
|
6472
|
+
}
|
|
6473
|
+
};
|
|
6474
|
+
switch (subcommand) {
|
|
6475
|
+
case "init":
|
|
6476
|
+
return runConfiguratorCommand(
|
|
6477
|
+
"initialisation",
|
|
6478
|
+
() => runConfiguratorInit(subcommandArgs)
|
|
6479
|
+
);
|
|
6480
|
+
case "upgrade":
|
|
6481
|
+
return runConfiguratorCommand(
|
|
6482
|
+
"environment upgrade",
|
|
6483
|
+
() => runConfiguratorUpgrade()
|
|
6484
|
+
);
|
|
6485
|
+
case "update-workflows":
|
|
6486
|
+
return runConfiguratorCommand(
|
|
6487
|
+
"workflow update",
|
|
6488
|
+
() => runConfiguratorUpdateWorkflows(subcommandArgs)
|
|
6489
|
+
);
|
|
6490
|
+
default:
|
|
6491
|
+
console.error(`Unknown subcommand: configurator ${subcommand}`);
|
|
6492
|
+
process.exit(1);
|
|
6493
|
+
}
|
|
6494
|
+
}
|
|
5411
6495
|
function readToolkitVersion() {
|
|
5412
6496
|
const packageJson = JSON.parse(
|
|
5413
6497
|
import_fs9.default.readFileSync(import_path15.default.join(__dirname, "..", "package.json"), "utf-8")
|