@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
1 lines • 9.23 kB
Source Map (JSON)
{"version":3,"sources":["../../../packages/core/data/schedule-reboot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAM,MAAM,MAAM,CAAC;AAMtC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAItE,oBAAY,0BAA0B;IAClC,WAAW,MAAM;IACjB,OAAO,MAAM;CAChB;AAED,MAAM,WAAW,oBAAoB;IAEjC,aAAa,EAAE,0BAA0B,CAAC;IAG1C,iBAAiB,EAAE,MAAM,CAAC;IAG1B,iBAAiB,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,6BAA6B;IAC1C,gBAAgB,CAAC,EAAE,IAAI,CAAC;IACxB,gBAAgB,CAAC,EAAE,IAAI,CAAC;IACxB,MAAM,CAAC,EAAE,oBAAoB,CAAC;CACjC;AAED,MAAM,WAAW,cAAc;IAC3B;;;MAGE;IACF,YAAY,EAAE,OAAO,CAAC;IACtB,WAAW,CAAC,EAAE,IAAI,CAAC;CACtB;AAED,MAAM,WAAW,0BAA0B;IACvC,OAAO,EAAE,OAAO,CAAC;IACjB,aAAa,EAAE,cAAc,CAAC;CACjC;AAED,MAAM,WAAW,sBAAsB;IACnC,aAAa,EAAE,cAAc,CAAC;IAC9B,OAAO,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,qBAAa,qBAAqB;IAU1B,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,eAAe;IAV3B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAA8C;IAC7D,OAAO,CAAC,qBAAqB,CAAC;IAE9B,SAAS,KAAK,aAAa,WAE1B;gBAGW,UAAU,EAAE,UAAU,EACtB,eAAe,EAAE,eAAe;IAM5C,OAAO,CAAC,yBAAyB;IAKjC,OAAO,CAAC,SAAS;IAOjB,OAAO,CAAC,qBAAqB;IAM7B,OAAO,CAAC,gBAAgB;IA2BxB,OAAO,CAAC,OAAO;IAwCR,cAAc,CAAC,IAAI,CAAC,EAAE,6BAA6B,GAAG,UAAU,CAAC,sBAAsB,CAAC;CA6BlG","file":"schedule-reboot.d.ts","sourcesContent":["import { Observable, of } from 'rxjs';\r\nimport { AjaxError } from 'rxjs/ajax';\r\nimport { concatMap, map, mergeMap, retryWhen, take } from 'rxjs/operators';\r\nimport { Logging } from '../diagnostics/logging';\r\nimport { PowerShellScripts } from '../generated/powershell-scripts';\r\nimport { Strings } from '../generated/strings';\r\nimport { AppContext } from './app-context';\r\nimport { ExtensionBroker } from './extension-broker/extension-broker';\r\nimport { Net } from './net';\r\nimport { PowerShell } from './powershell';\r\n\r\nexport enum ScheduleRebootReasonOption {\r\n UserDefined = 'u',\r\n Planned = 'p',\r\n}\r\n\r\nexport interface ScheduleRebootReason {\r\n // p (planned), u: user-defined, undefined: unplanned\r\n restartReason: ScheduleRebootReasonOption;\r\n\r\n // 0-255\r\n reasonNumberMajor: number;\r\n\r\n // 0-65536\r\n reasonNumberMinor: number;\r\n}\r\n\r\nexport interface ScheduleRebootDialogParameter {\r\n minDateTimeLocal?: Date;\r\n maxDateTimeLocal?: Date;\r\n reason?: ScheduleRebootReason;\r\n}\r\n\r\nexport interface RestartSetting {\r\n /**\r\n * The flag indicates restart later or not\r\n * true: restart later, false: restart immediately\r\n */\r\n restartLater: boolean;\r\n restartTime?: Date;\r\n}\r\n\r\nexport interface ScheduleRebootDialogResult {\r\n confirm: boolean;\r\n restartOption: RestartSetting;\r\n}\r\n\r\nexport interface ScheduleRebootResponse {\r\n restartOption: RestartSetting;\r\n success?: boolean;\r\n}\r\n\r\nexport class ScheduleRebootManager {\r\n private nodeName: string;\r\n private strings = MsftSme.getStrings<Strings>().MsftSmeShell;\r\n private scheduleRebootStrings;\r\n\r\n protected get logSourceName() {\r\n return 'ScheduleRebootManager';\r\n }\r\n\r\n constructor(\r\n private appContext: AppContext,\r\n private extensionBroker: ExtensionBroker\r\n ) {\r\n this.nodeName = this.appContext.activeConnection.nodeName;\r\n this.scheduleRebootStrings = this.strings.Core.ScheduleReboot;\r\n }\r\n\r\n private showScheduleRestartDialog(args?: ScheduleRebootDialogParameter): Observable<ScheduleRebootDialogResult> {\r\n return this.extensionBroker.showDialog(\r\n 'msft.sme.shell-extensions!scheduleRebootDialog', 0, args);\r\n }\r\n\r\n private onRestart(restartOption: RestartSetting, reason?: ScheduleRebootReason): Observable<any> {\r\n const params = { ...restartOption, ...reason };\r\n const session = this.appContext.powerShell.createAutomaticSession(this.nodeName);\r\n const command = PowerShell.createCommand(PowerShellScripts.Start_ScheduledReboot, params);\r\n return this.appContext.powerShell.run(session, command);\r\n }\r\n\r\n private cancelScheduledReboot(): Observable<any> {\r\n const session = this.appContext.powerShell.createAutomaticSession(this.nodeName);\r\n const command = PowerShell.createCommand(PowerShellScripts.Stop_Reboot);\r\n return this.appContext.powerShell.run(session, command);\r\n }\r\n\r\n private showNotification(restartOption: RestartSetting, success: boolean, error?: any) {\r\n const notification = this.appContext.notification.create(\r\n this.appContext.gateway.gatewayName);\r\n if (success) {\r\n if (restartOption.restartLater) {\r\n notification.showSuccess(\r\n this.scheduleRebootStrings.Schedule.Success.Title,\r\n this.scheduleRebootStrings.Schedule.Success.Message.format(\r\n this.nodeName, restartOption.restartTime));\r\n } else {\r\n notification.showInProgress(\r\n this.scheduleRebootStrings.Restart.Inprogress.Title,\r\n this.scheduleRebootStrings.Restart.Inprogress.Message.format(this.nodeName));\r\n }\r\n } else {\r\n if (restartOption.restartLater) {\r\n notification.showError(\r\n this.scheduleRebootStrings.Schedule.Fail.Title,\r\n this.scheduleRebootStrings.Schedule.Fail.Message.format(this.nodeName, Net.getErrorMessage(error)));\r\n } else {\r\n notification.showError(\r\n this.scheduleRebootStrings.Restart.Fail.Title,\r\n this.scheduleRebootStrings.Restart.Fail.Message.format(this.nodeName, Net.getErrorMessage(error)));\r\n }\r\n }\r\n }\r\n\r\n private restart(restartOption: RestartSetting, reason: ScheduleRebootReason): Observable<ScheduleRebootResponse> {\r\n return this.onRestart(restartOption, reason).pipe(\r\n retryWhen(errors => {\r\n let retries = 1;\r\n return errors.pipe(\r\n mergeMap(error => {\r\n let rebootScheduledError = false;\r\n if (error.responseType === 'json') {\r\n const errorJson = JSON.parse(JSON.stringify(error));\r\n const errorsList = errorJson.response ? errorJson.response.errors : [];\r\n\r\n // Error 1190: A system shutdown has already been scheduled.(1190)\r\n rebootScheduledError = errorsList && errorsList.some(\r\n x => x && x.message ? x.message.includes('1190') : false);\r\n }\r\n if (retries === 0 || !rebootScheduledError) {\r\n throw error;\r\n }\r\n // Cancelling scheduled system reboot\r\n Logging.logInformational(\r\n this.logSourceName, this.scheduleRebootStrings.Info.CancellingReboot);\r\n retries--;\r\n return this.cancelScheduledReboot().pipe(take(1));\r\n })\r\n );\r\n }),\r\n map(\r\n () => {\r\n // Success: true\r\n this.showNotification(restartOption, true);\r\n return { success: true, restartOption: restartOption };\r\n },\r\n (error: AjaxError) => {\r\n // Success: false\r\n this.showNotification(restartOption, false, error);\r\n return { success: false, restartOption: restartOption };\r\n })\r\n );\r\n }\r\n\r\n public scheduleReboot(args?: ScheduleRebootDialogParameter): Observable<ScheduleRebootResponse> {\r\n return this.showScheduleRestartDialog(args).pipe(\r\n concatMap(\r\n (dialogResult: ScheduleRebootDialogResult) => {\r\n const restartOption = dialogResult.restartOption;\r\n\r\n // On hide/cancel, do nothing\r\n if (!dialogResult.confirm) {\r\n return of({ restartOption: restartOption });\r\n }\r\n\r\n if (MsftSme.isNullOrUndefined(restartOption.restartLater)) {\r\n throw new Error(\r\n `${this.logSourceName}: ${this.scheduleRebootStrings.Error.NoRestartOptions}`);\r\n }\r\n\r\n let reason: ScheduleRebootReason;\r\n if (!MsftSme.isEmpty(args) && args.reason) {\r\n reason = {\r\n restartReason: args.reason.restartReason,\r\n reasonNumberMajor: args.reason.reasonNumberMajor,\r\n reasonNumberMinor: args.reason.reasonNumberMinor\r\n };\r\n }\r\n\r\n return this.restart(restartOption, reason);\r\n })\r\n );\r\n }\r\n}\r\n"]}