donbot-cli 1.0.2 → 1.0.4
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/.gitattributes +2 -0
- package/LICENSE +21 -0
- package/cli/access.js +100 -65
- package/cli/deploy.js +43 -11
- package/donbot.js +5 -4
- package/package.json +1 -1
package/.gitattributes
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 oscarcorebiz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/cli/access.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
'use strict'
|
|
2
|
-
require('dotenv').config();
|
|
2
|
+
require('dotenv').config({ path: './.env.development' });
|
|
3
3
|
var jwt = require('jsonwebtoken');
|
|
4
4
|
const bcrypt = require("bcryptjs");
|
|
5
5
|
var MongoClient = require('mongodb').MongoClient;
|
|
@@ -23,85 +23,120 @@ const registry = async (mail, first_name, last_name, password) => {
|
|
|
23
23
|
process.exit(1);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
var user_new = {
|
|
38
|
-
first_name: first_name,
|
|
39
|
-
last_name: last_name,
|
|
40
|
-
mail: mail,
|
|
41
|
-
password: hash
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
await dbo.collection("users").insertOne(user_new);
|
|
45
|
-
console.log("Usuario creado.");
|
|
46
|
-
process.exit();
|
|
47
|
-
|
|
48
|
-
}else {
|
|
26
|
+
const uri = 'mongodb://'+process.env.DB_USER+':'+process.env.DB_PASS+'@'+process.env.DB_HOST+':'+process.env.DB_PORT+'/'+process.env.DB_NAME;
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
|
|
30
|
+
console.log("El usuario se creara en: " + process.env.DB_HOST + ".");
|
|
31
|
+
|
|
32
|
+
const client = new MongoClient(uri);
|
|
33
|
+
await client.connect();
|
|
34
|
+
const dbo = client.db(process.env.DB_NAME);
|
|
35
|
+
const usersCollection = dbo.collection("users");
|
|
49
36
|
|
|
50
|
-
|
|
51
|
-
|
|
37
|
+
const existingUser = await usersCollection.findOne({ mail });
|
|
38
|
+
if (existingUser) {
|
|
39
|
+
console.error("❌ El usuario ya está registrado.");
|
|
40
|
+
} else {
|
|
41
|
+
// Generar hash de contraseña
|
|
42
|
+
const salt = await bcrypt.genSalt(10);
|
|
43
|
+
const hash = await bcrypt.hash(password, salt);
|
|
44
|
+
|
|
45
|
+
const user_new = {
|
|
46
|
+
first_name,
|
|
47
|
+
last_name,
|
|
48
|
+
mail,
|
|
49
|
+
password: hash,
|
|
50
|
+
active:true,
|
|
51
|
+
role:"admin"
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
await usersCollection.insertOne(user_new);
|
|
55
|
+
console.log("✅ Usuario creado exitosamente.");
|
|
52
56
|
}
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
|
|
58
|
+
await client.close(); // Cerrar conexión
|
|
59
|
+
process.exit(0);
|
|
60
|
+
|
|
61
|
+
} catch (err) {
|
|
62
|
+
console.error("🚨 Error en la conexión:", err.message);
|
|
63
|
+
process.exit(1);
|
|
64
|
+
}
|
|
55
65
|
|
|
56
66
|
};
|
|
57
67
|
|
|
58
|
-
const createKey = (user, description, expiration) => {
|
|
68
|
+
const createKey = async (user, description, expiration) => {
|
|
59
69
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
70
|
+
console.log(`
|
|
71
|
+
_____ _______ __ _ ______ _______ _______
|
|
72
|
+
| | | || | | || _ | | || |
|
|
73
|
+
| _ | | _ || || || | | | | _ ||_ _|
|
|
74
|
+
| | | || | | || || | | | | | | |
|
|
75
|
+
| |_| | | |_| || _ || _ | | |_| | | |
|
|
76
|
+
| | | || | | || |_| | | | | |
|
|
77
|
+
|____| |_______||_| |___||_____| |_______| |___|
|
|
78
|
+
|
|
79
|
+
DonBot CLI - Herramienta para operaciones automatizadas.
|
|
80
|
+
`);
|
|
63
81
|
|
|
64
|
-
|
|
65
|
-
return [...Array(128)]
|
|
66
|
-
.map((e) => ((Math.random() * 36) | 0).toString(36))
|
|
67
|
-
.join('');
|
|
68
|
-
};
|
|
82
|
+
console.log("Crear llave...");
|
|
69
83
|
|
|
70
|
-
|
|
84
|
+
const uri = 'mongodb://'+process.env.DB_USER+':'+process.env.DB_PASS+'@'+process.env.DB_HOST+':'+process.env.DB_PORT+'/'+process.env.DB_NAME;
|
|
71
85
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
86
|
+
const client = new MongoClient(uri);
|
|
87
|
+
await client.connect();
|
|
88
|
+
const dbo = client.db(process.env.DB_NAME);
|
|
89
|
+
const accesstokensCollection = dbo.collection("accesstokens");
|
|
90
|
+
|
|
91
|
+
let today = new Date().toISOString();
|
|
92
|
+
|
|
93
|
+
const payload = {
|
|
94
|
+
user: user,
|
|
95
|
+
creationDate: today,
|
|
96
|
+
expiresIn: expiration
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
let expiresobj;
|
|
100
|
+
let expiresday;
|
|
101
|
+
if(expiration == "noexpire"){
|
|
102
|
+
expiresobj = "";
|
|
103
|
+
expiresday = expiration;
|
|
104
|
+
}else{
|
|
105
|
+
expiresobj = {
|
|
75
106
|
expiresIn: expiration
|
|
76
107
|
};
|
|
108
|
+
expiresday = expiration;
|
|
109
|
+
}
|
|
77
110
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
111
|
+
const access_token = jwt.sign(payload, process.env.TOKEN_KEY, expiresobj);
|
|
112
|
+
|
|
113
|
+
let keydata = {
|
|
114
|
+
user: user,
|
|
115
|
+
description: description,
|
|
116
|
+
key: access_token,
|
|
117
|
+
creationDate: today,
|
|
118
|
+
expirationDate: expiresday,
|
|
119
|
+
active: true
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
await accesstokensCollection.insertOne(keydata);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.error("❌ Error en la creación de usuario:", error.message);
|
|
126
|
+
process.exit(1);
|
|
127
|
+
}
|
|
89
128
|
|
|
90
|
-
|
|
129
|
+
let keyreso = {
|
|
130
|
+
user: keydata.user,
|
|
131
|
+
description: keydata.description,
|
|
132
|
+
key: keydata.key
|
|
133
|
+
};
|
|
91
134
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
user: user,
|
|
95
|
-
description: description,
|
|
96
|
-
key: access_token,
|
|
97
|
-
creationDate: today,
|
|
98
|
-
expirationDate: expiresday,
|
|
99
|
-
active: true
|
|
100
|
-
};
|
|
101
|
-
await dbo.collection("accesstokens").insertOne(key);
|
|
102
|
-
process.exit();
|
|
135
|
+
console.log("✅ Usuario creado exitosamente el usuario " + user + ".");
|
|
136
|
+
console.table(keyreso);
|
|
103
137
|
|
|
104
|
-
|
|
138
|
+
await client.close(); // Cerrar conexión
|
|
139
|
+
process.exit(0);
|
|
105
140
|
|
|
106
141
|
};
|
|
107
142
|
|
package/cli/deploy.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const { Client } = require("ssh2");
|
|
2
2
|
const path = require("path");
|
|
3
|
-
const { exec } = require("child_process");
|
|
3
|
+
const { exec, execSync } = require("child_process");
|
|
4
4
|
const readline = require('readline');
|
|
5
5
|
const fs = require('fs');
|
|
6
6
|
let config;
|
|
@@ -9,7 +9,7 @@ function getGitBranch() {
|
|
|
9
9
|
return new Promise((resolve, reject) => {
|
|
10
10
|
exec('git rev-parse --abbrev-ref HEAD', (error, stdout, stderr) => {
|
|
11
11
|
if (error) {
|
|
12
|
-
reject(
|
|
12
|
+
reject(stderr);
|
|
13
13
|
} else {
|
|
14
14
|
resolve(stdout.trim()); // La salida de git es la rama actual
|
|
15
15
|
}
|
|
@@ -17,6 +17,18 @@ function getGitBranch() {
|
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
function validCode() {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
execSync('npm run lint', (error, stdout, stderr) => {
|
|
23
|
+
if (error) {
|
|
24
|
+
reject(`Error: ${stderr}`);
|
|
25
|
+
} else {
|
|
26
|
+
resolve(stdout.trim());
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
20
32
|
const askPassphrase = () => {
|
|
21
33
|
return new Promise((resolve) => {
|
|
22
34
|
const rl = readline.createInterface({
|
|
@@ -32,7 +44,7 @@ const askPassphrase = () => {
|
|
|
32
44
|
});
|
|
33
45
|
};
|
|
34
46
|
|
|
35
|
-
const uploadCodeToServer = async (uploadCodeToServer) => {
|
|
47
|
+
const uploadCodeToServer = async (uploadCodeToServer, options) => {
|
|
36
48
|
|
|
37
49
|
console.log(`
|
|
38
50
|
_____ _______ __ _ ______ _______ _______
|
|
@@ -46,13 +58,34 @@ const uploadCodeToServer = async (uploadCodeToServer) => {
|
|
|
46
58
|
DonBot CLI - Herramienta para operaciones automatizadas.
|
|
47
59
|
`);
|
|
48
60
|
|
|
61
|
+
//Validamos codigo
|
|
62
|
+
if (!options.novalidcode) {
|
|
63
|
+
|
|
64
|
+
console.log("Validando codigo...");
|
|
65
|
+
|
|
66
|
+
validCode()
|
|
67
|
+
.then()
|
|
68
|
+
.catch((err) => {
|
|
69
|
+
console.log("❌ El codigo no es valido.");
|
|
70
|
+
console.error(err.stdout.toString());
|
|
71
|
+
process.exit(1);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
49
79
|
let host, user, path_cert, port, path_dest, sourceDir;
|
|
50
80
|
|
|
51
81
|
getGitBranch()
|
|
52
82
|
.then(async (branch) => {
|
|
53
83
|
|
|
84
|
+
//No hubo error en codigo
|
|
85
|
+
console.log("✅El codigo es valido.");
|
|
86
|
+
|
|
54
87
|
try {
|
|
55
|
-
config = JSON.parse(fs.readFileSync('config-donbot-cli.json', 'utf8'));
|
|
88
|
+
config = JSON.parse(fs.readFileSync('./.conf/config-donbot-cli.json', 'utf8'));
|
|
56
89
|
} catch (error) {
|
|
57
90
|
console.error('Error al leer el archivo de configuración:', error.message);
|
|
58
91
|
process.exit(1);
|
|
@@ -61,11 +94,11 @@ const uploadCodeToServer = async (uploadCodeToServer) => {
|
|
|
61
94
|
if(branch === uploadCodeToServer){
|
|
62
95
|
|
|
63
96
|
|
|
64
|
-
({ host, user, path_cert, port, path_dest, sourceDir } = config[uploadCodeToServer]);
|
|
97
|
+
({ host, user, path_cert, port, path_dest, sourceDir, container_name } = config[uploadCodeToServer]);
|
|
65
98
|
|
|
66
99
|
console.log("Actualizando codigo en: " + host)
|
|
67
100
|
|
|
68
|
-
if (!host || !user || !path_cert || !port || !path_dest || !sourceDir) {
|
|
101
|
+
if (!host || !user || !path_cert || !port || !path_dest || !sourceDir || !container_name) {
|
|
69
102
|
console.error('Faltan valores necesarios en el archivo de configuración.');
|
|
70
103
|
console.error('Verifique que los siguientes valores estén definidos: host, user, path_cert, port, path_dest, sourceDir.');
|
|
71
104
|
process.exit(1);
|
|
@@ -80,9 +113,8 @@ const uploadCodeToServer = async (uploadCodeToServer) => {
|
|
|
80
113
|
console.error('Error al verificar la ruta de la clave privada:', error.message);
|
|
81
114
|
process.exit(1);
|
|
82
115
|
}
|
|
83
|
-
|
|
116
|
+
|
|
84
117
|
let rsyncCommand;
|
|
85
|
-
|
|
86
118
|
let connectionConfig = {
|
|
87
119
|
host,
|
|
88
120
|
port,
|
|
@@ -95,7 +127,7 @@ const uploadCodeToServer = async (uploadCodeToServer) => {
|
|
|
95
127
|
ssh.on("ready", () => {
|
|
96
128
|
console.log("Conectado al servidor");
|
|
97
129
|
|
|
98
|
-
rsyncCommand = `rsync -avzh --progress -e "ssh -i ${path_cert}" --exclude-from="exclude-deploy-donbot-list.txt" "${sourceDir}" ${user}@${host}:${path_dest}`;
|
|
130
|
+
rsyncCommand = `rsync -avzh --progress -e "ssh -i ${path_cert}" --exclude-from="./.conf/exclude-deploy-donbot-list.txt" "${sourceDir}" ${user}@${host}:${path_dest}`;
|
|
99
131
|
|
|
100
132
|
exec(rsyncCommand, (err, stdout, stderr) => {
|
|
101
133
|
if (err) {
|
|
@@ -106,13 +138,13 @@ const uploadCodeToServer = async (uploadCodeToServer) => {
|
|
|
106
138
|
console.log(stdout);
|
|
107
139
|
|
|
108
140
|
// Reiniciar Docker Compose después de la transferencia
|
|
109
|
-
const dockerCommand = `ssh -i ${path_cert} ${user}@${host} "cd && docker
|
|
141
|
+
const dockerCommand = `ssh -i ${path_cert} ${user}@${host} "cd && docker restart ${container_name}"`;
|
|
110
142
|
exec(dockerCommand, (dockerErr, dockerStdout, dockerStderr) => {
|
|
111
143
|
if (dockerErr) {
|
|
112
144
|
console.error(`Error al reiniciar Docker Compose: ${dockerStderr}`);
|
|
113
145
|
return;
|
|
114
146
|
}
|
|
115
|
-
console.log("
|
|
147
|
+
console.log("🐳 Contenedor de app reiniciado correctamente:");
|
|
116
148
|
console.log(dockerStdout);
|
|
117
149
|
});
|
|
118
150
|
|
package/donbot.js
CHANGED
|
@@ -11,7 +11,7 @@ const flows = require('./cli/flows');
|
|
|
11
11
|
|
|
12
12
|
//Version
|
|
13
13
|
program
|
|
14
|
-
.version("DonBot 1.
|
|
14
|
+
.version("DonBot 1.0.3")
|
|
15
15
|
.description( `
|
|
16
16
|
_____ _______ __ _ ______ _______ _______
|
|
17
17
|
| | | || | | || _ | | || |
|
|
@@ -67,8 +67,9 @@ program.
|
|
|
67
67
|
<path_dest> -- Path de ubicacion en servidor destino
|
|
68
68
|
|
|
69
69
|
`).
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
option("--novalidcode", "Omitir la validación de código antes del deploy").
|
|
71
|
+
action((type_deploy, options) => {
|
|
72
|
+
deploy.uploadCodeToServer(type_deploy, options);
|
|
72
73
|
});
|
|
73
74
|
|
|
74
75
|
// Flow
|
|
@@ -77,7 +78,7 @@ program.
|
|
|
77
78
|
command("newcontroller <name>").
|
|
78
79
|
alias("nc").
|
|
79
80
|
description("Crea un nuevo flujo").
|
|
80
|
-
option("--cron", "Genera también un archivo de
|
|
81
|
+
option("--cron", "Genera también un archivo de crons para iniciar los flujos").
|
|
81
82
|
option("--endpoint", "Genera también un archivo de rutas para el controlador").
|
|
82
83
|
action((name,options) => {
|
|
83
84
|
flows.newflow(name,options);
|