-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
286 lines (242 loc) · 9.39 KB
/
index.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const app = require("express")()
require('dotenv').config()
const bodyParser = require('body-parser');
const cryptoJs = require("crypto-js");
const fs = require('fs');
const process = require('process');
const util = require('util')
const exec = util.promisify(require('child_process').exec);
app.use(bodyParser.json())
app.post('/', async (req, res) => {
//res.status(202).json({ status: 'processando' })
app.locals.res = res
app.locals.body = req.body
let bd = JSON.stringify(req.body)
let hmacCoded = 'sha256=' + hmac(process.env.HASH_PASS, bd)
let shaHead = req.headers["x-hub-signature-256"]
if (shaHead == hmacCoded) {
findRepo(req.body)
} else {
app.locals.res.status(403).json({ status: 'error - SHA256', repoStatus: 'not updated', serviceStatus: 'not running', error: "sha256 don't match" })
}
})
function hmac(key, data) {
let bytes = cryptoJs.HmacSHA256(data, key)
return bytes.toString()
}
function findRepo(body) {
if (!body.repository) {
app.locals.res.status(403).json({ status: 'error - no repository', repoStatus: 'not updated', serviceStatus: 'not running', error: "no repository in body" })
return
}
let repoName = body.repository.name
let exists = false
fs.readdir('/home/repos', function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
for (let c = 0; c <= files.length; c++) {
if (files[c] == repoName) {
exists = true
break
}
}
exists ? pull(body.repository) : clone(body.repository)
})
}
async function pull(repo) {
try {
// cd into the repo dir
process.chdir(`/home/repos/${repo.name}`);
// exec the pull to update the repo
const { stdout, stderr } = await exec(`git pull`)
if (stdout) {
console.log("[stdout] => git pull: ", stdout)
} else if (stderr) {
console.log("[stderr] => git pull: ", stderr)
}
} catch (err) {
app.locals.res.status(500).json({ status: 'error -> pull', repoStatus: 'not updated', serviceStatus: 'not running', error: err })
console.error(err);
return
}
goLive(repo)
}
async function clone(repo) {
try {
// cd into de the repos dir
process.chdir(`/home/repos`);
const { stdout, stderr } = await exec(`git clone ${repo.ssh_url}`)
if (stdout) {
console.log("[stdout] => git clone: ", stdout)
} else if (stderr) {
console.log("[stderr] => git clone: ", stderr)
}
} catch (err) {
app.locals.res.status(500).json({ status: 'error -> clone', repoStatus: 'not updated', serviceStatus: 'not running', error: err })
console.error(err)
return
}
goLive(repo)
}
async function goLive(repo) {
// find if the repo has an init.sh file
let files = fs.readdirSync(`/home/repos/${repo.name}`, function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
} else {
return files
}
})
for (let c = 0; c <= files.length; c++) {
if (files[c] == 'init.sh') {
let isIllegal = await testInit(repo.name)
if (!isIllegal) {
// checker de user, only admins can run the proj
let admins = process.env.FULL_ACCESS.split(',')
if (admins.indexOf(app.locals.body.head_commit.author.username) === -1) {
app.locals.res.status(403).json({ status: 'error -> not admin', repoStatus: 'updated', serviceStatus: 'not created/ not running' })
} else {
createService(repo)
}
}
break;
}
}
}
// func to validate o init.sh and validate the users that can run the service
async function testInit(repo) {
let data = fs.readFileSync(`/home/repos/${repo}/init.sh`, (err, data) => {
if (err) {
app.locals.res.status(500).json({ status: 'error -> read sh', repoStatus: 'updated', serviceStatus: ' not created/ not running', error: err })
console.error(err)
return
} else {
data = data.toLocaleString()
return data
}
})
let regex = /(crontab|wget|mkfs|pwd|mkdir|vim|vi|rm|rmdir|mv|cp|apt|remove|shutdown|fdisk|mount|unmount|ssh|chmod|chown|chgrp|useradd|adduser|deluser|usermod|kill|kilall|modprobe|hdparm)/
let test = regex.test(data)
if (test) {
app.locals.res.status(403).json({ status: 'error -> init.sh had invalid arguments', repoStatus: 'updated', serviceStatus: ' not created/ not running' })
return true
} else {
return false
}
}
function createService(repo) {
let description;
repo.description != null ? description = repo.description : description = 'no description provided'
let data =
`[Unit]
Description=${repo.name} - ${description}
[Service]
User=root
WorkingDirectory=/home/repos/${repo.name}/
ExecStart=/bin/bash /home/repos/${repo.name}/init.sh
Restart=always
[Install]
WantedBy=multi-user.target`
// reads the repository to find if there is already an .service file, ifnot, create, else exec it
/*
- note -
if you make change to an existing .service file you will need to run 'systemctl daemon-reload'
*/
fs.readdir(`/etc/systemd/system`, function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
if (files.includes(repo.name.toLowerCase() + '.service')) {
runer(repo, true)
} else {
process.chdir(`/etc/systemd/system/`)
fs.writeFileSync(repo.name.toLowerCase() + '.service', data)
runer(repo, false)
}
})
}
async function runer(repo, exists) {
process.chdir(`/etc/systemd/system/`)
// if it already exists we just need to reload de daemon and restart
if (exists) {
daemonReload()
try {
const { stdout, stderr } = await exec(`systemctl restart ${repo.name.toLowerCase()}`)
if (stdout) {
console.log("[stdout] => sysctl restart: ", stdout)
} else if (stderr) {
console.log("[stderr] => sysctl restart: ", stderr)
}
app.locals.res.status(200).json({ status: 'ok -> reloaded', repoStatus: 'updated', serviceStatus: 'running / enabled' })
} catch (err) {
app.locals.res.status(500).json({ status: 'error -> restart service', repoStatus: 'updated', serviceStatus: 'not running', error: err })
console.error(err)
return
}
} else {
// runs chmod to change permissions
try {
const { stdout, stderr } = await exec(`chmod +x ${repo.name.toLowerCase()}.service`)
if (stdout) {
console.log("[stdout] => chmod: ", stdout)
} else if (stderr) {
console.log("[stderr] => chmod: ", stderr)
}
} catch (err) {
app.locals.res.status(500).json({ status: 'error -> chmod', repoStatus: 'updated', serviceStatus: 'not running', error: err })
console.error(err)
return
}
// enables the service to run on startup
try {
const { stdout, stderr } = await exec(`systemctl enable ${repo.name.toLowerCase()}.service &`)
if (stdout) {
console.log("[stdout] => enable: ", stdout)
} else if (stderr) {
console.log("[stderr] => enable: ", stderr)
}
} catch (err) {
app.locals.res.status(500).json({ status: 'error -> sysctl enable', repoStatus: 'updated', serviceStatus: 'running / enabled', error: err })
console.error(err)
return
}
daemonReload()
// runs the file
try {
const { stdout, stderr } = await exec(`systemctl start ${repo.name.toLowerCase()}.service &`)
if (stdout) {
console.log("[stdout] => start script: ", stdout)
} else if (stderr) {
console.log("[stderr] => start script: ", stderr)
}
app.locals.res.status(200).json({ status: 'ok -> started', repoStatus: 'updated', serviceStatus: 'running / enabled' })
} catch (err) {
app.locals.res.status(500).json({ status: 'error -> run', repoStatus: 'updated', serviceStatus: 'not running', error: err })
console.error(err)
return
}
}
async function daemonReload() {
// atualizes the daemon
if (!exists) {
try {
const { stdout, stderr } = await exec(`systemctl daemon-reload`)
if (stdout) {
console.log("[stdout] => daemon-reload: ", stdout)
} else if (stderr) {
console.log("[stderr] => daemon-reload: ", stderr)
}
} catch (err) {
app.locals.res.status(500).json({ status: 'error -> daemon-reload', repoStatus: 'updated', serviceStatus: 'not running', error: err })
console.error(err)
return
}
}
}
}
app.listen(process.env.PORT, () => {
console.log(`[NODE.JS] => Server (githubCI) escutando na porta: ${process.env.PORT}`)
})