@opencrvs/toolkit 2.0.0-rc.c2e6093 → 2.0.0-rc.c2f305f

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.
Files changed (98) hide show
  1. package/README.md +15 -0
  2. package/build.sh +3 -2
  3. package/dist/application-config/index.js +55 -35
  4. package/dist/cli.js +3251 -45
  5. package/dist/commons/api/router.d.ts +94 -17
  6. package/dist/commons/application-config/index.d.ts +1 -0
  7. package/dist/commons/conditionals/conditionals.d.ts +348 -4
  8. package/dist/commons/conditionals/validate.d.ts +51 -2
  9. package/dist/commons/events/ActionConfig.d.ts +28 -2
  10. package/dist/commons/events/ActionDocument.d.ts +6 -0
  11. package/dist/commons/events/ActionInput.d.ts +6 -0
  12. package/dist/commons/events/AdvancedSearchConfig.d.ts +342 -0
  13. package/dist/commons/events/CountryConfigQueryInput.d.ts +42 -0
  14. package/dist/commons/events/EventDocument.d.ts +3 -0
  15. package/dist/commons/events/EventIndex.d.ts +14 -0
  16. package/dist/commons/events/FieldConfig.d.ts +378 -50
  17. package/dist/commons/events/FieldType.d.ts +1 -0
  18. package/dist/commons/events/FieldTypeMapping.d.ts +1 -1
  19. package/dist/commons/events/FieldValue.d.ts +6 -0
  20. package/dist/commons/events/WorkqueueConfig.d.ts +39 -0
  21. package/dist/commons/events/field.d.ts +53 -1
  22. package/dist/commons/events/index.d.ts +1 -0
  23. package/dist/commons/events/state/flags.d.ts +1 -0
  24. package/dist/commons/events/state/utils.d.ts +12 -287
  25. package/dist/commons/events/utils.d.ts +12 -1
  26. package/dist/conditionals/index.js +48 -1
  27. package/dist/environment/custom-types.d.ts +33 -0
  28. package/dist/environment/custom-types.d.ts.map +1 -0
  29. package/dist/environment/custom-types.js +12 -0
  30. package/dist/environment/derived-variables.d.ts +134 -0
  31. package/dist/environment/derived-variables.d.ts.map +1 -0
  32. package/dist/environment/derived-variables.js +169 -0
  33. package/dist/environment/editor-questions.d.ts +4 -0
  34. package/dist/environment/editor-questions.d.ts.map +1 -0
  35. package/dist/environment/editor-questions.js +67 -0
  36. package/dist/environment/git.d.ts +5 -0
  37. package/dist/environment/git.d.ts.map +1 -0
  38. package/dist/environment/git.js +38 -0
  39. package/dist/environment/github.d.ts +26 -0
  40. package/dist/environment/github.d.ts.map +1 -0
  41. package/dist/environment/github.js +247 -0
  42. package/dist/environment/index.d.ts +5 -0
  43. package/dist/environment/index.d.ts.map +1 -0
  44. package/dist/environment/index.js +71 -0
  45. package/dist/environment/logger.d.ts +15 -0
  46. package/dist/environment/logger.d.ts.map +1 -0
  47. package/dist/environment/logger.js +25 -0
  48. package/dist/environment/manage-users.d.ts +11 -0
  49. package/dist/environment/manage-users.d.ts.map +1 -0
  50. package/dist/environment/manage-users.js +497 -0
  51. package/dist/environment/questions-helper.d.ts +15 -0
  52. package/dist/environment/questions-helper.d.ts.map +1 -0
  53. package/dist/environment/questions-helper.js +70 -0
  54. package/dist/environment/questions.d.ts +199 -0
  55. package/dist/environment/questions.d.ts.map +1 -0
  56. package/dist/environment/questions.js +412 -0
  57. package/dist/environment/setup-environment.d.ts +2 -0
  58. package/dist/environment/setup-environment.d.ts.map +1 -0
  59. package/dist/environment/setup-environment.js +794 -0
  60. package/dist/environment/ssh-keygen.d.ts +5 -0
  61. package/dist/environment/ssh-keygen.d.ts.map +1 -0
  62. package/dist/environment/ssh-keygen.js +21 -0
  63. package/dist/environment/swarm-to-k8s.d.ts +2 -0
  64. package/dist/environment/swarm-to-k8s.d.ts.map +1 -0
  65. package/dist/environment/swarm-to-k8s.js +173 -0
  66. package/dist/environment/templates.d.ts +27 -0
  67. package/dist/environment/templates.d.ts.map +1 -0
  68. package/dist/environment/templates.js +162 -0
  69. package/dist/environment/update-workflows.d.ts +2 -0
  70. package/dist/environment/update-workflows.d.ts.map +1 -0
  71. package/dist/environment/update-workflows.js +169 -0
  72. package/dist/environment/users.d.ts +2 -0
  73. package/dist/environment/users.d.ts.map +1 -0
  74. package/dist/environment/users.js +112 -0
  75. package/dist/environment/utils.d.ts +18 -0
  76. package/dist/environment/utils.d.ts.map +1 -0
  77. package/dist/environment/utils.js +100 -0
  78. package/dist/events/index.js +384 -148
  79. package/dist/migrations/v2.0/checkout-upstream-files.d.ts +1 -1
  80. package/dist/migrations/v2.0/checkout-upstream-files.d.ts.map +1 -1
  81. package/dist/migrations/v2.0/checkout-upstream-files.js +1 -1
  82. package/dist/migrations/v2.0/index.js +10 -4
  83. package/dist/notification/index.js +250 -40
  84. package/dist/scopes/index.d.ts +4 -6
  85. package/dist/scopes/index.js +9 -3
  86. package/dist/templates/charts-values/dependencies/values.override.yaml +4 -0
  87. package/dist/templates/charts-values/dependencies/values.yaml +83 -0
  88. package/dist/templates/charts-values/opencrvs-services/values.override.yaml +4 -0
  89. package/dist/templates/charts-values/opencrvs-services/values.yaml +80 -0
  90. package/dist/templates/charts-values/traefik/values.override.yaml +4 -0
  91. package/dist/templates/charts-values/traefik/values.yaml +115 -0
  92. package/dist/templates/inventory/inventory.template.yml +100 -0
  93. package/dist/templates/inventory/production.example.yml +77 -0
  94. package/dist/templates/inventory/qa.example.yml +48 -0
  95. package/opencrvs-toolkit-2.0.0-rc.c2f305f.tgz +0 -0
  96. package/package.json +15 -1
  97. package/tsconfig.tsbuildinfo +1 -1
  98. package/opencrvs-toolkit-2.0.0-rc.c2e6093.tgz +0 -0
@@ -0,0 +1,5 @@
1
+ export declare function generateSSHKeyPair(): {
2
+ publicKey: string;
3
+ privateKey: string;
4
+ };
5
+ //# sourceMappingURL=ssh-keygen.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ssh-keygen.d.ts","sourceRoot":"","sources":["../../src/environment/ssh-keygen.ts"],"names":[],"mappings":"AAaA,wBAAgB,kBAAkB,IAAI;IACpC,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,EAAE,MAAM,CAAA;CACnB,CAMA"}
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ *
7
+ * OpenCRVS is also distributed under the terms of the Civil Registration
8
+ * & Healthcare Disclaimer located at http://opencrvs.org/license.
9
+ *
10
+ * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
11
+ */
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.generateSSHKeyPair = generateSSHKeyPair;
14
+ const ssh2_1 = require("ssh2");
15
+ function generateSSHKeyPair() {
16
+ const keys = ssh2_1.utils.generateKeyPairSync('ed25519');
17
+ return {
18
+ publicKey: keys.public.toString(),
19
+ privateKey: keys.private.toString()
20
+ };
21
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=swarm-to-k8s.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swarm-to-k8s.d.ts","sourceRoot":"","sources":["../../src/environment/swarm-to-k8s.ts"],"names":[],"mappings":""}
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ *
7
+ * OpenCRVS is also distributed under the terms of the Civil Registration
8
+ * & Healthcare Disclaimer located at http://opencrvs.org/license.
9
+ *
10
+ * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
11
+ */
12
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ var desc = Object.getOwnPropertyDescriptor(m, k);
15
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
16
+ desc = { enumerable: true, get: function() { return m[k]; } };
17
+ }
18
+ Object.defineProperty(o, k2, desc);
19
+ }) : (function(o, m, k, k2) {
20
+ if (k2 === undefined) k2 = k;
21
+ o[k2] = m[k];
22
+ }));
23
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
24
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
25
+ }) : function(o, v) {
26
+ o["default"] = v;
27
+ });
28
+ var __importStar = (this && this.__importStar) || function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
42
+ });
43
+ };
44
+ var __importDefault = (this && this.__importDefault) || function (mod) {
45
+ return (mod && mod.__esModule) ? mod : { "default": mod };
46
+ };
47
+ Object.defineProperty(exports, "__esModule", { value: true });
48
+ const path = __importStar(require("path"));
49
+ /* eslint-disable no-console */
50
+ const kleur_1 = __importDefault(require("kleur"));
51
+ const logger_1 = require("./logger");
52
+ const utils_1 = require("./utils");
53
+ const templates_1 = require("./templates");
54
+ const core_1 = require("@octokit/core");
55
+ const ssh_keygen_1 = require("./ssh-keygen");
56
+ const github_1 = require("./github");
57
+ (() => __awaiter(void 0, void 0, void 0, function* () {
58
+ (0, logger_1.log)(kleur_1.default.bold('------------------------------------------------\n' +
59
+ 'OpenCRVS Infrastructure migration script: \n' +
60
+ 'Migrating Swarm configurations to Kubernetes\n' +
61
+ '------------------------------------------------\n'));
62
+ const environment_type = process.env.ENVIRONMENT_TYPE || 'production';
63
+ const environment = process.env.ENVIRONMENT || '';
64
+ const githubToken = process.env.INFRA_TOKEN || '';
65
+ const githubOrganisation = process.env.INFRA_ORGANISATION || '';
66
+ const githubRepository = process.env.INFRA_REPOSITORY || '';
67
+ if (!environment) {
68
+ (0, logger_1.error)('\n', 'Environment variable ENVIRONMENT is not set. Exiting.');
69
+ process.exit(1);
70
+ }
71
+ if (!githubToken) {
72
+ (0, logger_1.error)('\n', 'Environment variable GITHUB_TOKEN is not set. Exiting.');
73
+ process.exit(1);
74
+ }
75
+ if (!githubOrganisation) {
76
+ (0, logger_1.error)('\n', 'Environment variable INFRA_ORGANISATION is not set. Exiting.');
77
+ process.exit(1);
78
+ }
79
+ if (!githubRepository) {
80
+ (0, logger_1.error)('\n', 'Environment variable INFRA_REPOSITORY is not set. Exiting.');
81
+ process.exit(1);
82
+ }
83
+ if (['backup', 'jumpbox'].includes(environment)) {
84
+ (0, logger_1.info)(` > ${environment} environment will not be migrated, see migration notes`);
85
+ process.exit(0);
86
+ }
87
+ (0, logger_1.log)(kleur_1.default.bold().underline('Migration properties:'));
88
+ (0, logger_1.log)(` ✓ Environment: ${environment}`);
89
+ const old_inventory_path = process.env.OLD_INVENTORY_PATH || '';
90
+ if (!old_inventory_path) {
91
+ (0, logger_1.error)('\n', 'Environment variable OLD_INVENTORY_PATH is not set. Exiting.');
92
+ (0, logger_1.log)('\n', 'Old inventory path is required to read existing Swarm configurations.');
93
+ process.exit(1);
94
+ }
95
+ const ansible_inventory = path.join(old_inventory_path, environment + '.yml');
96
+ const data = (0, utils_1.readYamlFile)(ansible_inventory);
97
+ (0, logger_1.log)(` ✓ Loaded old inventory file: ${ansible_inventory}`);
98
+ const master = (0, templates_1.dockerManagerFirst)(data) || '';
99
+ (0, logger_1.log)(` ✓ Kubernetes API Host (Docker Manager): ${master}`);
100
+ const users = (0, templates_1.extractAndModifyUsers)(data);
101
+ const kube_worker_nodes = (0, templates_1.extractWorkerNodes)(data);
102
+ (0, logger_1.log)(` ✓ Worker nodes: ${kube_worker_nodes.join(', ')}`);
103
+ const backup_host = (0, templates_1.extractBackupNode)(data);
104
+ (0, logger_1.log)(` ✓ Backup host: ${backup_host}`);
105
+ (0, templates_1.generateInventory)(environment, {
106
+ kube_worker_nodes: kube_worker_nodes,
107
+ users: users,
108
+ backup_host: environment === 'production' ? backup_host : '',
109
+ kube_api_host: master
110
+ });
111
+ (0, templates_1.copyChartsValues)(environment, {
112
+ env: environment,
113
+ environment_type: environment_type,
114
+ // FIXME: In general that should be environment_type,
115
+ // Hardcode like this blocks us from being generic:
116
+ // https://github.com/opencrvs/opencrvs-core/issues/11171
117
+ two_fa_enabled: environment !== 'production' ? false : true,
118
+ backup_enabled: environment === 'production' ? 'true' : 'false',
119
+ restore_enabled: environment === 'staging' ? 'true' : 'false',
120
+ restore_environment_name: environment === 'staging' ? 'production' : '',
121
+ restore_type: 'dump',
122
+ traefik_mode: 'static_ssl',
123
+ backup_type: 'dump'
124
+ });
125
+ if (environment === 'staging') {
126
+ const octokit = new core_1.Octokit({
127
+ auth: githubToken
128
+ });
129
+ const repositoryId = yield (0, github_1.getRepositoryId)(octokit, githubOrganisation, githubRepository);
130
+ try {
131
+ yield (0, github_1.createEnvironmentVariable)(octokit, repositoryId, environment, 'RESTORE_ENVIRONMENT_NAME', 'production');
132
+ }
133
+ catch (err) {
134
+ (0, logger_1.error)(`Failed to create variable RESTORE_ENVIRONMENT_NAME for environment ${environment}:`, err);
135
+ }
136
+ }
137
+ if (environment === 'production') {
138
+ const octokit = new core_1.Octokit({
139
+ auth: githubToken
140
+ });
141
+ console.log('Fetching repository ID for:', githubOrganisation, githubRepository);
142
+ const repositoryId = yield (0, github_1.getRepositoryId)(octokit, githubOrganisation, githubRepository);
143
+ console.log('Repository ID:', repositoryId);
144
+ const { publicKey, privateKey } = (0, ssh_keygen_1.generateSSHKeyPair)();
145
+ const secretsToCreate = {
146
+ BACKUP_HOST_PRIVATE_KEY: privateKey,
147
+ BACKUP_HOST_PUBLIC_KEY: publicKey,
148
+ BACKUP_SERVER_USER: 'backup'
149
+ };
150
+ const variablesToCreate = {
151
+ BACKUP_HOST: backup_host,
152
+ KUBE_WORKER_NODES: kube_worker_nodes.join(',')
153
+ };
154
+ for (const [secretName, secretValue] of Object.entries(secretsToCreate)) {
155
+ try {
156
+ yield (0, github_1.createEnvironmentSecret)(octokit, repositoryId, environment, secretName, secretValue, githubOrganisation, githubRepository);
157
+ (0, logger_1.log)(` ✓ Created secret ${secretName} for environment ${environment}`);
158
+ }
159
+ catch (err) {
160
+ (0, logger_1.error)(`Failed to create secret ${secretName} for environment ${environment}:`, err);
161
+ }
162
+ }
163
+ for (const [variableName, variableValue] of Object.entries(variablesToCreate)) {
164
+ try {
165
+ yield (0, github_1.createEnvironmentVariable)(octokit, repositoryId, environment, variableName, variableValue);
166
+ (0, logger_1.log)(` ✓ Created variable ${variableName} for environment ${environment}`);
167
+ }
168
+ catch (err) {
169
+ (0, logger_1.error)(`Failed to create variable ${variableName} for environment ${environment}:`, err);
170
+ }
171
+ }
172
+ }
173
+ }))();
@@ -0,0 +1,27 @@
1
+ export declare function getUsers(data: any): any;
2
+ export declare function extractAndModifyUsers(data: any): any;
3
+ export declare function dockerManagerFirst(data: any): string;
4
+ export declare function extractBackupNode(data: any): string;
5
+ export declare function extractWorkerNodes(data: any): string[];
6
+ /**
7
+ * Copy charts-values directory into environments/<env>
8
+ * @param env Environment name
9
+ * Usage: copyChartsValues('dev')
10
+ */
11
+ /**
12
+ * Recursively copy a directory and replace placeholders in text files.
13
+ */
14
+ export declare function copyChartsValues(env: string, values: Record<string, string | boolean>): void;
15
+ /**
16
+ * Generate Ansible inventory file from template
17
+ * @param env Environment name
18
+ * @param number_of_servers Number of servers (1 for single-node, >1 for multi-node)
19
+ * @param values Key-value pairs to replace in the template
20
+ * Usage: generateInventory('dev', 1, {
21
+ * HOST: process.env.APP_HOST || "localhost",
22
+ * USER: process.env.SSH_USER || "ubuntu",
23
+ * PORT: process.env.SSH_PORT || "22"
24
+ * });
25
+ */
26
+ export declare function generateInventory(env: string, values: Record<string, any>): void;
27
+ //# sourceMappingURL=templates.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../../src/environment/templates.ts"],"names":[],"mappings":"AAsBA,wBAAgB,QAAQ,CAAC,IAAI,EAAE,GAAG,GAAG,GAAG,CAKvC;AAID,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,GAAG,GAAG,GAAG,CAYpD;AAED,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM,CAWpD;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM,CASnD;AAED,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM,EAAE,CAStD;AAGD;;;;GAIG;AACH;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,QAyCzC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QA4BzE"}
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ *
7
+ * OpenCRVS is also distributed under the terms of the Civil Registration
8
+ * & Healthcare Disclaimer located at http://opencrvs.org/license.
9
+ *
10
+ * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
11
+ */
12
+ var __rest = (this && this.__rest) || function (s, e) {
13
+ var t = {};
14
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
15
+ t[p] = s[p];
16
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
17
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
18
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
19
+ t[p[i]] = s[p[i]];
20
+ }
21
+ return t;
22
+ };
23
+ var __importDefault = (this && this.__importDefault) || function (mod) {
24
+ return (mod && mod.__esModule) ? mod : { "default": mod };
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.getUsers = getUsers;
28
+ exports.extractAndModifyUsers = extractAndModifyUsers;
29
+ exports.dockerManagerFirst = dockerManagerFirst;
30
+ exports.extractBackupNode = extractBackupNode;
31
+ exports.extractWorkerNodes = extractWorkerNodes;
32
+ exports.copyChartsValues = copyChartsValues;
33
+ exports.generateInventory = generateInventory;
34
+ const fs_1 = __importDefault(require("fs"));
35
+ const path_1 = __importDefault(require("path"));
36
+ const logger_1 = require("./logger");
37
+ const handlebars_1 = __importDefault(require("handlebars"));
38
+ // Register a helper to increment numbers
39
+ handlebars_1.default.registerHelper('data_label_idx', function (value) {
40
+ return parseInt(value) + 2;
41
+ });
42
+ function getUsers(data) {
43
+ var _a, _b;
44
+ if (!((_b = (_a = data === null || data === void 0 ? void 0 : data.all) === null || _a === void 0 ? void 0 : _a.vars) === null || _b === void 0 ? void 0 : _b.users)) {
45
+ return { users: [] };
46
+ }
47
+ return data.all.vars.users;
48
+ }
49
+ // START: TEMPORAL SECTION FOR TRANSITION FROM DOCKER SWARM TO K8s
50
+ // Extract users from the old inventory
51
+ // Docker swarm format to new
52
+ function extractAndModifyUsers(data) {
53
+ var _a, _b;
54
+ if (!((_b = (_a = data === null || data === void 0 ? void 0 : data.all) === null || _a === void 0 ? void 0 : _a.vars) === null || _b === void 0 ? void 0 : _b.users)) {
55
+ return { users: [] };
56
+ }
57
+ const users = data.all.vars.users.map((user) => {
58
+ const { sudoer } = user, rest = __rest(user, ["sudoer"]);
59
+ return Object.assign(Object.assign({}, rest), { role: sudoer ? 'admin' : 'operator' });
60
+ });
61
+ return users;
62
+ }
63
+ function dockerManagerFirst(data) {
64
+ var _a;
65
+ if (!((_a = data === null || data === void 0 ? void 0 : data['docker-manager-first']) === null || _a === void 0 ? void 0 : _a.hosts)) {
66
+ throw new Error('Invalid YAML structure: missing docker-manager-first.hosts');
67
+ }
68
+ const hosts = data['docker-manager-first'].hosts;
69
+ const dockerManagerFirst = Object.values(hosts)
70
+ .filter((host) => host.ansible_host)
71
+ .map((host) => host.ansible_host);
72
+ return dockerManagerFirst.length === 1 ? dockerManagerFirst[0] : '';
73
+ }
74
+ function extractBackupNode(data) {
75
+ var _a;
76
+ if (!((_a = data === null || data === void 0 ? void 0 : data['backups']) === null || _a === void 0 ? void 0 : _a.hosts)) {
77
+ return '';
78
+ }
79
+ const hosts = data['backups'].hosts;
80
+ const backupHostEntry = Object.values(hosts)
81
+ .filter((host) => host.ansible_host)
82
+ .map((host) => host.ansible_host);
83
+ return backupHostEntry.length === 1 ? backupHostEntry[0] : '';
84
+ }
85
+ function extractWorkerNodes(data) {
86
+ var _a;
87
+ if (!((_a = data === null || data === void 0 ? void 0 : data['docker-workers']) === null || _a === void 0 ? void 0 : _a.hosts)) {
88
+ return [];
89
+ }
90
+ const hosts = data['docker-workers'].hosts;
91
+ const worker_hosts = Object.values(hosts)
92
+ .filter((host) => host.ansible_host)
93
+ .map((host) => host.ansible_host);
94
+ return worker_hosts;
95
+ }
96
+ // END: TEMPORAL SECTION FOR TRANSITION FROM DOCKER SWARM TO K8s
97
+ /**
98
+ * Copy charts-values directory into environments/<env>
99
+ * @param env Environment name
100
+ * Usage: copyChartsValues('dev')
101
+ */
102
+ /**
103
+ * Recursively copy a directory and replace placeholders in text files.
104
+ */
105
+ function copyChartsValues(env, values) {
106
+ const srcDir = path_1.default.resolve(__dirname, 'templates', 'charts-values');
107
+ const destDir = path_1.default.resolve(process.cwd(), 'environments', env);
108
+ fs_1.default.mkdirSync(destDir, { recursive: true });
109
+ values['lets_encrypt'] = values['traefik_mode'] === 'lets_encrypt';
110
+ values['static_ssl'] = values['traefik_mode'] === 'static_ssl';
111
+ function copyRecursive(src, dest) {
112
+ const stat = fs_1.default.statSync(src);
113
+ if (stat.isDirectory()) {
114
+ fs_1.default.mkdirSync(dest, { recursive: true });
115
+ for (const item of fs_1.default.readdirSync(src)) {
116
+ copyRecursive(path_1.default.join(src, item), path_1.default.join(dest, item));
117
+ }
118
+ }
119
+ else {
120
+ const content = fs_1.default.readFileSync(src, 'utf8');
121
+ const template = handlebars_1.default.compile(content);
122
+ const updated = template(values);
123
+ const isOverridesFile = dest.endsWith('override.yaml');
124
+ // Skip overwrite for existing overrides.yaml
125
+ if (isOverridesFile && fs_1.default.existsSync(dest)) {
126
+ (0, logger_1.log)(` ↷ Skipped existing overrides file: ${dest}`);
127
+ return;
128
+ }
129
+ fs_1.default.writeFileSync(dest, updated, 'utf8');
130
+ (0, logger_1.log)(` ✓ Created: ${dest}`);
131
+ }
132
+ }
133
+ (0, logger_1.log)(`\n📋 Copying charts-values templates to ${destDir}:`);
134
+ copyRecursive(srcDir, destDir);
135
+ (0, logger_1.success)(`✅ Completed copying charts-values.\n`);
136
+ }
137
+ /**
138
+ * Generate Ansible inventory file from template
139
+ * @param env Environment name
140
+ * @param number_of_servers Number of servers (1 for single-node, >1 for multi-node)
141
+ * @param values Key-value pairs to replace in the template
142
+ * Usage: generateInventory('dev', 1, {
143
+ * HOST: process.env.APP_HOST || "localhost",
144
+ * USER: process.env.SSH_USER || "ubuntu",
145
+ * PORT: process.env.SSH_PORT || "22"
146
+ * });
147
+ */
148
+ function generateInventory(env, values) {
149
+ // Template and output paths
150
+ const templatePath = path_1.default.join(__dirname, 'templates', 'inventory', 'inventory.template.yml');
151
+ const outputPath = path_1.default.join(process.cwd(), 'infrastructure', 'server-setup', 'inventory', `${env}.yml`);
152
+ const templateFile = fs_1.default.readFileSync(templatePath, 'utf-8');
153
+ const template = handlebars_1.default.compile(templateFile);
154
+ values['single_node'] =
155
+ values['kube_worker_nodes'].length > 0 || values['backup_host']
156
+ ? 'false'
157
+ : 'true';
158
+ const updated = template(values);
159
+ fs_1.default.mkdirSync(path_1.default.dirname(outputPath), { recursive: true });
160
+ fs_1.default.writeFileSync(outputPath, updated);
161
+ (0, logger_1.log)(`\n✅ Generated inventory file at ${outputPath}\n`);
162
+ }
@@ -0,0 +1,2 @@
1
+ export declare function updateWorkflowEnvironments(): Promise<void>;
2
+ //# sourceMappingURL=update-workflows.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"update-workflows.d.ts","sourceRoot":"","sources":["../../src/environment/update-workflows.ts"],"names":[],"mappings":"AAgHA,wBAAsB,0BAA0B,IAAI,OAAO,CAAC,IAAI,CAAC,CAgDhE"}
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ *
7
+ * OpenCRVS is also distributed under the terms of the Civil Registration
8
+ * & Healthcare Disclaimer located at http://opencrvs.org/license.
9
+ *
10
+ * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
11
+ */
12
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ var desc = Object.getOwnPropertyDescriptor(m, k);
15
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
16
+ desc = { enumerable: true, get: function() { return m[k]; } };
17
+ }
18
+ Object.defineProperty(o, k2, desc);
19
+ }) : (function(o, m, k, k2) {
20
+ if (k2 === undefined) k2 = k;
21
+ o[k2] = m[k];
22
+ }));
23
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
24
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
25
+ }) : function(o, v) {
26
+ o["default"] = v;
27
+ });
28
+ var __importStar = (this && this.__importStar) || function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
42
+ });
43
+ };
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ exports.updateWorkflowEnvironments = updateWorkflowEnvironments;
46
+ const promises_1 = require("fs/promises");
47
+ /* eslint-disable no-console */
48
+ const fs_1 = require("fs");
49
+ const path_1 = require("path");
50
+ const glob = __importStar(require("glob"));
51
+ const yaml = __importStar(require("js-yaml"));
52
+ const logger_1 = require("./logger");
53
+ function extractInfrastructureNames() {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ const files = glob.sync('infrastructure/server-setup/inventory/*.yml');
56
+ const infraEnvironments = files.map((file) => (0, path_1.basename)(file, '.yml'));
57
+ if (infraEnvironments.length === 0) {
58
+ console.log('⚠️ Warning: No environment directories found in infrastructure/server-setup/inventory/');
59
+ return [];
60
+ }
61
+ (0, logger_1.log)('🔍 Found infrastructure configurations:', infraEnvironments.join(', '));
62
+ return infraEnvironments;
63
+ });
64
+ }
65
+ function extractEnvironmentNames() {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const entries = yield (0, promises_1.readdir)('environments');
68
+ // Filter only directories
69
+ const environments = entries.filter((entry) => {
70
+ const fullPath = (0, path_1.join)('environments', entry);
71
+ return (0, fs_1.statSync)(fullPath).isDirectory();
72
+ });
73
+ if (environments.length === 0) {
74
+ console.log('⚠️ Warning: No environment directories found in environments/');
75
+ return [];
76
+ }
77
+ (0, logger_1.log)('🔍 Found OpenCRVS configurations:', environments.join(', '));
78
+ return environments;
79
+ });
80
+ }
81
+ function updateOptionsInYaml(content, envList) {
82
+ // Find the options array pattern
83
+ // Matches: options: followed by array items (- item)
84
+ const optionsRegex = /([ ]*options:[ ]*\n)((?:[ ]*-[^\n]*\n)+)/;
85
+ const match = content.match(optionsRegex);
86
+ if (!match) {
87
+ throw new Error('Could not find options array in workflow file');
88
+ }
89
+ // Get the indentation from the first array item
90
+ const firstItemMatch = match[2].match(/^([ ]*)-/);
91
+ const itemIndent = firstItemMatch ? firstItemMatch[1] : ' ';
92
+ // Create new options array
93
+ const newOptions = match[1] + envList.map((env) => `${itemIndent}- ${env}`).join('\n') + '\n';
94
+ // Replace the old options array with the new one
95
+ return content.replace(optionsRegex, newOptions);
96
+ }
97
+ function updateWorkflows(envList, config) {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ var _a, _b, _c, _d;
100
+ const { workflows } = config;
101
+ for (const workflowPath of workflows) {
102
+ try {
103
+ const fileContents = (0, fs_1.readFileSync)(workflowPath, 'utf8');
104
+ // Verify the file is valid YAML and has the expected structure
105
+ const workflowData = yaml.load(fileContents);
106
+ if (!((_d = (_c = (_b = (_a = workflowData === null || workflowData === void 0 ? void 0 : workflowData.on) === null || _a === void 0 ? void 0 : _a.workflow_dispatch) === null || _b === void 0 ? void 0 : _b.inputs) === null || _c === void 0 ? void 0 : _c.environment) === null || _d === void 0 ? void 0 : _d.options)) {
107
+ throw new Error('Workflow does not have the expected structure: on.workflow_dispatch.inputs.environment.options');
108
+ }
109
+ // Update only the options array while preserving everything else
110
+ const updatedContent = updateOptionsInYaml(fileContents, envList);
111
+ (0, fs_1.writeFileSync)(workflowPath, updatedContent, 'utf8');
112
+ (0, logger_1.log)(` ✓ Successfully updated ${workflowPath}`);
113
+ }
114
+ catch (error) {
115
+ console.error(`\n⚠️ Error updating ${workflowPath} with environments: [${envList.join(', ')}]`);
116
+ console.error(`✗ Failed to update ${workflowPath}:`, error);
117
+ throw error;
118
+ }
119
+ }
120
+ });
121
+ }
122
+ function updateWorkflowEnvironments() {
123
+ return __awaiter(this, void 0, void 0, function* () {
124
+ try {
125
+ let didUpdateWorkflows = false;
126
+ // Extract infrastructure names
127
+ const infraEnvironments = yield extractInfrastructureNames();
128
+ if (infraEnvironments.length > 0) {
129
+ console.log('🔄 Updating infrastructure workflows:');
130
+ yield updateWorkflows(infraEnvironments, {
131
+ workflows: [
132
+ '.github/workflows/provision.yml',
133
+ '.github/workflows/reset-2fa.yml'
134
+ ],
135
+ path: 'on.workflow_dispatch.inputs.environment.options'
136
+ });
137
+ didUpdateWorkflows = true;
138
+ }
139
+ // Extract environment names (only directories)
140
+ const environments = yield extractEnvironmentNames();
141
+ if (environments.length > 0) {
142
+ const workflows = [
143
+ '.github/workflows/deploy-dependencies.yml',
144
+ '.github/workflows/deploy-opencrvs.yml',
145
+ '.github/workflows/clear-all-data.yml',
146
+ '.github/workflows/seed-data.yml',
147
+ '.github/workflows/reindex.yml',
148
+ '.github/workflows/github-to-k8s-sync-env.yml'
149
+ ];
150
+ (0, logger_1.log)('📋 Updating OpenCRVS application workflows:');
151
+ yield updateWorkflows(environments, {
152
+ workflows,
153
+ path: 'on.workflow_dispatch.inputs.environment.options'
154
+ });
155
+ didUpdateWorkflows = true;
156
+ }
157
+ if (didUpdateWorkflows) {
158
+ (0, logger_1.success)('✅ Workflow updates completed successfully!');
159
+ }
160
+ else {
161
+ (0, logger_1.log)('ℹ️ No workflows were updated.');
162
+ }
163
+ }
164
+ catch (error) {
165
+ console.error('\n❌ Error updating workflows:', error);
166
+ process.exit(1);
167
+ }
168
+ });
169
+ }
@@ -0,0 +1,2 @@
1
+ export declare function manageEnvironmentUsers(): Promise<void>;
2
+ //# sourceMappingURL=users.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"users.d.ts","sourceRoot":"","sources":["../../src/environment/users.ts"],"names":[],"mappings":"AA4EA,wBAAsB,sBAAsB,kBAO3C"}