forked from volumio/Volumio2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-helper.js
executable file
·166 lines (147 loc) · 4.51 KB
/
update-helper.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
var fs = require('fs-extra');
var execSync = require('child_process').execSync;
var io = require('socket.io-client');
var inquirer = require('inquirer');
var args = [];
var errorMessage = 'Error, please provide the required updater action: forceupdate | factory | userdata | testmode | cleanupdate | restorevolumio';
if (process.argv[3]) {
var argument = process.argv[3];
if (process.argv.length >= 4) {
args = process.argv.slice(4);
}
switch (argument) {
case 'forceupdate':
forceUpdate();
break;
case 'factory':
factoryReset();
break;
case 'userdata':
deleteUserData();
break;
case 'testmode':
testMode();
break;
case 'cleanupdate':
cleanUpdate();
break;
case 'restorevolumio':
restoreVolumio();
break;
default:
console.log(errorMessage);
}
} else {
console.log(errorMessage);
}
function forceUpdate (data) {
var clean = false;
var socket = io.connect('http://localhost:3000');
console.log('Checking for new Updates');
socket.emit('updateCheck');
if (data && data.clean) {
clean = true;
}
function handleUpdate() {
if (clean) {
console.log('Executing a clean update');
execSync('/bin/touch /boot/user_data', {uid: 1000, gid: 1000});
}
executeUpdate(socket);
}
socket.on('updateReady', function (data) {
if (data.updateavailable) {
var question = [
{
type: 'confirm',
name: 'executeUpdate',
message: data.title + ' is available. Do you want to Update?'
}
];
if (args.includes('--yes') || args.includes('-y')) {
handleUpdate();
}
inquirer.prompt(question).then((answer) => {
if (answer.executeUpdate) {
handleUpdate();
} else {
console.log('Exiting...');
return process.exit(0);
}
});
} else {
console.log('No update available');
return process.exit(0);
}
//
});
}
function cleanUpdate () {
forceUpdate({'clean': true});
}
function executeUpdate (socket) {
console.log('Starting Update...');
var payload = {'ignoreIntegrityCheck': true};
socket.emit('update', payload);
socket.on('updateProgress', function (data) {
if (data.progress && data.status) {
console.log('Updating: ' + data.progress + '% : ' + data.status);
}
});
socket.on('updateDone', function (data) {
if (data.status === 'Error') {
console.log('Cannot complete update, error');
} else {
console.log('Update completed successfully, restarting');
return reboot(socket);
}
});
}
function factoryReset () {
console.log('Factory reset initiated, the system will restart now.');
execSync('/bin/touch /boot/factory_reset', {uid: 1000, gid: 1000});
return reboot();
}
function deleteUserData () {
console.log('User data reset initiated, the system will restart now.');
execSync('/bin/touch /boot/user_data', {uid: 1000, gid: 1000});
return reboot();
}
function testMode () {
try {
fs.statSync('/data/test');
execSync('/bin/rm /data/test', {uid: 1000, gid: 1000});
console.log('Test mode DISABLED');
} catch (e) {
execSync('/bin/touch /data/test', {uid: 1000, gid: 1000});
console.log('Test mode ENABLED');
}
}
function reboot (socket) {
console.log('Restarting');
if (socket) {
socket.emit('closeModals');
}
execSync('/bin/sync', {uid: 1000, gid: 1000});
execSync('/usr/bin/sudo /sbin/reboot', {uid: 1000, gid: 1000});
}
function restoreVolumio () {
console.log('Deleting Overlay content of /volumio folder');
try {
console.log('Mounting overlay partition');
if (!fs.existsSync('/mnt/overlay')) {
execSync('/bin/mkdir /mnt/overlay', {uid: 1000, gid: 1000});
}
execSync('/usr/bin/sudo /bin/mount /dev/mmcblk0p3 /mnt/overlay', {uid: 1000, gid: 1000});
console.log('Overlay partition mounted, deleting /volumio folder on overlay');
execSync('/bin/rm -rf /mnt/overlay/dyn/volumio', {uid: 1000, gid: 1000});
console.log('Cleaning environment');
execSync('/usr/bin/sudo /bin/umount /mnt/overlay', {uid: 1000, gid: 1000});
execSync('/bin/rm -rf /mnt/overlay', {uid: 1000, gid: 1000});
execSync('/bin/sync', {uid: 1000, gid: 1000});
console.log('Done, restarting system');
return reboot();
} catch (e) {
console.log('Could not restore /volumio folder: ' + e);
}
}