-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-mirror-syncd.lua
executable file
·181 lines (145 loc) · 4.08 KB
/
git-mirror-syncd.lua
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env lua
local mqtt = require 'mosquitto'
-- Allow either cjson, or th-LuaJSON
local ok, json = pcall(require, 'cjson')
if not ok then json = require 'json' end
-- unpack is not global since Lua 5.3
local unpack = table.unpack or unpack
local concat = table.concat
local sh = os.execute
local VERSION = '0.3.1'
local CONFIG = os.getenv('CONFIG') or './config.lua'
local DEBUG = os.getenv('DEBUG') ~= nil
-- Default configuration
local conf = {
mqtt_host = nil,
mqtt_port = 1883,
mqtt_tls = false,
mqtt_keepalive = 300,
mqtt_topics = {},
tls_ca_path = '/etc/ssl/certs',
log_date_format = '%Y-%m-%dT%H:%M:%S',
cache_dir = './tmp',
origin_url = nil,
mirror_url = nil,
ssh_command = 'ssh',
}
-------- Functions --------
-- String interpolation using %.
getmetatable('').__mod = function(str, args)
if type(args) ~= 'table' then
args = { args }
end
return str:format(unpack(args)):gsub('($%b{})', function(placeholder)
return args[placeholder:sub(3, -2)] or placeholder
end)
end
-- Merges tables.
local function merge (...)
local res = {}
for _, tab in ipairs {...} do
for k, v in pairs(tab) do res[k] = v end
end
return res
end
local function log (msg)
if conf.log_date_format ~= '' then
msg = os.date(conf.log_date_format)..' '..msg
end
io.stderr:write(msg..'\n')
end
local function load_config (path)
local env = setmetatable({}, { __index = _G })
local func, err = loadfile(path)
if not func then
return nil, err
end
assert(pcall(setfenv(func, env)))
setmetatable(env, nil)
return env
end
local function is_dir (path)
return sh("test -d '%s'" % path) == 0
end
local function repo_conf (repo_name)
return {
repo_name = repo_name,
clone_dir = conf.cache_dir..'/'..repo_name..'.git',
origin_url = conf.origin_url:format(repo_name),
mirror_url = conf.mirror_url:format(repo_name),
git_opts = (DEBUG and '' or '--quiet'),
ssh_command = conf.ssh_command:gsub('"', '\\"'),
}
end
local function git_clone (repo_conf)
return sh([[
set -e
mkdir -p "$(dirname "${clone_dir}")"
export GIT_SSH_COMMAND="${ssh_command}"
git clone --mirror "${origin_url}" ${git_opts} "${clone_dir}"
git -C "${clone_dir}" remote set-url --push origin "${mirror_url}"
]] % repo_conf)
end
local function git_update (repo_conf)
return sh([[
set -e
cd "${clone_dir}"
export GIT_SSH_COMMAND="${ssh_command}"
git fetch --prune ${git_opts}
git push --mirror ${git_opts}
]] % repo_conf)
end
local function sync_mirror (repo_name)
local conf = repo_conf(repo_name)
if not is_dir(conf.clone_dir) and git_clone(conf) ~= 0 then
return nil, 'Failed to clone repository: '..conf.origin_url
end
if git_update(conf) ~= 0 then
return nil, 'Failed to update repository: '..conf.mirror_url
end
return true
end
-------- M a i n --------
local myconf, err = load_config(CONFIG)
if not myconf then
log('ERROR: Failed to load config file '..err)
os.exit(1)
end
conf = merge(conf, myconf)
local client = mqtt.new()
client.ON_CONNECT = function()
log('INFO: Subscribing to '..concat(conf.mqtt_topics, ', '))
for _, topic in ipairs(conf.mqtt_topics) do
client:subscribe(topic, 1)
end
end
client.ON_MESSAGE = function(mid, topic, payload)
local ok, payload = pcall(json.decode, payload)
if not ok then
log('ERROR: Failed to encode JSON payload from topic '..topic)
return
end
local repo_name = payload.repo
if not (repo_name or ''):match('^[%w%._/-]+$') then
log('ERROR: Invalid repository name: '..tostring(repo_name))
return
end
log('INFO: Synchronizing mirror '..repo_name)
local ok, err = sync_mirror(repo_name)
if ok then
log('INFO: Completed')
else
log('ERROR: '..err)
end
end
log('INFO: Starting git-mirror-syncd '..VERSION)
log('INFO: Connecting to %s:%s' % { conf.mqtt_host, conf.mqtt_port })
if conf.mqtt_tls then
if is_dir(conf.tls_ca_path) then
client:tls_set(nil, conf.tls_ca_path)
else
client:tls_set(conf.tls_ca_path)
end
end
client:connect(conf.mqtt_host, conf.mqtt_port, conf.mqtt_keepalive)
client:loop_forever()