-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCommandStop.ts
More file actions
68 lines (66 loc) · 2.47 KB
/
CommandStop.ts
File metadata and controls
68 lines (66 loc) · 2.47 KB
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
import type PolykeyClient from '../../PolykeyClient';
import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
import * as binProcessors from '../utils/processors';
import * as binErrors from '../errors';
class CommandStop extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
super(...args);
this.name('stop');
this.description('Stop the Polykey Agent');
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
this.addOption(binOptions.clientPort);
this.action(async (options) => {
const { default: PolykeyClient } = await import('../../PolykeyClient');
const utilsPB = await import('../../proto/js/polykey/v1/utils/utils_pb');
const clientStatus = await binProcessors.processClientStatus(
options.nodePath,
options.nodeId,
options.clientHost,
options.clientPort,
this.fs,
this.logger.getChild(binProcessors.processClientOptions.name),
);
const statusInfo = clientStatus.statusInfo;
if (statusInfo?.status === 'DEAD') {
this.logger.info('Agent is already dead');
return;
} else if (statusInfo?.status === 'STOPPING') {
this.logger.info('Agent is already stopping');
return;
} else if (statusInfo?.status === 'STARTING') {
throw new binErrors.ErrorCLIStatusStarting();
}
const meta = await binProcessors.processAuthentication(
options.passwordFile,
this.fs,
);
// Either the statusInfo is undefined or LIVE
// Either way, the connection parameters now exist
let pkClient: PolykeyClient;
this.exitHandlers.handlers.push(async () => {
if (pkClient != null) await pkClient.stop();
});
try {
pkClient = await PolykeyClient.createPolykeyClient({
nodePath: options.nodePath,
nodeId: clientStatus.nodeId!,
host: clientStatus.clientHost!,
port: clientStatus.clientPort!,
logger: this.logger.getChild(PolykeyClient.name),
});
const emptyMessage = new utilsPB.EmptyMessage();
await binUtils.retryAuthentication(
(auth) => pkClient.grpcClient.agentStop(emptyMessage, auth),
meta,
);
this.logger.info('Stopping Agent');
} finally {
if (pkClient! != null) await pkClient.stop();
}
});
}
}
export default CommandStop;