feat: rename set-startup command in set-boot

This commit is contained in:
Guillaume ARM 2022-07-18 23:28:17 +02:00
parent 7303bc7580
commit 3abb18195f
7 changed files with 57 additions and 39 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.cuberc
.cubestartup
.cubeboot

View File

@ -14,7 +14,7 @@ All servers are automatically started at boot
- `/servers/ping-server`: allow a machine to respond to a `ping` command.
- `/servers/cube-server`: allow a machine to be controllable via `cube`.
- `/servers/cube-startup.lua`: `cube` startup script.
- `/servers/cube-boot.lua`: `cube` boot script.
## Programs
- `router`: route messages (you need to setup a router to be able to use all `apis/net` based programs and libs)

View File

@ -6,7 +6,7 @@ local LIST_FILES = {
-- servers
'servers/ping-server.lua',
'servers/cube-server.lua',
'servers/cube-startup.lua',
'servers/cube-boot.lua',
-- programs
'programs/router.lua', -- router is not in servers folder because he's not ran on every machines
'programs/ping.lua',

View File

@ -1,4 +1,4 @@
local _VERSION = '1.4.0';
local _VERSION = '2.0.0';
local CUBE_CHANNEL = 64;
local net = require('/apis/net')();
@ -42,7 +42,7 @@ local function printUsage()
print();
print('\t\t\tcube ls');
print('\t\t\tcube configure');
print('\t\t\tcube set-startup <machineId> [command]')
print('\t\t\tcube set-boot <machineId> [command]')
print('\t\t\tcube reboot <machineId>')
print('\t\t\tcube deploy')
print('\t\t\tcube version')
@ -50,6 +50,11 @@ local function printUsage()
end
local function printUsageCommand(commandName)
local function setBootUsage()
print('\t\t\tcube set-boot <machineId> [command]')
print('Setup a startup shell command on a remote cube.')
end
local USAGES = {
ls = function()
print('\t\t\tcube ls');
@ -59,10 +64,12 @@ local function printUsageCommand(commandName)
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,
["set-boot"] = setBootUsage,
["setboot"] = setBootUsage,
["set-start"] = setBootUsage,
["setstart"] = setBootUsage,
["set-startup"] = setBootUsage,
["setstartup"] = setBootUsage,
reboot = function()
print('\t\t\tcube reboot <machineId>')
print('Reboot a cube machine.');
@ -110,7 +117,32 @@ local rebootCommand = function(machineId)
for k in ipairs(results) do
local packet = packets[k];
print('reboot machine \'' .. tostring(packet.sourceId) .. '\'.');
print('reboot machine \'' .. tostring(packet.sourceId) .. '\'');
end
end
local setBootCommand = function(machineId, shellCommand)
if not machineId then
printUsageCommand('set-boot');
return;
end
local ok, results, packets = net.sendMultipleRequests(CUBE_CHANNEL, 'set-boot', shellCommand, machineId);
if not ok then
error(results);
end
for k in ipairs(results) do
local packet = packets[k];
if shellCommand == nil or shellCommand == '' then
print('boot DELETED');
else
print('boot UPDATED');
end
rebootCommand(packet.sourceId);
end
end
@ -136,27 +168,12 @@ local COMMANDS = {
configure = function()
print('not implemented yet.');
end,
["set-startup"] = function(machineId, shellCommand)
if not machineId then
printUsageCommand('set-startup');
return;
end
local ok, results, packets = net.sendMultipleRequests(CUBE_CHANNEL, 'set-startup', shellCommand, machineId);
if not ok then
error(results);
end
for k in ipairs(results) do
local packet = packets[k];
print('changed startup script on machine \'' ..
tostring(packet.sourceId) .. '\' by \'' .. tostring(shellCommand or '') .. '\'');
rebootCommand(packet.sourceId);
end
end,
["set-boot"] = setBootCommand,
["setboot"] = setBootCommand,
["set-start"] = setBootCommand,
["setstart"] = setBootCommand,
["set-startup"] = setBootCommand,
["setstartup"] = setBootCommand,
reboot = rebootCommand,
deploy = function()
print('not implemented yet.');

View File

@ -1,4 +1,4 @@
local _VERSION = '1.1.0';
local _VERSION = '2.0.0';
local function trim(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
@ -17,11 +17,11 @@ local function readFile(path)
return contents
end
local startupCommand = trim(readFile('.cubestartup') or readFile('.cubestart') or "");
local startupCommand = trim(readFile('.cubeboot') or "");
if startupCommand ~= "" then
print('cube-startup v' .. _VERSION .. ': execute \'' .. startupCommand .. '\'...');
print('cube-boot v' .. _VERSION .. ': execute \'' .. startupCommand .. '\'...');
shell.run(startupCommand);
else
print('cube-startup v' .. _VERSION .. ' no startup command detected.')

View File

@ -1,4 +1,4 @@
local _VERSION = '1.0.0';
local _VERSION = '2.0.0';
local net = require('/apis/net')();
@ -35,7 +35,7 @@ local function writeFile(path, content)
end
local function getStartupCommand()
return trim(readFile('.cubestartup') or "")
return trim(readFile('.cubeboot') or "")
end
-- ping event
@ -54,9 +54,9 @@ net.listenRequest(CUBE_CHANNEL, "reboot", function(_, reply)
end, 0.1);
end)
-- set-startup event
net.listenRequest(CUBE_CHANNEL, "set-startup", function(startupCommand, reply)
local res = writeFile('/.cubestartup', startupCommand);
-- set-boot event
net.listenRequest(CUBE_CHANNEL, "set-boot", function(startupCommand, reply)
local res = writeFile('/.cubeboot', startupCommand);
reply(res);
end)

View File

@ -3,7 +3,7 @@ local _VERSION = '1.1.1'
local SERVERS = {
"servers/ping-server",
"servers/cube-server.lua",
"servers/cube-startup.lua",
"servers/cube-boot.lua",
};
local function init()