-
Notifications
You must be signed in to change notification settings - Fork 8
/
ur-driver
77 lines (66 loc) · 2.04 KB
/
ur-driver
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
#!/usr/bin/env node
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright (c) 2014, Joyent, Inc.
*/
amqp = require('./amqp');
sys = require('sys');
var creds =
{ host: process.env['AMQP_HOST'] || 'localhost'
, port: process.env['AMQP_PORT'] || 5672
, login: process.env['AMQP_LOGIN'] || 'guest'
, password: process.env['AMQP_PASSWORD'] || 'guest'
, vhost: process.env['AMQP_VHOST'] || '/'
};
var connection = amqp.createConnection(creds);
var exchange;
connection.addListener('ready', function () {
exchange = connection.exchange('amq.topic', { type: 'topic' });
var queue = connection.queue('ur.'+Math.random());
queue.addListener("open", function () {
console.log("Ready to receive messages");
});
queue.bind('amq.topic', 'ur.startup.#');
queue.bind('amq.topic', 'ur.execute-reply.*.*');
queue.subscribeJSON(function (m) {
var rkParts = m._routingKey.split('.', 4);
console.log("\"Head Node\" received message:");
console.dir(m);
if (rkParts[1] === "startup") {
exchange.publish
( 'ur.execute.' + rkParts[2] + '.' + genId()
, { type: 'file'
, file: './my-test-script.sh'
, args: []
, env: {}
}
);
exchange.publish
( 'ur.execute.' + rkParts[2] + '.' + genId()
, { type: 'script'
, script: "#!/bin/bash\necho hello world\nexit 0"
, args: []
, env: {}
}
);
setTimeout(function () {
exchange.publish
( 'ur.execute.' + rkParts[2] + '.' + genId()
, { type: 'script'
, script: "#!/bin/bash\necho goodbye, cruel world\nexit 113"
, args: []
, env: {}
}
);
}, 2000);
}
});
});
function genId() {
return Math.floor(Math.random() * 0xffffffff).toString(16);
};
// vim:ft=javascript