feat(cube): set-startup command

This commit is contained in:
Guillaume ARM 2022-07-18 02:17:29 +02:00
parent f6dc7bdef9
commit f880a577a4

View File

@ -1,4 +1,4 @@
local _VERSION = '1.0.0'; local _VERSION = '1.1.0';
local CUBE_CHANNEL = 64; local CUBE_CHANNEL = 64;
local net = require('/apis/net')(); local net = require('/apis/net')();
@ -94,6 +94,30 @@ if cubeCommand == nil or cubeCommand == '' or isHelpFlag(cubeCommand) then
return; return;
end end
local rebootCommand = function(machineId)
if not isConfigFileExists() then
print('Error: unable to deploy because \'.cuberc\' file is missing\nTry: \'cube init\' command')
return;
end
if not machineId or machineId == '' then
printUsageCommand('reboot');
return;
end
local ok, results, packets = net.sendMultipleRequests(CUBE_CHANNEL, 'reboot', true, machineId);
if not ok then
error(results);
end
for k in ipairs(results) do
local packet = packets[k];
print('reboot machine \'' .. tostring(packet.sourceId) .. '\'.');
end
end
local COMMANDS = { local COMMANDS = {
init = function() init = function()
if isConfigFileExists() then if isConfigFileExists() then
@ -117,48 +141,18 @@ local COMMANDS = {
end end
print('TODO: configure'); print('TODO: configure');
end, end,
["set-startup"] = function() ["set-startup"] = function(machineId, shellCommand)
if not isConfigFileExists() then if not isConfigFileExists() then
print('Error: unable to deploy because \'.cuberc\' file is missing\nTry: \'cube init\' command') print('Error: unable to deploy because \'.cuberc\' file is missing\nTry: \'cube init\' command')
return; return;
end end
local machineId = firstArg;
local shellCommand = secondArg;
if not machineId then if not machineId then
printUsageCommand('set-startup'); printUsageCommand('set-startup');
return; return;
end end
print('changed startup script on machine \'' .. local ok, results, packets = net.sendMultipleRequests(CUBE_CHANNEL, 'set-startup', shellCommand, machineId);
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
if tonumber(machineId) then
local ok, result, packet = net.sendRequest(CUBE_CHANNEL, 'reboot', true, tonumber(machineId))
if not ok then
error(result)
end
print('reboot machine \'' .. tostring(packet.sourceId) .. '\'.');
return
end
local ok, results, packets = net.sendMultipleRequests(CUBE_CHANNEL, 'reboot', true, machineId);
if not ok then if not ok then
error(results); error(results);
@ -167,9 +161,13 @@ local COMMANDS = {
for k in ipairs(results) do for k in ipairs(results) do
local packet = packets[k]; local packet = packets[k];
print('reboot machine \'' .. tostring(packet.sourceId) .. '\'.'); print('changed startup script on machine \'' ..
tostring(packet.sourceId) .. '\' by \'' .. tostring(shellCommand or '') .. '\'');
rebootCommand(packet.sourceId);
end end
end, end,
reboot = rebootCommand,
deploy = function() deploy = function()
if not isConfigFileExists() then if not isConfigFileExists() then
print('Error: unable to deploy because \'.cuberc\' file is missing\nTry: \'cube init\' command') print('Error: unable to deploy because \'.cuberc\' file is missing\nTry: \'cube init\' command')
@ -181,8 +179,7 @@ local COMMANDS = {
version = function() version = function()
print('cube client v' .. _VERSION); print('cube client v' .. _VERSION);
end, end,
help = function() help = function(commandName)
local commandName = firstArg;
printUsageCommand(commandName); printUsageCommand(commandName);
end end
} }
@ -204,4 +201,4 @@ if (isHelpFlag(firstArg)) then
return; return;
end end
cmd(); cmd(firstArg, secondArg);