This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.js
executable file
·90 lines (78 loc) · 3.22 KB
/
process.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
const { exec } = require("child_process");
const fs = require('fs');
const dateFormat = require('dateformat');
const querystring = require('querystring');
const https = require("https");
require('dotenv').config();
var container = process.env.CONTAINER;
var logPath = '/var/lib/docker/containers/'+container+'/'+container+'-json.log';
var date = dateFormat(new Date(), "yyyy-mm-dd-h:MM:ss");
var copyLogs = 'sudo mkdir -p ~/NodeArchive && sudo cp '+logPath+' ~/NodeArchive/nodeLogs';
var archiveCopy = 'sudo tar -czf ~/NodeArchive/nodeLogs_'+date+'.tar --absolute-names ~/NodeArchive/nodeLogs';
var trimCopy = 'sudo rm -rf ~/NodeArchive/nodeLogs';
function PushNotification(PushTitle, PushText)
{
var apiKey = process.env.APIKEY
var postdata = querystring.stringify({
'ApiKey': apiKey,
'PushTitle': PushTitle,
'PushText': PushText,
});
var options = {
hostname: 'www.notifymydevice.com',
port: 443,
path: '/push?',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postdata.length
}
};
callback = function (response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
console.log('Response: ' + str);
});
}
var req = https.request(options, callback);
req.write(postdata);
req.end();
req.on('error', function (e) {
Log(e);
});
}
exec(copyLogs, (error, copy, stderr) => {
if (error){
console.log('Failed to copy logs.');
PushNotification(process.env.NODENAME + " log archiving failed.",'Failed to copy.');
} else{
console.log('Logs copied.');
console.log('Archiving Logs...');
exec(archiveCopy, (error, archive, stderr) => {
if (error){
console.log('Failed to archive logs.');
PushNotification(process.env.NODENAME + " log archiving failed.",'Failed to archive.');
} else{
console.log('Logs Archived.');
console.log('Trimming extra copy...');
exec(trimCopy, (error, trim, stderr) => {
if (error){
console.log('Failed to trim copy.');
PushNotification(process.env.NODENAME + " log archiving failed.",'Failed to trim copy.');
} else{
console.log('Log trimmed.');
console.log('Truncating Node logs...');
fs.truncate(logPath,0,function(){
PushNotification(process.env.NODENAME + " log archiving complete.",'Complete.');
});
}
});
}
});
}
});