-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
296 lines (257 loc) · 8.46 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
287
288
289
290
291
292
293
294
295
296
const core = require('@actions/core');
const github = require('@actions/github');
const exec = require('@actions/exec');
const io = require('@actions/io');
const path = require("path");
const fs = require("fs");
const crypto = require('crypto');
const os = require("os");
async function main() {
try {
const dataFolder = core.getInput('data-folder');
const packageName = core.getInput('package');
const version = core.getInput('version');
const license = core.getInput('license');
const architecture = core.getInput('architecture');
const maintainer = core.getInput('maintainer');
const homepage = core.getInput('homepage');
const description = core.getInput('description');
const beforeInstall = core.getInput('before-install');
const afterInstall = core.getInput('after-install');
const beforeUpgrade = core.getInput('before-upgrade');
const afterUpgrade = core.getInput('after-upgrade');
const beforeRemove = core.getInput('before-remove');
const afterRemove = core.getInput('after-remove');
const afterPurge = core.getInput('after-purge');
const group = core.getInput('group');
const user = core.getInput('user');
const suggestedPackages = getSuggestedPackages();
//there can be an annoying issue where perms aren't right... just reset now
const username = os.userInfo().username;
await exec.exec('/bin/sh', ['-c', `sudo chown -R ${username}:${username} ${dataFolder}`]);
const allFiles = getFiles(dataFolder);
//create supplement files for handling the debian aspect
const debianDir = path.join(dataFolder, "DEBIAN")
if (fs.existsSync(debianDir)) {
console.log(`Cleaning up ${debianDir}`);
fs.rmdirSync(debianDir, {recursive: true});
}
console.log(`Creating ${debianDir}`);
fs.mkdirSync(debianDir);
//there's several files we need
//first, we'll do the conffiles. This is a list of files in the /etc folder that we flag as "configs"
const etcFiles = getFiles(dataFolder, 'etc');
const conffiles = path.join(debianDir, "conffiles");
console.log(`Creating ${conffiles}`);
fs.writeFileSync(conffiles, etcFiles.join('\n') + '\n');
//generate our md5 file
const md5sumFile = path.join(debianDir, 'md5sums');
console.log(`Creating ${md5sumFile}`)
fs.writeFileSync(md5sumFile, '');
for(const i in allFiles) {
const file = allFiles[i];
if (file.startsWith('/DEBIAN')) {
continue;
}
const buf = fs.readFileSync(path.join(dataFolder, file.slice(1)));
const hash = crypto.createHash('md5').update(buf).digest("hex");
fs.appendFileSync(md5sumFile, hash + ' ' + file.slice(1) + '\n');
}
//generate the control file
const controlFile = path.join(debianDir, 'control');
console.log(`Creating ${controlFile}`);
fs.writeFileSync(controlFile, `Package: ${packageName}
Version: ${version}
License: ${license}
Vendor: pufferpanel-debbuilder
Architecture: ${architecture}
Maintainer: ${maintainer}
Installed-Size: 0
Section: default
Priority: extra
Homepage: ${homepage}
Description: ${description}
${suggestedPackages}
`);
//generate the scripts
//at this point, assume there is a script, because the template can handle "empty" values
let scriptFile = '';
scriptFile = replaceIn(PREINST, 'before-upgrade', beforeUpgrade);
scriptFile = replaceIn(scriptFile, 'before-install', beforeInstall);
const preinstFile = path.join(debianDir, 'preinst');
console.log(`Creating ${preinstFile}`);
fs.writeFileSync(preinstFile, scriptFile, {mode: '0775'});
scriptFile = replaceIn(PRERM, 'before-remove', beforeRemove);
const prermFile = path.join(debianDir, 'prerm');
console.log(`Creating ${prermFile}`);
fs.writeFileSync(prermFile, scriptFile, {mode: '0775'});
scriptFile = replaceIn(POSTINST, 'after-upgrade', afterUpgrade);
scriptFile = replaceIn(scriptFile, 'after-install', afterInstall);
const postinitFile = path.join(debianDir, 'postinst');
console.log(`Creating ${postinitFile}`);
fs.writeFileSync(postinitFile, scriptFile, {mode: '0775'});
scriptFile = replaceIn(POSTRM, 'after-remove', afterRemove);
scriptFile = replaceIn(scriptFile, 'after-purge', afterPurge);
const postrmFile = path.join(debianDir, 'postrm');
console.log(`Creating ${postrmFile}`);
fs.writeFileSync(postrmFile, scriptFile, {mode: '0775'});
//we have to change file owners so it works okay
await exec.exec('/bin/sh', ['-c', `sudo chown -R root:root ${dataFolder}`]);
const resultFile = path.resolve(dataFolder, '..', `${packageName}_${version}_${architecture}.deb`);
//now we can build the package
await exec.exec('/bin/sh', ['-c', `sudo dpkg -b ${dataFolder} ${resultFile}`]);
core.setOutput('file', resultFile);
//reset perms to be what our user is
await exec.exec('/bin/sh', ['-c', `sudo chown -R ${username}:${username} ${dataFolder}`]);
//remove our DEBIAN dir
await exec.exec('/bin/sh', ['-c', `sudo rm -rf ${debianDir}`]);
} catch (error) {
core.setFailed(error.message);
}
}
/**
*
* @param root {String}
* @param item {String}
* @return {[]}
*/
function getFiles(root, item = '') {
const result = [];
fs.readdirSync(path.join(root, item), {withFileTypes: true}).forEach(file => {
const itemPath = path.join(root, item, file.name).slice(root.length);
if (file.isDirectory()) {
const sub = getFiles(root, path.join(item, file.name));
for(const i in sub) {
result.push(sub[i]);
}
} else {
result.push(itemPath);
}
});
return result;
}
/**
*
* @param data {String}
* @param key {String}
* @param file {String}
* @return {String}
*/
function replaceIn(data, key, file) {
let fileContents = '';
if (file && file !== '' && fs.existsSync(file)) {
fileContents = fs.readFileSync(file).toString();
}
return data.replace(`{${key}}`, fileContents);
}
/**
*
* @return {String}
*/
function getSuggestedPackages() {
const packages = core.getInput('suggested-packages').split(/\r?\n/).reduce(
(acc, line) =>
acc
.concat(line.split(','))
.map(p => p.trim()),
[]
);
if (packages.length > 0) {
return 'Suggests: ' + packages.join(', ');
}
return '';
}
const PREINST = `#!/bin/sh
before_upgrade() {
:
{before-upgrade}
}
before_install() {
:
{before-install}
}
if [ "\${1}" = "install" -a -z "\${2}" ]
then
before_install
elif [ "\${1}" = "upgrade" -a -n "\${2}" ]
then
upgradeFromVersion="\${2}"
before_upgrade "\${upgradeFromVersion}"
elif [ "\${1}" = "install" -a -n "\${2}" ]
then
upgradeFromVersion="\${2}"
before_upgrade "\${upgradeFromVersion}"
elif echo "\${1}" | grep -E -q '(fail|abort)'
then
echo "Failed to install before the pre-installation script was run." >&2
exit 1
fi`;
const PRERM = `#!/bin/sh
before_remove() {
:
{before-remove}
}
dummy() {
:
}
if [ "\${1}" = "remove" -a -z "\${2}" ]
then
# "before remove" goes here
before_remove
elif [ "\${1}" = "upgrade" ]
then
dummy
elif echo "\${1}" | grep -E -q "(fail|abort)"
then
echo "Failed to install before the pre-removal script was run." >&2
exit 1
fi`;
const POSTINST = `#!/bin/sh
after_upgrade() {
:
{after-upgrade}
}
after_install() {
:
{after-install}
}
if [ "\${1}" = "configure" -a -z "\${2}" ] || \\
[ "\${1}" = "abort-remove" ]
then
after_install
elif [ "\${1}" = "configure" -a -n "\${2}" ]
then
upgradeFromVersion="\${2}"
after_upgrade "\${2}"
elif echo "\${1}" | grep -E -q "(abort|fail)"
then
echo "Failed to install before the post-installation script was run." >&2
exit 1
fi`;
const POSTRM = `#!/bin/sh
after_remove() {
:
{after-remove}
}
after_purge() {
:
{after-purge}
}
dummy() {
:
}
if [ "\${1}" = "remove" -o "\${1}" = "abort-install" ]
then
after_remove
elif [ "\${1}" = "purge" -a -z "\${2}" ]
then
after_purge
elif [ "\${1}" = "upgrade" ]
then
dummy
elif echo "\${1}" | grep -E -q '(fail|abort)'
then
echo "Failed to install before the post-removal script was run." >&2
exit 1
fi`;
main();