Skip to content

Commit

Permalink
mesh upgrade lib: improves tests, creates states as constants
Browse files Browse the repository at this point in the history
  • Loading branch information
javierbrk committed Oct 31, 2023
1 parent 62b6f12 commit d7d3d30
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 48 deletions.
94 changes: 62 additions & 32 deletions packages/lime-mesh-upgrade/files/usr/lib/lua/lime-mesh-upgrade.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,50 @@

local libuci = require "uci"
local eupgrade = require 'eupgrade'
local config = require "lime.config"

local mesh_upgrade = {}

function mesh_upgrade.set_workdir(workdir)
if not utils.file_exists(workdir) then
os.execute('mkdir -p ' .. workdir)
end
if fs.stat(workdir, "type") ~= "dir" then
error("Can't configure workdir " .. workdir)
end
mesh_upgrade.WORKDIR = workdir
--mup.DOWNLOAD_INFO_CACHE_FILE = mup.WORKDIR .. '/download_status'
mesh_upgrade.FIRMWARE_LATEST_JSON = mup.WORKDIR .. "/firmware_latest_mesh_wide.json"
--mup.FIRMWARE_LATEST_JSON_SIGNATURE = mup.FIRMWARE_LATEST_JSON .. '.sig'
end

mesh_upgrade.set_workdir("/tmp/mesh_upgrade")
local mesh_upgrade = {
--posible tranactin states
transaction_states = {
NO_TRANSACTION="no_transaction",
STARTED = "started", --ther is a transaction in progress
ABORTED = "aborted",
FINISHED = "finished"
},
--psible upgrade states enumeration
upgrade_states = {
DOWNLOADING= "downloading",
READY_FOR_UPGRADE="ready_for_upgrade",
UPGRADE_SCHELUDED="upgrade_scheluded",
CONFIRMATION_PENDING="confirmation_pending",
CONFIRMED="confirmed",
UPDATED="updated",
ERROR= "error"
},
-- list of possible errors
errors =
{
DOWNLOAD_FAILED="download failed",
CONFIRMATION_TIME_OUT="confirmation timeout"
}
}

-- function mesh_upgrade.set_workdir(workdir)
-- if not utils.file_exists(workdir) then
-- os.execute('mkdir -p ' .. workdir)
-- end
-- if fs.stat(workdir, "type") ~= "dir" then
-- error("Can't configure workdir " .. workdir)
-- end
-- mesh_upgrade.WORKDIR = workdir
-- --mup.DOWNLOAD_INFO_CACHE_FILE = mup.WORKDIR .. '/download_status'
-- mesh_upgrade.FIRMWARE_LATEST_JSON = mup.WORKDIR .. "/firmware_latest_mesh_wide.json"
-- --mup.FIRMWARE_LATEST_JSON_SIGNATURE = mup.FIRMWARE_LATEST_JSON .. '.sig'
-- end

-- mesh_upgrade.set_workdir("/tmp/mesh_upgrade")

-- This function will download latest librerouter os firmware and expose it as
-- a local repository in order to be used for other nodes
Expand All @@ -29,7 +56,7 @@ function mesh_upgrade.set_up_firmware_repository()
if latest_data then
utils.execute_daemonized("eupgrade-download")
else
ret = {status = 'error', message = 'New version is not availabe'}
ret = { status = 'error', message = 'New version is not availabe' }
end

-- 2. Create local repository json data
Expand All @@ -44,41 +71,44 @@ end

-- Shared state functions

-- Validate if the upgrade is already started
-- Validate if the upgrade has already started
function mesh_upgrade.mesh_upgrade_is_started()
local uci = libuci.cursor()
return uci:get('mesh-upgrade', 'main', 'transaction_state') == 'started'
--todo: what happens if a mesh_upgrade has started more than an hour ago ? should this node abort it ?
end

function mesh_upgrade.mesh_upgrade_abort()
local uci = libuci.cursor()
uci:set('mesh-upgrade', 'main', 'transaction_state', 'aborted')
uci:set('mesh-upgrade', 'main', 'transaction_state', mesh_upgrade.transaction_states.ABORTED)
uci:save('mesh-upgrade')
uci:commit('mesh-upgrade')
-- stop and delete everything
-- trigger a shared state publish
end

-- It set up the information of where to download the new firmware.
-- Called by a shared state hook
-- set download information for the new firmware from master node
-- Called by a shared state hook in non master nodes
function mesh_upgrade.set_mesh_upgrade_info(upgrade_data)
local uci = libuci.cursor()
local uci = config.get_uci_cursor()
if (type(upgrade_data.id) == "number") and
string.match(upgrade_data.data.repo_url, "https?://[%w-_%.%?%.:/%+=&]+") ~= nil
--perform aditional checks
--perform aditional checks
then
print (uci:set('mesh-upgrade', 'main', "mesh-upgrade"))
print(uci:set('mesh-upgrade', 'main', 'id', upgrade_data.id))
uci:get('mesh-upgrade', 'main','repo_url')
uci:set('mesh-upgrade', 'main', "mesh-upgrade")
uci:set('mesh-upgrade', 'main', 'id', upgrade_data.id)
uci:set('mesh-upgrade', 'main', 'repo_url', upgrade_data.data.repo_url)
uci:set('mesh-upgrade', 'main', 'firmware_ver', upgrade_data.data.firmware_ver)
print(uci:set('mesh-upgrade', 'main', 'upgrade_state', 'starting'))
uci:set('mesh-upgrade', 'main', 'upgrade_state', mesh_upgrade.upgrade_states.starting)
uci:set('mesh-upgrade', 'main', 'error', 0)
uci:set('mesh-upgrade', 'main', 'timestamp', upgrade_data.timestamp)
uci:set('mesh-upgrade', 'main', 'master_node', upgrade_data.master_node)
uci:set('mesh-upgrade', 'main', 'transaction_state', 'started')
uci:set('mesh-upgrade', 'main', 'transaction_state', mesh_upgrade.transaction_states.STARTED)
uci:save('mesh-upgrade')
uci:commit('mesh-upgrade')
--trigger a shared state publish
--wait for the download to be ready to trigger the shared state upgrade.
--trigger firmware download from master_node url
else
utils.log("upgrade not started input data errors")
print("upgrade not started input data errors")
Expand All @@ -94,7 +124,7 @@ end
-- upgrde_state="starting,downloading|ready_for_upgrade|upgrade_scheluded|confirmation_pending|~~confirmed~~|updated|error",
-- error="CODE",
-- safe_upgrade_status="",
-- eup_STATUS="",
-- eup_STATUS="",eup.STATUS_DEFAULT = 'not-initiated' eup.STATUS_DOWNLOADING = 'downloading' eup.STATUS_DOWNLOADED = 'downloaded' eup.STATUS_DOWNLOAD_FAILED = 'download-failed'
-- },
-- timestamp=231354654,
-- id="",
Expand All @@ -103,17 +133,17 @@ end
-- }
--
function mesh_upgrade.get_mesh_upgrade_status()
local uci = libuci.cursor()
local uci = config.get_uci_cursor()
local upgrade_data = {}
upgrade_data.data={}
upgrade_data.type= "upgrade"
upgrade_data.data = {}
upgrade_data.type = "upgrade"
upgrade_data.id = uci:get('mesh-upgrade', 'main', 'id')
upgrade_data.data.firmware_ver = uci:get('mesh-upgrade', 'main', 'firmware_ver')
upgrade_data.data.repo_url = uci:get('mesh-upgrade', 'main', 'repo_url')
upgrade_data.data.upgrade_state = uci:get('mesh-upgrade', 'main', 'upgrade_state')
upgrade_data.data.error = uci:get('mesh-upgrade', 'main', 'error')
upgrade_data.data.safe_upgrade_status=uci:get('mesh-upgrade', 'main', 'safe_upgrade_status')
upgrade_data.data.eup_STATUS=uci:get('mesh-upgrade', 'main', 'eup_STATUS')
upgrade_data.data.safe_upgrade_status = uci:get('mesh-upgrade', 'main', 'safe_upgrade_status')
upgrade_data.data.eup_STATUS = uci:get('mesh-upgrade', 'main', 'eup_STATUS')
upgrade_data.timestamp = uci:get('mesh-upgrade', 'main', 'timestamp')
upgrade_data.master_node = uci:get('mesh-upgrade', 'main', 'master_node')
upgrade_data.transaction_state = uci:get('mesh-upgrade', 'main', 'transaction_state')
Expand Down
31 changes: 20 additions & 11 deletions packages/lime-mesh-upgrade/tests/test_lime-mesh-upgrade.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ local json = require 'luci.jsonc'


-- disable logging in config module
config.log = function() end
config.log = function(text)
print (text)
end

local uci

Expand All @@ -26,29 +28,36 @@ local upgrade_data =
timestamp=231354654,
id=21,
transaction_state="started/aborted/finished",
master_node=""
master_node="prmiero"
}

describe('LiMe mesh upgrade', function()
it('test config', function()
print("pruebaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa2")
print (uci:set('mesh-upgrade', 'main', "mesh-upgrade"))
print ("test")
lime_mesh_upgrade.start(upgrade_data)
utils.printJson(lime_mesh_upgrade.get_status())
it('test set mesh config', function()
local status = lime_mesh_upgrade.get_mesh_upgrade_status()
utils.printJson(status)
assert.is.equal(status.transaction_state, lime_mesh_upgrade.transaction_states.NO_TRANSACTION)
lime_mesh_upgrade.set_mesh_upgrade_info(upgrade_data)
status = lime_mesh_upgrade.get_mesh_upgrade_status()
utils.printJson(status)

assert.is.equal(status.master_node, lime_mesh_upgrade.upgrade_states.STARTING)
assert.is.equal(status.data.repo_url,upgrade_data.data.repo_url )

assert.is.equal(status.upgrade_state, lime_mesh_upgrade.upgrade_states.STARTING)
assert.is.equal(status.transaction_state, lime_mesh_upgrade.transaction_states.STARTED)
end)

it('test config 2', function()
print("pruebaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaa2")
config.log("pruebaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaa2")
end)

before_each('', function()
print("pruebaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1")
config.log("pruebaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1")
uci = test_utils.setup_test_uci()
end)

after_each('', function()
print("pruebaaaaaaaaaaaaaaaaaaaaaaaaaa3")
config.log("pruebaaaaaaaaaaaaaaaaaaaaaaaaaa3")

test_utils.teardown_test_uci(uci)
end)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ local mesh_upgrade = require "lime.mesh-upgrade"


--if already involved in an upgrade transaction do nothing
if not mesh_upgrade.upgrade_in_progress() then
if not mesh_upgrade.mesh_upgrade_is_started() then
for node, s_s_data in pairs(JSON.parse(io.stdin:read("*all"))) do
--if any node has started an upgrade process and start one too?
--fetch the info from the master node publication?
--only fetch the info from the master node publication?
if s_s_data.data.master_node == node then
mesh_upgrade.start(s_s_data.data)
utils.unsafe_shell('logger -p daemon.info -t "messwideupgrade" "starting upgrade from"' ..
mesh_upgrade.set_mesh_upgrade_info(s_s_data.data)
utils.unsafe_shell('logger -p daemon.info -t "mesh upgrade" "mesh upgrade detected upgrade from"' ..
s_s_data.data.repo_url .. " " .. node)
--start the upgrade process referencing the original upgrade publication
--start_upgrade(s_s_data.timestamp,s_s_data.id,sate="started",s_s_data.data.master_node)
end
utils.unsafe_shell('logger -p daemon.info -t "messwideupgrade" "hoock +++++ "' ..
Expand Down

0 comments on commit d7d3d30

Please sign in to comment.