-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhosts.lua
More file actions
96 lines (73 loc) · 1.95 KB
/
hosts.lua
File metadata and controls
96 lines (73 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
--[[
This module relates to storing a version number for each lockbox on each host.
using this we can detect if a lockbox should be imported or not
]]--
function HostsInit()
local hosts={}
hosts.items={}
hosts.path=function()
return process.getenv("HOME").."/.treasury/hosts.dat"
end
-- generate a unique id for our host
hosts.my_id=function(self)
local S, str
local long_id=""
str=stream.get("/etc/machine-id")
if str ~= nil then long_id=long_id..str.."-" end
str=stream.get("/sys/class/dmi/id/board_asset_tag")
if str ~= nil then long_id=long_id..str.."-" end
if strutil.strlen(long_id)==0
then
-- this is the MAC address of eth0, only use it if we've found nothing better
str=stream.get("/sys/class/net/eth0/address")
if str ~= nil then long_id=long_id..str.."-" end
end
if strutil.strlen(long_id)==0
then
-- this is the MAC address of wlan0, only use it if we've found nothing better
str=stream.get("/sys/class/net/wlan0/address")
if str ~= nil then long_id=long_id..str.."-" end
end
str=sys.hostname() .. "-".. hash.hashstr(long_id, "md5", "p64")
return str
end
--load our list of stored version nummbers for host lockboxes
hosts.load=function(self)
local S, str, toks, tok
S=stream.STREAM(self:path(), "r")
if S ~= nil
then
str=S:readln()
while str ~= nil
do
str=strutil.trim(str)
toks=strutil.TOKENIZER(str, " ")
self.items[toks:next()]=tonumber(toks:remaining())
str=S:readln()
end
S:close()
end
end
hosts.save=function(self)
local S, id, version
S=stream.STREAM(self:path(), "w")
if S ~= nil
then
for id,version in pairs(self.items)
do
S:writeln(id.." "..tostring(version).."\n")
end
S:close()
end
end
hosts.update=function(self, hostid, version)
self.items[hostid]=version
end
hosts.check_version=function(self, hostid, version)
-- no previous version for this hostid, we must need to import
if self.items[hostid] == nil then return true end
if self.items[hostid] < tonumber(version) then return true end
return false
end
return hosts
end