UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

113 lines (111 loc) 5.46 kB
import { of } from 'rxjs'; import { concatMap, map, mergeMap, retryWhen, take } from 'rxjs/operators'; import { Logging } from '../diagnostics/logging'; import { PowerShellScripts } from '../generated/powershell-scripts'; import { Net } from './net'; import { PowerShell } from './powershell'; export var ScheduleRebootReasonOption; (function (ScheduleRebootReasonOption) { ScheduleRebootReasonOption["UserDefined"] = "u"; ScheduleRebootReasonOption["Planned"] = "p"; })(ScheduleRebootReasonOption || (ScheduleRebootReasonOption = {})); export class ScheduleRebootManager { appContext; extensionBroker; nodeName; strings = MsftSme.getStrings().MsftSmeShell; scheduleRebootStrings; get logSourceName() { return 'ScheduleRebootManager'; } constructor(appContext, extensionBroker) { this.appContext = appContext; this.extensionBroker = extensionBroker; this.nodeName = this.appContext.activeConnection.nodeName; this.scheduleRebootStrings = this.strings.Core.ScheduleReboot; } showScheduleRestartDialog(args) { return this.extensionBroker.showDialog('msft.sme.shell-extensions!scheduleRebootDialog', 0, args); } onRestart(restartOption, reason) { const params = { ...restartOption, ...reason }; const session = this.appContext.powerShell.createAutomaticSession(this.nodeName); const command = PowerShell.createCommand(PowerShellScripts.Start_ScheduledReboot, params); return this.appContext.powerShell.run(session, command); } cancelScheduledReboot() { const session = this.appContext.powerShell.createAutomaticSession(this.nodeName); const command = PowerShell.createCommand(PowerShellScripts.Stop_Reboot); return this.appContext.powerShell.run(session, command); } showNotification(restartOption, success, error) { const notification = this.appContext.notification.create(this.appContext.gateway.gatewayName); if (success) { if (restartOption.restartLater) { notification.showSuccess(this.scheduleRebootStrings.Schedule.Success.Title, this.scheduleRebootStrings.Schedule.Success.Message.format(this.nodeName, restartOption.restartTime)); } else { notification.showInProgress(this.scheduleRebootStrings.Restart.Inprogress.Title, this.scheduleRebootStrings.Restart.Inprogress.Message.format(this.nodeName)); } } else { if (restartOption.restartLater) { notification.showError(this.scheduleRebootStrings.Schedule.Fail.Title, this.scheduleRebootStrings.Schedule.Fail.Message.format(this.nodeName, Net.getErrorMessage(error))); } else { notification.showError(this.scheduleRebootStrings.Restart.Fail.Title, this.scheduleRebootStrings.Restart.Fail.Message.format(this.nodeName, Net.getErrorMessage(error))); } } } restart(restartOption, reason) { return this.onRestart(restartOption, reason).pipe(retryWhen(errors => { let retries = 1; return errors.pipe(mergeMap(error => { let rebootScheduledError = false; if (error.responseType === 'json') { const errorJson = JSON.parse(JSON.stringify(error)); const errorsList = errorJson.response ? errorJson.response.errors : []; // Error 1190: A system shutdown has already been scheduled.(1190) rebootScheduledError = errorsList && errorsList.some(x => x && x.message ? x.message.includes('1190') : false); } if (retries === 0 || !rebootScheduledError) { throw error; } // Cancelling scheduled system reboot Logging.logInformational(this.logSourceName, this.scheduleRebootStrings.Info.CancellingReboot); retries--; return this.cancelScheduledReboot().pipe(take(1)); })); }), map(() => { // Success: true this.showNotification(restartOption, true); return { success: true, restartOption: restartOption }; }, (error) => { // Success: false this.showNotification(restartOption, false, error); return { success: false, restartOption: restartOption }; })); } scheduleReboot(args) { return this.showScheduleRestartDialog(args).pipe(concatMap((dialogResult) => { const restartOption = dialogResult.restartOption; // On hide/cancel, do nothing if (!dialogResult.confirm) { return of({ restartOption: restartOption }); } if (MsftSme.isNullOrUndefined(restartOption.restartLater)) { throw new Error(`${this.logSourceName}: ${this.scheduleRebootStrings.Error.NoRestartOptions}`); } let reason; if (!MsftSme.isEmpty(args) && args.reason) { reason = { restartReason: args.reason.restartReason, reasonNumberMajor: args.reason.reasonNumberMajor, reasonNumberMinor: args.reason.reasonNumberMinor }; } return this.restart(restartOption, reason); })); } } //# sourceMappingURL=schedule-reboot.js.map