feat: implement cube and cube-server
This commit is contained in:
parent
4db7faa956
commit
653a22ce4d
183
cube.lua
Normal file
183
cube.lua
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
local _VERSION = '1.0.0';
|
||||||
|
|
||||||
|
local cubeCommand, firstArg, secondArg = ...;
|
||||||
|
|
||||||
|
local function isFlag(name)
|
||||||
|
return function(arg)
|
||||||
|
return arg == '-' .. name or arg == '--' .. name;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local isHelpFlag = isFlag('help');
|
||||||
|
local isVersionFlag = isFlag('version');
|
||||||
|
|
||||||
|
local function writeFile(path, content)
|
||||||
|
local file = fs.open(path, "w");
|
||||||
|
|
||||||
|
if not file then
|
||||||
|
return false;
|
||||||
|
end
|
||||||
|
|
||||||
|
file.write(content)
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
end
|
||||||
|
|
||||||
|
local function isConfigFileExists()
|
||||||
|
return fs.exists('.cuberc');
|
||||||
|
end
|
||||||
|
|
||||||
|
local function printUsage()
|
||||||
|
print('cube usage:')
|
||||||
|
print();
|
||||||
|
print('\t\t\tcube init');
|
||||||
|
print('\t\t\tcube ls');
|
||||||
|
print('\t\t\tcube configure');
|
||||||
|
print('\t\t\tcube set-startup <machineId> [command]')
|
||||||
|
print('\t\t\tcube reboot <machineId>')
|
||||||
|
print('\t\t\tcube deploy')
|
||||||
|
print('\t\t\tcube version')
|
||||||
|
print('\t\t\tcube help <command>')
|
||||||
|
end
|
||||||
|
|
||||||
|
local function printUsageCommand(commandName)
|
||||||
|
local USAGES = {
|
||||||
|
init = function()
|
||||||
|
print('\t\t\tcube init');
|
||||||
|
print('Init the master cube directory.')
|
||||||
|
end,
|
||||||
|
ls = function()
|
||||||
|
print('\t\t\tcube ls');
|
||||||
|
print('Print all available cubes in the cluster.')
|
||||||
|
end,
|
||||||
|
configure = function()
|
||||||
|
print('\t\t\tcube configure');
|
||||||
|
print('Setup remote slave cubes.')
|
||||||
|
end,
|
||||||
|
["set-startup"] = function()
|
||||||
|
print('\t\t\tcube set-startup <machineId> [command]')
|
||||||
|
print('Setup a startup shell command on a remote cube.')
|
||||||
|
end,
|
||||||
|
reboot = function()
|
||||||
|
print('\t\t\tcube reboot <machineId>')
|
||||||
|
print('Reboot a cube machine.');
|
||||||
|
end,
|
||||||
|
deploy = function()
|
||||||
|
print('\t\t\tcube deploy')
|
||||||
|
print('Transfer files on all slave cubes.')
|
||||||
|
end,
|
||||||
|
version = function()
|
||||||
|
print('\t\t\tcube version')
|
||||||
|
print('Print the program version.')
|
||||||
|
end,
|
||||||
|
help = function()
|
||||||
|
print('\t\t\tcube help <command>')
|
||||||
|
print('Print help on commands.')
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
local usageFn = USAGES[commandName]
|
||||||
|
|
||||||
|
if not usageFn then
|
||||||
|
return printUsage();
|
||||||
|
end
|
||||||
|
|
||||||
|
return usageFn();
|
||||||
|
end
|
||||||
|
|
||||||
|
if cubeCommand == nil or cubeCommand == '' or isHelpFlag(cubeCommand) then
|
||||||
|
printUsage();
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
|
local COMMANDS = {
|
||||||
|
init = function()
|
||||||
|
if isConfigFileExists() then
|
||||||
|
print('cube is already initialized.');
|
||||||
|
else
|
||||||
|
local ok = writeFile('.cuberc', '');
|
||||||
|
if ok then
|
||||||
|
print('.cuberc file created');
|
||||||
|
else
|
||||||
|
error('Cannot create .cuberc file');
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
ls = function()
|
||||||
|
print('TODO: ls');
|
||||||
|
end,
|
||||||
|
configure = function()
|
||||||
|
if not isConfigFileExists() then
|
||||||
|
print('Error: unable to configure because \'.cuberc\' file is missing\nTry: \'cube init\' command')
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
print('TODO: configure');
|
||||||
|
end,
|
||||||
|
["set-startup"] = function()
|
||||||
|
if not isConfigFileExists() then
|
||||||
|
print('Error: unable to deploy because \'.cuberc\' file is missing\nTry: \'cube init\' command')
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
|
local machineId = firstArg;
|
||||||
|
local shellCommand = secondArg;
|
||||||
|
|
||||||
|
if not machineId then
|
||||||
|
printUsageCommand('set-startup');
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
|
print('changed startup script on machine \'' ..
|
||||||
|
tostring(machineId) .. '\' by \'' .. tostring(shellCommand or '') .. '\'');
|
||||||
|
end,
|
||||||
|
reboot = function()
|
||||||
|
if not isConfigFileExists() then
|
||||||
|
print('Error: unable to deploy because \'.cuberc\' file is missing\nTry: \'cube init\' command')
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
|
local machineId = firstArg;
|
||||||
|
|
||||||
|
if not machineId or machineId == '' then
|
||||||
|
printUsageCommand('reboot');
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
|
print('TODO: reboot machine \'' .. tostring(machineId) .. '\'.');
|
||||||
|
end,
|
||||||
|
deploy = function()
|
||||||
|
if not isConfigFileExists() then
|
||||||
|
print('Error: unable to deploy because \'.cuberc\' file is missing\nTry: \'cube init\' command')
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
|
print('TODO: deploy.');
|
||||||
|
end,
|
||||||
|
version = function()
|
||||||
|
print('cube client v' .. _VERSION);
|
||||||
|
end,
|
||||||
|
help = function()
|
||||||
|
local commandName = firstArg;
|
||||||
|
printUsageCommand(commandName);
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
local cmd;
|
||||||
|
if isVersionFlag(cubeCommand) then
|
||||||
|
cmd = COMMANDS.version;
|
||||||
|
else
|
||||||
|
cmd = COMMANDS[cubeCommand];
|
||||||
|
end
|
||||||
|
|
||||||
|
if not cmd then
|
||||||
|
printUsage();
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
|
if (isHelpFlag(firstArg)) then
|
||||||
|
printUsageCommand(cubeCommand);
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
|
cmd();
|
||||||
9
ping.lua
9
ping.lua
@ -1,4 +1,11 @@
|
|||||||
-- ping v2.0.1
|
local _VERSION = '2.0.1';
|
||||||
|
|
||||||
|
local firstArg = ...;
|
||||||
|
if firstArg == '-version' or firstArg == '--version' then
|
||||||
|
print('v' .. _VERSION);
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
local PING_CHANNEL = 9;
|
local PING_CHANNEL = 9;
|
||||||
|
|
||||||
local createNet = require('/apis/net');
|
local createNet = require('/apis/net');
|
||||||
|
|||||||
14
router.lua
14
router.lua
@ -1,4 +1,16 @@
|
|||||||
-- router v1.2.0
|
local _VERSION = '1.2.0';
|
||||||
|
|
||||||
|
local firstArg = ...;
|
||||||
|
|
||||||
|
if firstArg == '-version' or firstArg == '--version' then
|
||||||
|
print('v' .. _VERSION);
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
|
if firstArg then
|
||||||
|
error('Invalid router argument.');
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local ROUTER_CHANNEL = 10;
|
local ROUTER_CHANNEL = 10;
|
||||||
local VERBOSE = true;
|
local VERBOSE = true;
|
||||||
|
|||||||
69
servers/cube-server.lua
Normal file
69
servers/cube-server.lua
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
local _VERSION = '1.0.0';
|
||||||
|
|
||||||
|
local net = require('/apis/net')();
|
||||||
|
|
||||||
|
local CUBE_CHANNEL = 64;
|
||||||
|
|
||||||
|
local function trim(str)
|
||||||
|
return str:gsub("%s+", "");
|
||||||
|
end
|
||||||
|
|
||||||
|
local function readFile(path)
|
||||||
|
local file = fs.open(path, "r");
|
||||||
|
|
||||||
|
if not file then
|
||||||
|
return nil;
|
||||||
|
end
|
||||||
|
|
||||||
|
local contents = file.readAll()
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
return contents
|
||||||
|
end
|
||||||
|
|
||||||
|
local function writeFile(path, content)
|
||||||
|
local file = fs.open(path, "w");
|
||||||
|
|
||||||
|
if not file then
|
||||||
|
return false;
|
||||||
|
end
|
||||||
|
|
||||||
|
file.write(content)
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
end
|
||||||
|
|
||||||
|
local function getStartupCommand()
|
||||||
|
return trim(readFile('.cubestartup') or "")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- ping event
|
||||||
|
net.listenRequest(CUBE_CHANNEL, "ping", function(_, reply)
|
||||||
|
local startupCommand = getStartupCommand();
|
||||||
|
|
||||||
|
reply({ startup = startupCommand });
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- reboot event
|
||||||
|
net.listenRequest(CUBE_CHANNEL, "reboot", function(_, reply)
|
||||||
|
reply(true);
|
||||||
|
os.reboot();
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- set-startup event
|
||||||
|
net.listenRequest(CUBE_CHANNEL, "set-startup", function(startupCommand, reply)
|
||||||
|
local res = writeFile('/.cubestartup', startupCommand);
|
||||||
|
reply(res);
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- deploy-file event
|
||||||
|
net.listenRequest(CUBE_CHANNEL, "deploy-file", function(payload, reply)
|
||||||
|
writeFile(payload.path, payload.content);
|
||||||
|
reply(true);
|
||||||
|
end)
|
||||||
|
|
||||||
|
print('cube-server v' .. _VERSION .. ' started.')
|
||||||
|
|
||||||
|
-- start event loop
|
||||||
|
net.startLoop();
|
||||||
28
servers/cube-startup.lua
Normal file
28
servers/cube-startup.lua
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
local _VERSION = '1.0.0';
|
||||||
|
|
||||||
|
local function trim(str)
|
||||||
|
return str:gsub("%s+", "");
|
||||||
|
end
|
||||||
|
|
||||||
|
local function readFile(path)
|
||||||
|
local file = fs.open(path, "r");
|
||||||
|
|
||||||
|
if not file then
|
||||||
|
return nil;
|
||||||
|
end
|
||||||
|
|
||||||
|
local contents = file.readAll()
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
return contents
|
||||||
|
end
|
||||||
|
|
||||||
|
local startupCommand = trim(readFile('.cubestartup') or "");
|
||||||
|
|
||||||
|
|
||||||
|
if startupCommand ~= "" then
|
||||||
|
print('cube-startup v' .. _VERSION .. ': execute \'' .. startupCommand .. '\'...')
|
||||||
|
shell.run(startupCommand);
|
||||||
|
else
|
||||||
|
print('cube-startup v' .. _VERSION .. ' no startup command detected.')
|
||||||
|
end
|
||||||
@ -1,4 +1,4 @@
|
|||||||
-- ping-server v2.0.0
|
local _VERSION = "2.0.0"
|
||||||
|
|
||||||
-- -- Example: implementation simple de ping-server
|
-- -- Example: implementation simple de ping-server
|
||||||
local PING_CHANNEL = 9;
|
local PING_CHANNEL = 9;
|
||||||
@ -26,5 +26,6 @@ net.listenRequest(PING_CHANNEL, 'ping', function(message, reply)
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
print('ping-server v' .. _VERSION .. ' started.')
|
||||||
|
|
||||||
net.start();
|
net.start();
|
||||||
|
|||||||
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
local SERVERS = {
|
local SERVERS = {
|
||||||
"servers/ping-server",
|
"servers/ping-server",
|
||||||
|
"servers/cube-server.lua",
|
||||||
|
"servers/cube-startup.lua",
|
||||||
};
|
};
|
||||||
|
|
||||||
local function shellFn()
|
local function shellFn()
|
||||||
@ -33,7 +35,7 @@ for _, v in ipairs(SERVERS) do
|
|||||||
print("\t\t" .. v)
|
print("\t\t" .. v)
|
||||||
end
|
end
|
||||||
|
|
||||||
parallel.waitForAny(shellFn, table.unpack(servers));
|
parallel.waitForAll(shellFn, table.unpack(servers));
|
||||||
|
|
||||||
print("Servers stopped, reboot the machine...");
|
print("Servers stopped, reboot the machine...");
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user