|
| 1 | +import type PolykeyClient from '../../PolykeyClient'; |
| 2 | +import CommandPolykey from '../CommandPolykey'; |
| 3 | +import * as binUtils from '../utils'; |
| 4 | +import * as binOptions from '../utils/options'; |
| 5 | +import * as binProcessors from '../utils/processors'; |
| 6 | +import * as binErrors from '../errors'; |
| 7 | +import config from '../../config'; |
| 8 | + |
| 9 | +class CommandRestart extends CommandPolykey { |
| 10 | + constructor(...args: ConstructorParameters<typeof CommandPolykey>) { |
| 11 | + super(...args); |
| 12 | + this.name('restart'); |
| 13 | + this.description('Restart the Polykey Agent'); |
| 14 | + this.addOption(binOptions.nodeId); |
| 15 | + this.addOption(binOptions.clientHost); |
| 16 | + this.addOption(binOptions.clientPort); |
| 17 | + this.addOption(binOptions.ingressHost); |
| 18 | + this.addOption(binOptions.ingressPort); |
| 19 | + this.addOption(binOptions.fresh); |
| 20 | + this.action(async (options) => { |
| 21 | + const { default: PolykeyClient } = await import('../../PolykeyClient'); |
| 22 | + const agentPB = await import('../../proto/js/polykey/v1/agent/agent_pb'); |
| 23 | + const clientStatus = await binProcessors.processClientStatus( |
| 24 | + options.nodePath, |
| 25 | + options.nodeId, |
| 26 | + options.clientHost, |
| 27 | + options.clientPort, |
| 28 | + this.fs, |
| 29 | + this.logger.getChild(binProcessors.processClientOptions.name), |
| 30 | + ); |
| 31 | + const statusInfo = clientStatus.statusInfo; |
| 32 | + if (statusInfo?.status === 'DEAD') { |
| 33 | + this.logger.info('Agent is already dead'); |
| 34 | + return; |
| 35 | + } else if (statusInfo?.status === 'STOPPING') { |
| 36 | + this.logger.info('Agent is already stopping'); |
| 37 | + return; |
| 38 | + } else if (statusInfo?.status === 'STARTING') { |
| 39 | + throw new binErrors.ErrorCLIStatusStarting(); |
| 40 | + } |
| 41 | + const meta = await binProcessors.processAuthentication( |
| 42 | + options.passwordFile, |
| 43 | + this.fs, |
| 44 | + ); |
| 45 | + const password = await binProcessors.processPassword( |
| 46 | + options.passwordFile, |
| 47 | + this.fs, |
| 48 | + ); |
| 49 | + // Either the statusInfo is undefined or LIVE |
| 50 | + // Either way, the connection parameters now exist |
| 51 | + let pkClient: PolykeyClient; |
| 52 | + this.exitHandlers.handlers.push(async () => { |
| 53 | + if (pkClient != null) await pkClient.stop(); |
| 54 | + }); |
| 55 | + try { |
| 56 | + pkClient = await PolykeyClient.createPolykeyClient({ |
| 57 | + nodePath: options.nodePath, |
| 58 | + nodeId: clientStatus.nodeId!, |
| 59 | + host: clientStatus.clientHost!, |
| 60 | + port: clientStatus.clientPort!, |
| 61 | + logger: this.logger.getChild(PolykeyClient.name), |
| 62 | + }); |
| 63 | + const restartMessage = new agentPB.RestartMessage(); |
| 64 | + restartMessage.setPassword(password); |
| 65 | + restartMessage.setClientHost( |
| 66 | + options.clientHost ?? config.defaults.networkConfig.clientHost, |
| 67 | + ); |
| 68 | + restartMessage.setClientPort( |
| 69 | + options.clientPort ?? config.defaults.networkConfig.clientPort, |
| 70 | + ); |
| 71 | + restartMessage.setIngressHost(options.ingressHost); |
| 72 | + restartMessage.setIngressPort(options.ingressPort); |
| 73 | + restartMessage.setFresh(options.fresh); |
| 74 | + await binUtils.retryAuthentication( |
| 75 | + (auth) => pkClient.grpcClient.agentRestart(restartMessage, auth), |
| 76 | + meta, |
| 77 | + ); |
| 78 | + this.logger.info('Restarting Agent'); |
| 79 | + } finally { |
| 80 | + if (pkClient! != null) await pkClient.stop(); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export default CommandRestart; |
0 commit comments