@cocreate/cli 1.45.0 → 1.45.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.45.2](https://github.com/CoCreate-app/CoCreate-cli/compare/v1.45.1...v1.45.2) (2023-12-01)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * metrics-server renamed to metrics ([6e3604d](https://github.com/CoCreate-app/CoCreate-cli/commit/6e3604d6fa8881e903cb9f24d27a6dd14043e88d))
7
+
8
+ ## [1.45.1](https://github.com/CoCreate-app/CoCreate-cli/compare/v1.45.0...v1.45.1) (2023-11-26)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * calculation renamed to calculate ([691d872](https://github.com/CoCreate-app/CoCreate-cli/commit/691d8727bc9f137bf9b7cb0a21fcb2782926ba1b))
14
+
1
15
  # [1.45.0](https://github.com/CoCreate-app/CoCreate-cli/compare/v1.44.0...v1.45.0) (2023-11-25)
2
16
 
3
17
 
@@ -54,7 +54,7 @@ module.exports = {
54
54
  "repo": "github.com/CoCreate-app/CoCreate-cache.git"
55
55
  },
56
56
  {
57
- "path": "../CoCreate-calculation",
57
+ "path": "../CoCreate-calculate",
58
58
  "repo": "github.com/CoCreate-app/CoCreate-calculation.git"
59
59
  },
60
60
  {
@@ -402,8 +402,8 @@ module.exports = {
402
402
  "repo": "github.com/CoCreate-app/CoCreate-loadtest.git"
403
403
  },
404
404
  {
405
- "path": "../CoCreate-metrics-server",
406
- "repo": "github.com/CoCreate-app/CoCreate-metrics-server.git"
405
+ "path": "../CoCreate-metrics",
406
+ "repo": "github.com/CoCreate-app/CoCreate-metrics.git"
407
407
  },
408
408
  {
409
409
  "path": "../CoCreate-mongodb",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/cli",
3
- "version": "1.45.0",
3
+ "version": "1.45.2",
4
4
  "description": "Polyrepo management bash CLI tool. Run all git commands and yarn commands on multiple repositories. Also includes a few custom macros for cloning, installing, etc.",
5
5
  "keywords": [
6
6
  "cli",
@@ -0,0 +1,67 @@
1
+ const AWS = require('aws-sdk');
2
+ const { NodeSSH } = require('node-ssh'); // npm install node-ssh
3
+ const ssh = new NodeSSH();
4
+
5
+ AWS.config.update({ region: 'us-west-2' });
6
+
7
+ const ec2 = new AWS.EC2({ apiVersion: '2016-11-15' });
8
+
9
+ const instanceParams = {
10
+ ImageId: 'ami-0abcdef1234567890',
11
+ InstanceType: 't2.micro',
12
+ KeyName: 'your-key-pair-name',
13
+ MinCount: 1,
14
+ MaxCount: 1
15
+ };
16
+
17
+ ec2.runInstances(instanceParams, function (err, data) {
18
+ if (err) {
19
+ console.error("Could not create instance", err);
20
+ return;
21
+ }
22
+ const instanceId = data.Instances[0].InstanceId;
23
+ console.log("Created instance", instanceId);
24
+
25
+ // Wait for instance to be in running state and get its Public DNS
26
+ waitForInstanceRunning(instanceId, (err, instanceData) => {
27
+ if (err) {
28
+ console.error("Error waiting for instance running", err);
29
+ return;
30
+ }
31
+
32
+ // SSH into the instance and install Node.js
33
+ ssh.connect({
34
+ host: instanceData.PublicDnsName,
35
+ username: 'ec2-user', // default username for Amazon AMI
36
+ privateKey: 'path/to/your/key-pair.pem'
37
+ })
38
+ .then(function () {
39
+ // Commands to install Node.js and start your server
40
+ const commands = [
41
+ 'curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -',
42
+ 'sudo apt-get install -y nodejs',
43
+ // additional commands to clone your project, install dependencies, and start the server
44
+ ];
45
+
46
+ return ssh.execCommand(commands.join(' && '));
47
+ })
48
+ .then(function (result) {
49
+ console.log('STDOUT: ' + result.stdout);
50
+ console.log('STDERR: ' + result.stderr);
51
+ })
52
+ .catch(function (error) {
53
+ console.error('SSH Connection Error: ' + error);
54
+ });
55
+ });
56
+ });
57
+
58
+ function waitForInstanceRunning(instanceId, callback) {
59
+ ec2.waitFor('instanceRunning', { InstanceIds: [instanceId] }, function (err, data) {
60
+ if (err) {
61
+ callback(err);
62
+ return;
63
+ }
64
+ const instanceData = data.Reservations[0].Instances[0];
65
+ callback(null, instanceData);
66
+ });
67
+ }
@@ -0,0 +1,56 @@
1
+ const { Compute } = require('@google-cloud/compute');
2
+ const path = require('path');
3
+ const { NodeSSH } = require('node-ssh');
4
+ const ssh = new NodeSSH();
5
+
6
+ // Set GCP Project ID and path to your service account key
7
+ const projectId = 'your-project-id';
8
+ const keyFilename = path.join(__dirname, 'path-to-your-service-account-key.json');
9
+
10
+ // Initialize Google Compute Engine API
11
+ const compute = new Compute({ projectId, keyFilename });
12
+
13
+ // Configuration for the VM instance
14
+ const zone = compute.zone('us-central1-a'); // change as per your requirement
15
+ const config = {
16
+ os: 'ubuntu',
17
+ machineType: 'n1-standard-1', // change as per your requirement
18
+ http: true,
19
+ https: true
20
+ };
21
+
22
+ async function createVM() {
23
+ try {
24
+ const vmName = 'your-vm-name'; // choose a name for your VM
25
+ const [, operation] = await zone.createVM(vmName, config);
26
+ await operation.promise();
27
+
28
+ // Retrieve the newly created VM metadata
29
+ const vm = zone.vm(vmName);
30
+ const [metadata] = await vm.getMetadata();
31
+
32
+ // Connect to the VM via SSH and install Node.js
33
+ // This assumes you have set up SSH keys for GCP VMs
34
+ const externalIP = metadata.networkInterfaces[0].accessConfigs[0].natIP;
35
+ await ssh.connect({
36
+ host: externalIP,
37
+ username: 'your-ssh-username', // default is often 'ubuntu' for Ubuntu VMs
38
+ privateKey: 'path/to/your/private/ssh/key'
39
+ });
40
+
41
+ const commands = [
42
+ 'curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -',
43
+ 'sudo apt-get install -y nodejs',
44
+ // Additional commands as needed
45
+ ];
46
+
47
+ const result = await ssh.execCommand(commands.join(' && '));
48
+ console.log('STDOUT:', result.stdout);
49
+ console.log('STDERR:', result.stderr);
50
+
51
+ } catch (err) {
52
+ console.error('Error during VM creation:', err);
53
+ }
54
+ }
55
+
56
+ createVM();
@@ -0,0 +1,76 @@
1
+ const fs = require('fs');
2
+ const { exec } = require('child_process');
3
+
4
+ const haproxyConfigPath = '/etc/haproxy/haproxy.cfg';
5
+ const backendSectionName = 'backend app_backend';
6
+
7
+ // Function to update HAProxy config with new servers
8
+ const updateHAProxyConfig = (newServers) => {
9
+ fs.readFile(haproxyConfigPath, 'utf8', (err, data) => {
10
+ if (err) {
11
+ console.error('Error reading HAProxy config:', err);
12
+ return;
13
+ }
14
+
15
+ const updatedConfig = updateBackendServers(data, newServers);
16
+ fs.writeFile(haproxyConfigPath, updatedConfig, 'utf8', (err) => {
17
+ if (err) {
18
+ console.error('Error writing updated HAProxy config:', err);
19
+ return;
20
+ }
21
+
22
+ reloadHAProxy();
23
+ });
24
+ });
25
+ };
26
+
27
+ // Function to replace backend server entries in the config
28
+ const updateBackendServers = (configData, newServers) => {
29
+ const lines = configData.split('\n');
30
+ let backendSectionStart = -1;
31
+ let backendSectionEnd = -1;
32
+
33
+ // Find the start and end of the backend section
34
+ lines.forEach((line, index) => {
35
+ if (line.trim() === backendSectionName) {
36
+ backendSectionStart = index;
37
+ }
38
+ if (backendSectionStart !== -1 && backendSectionEnd === -1 && line.trim() === '') {
39
+ backendSectionEnd = index;
40
+ }
41
+ });
42
+
43
+ if (backendSectionStart === -1 || backendSectionEnd === -1) {
44
+ console.error('Backend section not found in HAProxy config');
45
+ return configData;
46
+ }
47
+
48
+ // Replace the server list in the backend section
49
+ const newServerLines = newServers.map(server => ` server ${server.name} ${server.address} check`);
50
+ const updatedLines = [
51
+ ...lines.slice(0, backendSectionStart + 1),
52
+ ...newServerLines,
53
+ ...lines.slice(backendSectionEnd)
54
+ ];
55
+
56
+ return updatedLines.join('\n');
57
+ };
58
+
59
+ // Function to reload HAProxy
60
+ const reloadHAProxy = () => {
61
+ exec('systemctl reload haproxy', (err, stdout, stderr) => {
62
+ if (err) {
63
+ console.error('Error reloading HAProxy:', err);
64
+ return;
65
+ }
66
+ console.log('HAProxy reloaded successfully');
67
+ });
68
+ };
69
+
70
+ // Example usage
71
+ const newServers = [
72
+ { name: 'app1', address: '10.0.0.1:80' },
73
+ { name: 'app2', address: '10.0.0.2:80' }
74
+ ];
75
+
76
+ updateHAProxyConfig(newServers);
@@ -0,0 +1,63 @@
1
+ const fs = require('fs');
2
+ const { exec } = require('child_process');
3
+
4
+ const haproxyConfigPath = '/etc/haproxy/haproxy.cfg';
5
+
6
+ // Function to add a new configuration to HAProxy and restart it
7
+ const addHAProxyConfig = (frontendName, backendName, backendServers) => {
8
+ fs.readFile(haproxyConfigPath, 'utf8', (err, data) => {
9
+ if (err) {
10
+ console.error('Error reading HAProxy config:', err);
11
+ return;
12
+ }
13
+
14
+ // Append new frontend and backend configuration
15
+ const newConfig = `
16
+ # Frontend Configuration
17
+ frontend ${frontendName}
18
+ bind *:80
19
+ mode http
20
+ default_backend ${backendName}
21
+
22
+ # Backend Configuration
23
+ backend ${backendName}
24
+ mode http
25
+ balance roundrobin`;
26
+
27
+ // Add server entries
28
+ backendServers.forEach((server, index) => {
29
+ newConfig += `\n server server${index} ${server.address} check`;
30
+ });
31
+
32
+ // Write updated configuration
33
+ fs.writeFile(haproxyConfigPath, data + newConfig, 'utf8', (err) => {
34
+ if (err) {
35
+ console.error('Error writing updated HAProxy config:', err);
36
+ return;
37
+ }
38
+
39
+ restartHAProxy();
40
+ });
41
+ });
42
+ };
43
+
44
+ // Function to restart HAProxy
45
+ const restartHAProxy = () => {
46
+ exec('systemctl restart haproxy', (err, stdout, stderr) => {
47
+ if (err) {
48
+ console.error('Error restarting HAProxy:', err);
49
+ return;
50
+ }
51
+ console.log('HAProxy restarted successfully');
52
+ });
53
+ };
54
+
55
+ // Example usage
56
+ const frontendName = 'my_frontend';
57
+ const backendName = 'my_backend';
58
+ const backendServers = [
59
+ { address: '10.0.0.1:80' },
60
+ { address: '10.0.0.2:80' }
61
+ ];
62
+
63
+ addHAProxyConfig(frontendName, backendName, backendServers);
@@ -0,0 +1,33 @@
1
+ const fs = require('fs');
2
+
3
+ // Function to parse and display memory info
4
+ const readMemoryInfo = () => {
5
+ const memInfoContent = fs.readFileSync('/proc/meminfo', 'utf8');
6
+ const memInfoLines = memInfoContent.split('\n');
7
+ const memInfo = memInfoLines.reduce((info, line) => {
8
+ const parts = line.split(':');
9
+ if (parts.length === 2) {
10
+ info[parts[0].trim()] = parts[1].trim();
11
+ }
12
+ return info;
13
+ }, {});
14
+ console.log('Memory Info:', memInfo);
15
+ };
16
+
17
+ // Function to read CPU info (simplified)
18
+ const readCpuInfo = () => {
19
+ const cpuInfoContent = fs.readFileSync('/proc/stat', 'utf8');
20
+ const cpuLines = cpuInfoContent.split('\n');
21
+ const cpuLine = cpuLines.find(line => line.startsWith('cpu '));
22
+ if (cpuLine) {
23
+ // Example processing; more needed for actual CPU usage calculation
24
+ const cpuTimes = cpuLine.split(' ').slice(1).map(Number);
25
+ console.log('CPU Times:', cpuTimes);
26
+ }
27
+ };
28
+
29
+ // Read memory and CPU info
30
+ setInterval(() => {
31
+ readMemoryInfo();
32
+ readCpuInfo();
33
+ }, 500);
@@ -0,0 +1,76 @@
1
+ const fs = require('fs');
2
+ const { exec } = require('child_process');
3
+
4
+ const nginxConfigPath = '/etc/nginx/conf.d/myapp.conf';
5
+ const upstreamBlockName = 'upstream app_backend';
6
+
7
+ // Function to update NGINX config with new servers
8
+ const updateNginxConfig = (newServers) => {
9
+ fs.readFile(nginxConfigPath, 'utf8', (err, data) => {
10
+ if (err) {
11
+ console.error('Error reading NGINX config:', err);
12
+ return;
13
+ }
14
+
15
+ const updatedConfig = updateUpstreamServers(data, newServers);
16
+ fs.writeFile(nginxConfigPath, updatedConfig, 'utf8', (err) => {
17
+ if (err) {
18
+ console.error('Error writing updated NGINX config:', err);
19
+ return;
20
+ }
21
+
22
+ reloadNginx();
23
+ });
24
+ });
25
+ };
26
+
27
+ // Function to replace upstream server entries in the config
28
+ const updateUpstreamServers = (configData, newServers) => {
29
+ const lines = configData.split('\n');
30
+ let upstreamSectionStart = -1;
31
+ let upstreamSectionEnd = -1;
32
+
33
+ // Find the start and end of the upstream section
34
+ lines.forEach((line, index) => {
35
+ if (line.trim().startsWith(upstreamBlockName)) {
36
+ upstreamSectionStart = index;
37
+ }
38
+ if (upstreamSectionStart !== -1 && upstreamSectionEnd === -1 && line.trim() === '}') {
39
+ upstreamSectionEnd = index;
40
+ }
41
+ });
42
+
43
+ if (upstreamSectionStart === -1 || upstreamSectionEnd === -1) {
44
+ console.error('Upstream section not found in NGINX config');
45
+ return configData;
46
+ }
47
+
48
+ // Replace the server list in the upstream section
49
+ const newServerLines = newServers.map(server => ` server ${server.address};`);
50
+ const updatedLines = [
51
+ ...lines.slice(0, upstreamSectionStart + 1),
52
+ ...newServerLines,
53
+ ...lines.slice(upstreamSectionEnd)
54
+ ];
55
+
56
+ return updatedLines.join('\n');
57
+ };
58
+
59
+ // Function to reload NGINX
60
+ const reloadNginx = () => {
61
+ exec('systemctl reload nginx', (err, stdout, stderr) => {
62
+ if (err) {
63
+ console.error('Error reloading NGINX:', err);
64
+ return;
65
+ }
66
+ console.log('NGINX reloaded successfully');
67
+ });
68
+ };
69
+
70
+ // Example usage
71
+ const newServers = [
72
+ { address: '10.0.0.1:80' },
73
+ { address: '10.0.0.2:80' }
74
+ ];
75
+
76
+ updateNginxConfig(newServers);
@@ -6,6 +6,10 @@ const spawn = child_process.spawn;
6
6
 
7
7
  const localip = '3.231.17.247'
8
8
  const certificates = new Map()
9
+ const certbotPath = `/etc/letsencrypt/live/`;
10
+ const fullchainPath = `${certbotPath}/fullchain.pem`;
11
+ const privkeyPath = `${certbotPath}/privkey.pem`;
12
+ const combinedPath = `${certbotPath}/haproxy.pem`;
9
13
 
10
14
 
11
15
  async function checkDns(host) {
@@ -96,6 +100,36 @@ async function checkCert(host) {
96
100
  }
97
101
  }
98
102
 
103
+ const combineCertificate = (host) => {
104
+ const certbotPath = `/etc/letsencrypt/live/${host}`;
105
+ const fullchainPath = `${certbotPath}/fullchain.pem`;
106
+ const privkeyPath = `${certbotPath}/privkey.pem`;
107
+ const combinedPath = `${certbotPath}/haproxy.pem`;
108
+
109
+ fs.readFile(fullchainPath, (err, fullchainData) => {
110
+ if (err) {
111
+ console.error('Error reading fullchain.pem:', err);
112
+ return;
113
+ }
114
+
115
+ fs.readFile(privkeyPath, (err, privkeyData) => {
116
+ if (err) {
117
+ console.error('Error reading privkey.pem:', err);
118
+ return;
119
+ }
120
+
121
+ const combinedData = `${fullchainData}\n${privkeyData}`;
122
+ fs.writeFile(combinedPath, combinedData, (err) => {
123
+ if (err) {
124
+ console.error('Error writing combined haproxy.pem:', err);
125
+ } else {
126
+ console.log('Successfully combined certificates for HAProxy.');
127
+ }
128
+ });
129
+ });
130
+ });
131
+ };
132
+
99
133
  async function test(host) {
100
134
  try {
101
135