228 lines
8.1 KiB
TypeScript
228 lines
8.1 KiB
TypeScript
import * as plugins from './spark.plugins.ts';
|
|
import * as paths from './spark.paths.ts';
|
|
import { Spark } from './spark.classes.spark.ts';
|
|
import { logger } from './spark.logging.ts';
|
|
|
|
type TSparkCliArgv = Record<string, string | boolean | undefined>;
|
|
|
|
export const runCli = () => {
|
|
const smartshellInstance = new plugins.smartshell.Smartshell({
|
|
executor: 'bash',
|
|
});
|
|
const sparkInstance = new Spark();
|
|
const smartcliInstance = new plugins.smartcli.Smartcli();
|
|
smartcliInstance.standardCommand().subscribe(() => {
|
|
logger.log('info', 'no action specified! you can type:');
|
|
logger.log('info', '* installdaemon');
|
|
logger.log('info', '* metrics');
|
|
});
|
|
|
|
smartcliInstance.addCommand('installdaemon').subscribe(async (argvArg) => {
|
|
logger.log('ok', 'we are apparently not running as daemon yet');
|
|
logger.log('info', 'trying to set this up now');
|
|
await persistDaemonOptions(sparkInstance, argvArg);
|
|
const sparkService = await sparkInstance.smartdaemon.addService({
|
|
name: 'spark',
|
|
version: sparkInstance.sparkInfo.projectInfo.version,
|
|
command: 'spark asdaemon',
|
|
description: 'spark daemon service',
|
|
workingDir: paths.packageDir,
|
|
});
|
|
await sparkService.save();
|
|
await sparkService.enable();
|
|
await sparkService.start();
|
|
});
|
|
|
|
smartcliInstance.addCommand('updatedaemon').subscribe(async (argvArg) => {
|
|
logger.log('ok', 'we are apparently trying to update the daemon for spark');
|
|
logger.log('info', 'trying to set this up now');
|
|
await persistDaemonOptions(sparkInstance, argvArg);
|
|
const sparkService = await sparkInstance.smartdaemon.addService({
|
|
name: 'spark',
|
|
version: sparkInstance.sparkInfo.projectInfo.version,
|
|
command: 'spark asdaemon',
|
|
description: 'spark daemon service',
|
|
workingDir: paths.packageDir,
|
|
});
|
|
await sparkService.reload();
|
|
});
|
|
|
|
smartcliInstance.addCommand('asdaemon').subscribe(async (argvArg) => {
|
|
logger.log('success', 'looks like we are running as daemon now');
|
|
logger.log('info', 'starting spark in daemon mode');
|
|
|
|
await persistDaemonOptions(sparkInstance, argvArg);
|
|
|
|
// lets determine the mode if specified
|
|
let mode = readArg(argvArg, 'mode');
|
|
if (mode === 'cloudly') {
|
|
await sparkInstance.sparkConfig.kvStore.writeKey('mode', 'cloudly');
|
|
} else if (mode === 'coreflow-node') {
|
|
await sparkInstance.sparkConfig.kvStore.writeKey('mode', 'coreflow-node');
|
|
} else if (mode) {
|
|
logger.log('error', 'unknown mode specified');
|
|
Deno.exit(1);
|
|
} else {
|
|
// mode is not specified by cli, lets get it from the config
|
|
mode = await sparkInstance.sparkConfig.kvStore.readKey('mode');
|
|
}
|
|
|
|
if (!mode) {
|
|
logger.log('error', 'no mode specified by either cli or config');
|
|
Deno.exit(1);
|
|
} else if (mode === 'cloudly') {
|
|
sparkInstance.sparkUpdateManager.services.push({
|
|
name: `cloudly`,
|
|
image: `code.foss.global/serve.zone/cloudly`,
|
|
url: `cloudly`,
|
|
environment: `production`,
|
|
port: `3000`,
|
|
secretJson: {
|
|
SERVEZONE_PORT: `3000`,
|
|
SERVEZONE_ENVIRONMENT: `production`,
|
|
},
|
|
});
|
|
} else if (mode === 'coreflow-node') {
|
|
const cloudlyUrl = await readOption(sparkInstance, argvArg, 'cloudlyUrl', 'cloudly-url', 'CLOUDLY_URL');
|
|
const jumpcode = await readOption(sparkInstance, argvArg, 'jumpcode', 'jumpCode', 'JUMPCODE');
|
|
if (!cloudlyUrl || !jumpcode) {
|
|
logger.log('error', 'coreflow-node mode requires CLOUDLY_URL/cloudlyUrl and JUMPCODE/jumpcode');
|
|
Deno.exit(1);
|
|
}
|
|
sparkInstance.sparkUpdateManager.services.push({
|
|
name: `coreflow`,
|
|
image: `code.foss.global/serve.zone/coreflow`,
|
|
url: `coreflow`,
|
|
environment: `production`,
|
|
port: `3000`,
|
|
secretJson: {
|
|
SERVEZONE_PORT: `3000`,
|
|
SERVEZONE_ENVIRONMENT: `production`,
|
|
CLOUDLY_URL: cloudlyUrl,
|
|
JUMPCODE: jumpcode,
|
|
},
|
|
});
|
|
}
|
|
|
|
await sparkInstance.daemonStart();
|
|
});
|
|
|
|
smartcliInstance.addCommand('metrics').subscribe(async () => {
|
|
const metrics = await sparkInstance.sparkMetricsCollector.collectMetrics();
|
|
console.log(plugins.smartjson.stringify(metrics));
|
|
});
|
|
|
|
smartcliInstance.addCommand('logs').subscribe(async () => {
|
|
await smartshellInstance.exec(`journalctl -u smartdaemon_spark -f`);
|
|
});
|
|
|
|
smartcliInstance.addCommand('prune').subscribe(async () => {
|
|
// daemon
|
|
await smartshellInstance.exec(`systemctl stop smartdaemon_spark`);
|
|
logger.log('ok', 'stopped serverconfig daemon');
|
|
await plugins.smartdelay.delayFor(5000);
|
|
|
|
// services
|
|
await smartshellInstance.exec(`docker stack rm $(docker stack ls -q)`);
|
|
logger.log('ok', 'removed docker stacks');
|
|
await plugins.smartdelay.delayFor(5000);
|
|
|
|
// services
|
|
await smartshellInstance.exec(`docker service rm $(docker service ls -q)`);
|
|
logger.log('ok', 'removed docker services');
|
|
await plugins.smartdelay.delayFor(5000);
|
|
|
|
// secrets
|
|
await smartshellInstance.exec(`docker secret rm $(docker secret ls -q)`);
|
|
logger.log('ok', 'removed docker secrets');
|
|
await plugins.smartdelay.delayFor(5000);
|
|
|
|
// networks
|
|
await smartshellInstance.exec(`docker network rm szncorechat sznwebgateway`);
|
|
logger.log('ok', 'removed docker networks');
|
|
await plugins.smartdelay.delayFor(5000);
|
|
|
|
await smartshellInstance.exec(`docker system prune -af`);
|
|
logger.log('ok', 'pruned docker system');
|
|
await plugins.smartdelay.delayFor(5000);
|
|
|
|
// restart docker
|
|
await smartshellInstance.exec(`systemctl restart docker`);
|
|
logger.log('ok', 'restarted the docker service');
|
|
await plugins.smartdelay.delayFor(5000);
|
|
|
|
// serverconfig daemon
|
|
await smartshellInstance.exec(`systemctl start smartdaemon_spark`);
|
|
logger.log('ok', 'handed over control back to serverconfig daemon');
|
|
});
|
|
smartcliInstance.startParse();
|
|
};
|
|
|
|
const persistDaemonOptions = async (sparkInstance: Spark, argvArg: TSparkCliArgv) => {
|
|
const mode = readArg(argvArg, 'mode');
|
|
if (mode) {
|
|
await sparkInstance.sparkConfig.kvStore.writeKey('mode', mode);
|
|
}
|
|
|
|
const cloudlyUrl = readArg(argvArg, 'cloudlyUrl')
|
|
|| readArg(argvArg, 'cloudly-url')
|
|
|| Deno.env.get('CLOUDLY_URL');
|
|
if (cloudlyUrl) {
|
|
await sparkInstance.sparkConfig.kvStore.writeKey('cloudlyUrl', cloudlyUrl);
|
|
}
|
|
|
|
const jumpcode = readArg(argvArg, 'jumpcode') || readArg(argvArg, 'jumpCode') || Deno.env.get('JUMPCODE');
|
|
if (jumpcode) {
|
|
await sparkInstance.sparkConfig.kvStore.writeKey('jumpcode', jumpcode);
|
|
}
|
|
|
|
const nodeId = readArg(argvArg, 'nodeId') || readArg(argvArg, 'node-id') || Deno.env.get('SPARK_NODE_ID');
|
|
if (nodeId) {
|
|
await sparkInstance.sparkConfig.kvStore.writeKey('nodeId', nodeId);
|
|
}
|
|
|
|
const nodeToken = readArg(argvArg, 'nodeToken')
|
|
|| readArg(argvArg, 'node-token')
|
|
|| Deno.env.get('SPARK_NODE_TOKEN');
|
|
if (nodeToken) {
|
|
await sparkInstance.sparkConfig.kvStore.writeKey('nodeToken', nodeToken);
|
|
}
|
|
|
|
const enableHostUpdates = readArg(argvArg, 'enableHostUpdates')
|
|
|| readArg(argvArg, 'enable-host-updates');
|
|
if (typeof enableHostUpdates !== 'undefined') {
|
|
await sparkInstance.sparkConfig.kvStore.writeKey('enableHostUpdates', String(enableHostUpdates));
|
|
}
|
|
|
|
const heartbeatIntervalMs = readArg(argvArg, 'heartbeatIntervalMs')
|
|
|| readArg(argvArg, 'heartbeat-interval-ms');
|
|
if (heartbeatIntervalMs) {
|
|
await sparkInstance.sparkConfig.kvStore.writeKey('heartbeatIntervalMs', String(heartbeatIntervalMs));
|
|
}
|
|
};
|
|
|
|
const readOption = async (
|
|
sparkInstance: Spark,
|
|
argvArg: TSparkCliArgv,
|
|
primaryKey: string,
|
|
secondaryKey: string,
|
|
envKey: string,
|
|
) => {
|
|
return readArg(argvArg, primaryKey)
|
|
|| readArg(argvArg, secondaryKey)
|
|
|| Deno.env.get(envKey)
|
|
|| await sparkInstance.sparkConfig.kvStore.readKey(primaryKey)
|
|
|| await sparkInstance.sparkConfig.kvStore.readKey(secondaryKey);
|
|
};
|
|
|
|
const readArg = (argvArg: TSparkCliArgv, keyArg: string) => {
|
|
const value = argvArg[keyArg];
|
|
if (typeof value === 'string') {
|
|
return value;
|
|
}
|
|
if (typeof value === 'boolean') {
|
|
return String(value);
|
|
}
|
|
return undefined;
|
|
};
|