241 lines
7.3 KiB
TypeScript
241 lines
7.3 KiB
TypeScript
import { Component, Input, OnInit } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { Router } from "@angular/router";
|
|
import { ToastrService } from "ngx-toastr";
|
|
import { ConfigService } from '@services/config.service';
|
|
|
|
@Component({
|
|
selector: 'app-execute-command',
|
|
templateUrl: './execute-command.component.html',
|
|
styleUrls: ['./execute-command.component.css']
|
|
})
|
|
export class ExecuteCommandComponent implements OnInit {
|
|
@Input() clientState: string = 'off';
|
|
@Input() clientData: any[] = [];
|
|
@Input() buttonType: 'icon' | 'text' | 'menu-item' = 'icon';
|
|
@Input() buttonText: string = 'Ejecutar Comandos';
|
|
@Input() icon: string = 'terminal';
|
|
@Input() disabled: boolean = false;
|
|
@Input() runScriptContext: string = '';
|
|
baseUrl: string;
|
|
loading: boolean = true;
|
|
|
|
arrayCommands: any[] = [
|
|
{ name: 'Enceder', slug: 'power-on', disabled: false },
|
|
{ name: 'Apagar', slug: 'power-off', disabled: false },
|
|
{ name: 'Reiniciar', slug: 'reboot', disabled: false },
|
|
{ name: 'Iniciar Sesión', slug: 'login', disabled: true },
|
|
{ name: 'Crear imagen', slug: 'create-image', disabled: false },
|
|
{ name: 'Clonar/desplegar imagen', slug: 'deploy-image', disabled: false },
|
|
{ name: 'Eliminar Imagen Cache', slug: 'delete-image-cache', disabled: true },
|
|
{ name: 'Particionar y Formatear', slug: 'partition', disabled: false },
|
|
{ name: 'Inventario Software', slug: 'software-inventory', disabled: true },
|
|
{ name: 'Inventario Hardware', slug: 'hardware-inventory', disabled: true },
|
|
{ name: 'Ejecutar comando', slug: 'run-script', disabled: false },
|
|
];
|
|
|
|
client: any = {};
|
|
|
|
constructor(
|
|
private http: HttpClient,
|
|
private router: Router,
|
|
private configService: ConfigService,
|
|
private toastService: ToastrService
|
|
) {
|
|
this.baseUrl = this.configService.apiUrl;
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
this.clientData = this.clientData || [];
|
|
this.updateCommandStates();
|
|
}
|
|
|
|
ngOnChanges(): void {
|
|
this.updateCommandStates();
|
|
}
|
|
|
|
private updateCommandStates(): void {
|
|
let states: string[] = [];
|
|
|
|
if (this.clientData.length > 0) {
|
|
states = this.clientData.map(client => client.status);
|
|
} else if (this.clientState) {
|
|
states = [this.clientState];
|
|
}
|
|
|
|
const allOffOrDisconnected = states.every(state => state === 'off' || state === 'disconnected');
|
|
const allSameState = states.every(state => state === states[0]);
|
|
const multipleClients = this.clientData.length > 1;
|
|
|
|
this.arrayCommands = this.arrayCommands.map(command => {
|
|
if (allOffOrDisconnected) {
|
|
command.disabled = command.slug !== 'power-on';
|
|
} else if (allSameState) {
|
|
if (states[0] === 'off' || states[0] === 'disconnected') {
|
|
command.disabled = command.slug !== 'power-on';
|
|
} else {
|
|
command.disabled = !['power-off', 'reboot', 'login', 'create-image', 'deploy-image', 'partition', 'run-script'].includes(command.slug);
|
|
}
|
|
} else {
|
|
if (command.slug === 'create-image') {
|
|
command.disabled = multipleClients;
|
|
} else if (
|
|
['power-on', 'power-off', 'reboot', 'login', 'deploy-image', 'partition', 'run-script'].includes(command.slug)
|
|
) {
|
|
command.disabled = false;
|
|
} else {
|
|
command.disabled = true;
|
|
}
|
|
}
|
|
return command;
|
|
});
|
|
}
|
|
|
|
onCommandSelect(action: any): void {
|
|
if (action === 'partition') {
|
|
this.openPartitionAssistant();
|
|
}
|
|
|
|
if (action === 'create-image') {
|
|
this.openCreateImageAssistant();
|
|
}
|
|
|
|
if (action === 'deploy-image') {
|
|
this.openDeployImageAssistant();
|
|
}
|
|
|
|
if (action === 'run-script') {
|
|
this.openRunScriptAssistant();
|
|
}
|
|
|
|
if (action === 'login') {
|
|
this.loginClient();
|
|
}
|
|
|
|
if (action === 'reboot') {
|
|
this.rebootClient();
|
|
}
|
|
|
|
if (action === 'power-off') {
|
|
this.powerOffClient();
|
|
}
|
|
|
|
if (action === 'power-on') {
|
|
this.powerOnClient();
|
|
}
|
|
}
|
|
|
|
rebootClient(): void {
|
|
this.http.post(`${this.baseUrl}/clients/server/reboot`, {
|
|
clients: this.clientData.map((client: any) => client['@id'])
|
|
}).subscribe(
|
|
response => {
|
|
this.toastService.success('Cliente actualizado correctamente');
|
|
},
|
|
error => {
|
|
this.toastService.error('Error de conexión con el cliente');
|
|
}
|
|
);
|
|
}
|
|
|
|
loginClient(): void {
|
|
this.http.post(`${this.baseUrl}/clients/server/login-client`, {
|
|
clients: this.clientData.map((client: any) => client['@id'])
|
|
}).subscribe(
|
|
response => {
|
|
this.toastService.success('Cliente actualizado correctamente');
|
|
},
|
|
error => {
|
|
this.toastService.error('Error de conexión con el cliente');
|
|
}
|
|
);
|
|
}
|
|
|
|
powerOnClient(): void {
|
|
this.http.post(`${this.baseUrl}/image-repositories/wol`, {
|
|
clients: this.clientData.map((client: any) => client['@id'])
|
|
}).subscribe(
|
|
response => {
|
|
this.toastService.success('Petición de encendido enviada correctamente');
|
|
},
|
|
error => {
|
|
this.toastService.error('Error de conexión con el cliente');
|
|
}
|
|
);
|
|
}
|
|
|
|
powerOffClient(): void {
|
|
this.http.post(`${this.baseUrl}/clients/server/power-off`, {
|
|
clients: this.clientData.map((client: any) => client['@id'])
|
|
}).subscribe(
|
|
response => {
|
|
this.toastService.success('Petición de apagado enviada correctamente');
|
|
},
|
|
error => {
|
|
this.toastService.error('Error de conexión con el cliente');
|
|
}
|
|
);
|
|
}
|
|
|
|
openPartitionAssistant(): void {
|
|
const clientDataToSend = this.clientData.map(client => ({
|
|
name: client.name,
|
|
mac: client.mac,
|
|
uuid: '/clients/' + client.uuid,
|
|
status: client.status,
|
|
partitions: client.partitions,
|
|
firmwareType: client.firmwareType,
|
|
ip: client.ip
|
|
}));
|
|
|
|
this.router.navigate(['/clients/partition-assistant'], {
|
|
queryParams: { clientData: JSON.stringify(clientDataToSend) }
|
|
}).then(r => {
|
|
console.log('Navigated to partition assistant with data:', this.clientData);
|
|
});
|
|
}
|
|
|
|
openCreateImageAssistant(): void {
|
|
this.router.navigate([`/clients/${this.clientData[0].uuid}/create-image`]).then(r => {
|
|
console.log('navigated', r);
|
|
});
|
|
}
|
|
|
|
openDeployImageAssistant(): void {
|
|
const clientDataToSend = this.clientData.map(client => ({
|
|
name: client.name,
|
|
mac: client.mac,
|
|
uuid: '/clients/' + client.uuid,
|
|
status: client.status,
|
|
partitions: client.partitions,
|
|
ip: client.ip
|
|
}));
|
|
|
|
this.router.navigate(['/clients/deploy-image'], {
|
|
queryParams: { clientData: JSON.stringify(clientDataToSend) }
|
|
}).then(r => {
|
|
console.log('Navigated to deploy image with data:', this.clientData);
|
|
});
|
|
}
|
|
|
|
openRunScriptAssistant(): void {
|
|
const clientDataToSend = this.clientData.map(client => ({
|
|
name: client.name,
|
|
mac: client.mac,
|
|
uuid: '/clients/' + client.uuid,
|
|
status: client.status,
|
|
partitions: client.partitions,
|
|
ip: client.ip
|
|
}));
|
|
|
|
this.router.navigate(['/clients/run-script'], {
|
|
queryParams: {
|
|
clientData: JSON.stringify(clientDataToSend) ,
|
|
runScriptContext: this.runScriptContext
|
|
}
|
|
}).then(() => {
|
|
console.log('Navigated to run script with data:', clientDataToSend, 'runScriptContext:', this.runScriptContext);
|
|
});
|
|
}
|
|
}
|