@pai-tech/pai-os
Version:
PAI-OS wrapper
150 lines (113 loc) • 3.78 kB
JavaScript
/**
* @file PAI-OS wrapper module.
* @author <a href="mailto:ron@pai-tech.org">Ron Fridman</a>
* @copyright (c) PAI-TECH Artificial Intelligenec 2018
*/
const { PAICodeCommand, PAICodeCommandContext, PAICodeModule, PAICode, PAIModuleConfigParam, PAIModuleConfig, PAILogger, PAIModuleCommandSchema, PAIModuleCommandParamSchema } = require('@pai-tech/pai-code');
const exec = require('child_process').exec;
const childProcess = require('child_process');
const OSUtils = require('./src/os-utils');
const PAI_OS_LINUX_DEFAULT_FOLDER = './PAI';
const PAI_OS_WIN_DEFAULT_FOLDER = './PAI';
const CONFIG_PATH = 'PAI_OS_PATH';
class PCM_PAI_OS extends PAICodeModule {
constructor() {
let infoText = `
welcome to pai-os:
functions:
2. system-info
`;
super(infoText);
this.config.schema = [
//PAIModuleConfigParam(label, description, paramName, defaultValue)
new PAIModuleConfigParam("PAI OS Path", "Path to the directory where PAI_OS is installed", CONFIG_PATH, "/var/PAI")
];
}
async checkSystemVariable()
{
let PAI_SYSTEM_VARIABLE = (OSUtils.isWindows()) ? 'echo %PAI%' : 'echo $PAI' ;
let os_result = childProcess.execSync(PAI_SYSTEM_VARIABLE, {encoding: "utf8"}).trim();
return (os_result && os_result.length>0);
}
getOSPath()
{
let pai_os_folder = (OSUtils.isWindows()) ? PAI_OS_WIN_DEFAULT_FOLDER : PAI_OS_LINUX_DEFAULT_FOLDER;
return pai_os_folder;
}
/**
* load basic module commands from super
* and load all the functions for this module
*/
async load()
{
await super.load(this);
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "run",
func:"run_bash",
params:{
"command": new PAIModuleCommandParamSchema("command", "Run operation system commands", true,"Command to execute")
},
showOnInterface:false
}));
this.loadCommandWithSchema(new PAIModuleCommandSchema({
op: "system-info",
func:"system_info",
description: "Get Bot Operation System information"
}));
let paiosf = this.getOSPath();
PAILogger.info("PAI-OS folder: " + paiosf);
this.config.setConfigParam(CONFIG_PATH, paiosf);
}
/**
* @override
* @deprecated
* @return {string}
*/
setModuleName() {
return this.get_module_name();
}
get_module_name() {
return "pai-os";
}
/**
*
* @param {PAICodeCommand} cmd
*/
run_bash(cmd)
{
return new Promise( (resolve,reject) => {
let bashCommand = cmd.params["command"].value;
for (let i = 3; i < 100; i++) {
if(cmd.params.hasOwnProperty(i.toString()))
{
bashCommand += ' ' +cmd.params[i.toString()].value;
}
else {
break;
}
}
PAILogger.info('pai-os running: ' + bashCommand);
exec(bashCommand, function(error, stdout, stderr) {
if (error) {
PAILogger.error(error.code);
return reject(error);
}
else
{
return resolve(stdout);
}
});
});
}
/**
*
* @param {PAICodeCommand} cmd
*/
system_info(cmd)
{
return new Promise( (resolve,reject) => {
return resolve(OSUtils.getOperationSystemInformation());
});
}
}
module.exports = PCM_PAI_OS;