@docupace/pipeline 1.0.5-alpha.25 → 1.0.5-alpha.25.ervins.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docupace/pipeline",
3
- "version": "1.0.5-alpha.25",
3
+ "version": "1.0.5-alpha.25.ervins.10",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -22,7 +22,7 @@
22
22
  "lodash": "^4.17.21",
23
23
  "openai": "^4.76.3",
24
24
  "zod": "^3.24.1",
25
- "zx": "^8.2.4"
25
+ "zx": "^8.3.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@eslint/js": "^9.17.0",
@@ -0,0 +1,36 @@
1
+ import { $ } from 'zx';
2
+
3
+ export async function addApacheSite() {
4
+ if (
5
+ !process.env.ENVIRONMENT ||
6
+ (process.env.ENVIRONMENT !== 'dev' && process.env.ENVIRONMENT !== 'qa')
7
+ ) {
8
+ log("Error: ENVIRONMENT must be either 'dev' or 'qa'.");
9
+ process.exit(1);
10
+ }
11
+ if (!process.env.SITE) {
12
+ log('Error: SITE cannot be empty.');
13
+ process.exit(1);
14
+ }
15
+
16
+ // Save private key from base64
17
+ const haproxyPrivateKey = process.env.HAPROXY_PRIVATE_KEY;
18
+ if (!haproxyPrivateKey) {
19
+ log('Error: HAPROXY_PRIVATE_KEY not set.');
20
+ process.exit(1);
21
+ }
22
+
23
+ await fs.writeFile('private.key', Buffer.from(haproxyPrivateKey, 'base64'));
24
+ await fs.chmod('private.key', 0o600);
25
+
26
+ const server = `${process.env.ENVIRONMENT}-apalb01a.corp.docupace.com`;
27
+ console.log("Running command");
28
+ const sshCmd = (script) =>
29
+ $`ssh -i private.key -o StrictHostKeyChecking=no root@${server} ${script}`;
30
+
31
+ // The script is run twice in the Bash version
32
+ await sshCmd(`/root/repo/iis-preprod/scripts/addsite/add_core_site.sh ${process.env.ENVIRONMENT} ${process.env.SITE}`);
33
+ //await sshCmd(`/root/repo/iis-preprod/scripts/addsite/add_core_site.sh ${process.env.ENVIRONMENT} ${process.env.SITE}`);
34
+ }
35
+
36
+
package/src/next/cli.js CHANGED
@@ -7,6 +7,10 @@ import { publish } from './publish.js';
7
7
  import { deployFromSite } from './deploy.js';
8
8
  import { deployTemplate } from './deploy.js';
9
9
  import { copy } from './copy.js';
10
+ import { vaultAddSiteData } from './vault.js';
11
+ import { addApacheSite } from './apache.js';
12
+ import { haproxySync } from './haproxy.js';
13
+ import { connectClone } from './connect.js';
10
14
  import dotenv from 'dotenv';
11
15
 
12
16
  dotenv.config({ path: ['.env.pipeline.local', 'config/.env.pipeline.local'] });
@@ -17,6 +21,7 @@ const program = new Command();
17
21
  import { $ } from 'zx';
18
22
  import { deployCore } from './jenkins.js';
19
23
  import { runPipeline } from './pipline.js';
24
+
20
25
  const GIT_SHA = (await $`git log --pretty=format:'%h' -n 1`).stdout.trim();
21
26
 
22
27
  program
@@ -33,6 +38,39 @@ program
33
38
  await sonar(GIT_SHA);
34
39
  });
35
40
 
41
+ program
42
+ .command('addApacheSite')
43
+ .description('Add site to apache')
44
+ .action(async () => {
45
+ await addApacheSite();
46
+ });
47
+
48
+ program
49
+ .command('connectClone')
50
+ .description('Clone connect site')
51
+ .action(async () => {
52
+ await connectClone();
53
+ });
54
+
55
+
56
+ program
57
+ .command('haproxySync')
58
+ .description('Sync haproxy config')
59
+ .action(async () => {
60
+ await haproxySync();
61
+ });
62
+
63
+
64
+
65
+ program
66
+ .command('vaultAddSiteData')
67
+ .description('Add m2m auth0 data to vault')
68
+ .requiredOption('-s, --site-name <siteName>', 'Site name')
69
+ .action(async (options) => {
70
+ const { siteName, } = options;
71
+ await vaultAddSiteData(siteName);
72
+ });
73
+
36
74
  program
37
75
  .command('publish')
38
76
  .description('Publish a Docker image')
@@ -83,7 +121,8 @@ program
83
121
  });
84
122
  });
85
123
 
86
- program
124
+
125
+ program
87
126
  .command('copy')
88
127
  .description('Copy a Docker image between registries')
89
128
  .requiredOption('-a, --application <application>', 'Application name')
@@ -0,0 +1,212 @@
1
+ import 'zx/globals'; // npm i zx
2
+ import fs from 'fs-extra';
3
+ import path from 'path';
4
+ import axios from 'axios';
5
+ import { $ } from 'zx';
6
+
7
+
8
+ process.env.COMMON_VAULT_PATH = process.env.COMMON_VAULT_PATH || 'secret/data/common/config';
9
+ process.env.VAULT_SKIP_VERIFY = process.env.VAULT_SKIP_VERIFY || 'true';
10
+ process.env.CONNECT_DIR = process.env.CONNECT_DIR || 'connect';
11
+
12
+
13
+ function log(msg) {
14
+ console.log(`[*] ${msg}`);
15
+ }
16
+
17
+ /* --------------------------------
18
+ * 1. GIT CHECKOUT
19
+ * -------------------------------- */
20
+ async function gitCheckout(repository, destDir, branch = 'master') {
21
+ if (!(await fs.pathExists(destDir))) {
22
+ log(`Directory ${destDir} does not exist. Cloning repository...`);
23
+ } else {
24
+ log(`Directory ${destDir} exists. Removing it...`);
25
+ await fs.remove(destDir);
26
+ log(`Cloning ${destDir} repository...`);
27
+ }
28
+
29
+ // Since we use zx, the environment variables can be read from process.env
30
+ // e.g. $BITBUCKET_USERNAME, $BITBUCKET_APP_PASSWORD
31
+ const username = process.env.BITBUCKET_USERNAME;
32
+ const password = process.env.BITBUCKET_APP_PASSWORD;
33
+
34
+ // Construct clone command
35
+ const cloneUrl = `https://${username}:${password}@bitbucket.org/dp-cloud/${repository}.git`;
36
+
37
+ // Perform the clone
38
+ await $`git clone --branch=${branch} --depth 1 ${cloneUrl} ${destDir}`;
39
+ }
40
+
41
+ /* --------------------------------
42
+ * 2. CHECK VAULT
43
+ * -------------------------------- */
44
+ async function checkVault(vaultPath) {
45
+ if (!vaultPath) {
46
+ log('Error: VAULT_PATH is required.');
47
+ process.exit(1);
48
+ }
49
+
50
+ if (!process.env.VAULT_TOKEN || !process.env.VAULT_ADDR) {
51
+ log('Error: VAULT_TOKEN and VAULT_ADDR must be set.');
52
+ process.exit(1);
53
+ }
54
+
55
+ log(`Checking if ${vaultPath} exists and has data...`);
56
+
57
+ try {
58
+ const vaultAddr = process.env.VAULT_ADDR;
59
+ const response = await axios.get(`${vaultAddr}/v1/${vaultPath}`, {
60
+ headers: {
61
+ 'X-Vault-Token': process.env.VAULT_TOKEN,
62
+ },
63
+ // If you want to skip TLS verification, you might need a custom httpsAgent
64
+ // or globally define NODE_TLS_REJECT_UNAUTHORIZED=0.
65
+ // Here we demonstrate a minimal approach:
66
+ httpsAgent: new (require('https').Agent)({ rejectUnauthorized: false }),
67
+ });
68
+
69
+ // If data exists
70
+ if (response.data?.data?.data && Object.keys(response.data.data.data).length > 0) {
71
+ log(`Data exists at ${vaultPath}`);
72
+ } else if (response.data?.warnings) {
73
+ log(`Warning: ${response.data.warnings.join(', ')}`);
74
+ process.exit(1);
75
+ } else {
76
+ log(`No data found at ${vaultPath} or path does not exist.`);
77
+ }
78
+ } catch (error) {
79
+ log(`Error: Failed to connect to Vault server at ${process.env.VAULT_ADDR}`);
80
+ process.exit(1);
81
+ }
82
+ }
83
+
84
+ /* --------------------------------
85
+ * 3. GET VAULT SECRETS
86
+ * -------------------------------- */
87
+ async function getVaultSecrets(vaultPath) {
88
+ if (!vaultPath) {
89
+ log('Error: VAULT_PATH is required.');
90
+ process.exit(1);
91
+ }
92
+
93
+ try {
94
+ const vaultAddr = process.env.VAULT_ADDR;
95
+ const response = await axios.get(`${vaultAddr}/v1/${vaultPath}`, {
96
+ headers: {
97
+ 'X-Vault-Token': process.env.VAULT_TOKEN,
98
+ },
99
+ httpsAgent: new (require('https').Agent)({ rejectUnauthorized: false }),
100
+ });
101
+
102
+ // Check if the response is valid JSON
103
+ return response.data;
104
+ } catch (error) {
105
+ log(`Error: Failed to connect to Vault server at ${process.env.VAULT_ADDR}`);
106
+ process.exit(1);
107
+ }
108
+ }
109
+
110
+
111
+ export async function connectClone() {
112
+ const site = process.env.BUILD_SITE;
113
+ if (!site) {
114
+ log('Error: BUILD_SITE is not set');
115
+ process.exit(1);
116
+ }
117
+ const siteCommonVaultPath = `secret/data/${site}/common/config`;
118
+
119
+ // 1) checkVault
120
+ await checkVault(siteCommonVaultPath);
121
+
122
+ // 2) getVaultSecrets
123
+ const secretsJson = await getVaultSecrets(siteCommonVaultPath);
124
+ if (!secretsJson?.data?.data) {
125
+ log('Failed to retrieve secret data.');
126
+ process.exit(1);
127
+ }
128
+ const siteCommonSecrets = secretsJson.data.data;
129
+
130
+ // 3) Export secrets as environment variables
131
+ process.env.AUTH0_CLIENT_ID = siteCommonSecrets.AUTH0_CLIENT_ID;
132
+ process.env.AUTH0_CLIENT_SECRET = siteCommonSecrets.AUTH0_CLIENT_SECRET;
133
+
134
+ process.env.AUTH0_SUBDOMAIN="docupace-${BUILD_ENVIRONMENT}-${BUILD_SITE}"
135
+ process.env.AUTH0_REGION_DOMAIN="us.auth0.com"
136
+
137
+ log(`BUILD_ENVIRONMENT: ${process.env.BUILD_ENVIRONMENT}`);
138
+ log(`BUILD_SITE: ${process.env.BUILD_SITE}`);
139
+ log(`AUTH0_SUBDOMAIN: ${process.env.AUTH0_SUBDOMAIN}`);
140
+ log(`AUTH0_REGION_DOMAIN: ${process.env.AUTH0_REGION_DOMAIN}`);
141
+ log(`AUTH0_CLIENT_ID: ${process.env.AUTH0_CLIENT_ID}`);
142
+
143
+ process.env.AUTH0_DOMAIN = `${process.env.AUTH0_SUBDOMAIN}.${process.env.AUTH0_REGION_DOMAIN}`;
144
+
145
+ // 4) Clone 'connect-deploy' into CONNECT_DIR
146
+ await gitCheckout('connect-deploy', process.env.CONNECT_DIR);
147
+
148
+ // 5) Create site clone (emulating the dock.sh call)
149
+ const cloneFrom = `${process.env.FROM_BUILD_ENVIRONMENT}/${process.env.FROM_BUILD_SITE}`;
150
+ const projectHome = path.resolve(process.env.CONNECT_DIR);
151
+
152
+ const cloneFromPath = path.join(projectHome, 'envs', cloneFrom);
153
+ if (!(await fs.pathExists(cloneFromPath))) {
154
+ log('Clone template directory not found');
155
+ process.exit(1);
156
+ }
157
+
158
+ const newSitePath = path.join(
159
+ projectHome,
160
+ 'envs',
161
+ process.env.BUILD_ENVIRONMENT,
162
+ process.env.BUILD_SITE
163
+ );
164
+
165
+ if (!(await fs.pathExists(newSitePath))) {
166
+ log('New site does not exist. Performing cloning...');
167
+ // This is where the bash script does: bash dock.sh clone ...
168
+ // We'll replicate with zx:
169
+ process.env.IMAGE_VERSION=$`cd ${cloneFromPath} && grep -E '^IMAGE_VERSION=' dock.sh | cut -f2 -d=`
170
+ await $`bash dock.sh clone ${process.env.BUILD_ENVIRONMENT} ${process.env.BUILD_SITE} ${process.env.IMAGE_VERSION}`;
171
+ } else {
172
+ log('Site already exists. Continuing Auth0 setup...');
173
+ }
174
+
175
+ // 6) Retrieve Auth0 metadata
176
+ const targetSitePath = path.join(
177
+ projectHome,
178
+ 'envs',
179
+ process.env.BUILD_ENVIRONMENT,
180
+ process.env.BUILD_SITE
181
+ );
182
+ await $`cd ${targetSitePath} && bash dock.sh getAuth0Metadata`;
183
+
184
+ // 7) Check conf files, print logs
185
+ await $`grep "auth0" ${path.join(targetSitePath, 'conf/application.conf')}`;
186
+ await $`ls -all ${path.join(targetSitePath, 'conf')}`;
187
+ await $`ls -all ${path.join(targetSitePath, 'conf/saml')}`;
188
+ await $`cat ${path.join(targetSitePath, 'conf/saml/auth0.xml')}`;
189
+
190
+ // 8) Git add, commit, push
191
+ await $`cd ${targetSitePath} && git checkout master && git pull --ff-only`;
192
+ await $`cd ${targetSitePath} && git remote -v`;
193
+ await $`cd ${targetSitePath} && git branch`;
194
+ await $`cd ${targetSitePath} && git status`;
195
+ await $`cd ${targetSitePath} && git add . ../sites.sh`;
196
+ await $`cd ${targetSitePath} && git status`;
197
+
198
+ const numChanges = (
199
+ await $`cd ${targetSitePath} && git diff-index HEAD`
200
+ ).stdout.split('\n').filter(Boolean).length;
201
+
202
+ if (numChanges > 0) {
203
+ const jiraTicket = process.env.JIRA_TICKET || 'NO_JIRA';
204
+ const message = `${jiraTicket} usdoc-s-builui job download_auth0_metadata for ${process.env.BUILD_ENVIRONMENT}/${process.env.BUILD_SITE}`;
205
+ log('About to commit changes to git');
206
+ await $`cd ${targetSitePath} && git commit -m ${message}`;
207
+ await $`cd ${targetSitePath} && git push -u origin master`;
208
+ } else {
209
+ log('No changes have been made. The site likely already has the correct configuration.');
210
+ }
211
+ }
212
+
@@ -0,0 +1,50 @@
1
+ import { $ } from 'zx';
2
+
3
+
4
+ export async function haproxySync() {
5
+ const repoPath = '/root/repo/docupace-ansible';
6
+ const inventory = path.join(repoPath, 'inventories/preprod/inventory');
7
+
8
+ // Create haproxy.sh
9
+ const scriptContent = `
10
+ #!/bin/bash
11
+
12
+ [[ ! -f ${inventory} ]] && ( echo "Alert: no inventory file: ${inventory}" ; exit 1 )
13
+ cd ${repoPath}
14
+ pwd
15
+ git stash
16
+ git pull origin master
17
+ git checkout master
18
+ git submodule update --remote
19
+ git submodule foreach --recursive git checkout master
20
+ git submodule foreach --recursive git pull origin master
21
+
22
+ ansible-playbook -i inventories/preprod/inventory haproxy.yml -e "cluster=preprod"
23
+
24
+ echo "proceeding"
25
+ echo "committing changes"
26
+ git add preprod-haproxy.cfg
27
+ git commit -m "Sync haproxy"
28
+ git push origin master
29
+ `;
30
+
31
+ await fs.writeFile('haproxy.sh', scriptContent);
32
+
33
+ // Private key
34
+ const haproxyPrivateKey = process.env.HAPROXY_PRIVATE_KEY;
35
+ if (!haproxyPrivateKey) {
36
+ log('Error: HAPROXY_PRIVATE_KEY not set.');
37
+ process.exit(1);
38
+ }
39
+
40
+ await fs.writeFile('private.key', Buffer.from(haproxyPrivateKey, 'base64'));
41
+ await fs.chmod('private.key', 0o600);
42
+
43
+ await $`cat private.key`; // Just to mimic the script's behavior
44
+
45
+ // Copy script to remote
46
+ await $`scp -i private.key -o StrictHostKeyChecking=no haproxy.sh root@10.10.28.198:/tmp/haproxy.sh`;
47
+
48
+ // Execute script on remote
49
+ await $`ssh -i private.key -o StrictHostKeyChecking=no root@10.10.28.198 "bash /tmp/haproxy.sh"`;
50
+ }
@@ -0,0 +1,13 @@
1
+ import { $ } from 'zx';
2
+
3
+
4
+ export async function vaultAddSiteData(SITE) {
5
+ const AUTH0_CLIENT_ID = `${process.env.AUTH0_CLIENT_ID}`;
6
+ const AUTH0_CLIENT_SECRET = `${process.env.AUTH0_CLIENT_SECRET}`;
7
+ await $`export VAULT_SKIP_VERIFY=true`;
8
+ await $`vault kv put secret/dp-${SITE}/common/config AUTH0_CLIENT_ID="${AUTH0_CLIENT_ID}" AUTH0_CLIENT_SECRET="${AUTH0_CLIENT_SECRET}"`
9
+ }
10
+
11
+
12
+
13
+