lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I assume? hope! you are trying to work out a simple update system so
you can upgrade remote installations using a signed executable that
would be downloaded, checked and then replace your app, then restart
it? I'm thinking of building this and would be interested in what you
may have.
	As a first, naive, implementation please consider the attached
code.  It needs LFS and LuaSocket.  In your application, you could add
something like:

local lupdate = require"lupdate"
local files = {
    ["x1.lua"] = "http://localhost/lupdate/app1/x1.lua";,
    ["x2.lua"] = "http://localhost/lupdate/app1/x2.lua";,
}
lupdate.files (files)
-- Run application
assert(loadfile"x1.lua")()

	I'm not using this code, just wrote it a couple of days ago as
an exercise.  But let me know about your improvements!
		Tomas

--------------------------------------------------------------------------
-- File Updater
--
-- @name lupdate
-- @class module
-- @release $Id:$
--------------------------------------------------------------------------

local assert, pairs = assert, pairs
local http = require"socket.http"
local io = require"io"
local lfs = require"lfs"
local ltn12 = require"ltn12"
local os = require"os"
local string = require"string"

module ("lupdate")

--------------------------------------------------------------------------
-- Check what files in the list should be updated.
-- @param list Table of filenames and corresponding URL.
--------------------------------------------------------------------------
local months = {
	Jan = 1, Feb = 2, Mar = 3, Apr = 4,
	May = 5, Jun = 6, Jul = 7, Aug = 8,
	Sep = 9, Oct = 10, Nov = 11, Dec = 12,
}
function check (list)
	local old = {}
	for fn, url in pairs (list) do
		local localt = lfs.attributes (fn, "modification") or 0
		local r, c, h = http.request {
			url = url,
			method = "HEAD",
		}
		local remotet = {}
		string.gsub (h["last-modified"], "^(%w+), (%d+) (%w+) (%d+) (%d+):(%d+):(%d+) (%w+)$", function (w, d, m, y, h, min, s, z)
			remotet.day = d
			remotet.month = months[m]
			remotet.year = y
			remotet.hour = h
			remotet.min = min
			remotet.sec = s
		end)
		remotet = os.time (remotet)

		if localt < remotet then
			old[fn] = url
		end
	end
	return old
end

--------------------------------------------------------------------------
-- Update the files that are older than the indicated on the Web.
-- @param list Table of filenames and corresponding URL.
--------------------------------------------------------------------------
function files (list)
	local old = check (list)
	for fn, url in pairs (old) do
		local fh = assert (io.open (fn, "w+"))
		local r, c, h = http.request {
			url = url,
			--method = "GET",
			sink = ltn12.sink.file (fh),
		}
	end
end