-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver
executable file
·124 lines (106 loc) · 2.62 KB
/
receiver
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
#!/usr/bin/env node
var path = require('path')
, mongo = require('mongojs')
, mkdirp = require('mkdirp').sync
, fs = require('fs')
, Batch = require('batch')
, spawn = require('child_process').spawn
, exec = require('child_process').exec
, chown = require('fs').chownSync
var exit = process.exit;
var basename = path.basename;
var exists = fs.existsSync;
var cwd = process.cwd();
var argv = process.argv; argv.shift();
var repo = argv[1];
var commit = argv[2];
var user = argv[3];
var key = argv[4];
var tasks = new Batch().concurrency(1);
var dest = '/home/spotlet/.spotlets/'+ basename(cwd).replace('.git','');
var name = basename(dest);
var db = mongo('spotlets', ['ports', 'spotlets']);
var port = null;
var cid = null;
function echeck (err) {
if (err) {
console.error(err);
exit(1);
}
}
tasks.push(function (next) {
return next();
exec('rm -rf '+ dest +'/*', function (err) {
echeck(err);
next();
});
});
tasks.push(function (next) {
var args = ['-xf', '-', '-C', dest];
mkdirp(dest);
var tar = spawn('tar', args, {
stdio: [process.stdin, 'pipe', 'pipe']
});
tar.on('error', echeck);
tar.on('close', next);
});
if (!exists(dest)) {
tasks.push(function (next) {
db.ports
.find({$where: 'true != this.active'})
.sort({_id: -1}, function (err, res) {
echeck(err);
port = res[0].port;
next();
});
});
tasks.push(function (next) {
db.ports.update({port: port}, {active: true}, function (err) {
echeck(err);
next();
});
});
tasks.push(function (next) {
var up = {port: port, name: name, path: dest, keys: [key]};
db.spotlets.update({name: name}, up, {upsert: true}, function (err) {
echeck(err);
next();
});
});
} else {
tasks.push(function (next) {
db.spotlets.findOne({name: name}, function (err, spotlet) {
echeck(err);
port = spotlet.port;
next();
});
});
}
tasks.push(function mount (next) {
var cmd = 'spotlet-mq-client';
var input = 'mount '+ dest +' -p '+ port +':6889';
var child = spawn(cmd);
console.log("mounting...");
child.stdin.write(input);
child.on('error', echeck);
child.stdout.on('data', function (chunk) {
console.log("chunk %s", chunk);
cid = String(chunk);
});
child.on('close', function () {
console.log("Mount complete");
next();
});
});
tasks.end(function (err) {
echeck(err);
console.log();
console.log("====================");
console.log("port=%d", port);
console.log("cid=%s", cid);
console.log("====================");
console.log();
console.log("Hostname: %s.spotlet.io", name);
console.log();
exit(0)
});