-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpirun.js
More file actions
139 lines (112 loc) · 3.98 KB
/
pirun.js
File metadata and controls
139 lines (112 loc) · 3.98 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
// Generated by CoffeeScript 1.10.0
(function() {
var checkIP, dirname, i, ignorePatterns, ip, len, line, matchIgnore, name, path, pirunFile, pirunIgnore, pirunTime, program, request, shell, target, uploadFiles, user;
shell = require('shelljs');
request = require('sync-request');
program = require('commander');
path = require('path');
name = '';
target = '';
program.version('0.0.1')["arguments"]('<piname> [target]').option('-f, --force', 'Force to reupload everything').option('-s, --shell', 'Log in with ssh instead of running make').option('-r, --root', 'Run make (or log in if -s option is present) as root').action(function(piname, tar) {
name = piname;
return target = tar ? tar : '';
}).on('--help', function() {
console.log(' <piname> : name of the Raspberry Pi on RPC or an IPv4 address');
console.log(' [target] : the target of the Makefile to execute');
console.log('');
return shell.echo("").to('/tmp/pirun');
});
program.parse(process.argv);
ip = '';
checkIP = function(text) {
var i, len, ref, splitIP, val;
splitIP = text.split('.');
if (splitIP.length !== 4) {
return false;
}
for (i = 0, len = splitIP.length; i < len; i++) {
val = splitIP[i];
if (!((0 <= (ref = parseInt(val)) && ref < 256))) {
return false;
}
}
return true;
};
if (checkIP(name)) {
ip = name;
} else if (name.length === 0) {
program.help();
} else {
ip = request('GET', "http://bonetti.io/rpc/api/ip/" + name).getBody().toString();
if (ip === 'not found') {
console.log("Raspberry Pi '" + name + "' not found");
shell.echo("").to('/tmp/pirun');
process.exit(-1);
}
}
user = program.root ? 'root' : 'pi';
if (program.shell) {
shell.echo("ssh " + user + "@" + ip).to('/tmp/pirun');
process.exit(0);
}
dirname = shell.pwd().split('/').pop();
if (program.force) {
pirunTime = 0;
shell.exec("ssh " + user + "@" + ip + " 'rm -rf /var/pirun/" + dirname + "'");
} else {
shell.config.silent = true;
pirunFile = shell.ls('-l', "./.pirun." + name);
pirunTime = pirunFile.code !== 0 ? 0 : pirunFile[0].ctime;
}
pirunIgnore = shell.cat("./.pirunignore");
pirunIgnore = pirunIgnore.code !== 0 ? [] : pirunIgnore.split('\n');
shell.config.silent = false;
ignorePatterns = [/\.pirun\..*/g];
for (i = 0, len = pirunIgnore.length; i < len; i++) {
line = pirunIgnore[i];
line = '^' + line.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, '.*') + '$';
if (line.trim().length > 0) {
ignorePatterns.push(new RegExp(line));
}
}
matchIgnore = function(filename) {
var j, len1, pattern;
for (j = 0, len1 = ignorePatterns.length; j < len1; j++) {
pattern = ignorePatterns[j];
if (filename.match(pattern)) {
return true;
}
}
return false;
};
uploadFiles = function(dir) {
var didSomething, file, files, j, len1, output;
files = shell.ls('-lA', dir);
output = [];
didSomething = false;
for (j = 0, len1 = files.length; j < len1; j++) {
file = files[j];
if (!((file.mtime < pirunTime && !file.isDirectory()) || matchIgnore(path.join(dir, file.name)))) {
if (file.isDirectory()) {
didSomething |= uploadFiles(path.join(dir, file.name));
} else {
output.push(path.join(dir, file.name));
}
}
}
if (output.length > 0) {
shell.exec("ssh " + user + "@" + ip + " 'mkdir -p /var/pirun/" + (path.join(dirname, dir)) + "'");
shell.exec("scp " + (output.join(' ')) + " " + user + "@" + ip + ":/var/pirun/" + (path.join(dirname, dir)));
return true;
}
return didSomething;
};
process.stdout.write('Uploading files ...');
if (uploadFiles('.')) {
console.log(' OK\n');
} else {
console.log(' already up-to-date\n');
}
shell.touch("./.pirun." + name);
shell.echo("ssh " + user + "@" + ip + " 'make " + target + " -C /var/pirun/" + dirname + "'").to('/tmp/pirun');
}).call(this);