1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
import {ToasterService} from './toaster.service';
import {OgSweetAlertService} from './og-sweet-alert.service';
import {CommandService} from '../api/command.service';
import {OgCommonService} from './og-common.service';
import {TranslateService} from '@ngx-translate/core';
import {Router} from '@angular/router';
import {Injectable} from '@angular/core';
import * as _ from 'lodash';
import {environment} from '../../environments/environment';
@Injectable({
providedIn: 'root'
})
export class OGCommandsService {
public ogInstructions = '';
public execution: any;
private commands: any;
constructor(private router: Router, private ogCommonService: OgCommonService, private toaster: ToasterService, private ogSweetAlert: OgSweetAlertService, private commandService: CommandService, private translate: TranslateService) {
this.execution = {};
this.commands = environment.commands;
}
sendCommand() {
let result = true;
// TODO - Comprobar parametros
if (!this.execution.script) {
result = false;
this.toaster.pop({type: 'error', title: 'error', body: this.translate.instant('command_not_valid')});
} else if (!this.execution.clients) {
result = false;
this.toaster.pop({type: 'error', title: 'error', body: this.translate.instant('not_clients_selected')});
}
// Si no hubo ningun error
if (result === true) {
this.execution.script = this.execution.script.replace(/\"/g, '\\"').replace(/\$/g, '\\\$');
// Resetar las instrucciones del script opengnsys almacenadas.
this.ogInstructions = '';
this.commandService.execute(this.execution).subscribe(
(response: any[]) => {
// Buscar en la respuesta si hay algún statuscode diferente de 200
const errors = response.filter(function(value) {
return (value.statusCode && value.statusCode !== '!200');
} );
let errorStr = '';
let toasterOpts = {type: 'success', title: 'success', body: this.translate.instant('successfully_executed')};
if (errors.length > 0) {
for (let e = 0; e < errors.length; e++) {
errorStr += this.translate.instant('execution_failed_in') + ' ' + errors[e].name + '\n';
}
toasterOpts = {type: 'error', title: 'error', body: errorStr};
}
this.toaster.pop(toasterOpts);
this.router.navigate(['/app/ous']);
},
(error) => {
this.toaster.pop({type: 'error', title: 'error', body: error});
}
);
}
}
execute(command, params?) {
this.execution.type = command;
if (command === 'HISTORY_LOG') {
let clientIp = null;
// Abrir ventana de log
if (typeof params === 'undefined' || typeof params.clientIp === 'undefined') {
const client = this.ogCommonService.selectedClients[Object.keys(this.ogCommonService.selectedClients)[0]];
if (client) {
clientIp = client.ip;
}
} else {
clientIp = params.clientIp;
}
if (clientIp) {
const url = 'http://' + clientIp + environment.commands.HISTORY_LOG;
window.open(url, '', 'resizable=yes,toolbar=no,status=no,location=no,menubar=no,scrollbars=yes');
}
} else if (command === 'REALTIME_LOG') {
let clientIp = null;
// Abrir ventana de log
if (typeof params === 'undefined' || typeof params.clientIp === 'undefined') {
const client = this.ogCommonService.selectedClients[Object.keys(this.ogCommonService.selectedClients)[0]];
if (client) {
clientIp = client.ip;
}
} else {
clientIp = params.clientIp;
}
if (clientIp) {
const url = 'http://' + clientIp + environment.commands.REALTIME_LOG;
window.open(url, '', 'resizable=yes,toolbar=no,status=no,location=no,menubar=no,scrollbars=yes');
}
} else if (command === 'SOFTWARE_INVENTORY') {
const client = this.ogCommonService.selectedClients[Object.keys(this.ogCommonService.selectedClients)[0]];
// Preparar el scope para el sweet alert
const options = {
scope: {
partitions: [],
selectedPart: null
}
};
// Comprobar tipo de cada particion para ver si es clonable
// var parttable = $rootScope.constants.partitiontable[client.partitions[0].partitionCode-1];
// buscar las particiones que sean clonables
const clonablePartitions = [];
for (let index = 1; index < client.partitions.length; index++) {
if (client.partitions[index].osName !== 'DATA' && client.partitions[index].osName !== '') {
// Crear como nombre para mostrar, el disco y partición del sistema
const obj = Object.assign({}, client.partitions[index]);
const str = 'disco: ' + obj.numDisk + ', part: ' + obj.numPartition + ', SO: ' + client.partitions[index].osName;
clonablePartitions.push(obj.numDisk + ' ' + obj.numPartition)
options.scope.partitions.push(str);
}
}
const self = this;
this.ogSweetAlert.swal({
title: this.translate.instant('select_partition_to_inventary'),
// text: $filter("translate")("action_cannot_be_undone"),
type: 'info',
input: 'select',
inputOptions: options.scope.partitions,
showCancelButton: true,
confirmButtonColor: '#3c8dbc',
confirmButtonText: this.translate.instant('done'),
closeOnConfirm: true
}).then(
function(result) {
if (result.value) {
// Montar el script con el disco y partición elegida
self.execution.script = self.commands.SOFTWARE_INVENTORY + ' ' + clonablePartitions[result.value];
self.loadClients();
self.sendCommand();
}
},
null);
} else {
if (command === 'REBOOT') {
this.execution.script = environment.commands.REBOOT;
} else if (command === 'POWER_OFF') {
this.execution.script = environment.commands.POWER_OFF;
} else if (command === 'POWER_ON') {
this.execution.script = 'wakeonlan';
} else if (command === 'HARDWARE_INVENTORY') {
this.execution.script = environment.commands.HARDWARE_INVENTORY;
} else if (command === 'RUN_SCRIPT') {
this.execution.script = params ? (params.script || this.ogInstructions) : this.ogInstructions;
} else if (command === 'REFRESH_INFO') {
this.execution.script = environment.commands.REFRESH_INFO;
}
// Comprobar si en los parametros viene la opcion de guardar
if (typeof params !== 'undefined' && params.save === true) {
const self = this;
// Mostrar cuadro de dialogo para guardar procedimiento
this.ogSweetAlert.swal({
title: this.translate.instant('new_command_name'),
type: 'info',
html:
'<form style="text-align: left; padding-left: 10px">\
<div class="form-group">\
<label for="execute">' + this.translate.instant('execute') + '</label>\
<div class="checkbox clip-check check-primary checkbox-inline">\
<input id="execute" icheck checkbox-class="icheckbox_square-blue" radio-class="iradio_square-blue" type="checkbox" class="selection-checkbox" />\
</div>\
</div>\
<div class="form-group">\
<label>' + this.translate.instant('title') + '</label>\
<input type="text" class="form-control" id="command.title" />\
</div>\
<div class="form-group">\
<label for="parameters">' + this.translate.instant('parameters') + '</label>\
<div class="checkbox clip-check check-primary checkbox-inline">\
<input id="parameters" icheck checkbox-class="icheckbox_square-blue" radio-class="iradio_square-blue" type="checkbox" class="selection-checkbox" />\
</div>\
<p class="help-block">' + this.translate.instant('help_command_parameters') + '</p>\
</div>\
</form>',
showCancelButton: true,
confirmButtonColor: '#3c8dbc',
confirmButtonText: this.translate.instant('done'),
closeOnConfirm: true,
preConfirm: () => {
return {
execute: (<HTMLInputElement>document.getElementById('execute')).checked,
command: {
title: (<HTMLInputElement>document.getElementById('command.title')).value,
parameters: (<HTMLInputElement>document.getElementById('parameters')).checked
}
};
}
}).then(
function(response) {
if (response.value) {
response.value.command.script = self.execution.script;
response.value.command.type = self.execution.type;
self.commandService.create(response.value.command).subscribe(
(success) => {
// Si se seleccionó continuar con la ejecución
if (response.value.execute) {
self.loadClients();
self.sendCommand();
} else {
self.router.navigate(['app/commands']);
}
},
(error) => {
self.toaster.pop({type: 'error', title: 'error', body: error});
}
);
}
});
} else {
this.loadClients();
this.sendCommand();
}
}
}
loadClients() {
if (this.ogCommonService.selectedClients) {
this.execution.clients = _.join(Object.keys(this.ogCommonService.selectedClients));
}
}
}
|