-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e261da0
commit 4fa172d
Showing
12 changed files
with
550 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
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'; | ||
import config from '../../config'; | ||
|
||
class CommandRestart extends CommandPolykey { | ||
constructor(...args: ConstructorParameters<typeof CommandPolykey>) { | ||
super(...args); | ||
this.name('restart'); | ||
this.description('Restart the Polykey Agent'); | ||
this.addOption(binOptions.nodeId); | ||
this.addOption(binOptions.clientHost); | ||
this.addOption(binOptions.clientPort); | ||
this.addOption(binOptions.ingressHost); | ||
this.addOption(binOptions.ingressPort); | ||
this.addOption(binOptions.fresh); | ||
this.action(async (options) => { | ||
const { default: PolykeyClient } = await import('../../PolykeyClient'); | ||
const agentPB = await import('../../proto/js/polykey/v1/agent/agent_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, | ||
); | ||
const password = await binProcessors.processPassword( | ||
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 restartMessage = new agentPB.RestartMessage(); | ||
restartMessage.setPassword(password); | ||
restartMessage.setClientHost( | ||
options.clientHost ?? config.defaults.networkConfig.clientHost, | ||
); | ||
restartMessage.setClientPort( | ||
options.clientPort ?? config.defaults.networkConfig.clientPort, | ||
); | ||
restartMessage.setIngressHost(options.ingressHost); | ||
restartMessage.setIngressPort(options.ingressPort); | ||
restartMessage.setFresh(options.fresh); | ||
await binUtils.retryAuthentication( | ||
(auth) => pkClient.grpcClient.agentRestart(restartMessage, auth), | ||
meta, | ||
); | ||
this.logger.info('Restarting Agent'); | ||
} finally { | ||
if (pkClient! != null) await pkClient.stop(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default CommandRestart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { Host, Port } from '../../network/types'; | ||
import type * as grpc from '@grpc/grpc-js'; | ||
import type { Authenticate } from '../types'; | ||
import type PolykeyAgent from '../../PolykeyAgent'; | ||
import type * as agentPB from '../../proto/js/polykey/v1/agent/agent_pb'; | ||
import * as grpcUtils from '../../grpc/utils'; | ||
import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; | ||
|
||
function agentRestart({ | ||
authenticate, | ||
pkAgent, | ||
}: { | ||
authenticate: Authenticate; | ||
pkAgent: PolykeyAgent; | ||
}) { | ||
return async ( | ||
call: grpc.ServerUnaryCall<agentPB.RestartMessage, utilsPB.EmptyMessage>, | ||
callback: grpc.sendUnaryData<utilsPB.EmptyMessage>, | ||
): Promise<void> => { | ||
const response = new utilsPB.EmptyMessage(); | ||
try { | ||
const metadata = await authenticate(call.metadata); | ||
call.sendMetadata(metadata); | ||
// Respond first to close the GRPC connection | ||
callback(null, response); | ||
} catch (err) { | ||
callback(grpcUtils.fromError(err), null); | ||
return; | ||
} | ||
const password = call.request.getPassword(); | ||
const networkConfig = { | ||
clientHost: call.request.getClientHost() as Host, | ||
clientPort: call.request.getClientPort() as Port, | ||
ingressHost: call.request.getIngressHost() as Host, | ||
ingressPort: call.request.getIngressPort() as Port, | ||
}; | ||
const fresh = call.request.getFresh(); | ||
await pkAgent.restart({ | ||
password, | ||
networkConfig, | ||
fresh, | ||
}); | ||
return; | ||
}; | ||
} | ||
|
||
export default agentRestart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.