aboutsummaryrefslogtreecommitdiffstats
path: root/main/aports-build/report-build-errors.lua
blob: 3621765783aafa844feb313be1d0e7f34ea8a787 (plain)
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
local publish = require("mqtt.publish")
local json = require("cjson")

local hostname
local f = io.open("/proc/sys/kernel/hostname")
hostname = f:read()
f:close()

local function read_mosquitto_conf()
	local cfg = {}
	local f = io.open((os.getenv("XDG_CONFIG_HOME") or "").."/mosquitto_pub") or io.open((os.getenv("HOME") or "").."/.config/mosquitto_pub")
	if f == nil then
		return cfg
	end
	for line in f:lines() do
		key,value = line:match("^%-%-([^ ]+)%s+(.*)")
		if key and value then
			cfg[key] = value
		end
	end
	f:close()
	return cfg
end
local mcfg = read_mosquitto_conf()
publish.hostname = mcfg.hostname or "localhost"
publish.username = mcfg.username
publish.password = mcfg.pw

local m = {}

function shell_escape(args)
        local ret = {}
        for _,a in pairs(args) do
                s = tostring(a)
                if s:match("[^A-Za-z0-9_/:=-]") then
                        s = "'"..s:gsub("'", "'\\''").."'"
                end
                table.insert(ret,s)
        end
        return table.concat(ret, " ")
end

function run(args)
        local h = io.popen(shell_escape(args))
        local outstr = h:read("*a")
        return h:close(), outstr
end

function m.postbuild(conf, aport, success)
	-- upload log
	local loghost,logdirprefix = (conf.logtarget or ""):match("(.*):(.*)")
	if aport.logfile and loghost and logdirprefix then
		local logdir = logdirprefix.."/"..hostname.."/"..aport:get_repo_name().."/"..aport.pkgname.."/"
		run{"ssh", loghost, "mkdir", "-p", logdir}
		run{"scp", aport.logfile, loghost..":"..logdir}
	end

	if not conf.mqttbroker then
		return
	end

	local topic = ("build/%s/errors"):format(hostname)
	local payload = nil

	if not success then
		payload = json.encode{
			pkgname = aport.pkgname,
			hostname = hostname,
			reponame = aport:get_repo_name(),
			logurl = ("%s/%s/%s/%s-%s-r%s.log"):format(
					conf.logurlprefix or "https://build.alpinelinux.org/buildlogs",
					hostname,
					(string.match(aport.dir,"[^/]+/[^/]+$")),
					aport.pkgname, aport.pkgver, aport.pkgrel),
			status = success
		}
	end
	publish.single(topic, payload, nil, true, conf.mqttbroker)
end

return m