Repository navigation
Expand file tree
/
Copy pathfxosbuilds.js
More file actions
429 lines (384 loc) · 11.5 KB
/
Copy pathfxosbuilds.js
File metadata and controls
429 lines (384 loc) · 11.5 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#! /usr/bin/env node
'use strict';
var http = require('http');
var path = require('path');
var fs = require('fs');
var childProcess = require('child_process');
var JSFtp = require('jsftp');
var Promise = require('es6-promise').Promise;
var temp = require('temp');
var mkdirp = require('mkdirp');
var moment = require('moment');
var yargs = require('yargs');
var colors = require('colors');
// Helper
function promisify(fn) {
var args = Array.prototype.slice.call(arguments, 1);
return new Promise(function(resolve, reject) {
args.push(function(err) {
if (err != null) {
reject(err);
} else {
resolve.apply(null, Array.prototype.slice.call(arguments, 1));
}
});
if (Array.isArray(fn)) {
var scope = fn[0];
var method = fn[1];
scope[method].apply(scope, args);
} else {
fn.apply(null, args);
}
});
}
// Command-line arguments
var yarg = yargs.usage('CLI tool for FxOSBuilds updates.\nUsage: fxosbuilds [device] [version=central]')
.example('$0 zte-open 2.0', 'Flash a flame with 2.0 build.')
.example('$0 alcatel-one-touch-fire --folder ~/', 'Flash an Alcatel One Touch Fire with a nightly build (downloaded to ~/)')
.example('$0 zte-open --folder ~/ --local', 'Flash a Flame device with a previously downloaded build in ~/.')
.string([1, 'device', 'version', 'date'])
.alias({
'dir': 'd',
'version': 'v',
'device': 'i',
'date': 't',
'local': 'l',
'profile': 'p',
'remotify': 'r',
'help': 'h'
})
.boolean([ 'local', 'profile', 'help', 'remotify', 'help', 'only-remotify'])
.default({
version: '2.0',
date: 'latest'
})
.describe({
device: 'Device (zte-open, alcatel-one-touch-fire)',
version: 'Version (central, aurora, 1.4, …)',
date: 'Build date (for regression window testing)',
dir: 'Directory to keep downloads (defaults to temp)',
local: 'Use local files, skipping FTP (requires --dir)',
profile: 'Keep profile (no promises)',
remotify: 'Set device into development mode',
'only-remotify': 'Skip flashing, only set development mode',
help: 'Show this help'
})
.strict();
var argv = yarg.argv;
argv.device = (argv._[0] || argv.device || '').toLowerCase();
if (argv.device === 'zte-open') {
console.log('Link to guide here');
} else if (argv.device === 'alcatel-one-touch-fire') {
console.log('Link to guide here');
}
if (argv.help || (!argv.device && !argv['only-remotify'])) {
console.log(yarg.help());
if (!argv.device) {
console.log('Please provide a [device] argument!');
}
process.exit();
}
argv.version = String(argv._[1] || argv.version);
if (/^\d+$/.test(argv.version)) {
argv.version += '.0';
}
if (argv.date && argv.date != 'latest') {
argv.date = moment(argv.date);
if (!argv.date.isValid()) {
throw new Error('Invalid `date` argument');
}
} else {
argv.date = null;
}
var env = process.env;
// Constants
var FTP_HOST = 'ftp.mozilla.org';
var FTP_URL = 'http://' + FTP_HOST;
var FTP_PATH = '/pub/mozilla.org/b2g/nightly/';
var SCRIPT_PATH = path.join(__dirname, 'scripts');
var FLASH_SCRIPT_PATH = path.join(SCRIPT_PATH, 'shallow_flash.sh');
var BACKUP_SCRIPT_PATH = path.join(SCRIPT_PATH, 'backup_restore_profile.sh');
var TIMEOUT = 60 * 60 * 1000; // 1min
var ADB = process.env.ADB || 'adb';
// Set up dirs
temp.track();
var tempDir = temp.mkdirSync('flash-b2g');
var dir = argv.dir;
if (!dir) {
dir = tempDir;
} else {
dir = path.resolve(dir)
if (!fs.existsSync(dir)) {
mkdirp.sync(dir);
console.log('Created directory: %s', dir)
}
}
var profileDir = path.join(dir, 'flash-b2g-profile');
// At least on linux bash, getopt defines that optional arguments need to be
// like "--opt=arg". Only required arguments can do "--opt arg".
var argsSpace = (process.platform == 'linux') ? '=' : ' ';
if (argv['only-remotify']) {
setDeveloperPrefs()
.then(function() {
console.log('✓ Done (only-remotify)');
}, function(err) {
console.error(err);
})
.then(function() {
process.exit();
});
return;
}
var dateBit = (argv.date) ? argv.date.format('YYYY-MM-DD') : 'latest';
var versionBit = argv.version;
if (/\d/.test(versionBit)) {
versionBit = '.*v' + versionBit.replace(/\./, '_');
}
var pathBits = [dateBit, '.*', versionBit, argv.device];
var nameBits = [argv.device, versionBit];
nameBits.push(dateBit);
var pathMatch = new RegExp('^' + pathBits.join('-') + '$', 'i');
var ftpPath = FTP_PATH;
// Strip the regex bits that are filesystem wildcards or otherwise weird out.
var safeNameBits = nameBits.join('-').replace(/[^\w_-]+/gi, '_');
var localGaiaPath = path.join(dir, 'gaia-' + safeNameBits + '.zip');
var localB2gPath = path.join(dir, 'b2g-' + safeNameBits + '.tar.gz');
function setDeveloperPrefs() {
var prefs = {
'devtools.debugger.forbid-certified-apps': false,
'devtools.debugger.prompt-connection': false,
'b2g.adb.timeout': 0,
// Reduce noise in logs: http://kb.mozillazine.org/Layout.css.report_errors
'layout.css.report_errors': false
};
var settings = {
'developer.menu.enabled': true,
'ftu.manifestURL': null,
'debugger.remote-mode': 'adb-devtools',
'devtools.debugger.remote-enabled': true,
'screen.timeout': 600, // 10min
'lockscreen.locked': false,
'lockscreen.enabled': false
};
// Wait for device
console.log('Waiting for device (is remote debugging on?) …'.yellow);
return promisify(childProcess.exec, ADB + ' wait-for-device')
// Stop B2G
.then(function stopB2g() {
console.log('Stopping system');
return promisify(childProcess.exec, [
ADB + ' remount', // really needed?
ADB + ' shell stop b2g'
].join(' && '));
})
// Fetch settings.json
.then(function pullSettings() {
return promisify(childProcess.exec, ADB + ' shell cat /system/b2g/defaults/settings.json', {
maxBuffer: 524288
});
})
.then(function pushSettings(stdout) {
var content = JSON.parse(stdout);
for (var key in settings) {
content[key] = settings[key];
}
var settingsPath = path.join(tempDir, 'settings.json');
fs.writeFileSync(settingsPath, JSON.stringify(content));
console.log('Appending to settings.json:\n', settings);
return promisify(childProcess.exec, [
ADB + ' shell mount -o rw,remount /system',
ADB + ' push ' + settingsPath + ' /system/b2g/defaults/settings.json',
ADB + ' shell mount -o ro,remount /system'
].join(' && '));
})
// Push preferences
.then(function pushPreferences() {
var cmds = ['cd /data/b2g/mozilla/*.default/']
.concat(Object.keys(prefs).map(function(key) {
return 'echo \'user_pref(' + JSON.stringify(key) + ', ' +
JSON.stringify(prefs[key]) + ');\' >> prefs.js';
})).join(' && ');
console.log('Appending to prefs.js:\n', prefs);
return promisify(childProcess.exec, ADB + ' shell "' +
cmds.replace(/"/g, '\\"') + '"', {
maxBuffer: 524288
});
})
// Restart B2G
.then(function restartB2g() {
console.log('Restarting system');
return promisify(childProcess.exec, ADB + ' shell sync && ' +
ADB + ' shell start b2g')
});
};
function download() {
// FTP connection
// TODO: Allow login
var ftp = new JSFtp({
host: FTP_HOST
});
ftp.useList = true;
ftp.socket.setTimeout(TIMEOUT);
ftp.socket.once('timeout', function(data) {
ftp.socket.destroy(); // should call the "ls" callback "err"
});
console.log('Connecting to %s', FTP_HOST.underline.white);
// List files in main dir
return promisify([ftp, 'ls'], ftpPath)
// Find target dir
.then(function findTargetFolder(files) {
files = files.filter(function(file) {
return pathMatch.test(file.name);
});
if (!files.length) {
throw new Error('Could not find a directory matching ' + pathMatch + '.\nVerify your --device, --version or --date argument and that a matching directory exists on the Mozilla FTP:\n' + FTP_URL + ftpPath);
}
return files[0].name;
})
// List files in target dir
.then(function listTargetFiles(path) {
ftpPath += path + '/';
console.log('Listing files from %s', (FTP_HOST.underline + ftpPath).white);
return promisify([ftp, 'ls'], ftpPath);
})
// Find b2g package
.then(function findB2gFile(files) {
files = files.filter(function(file) {
return /android-arm\.tar\.gz$/.test(file.name);
});
if (!files.length) {
throw new Error('Could not find b2g package!');
}
return files[0].name;
})
// Open sockets for b2g and gaia packages
.then(function openStreams(file) {
return Promise.all([
new Promise(function(resolve, reject) {
http.get(FTP_URL + ftpPath + 'gaia.zip', resolve)
.on('error', reject);
}), new Promise(function(resolve, reject) {
http.get(FTP_URL + ftpPath + file, resolve)
.on('error', reject);
})
]);
})
// Stream socket data into files
.then(function downloadStreams(streams) {
console.log('Downloading from %s', (FTP_URL + ftpPath).underline.white);
var fileFlags = {
flags: 'w'
};
return Promise.all([
new Promise(function(resolve, reject) {
var file = fs.createWriteStream(localGaiaPath, fileFlags);
file.on('finish', function() {
file.close(resolve);
});
streams[0].pipe(file);
}),
new Promise(function(resolve, reject) {
var file = fs.createWriteStream(localB2gPath, {
flags: 'w'
});
file.on('finish', function() {
file.close(resolve);
});
streams[1].pipe(file);
})
]);
});
}
// Start process
Promise.resolve().then(function() {
if (argv.local) {
console.log('Skipping FTP');
return;
}
return download();
})
.then(function() {
if (!fs.existsSync(localGaiaPath) || !fs.existsSync(localB2gPath)) {
throw new Error('Local files not found:\n' + localGaiaPath + '\n' + localB2gPath);
}
})
.then(function() {
console.log('Waiting for device (is remote debugging on?) …'.yellow);
return promisify(childProcess.exec, ADB + ' wait-for-device')
})
.then(function() {
if (argv.profile) {
console.log('Exec ' + 'backup_restore_profile.sh'.grey.italic + ' to back up');
return promisify(childProcess.exec, [
BACKUP_SCRIPT_PATH,
'-p' + argsSpace + profileDir,
'-b'
].join(' '), {
cwd: SCRIPT_PATH
})
.then(function(stdout, stderr) {
if (stdout) {
console.log('[backup_restore_profile] '.grey + String(stdout).trim());
}
if (stderr) {
console.log('[backup_restore_profile] '.red + String(stderr).trim());
}
});
}
})
// Execute flash script
.then(function executeFlash(stdout) {
var args = [
'--gaia' + argsSpace + localGaiaPath,
'--gecko' + argsSpace + localB2gPath,
'-y'
].join(' ');
console.log('Exec ' + ('shallow_flash.sh ' + args).grey.italic);
console.log('This can take a bit, finishing with the device restarting …'.yellow);
return promisify(childProcess.exec, FLASH_SCRIPT_PATH + ' ' + args, {
cwd: SCRIPT_PATH
})
.then(function(stdout, stderr) {
if (stdout) {
console.log('[shallow_flash] '.grey + String(stdout).trim());
}
if (stderr) {
console.log('[shallow_flash] '.red + String(stderr).trim());
}
});
})
.then(function() {
if (argv.profile) {
console.log('Exec ' + 'backup_restore_profile.sh'.grey.italic + ' to restore');
return promisify(childProcess.exec, [
BACKUP_SCRIPT_PATH,
'-p' + argsSpace + profileDir,
'--no-reboot',
'-r'
].join(' '), {
cwd: SCRIPT_PATH
})
.then(function(stdout, stderr) {
if (stdout) {
console.log('[backup_restore_profile] '.grey + String(stdout).trim());
}
if (stderr) {
console.log('[backup_restore_profile] '.red + String(stderr).trim());
}
});
}
})
// Set developer prefs
.then(function() {
if (argv.remotify) {
return setDeveloperPrefs();
}
})
// Listo!
.then(function() {
console.log('✓ %s flashed to %s!'.bold.green, argv.device, argv.version);
process.exit();
}, function(err) {
console.error(String(err).bold.red, err);
process.exit(1);
});