hohserg

HoverHelm installer

Aug 22nd, 2020 (edited)
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. local filesystem=require"filesystem"
  2. local serialization=require"serialization"
  3. local internet=require"internet"
  4.  
  5. filesystem.makeDirectory("/home/lib/")
  6. if not filesystem.exists("/home/lib/json.lua") then
  7.     os.execute("wget -f https://raw.githubusercontent.com/rxi/json.lua/master/json.lua /home/lib/json.lua")
  8. end
  9.  
  10. local json=require"json"
  11.  
  12. local function flatMapSeq(seq, f)
  13.     local r = {}
  14.     for k=1, seq.n or #seq do
  15.         local c = f(seq[k])
  16.         for i=1, c.n or #c do
  17.             table.insert(r,c[i])
  18.         end
  19.     end
  20.     return r
  21. end
  22.  
  23. local baseUrl = "https://api.github.com/repos/hohserg1/HoverHelm/contents/"
  24.  
  25. local runPath = os.getenv("_")
  26.  
  27. local runPathList = filesystem.list(filesystem.path(runPath))
  28. runPathList()
  29.  
  30. local isFirstInstall = runPath:sub(1,5) == "/tmp/" or runPathList()==nil
  31.  
  32. if not isFirstInstall and hoverhelm then
  33.     print("HoverHelm server is runned. Stop HoverHelm server before update")
  34.     os.exit()
  35. end
  36.  
  37. print("isFirstInstall = "..tostring(isFirstInstall))
  38.  
  39. local hhConfig = not isFirstInstall and require"hoverhelm.config"
  40.  
  41. local serverInstallPath = "/home/hoverhelm/"
  42. local coreRootFolder = isFirstInstall and "/home/hoverhelm/device_core/" or hhConfig.coreRootFolder
  43.  
  44. local function recreateDirectory(path)
  45.     filesystem.remove(path)
  46.     filesystem.makeDirectory(path)
  47. end
  48.  
  49. recreateDirectory(serverInstallPath)
  50. recreateDirectory(coreRootFolder)
  51. recreateDirectory("/home/lib/hoverhelm/")
  52.  
  53.  
  54. local function getFilesInRepo(path, recursively)
  55.     local reps = internet.request(baseUrl..path)
  56.     local result = ""
  57.     repeat
  58.         local data = reps()
  59.         result = result .. (data or "")
  60.     until not data
  61.     local j = json.decode(result)
  62.     return flatMapSeq(j, function(entry)
  63.         return entry.type == "file"
  64.                 and {entry.path}
  65.                 or (recursively and getFilesInRepo(entry.path) or {})
  66.     end)
  67. end
  68.  
  69. local function download(path, destination)
  70.     print("dowloading to "..destination)
  71.     filesystem.makeDirectory(filesystem.path(destination))
  72.     os.execute("wget -f https://raw.githubusercontent.com/hohserg1/HoverHelm/master/"..path.." "..destination)
  73.     print(" ")
  74. end
  75.  
  76. --print(serialization.serialize(getFilesInRepo("/device_core"),true))
  77.  
  78.  
  79. print("Downloading device core")
  80. for _,v in ipairs(getFilesInRepo("/device_core", true)) do
  81.     download(v, coreRootFolder..(v:sub(#"/device_core")))
  82. end
  83.  
  84. print("Downloading device eeprom firmware")
  85. for _,v in ipairs(getFilesInRepo("/eeprom-client", true)) do
  86.     download(v, serverInstallPath..v)
  87. end
  88.  
  89. print("Downloading libs")
  90. local libPath = "/server/home/lib/hoverhelm"
  91. --print(serialization.serialize(getFilesInRepo(libPath),true))
  92.  
  93. for _,v in ipairs(getFilesInRepo(libPath, true)) do
  94.     download(v,v:sub(#"/server"))
  95. end
  96.  
  97. --https://api.github.com/repos/hohserg1/HoverHelm/contents/core/
  98.  
  99. -- print(serialization.serialize(getFilesInRepo("/server/home/hoverhelm", false),true))
  100.  
  101. print("Downloading server")
  102. for _,v in ipairs(getFilesInRepo("/server/home/hoverhelm", false)) do
  103.     download(v, serverInstallPath..filesystem.name(v))
  104. end
  105.  
  106. filesystem.copy("/home/lib/hoverhelm/utils.lua", coreRootFolder.."/lib/utils.lua")
  107.  
  108. os.execute("edit /home/lib/hoverhelm/config.lua")
Add Comment
Please, Sign In to add comment