From 653a22ce4d7eb124536c3584a79284ae09b382af Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Sun, 17 Jul 2022 22:03:30 +0200 Subject: [PATCH] feat: implement cube and cube-server --- cube.lua | 183 +++++++++++++++++++++++++++++++++++++++ ping.lua | 9 +- router.lua | 14 ++- servers/cube-server.lua | 69 +++++++++++++++ servers/cube-startup.lua | 28 ++++++ servers/ping-server.lua | 3 +- startup/servers.lua | 4 +- 7 files changed, 306 insertions(+), 4 deletions(-) create mode 100644 cube.lua create mode 100644 servers/cube-server.lua create mode 100644 servers/cube-startup.lua diff --git a/cube.lua b/cube.lua new file mode 100644 index 0000000..d173a11 --- /dev/null +++ b/cube.lua @@ -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 [command]') + print('\t\t\tcube reboot ') + print('\t\t\tcube deploy') + print('\t\t\tcube version') + print('\t\t\tcube help ') +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 [command]') + print('Setup a startup shell command on a remote cube.') + end, + reboot = function() + print('\t\t\tcube reboot ') + 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 ') + 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(); diff --git a/ping.lua b/ping.lua index 03c7c69..b8ea76e 100644 --- a/ping.lua +++ b/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 createNet = require('/apis/net'); diff --git a/router.lua b/router.lua index f68c1e0..fefc5db 100644 --- a/router.lua +++ b/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 VERBOSE = true; diff --git a/servers/cube-server.lua b/servers/cube-server.lua new file mode 100644 index 0000000..41996f9 --- /dev/null +++ b/servers/cube-server.lua @@ -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(); diff --git a/servers/cube-startup.lua b/servers/cube-startup.lua new file mode 100644 index 0000000..e274ea9 --- /dev/null +++ b/servers/cube-startup.lua @@ -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 diff --git a/servers/ping-server.lua b/servers/ping-server.lua index c319d4d..3adb2bf 100644 --- a/servers/ping-server.lua +++ b/servers/ping-server.lua @@ -1,4 +1,4 @@ --- ping-server v2.0.0 +local _VERSION = "2.0.0" -- -- Example: implementation simple de ping-server local PING_CHANNEL = 9; @@ -26,5 +26,6 @@ net.listenRequest(PING_CHANNEL, 'ping', function(message, reply) end end) +print('ping-server v' .. _VERSION .. ' started.') net.start(); diff --git a/startup/servers.lua b/startup/servers.lua index ec5f9ef..1942eb1 100644 --- a/startup/servers.lua +++ b/startup/servers.lua @@ -2,6 +2,8 @@ local SERVERS = { "servers/ping-server", + "servers/cube-server.lua", + "servers/cube-startup.lua", }; local function shellFn() @@ -33,7 +35,7 @@ for _, v in ipairs(SERVERS) do print("\t\t" .. v) end -parallel.waitForAny(shellFn, table.unpack(servers)); +parallel.waitForAll(shellFn, table.unpack(servers)); print("Servers stopped, reboot the machine...");