-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (84 loc) · 2.87 KB
/
index.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
'use strict';
var execFile = require('child_process').execFile;
var sqsConsumer = require('sqs-consumer');
var Marathon = require('./lib/carbono-mesos').Marathon;
var DEFAULT_CONTAINER_PORT = 8080;
var DEFAULT_API_VERSION = '2012-11-05';
var DEFAULT_REGION = 'us-east-1';
var STAGING_QUEUE = process.env.STAGING_QUEUE;
var MARATHON_URL = process.env.MARATHON_URL;
var SLAVE_URL = process.env.SLAVE_URL;
if (typeof MARATHON_URL === 'undefined') {
console.log("Environment variable MARATHON_URL cannot be empty!");
console.log("Define it before proceeding.");
process.exit(1);
}
if (typeof SLAVE_URL === 'undefined') {
console.log("Environment variable SLAVE_URL cannot be empty!");
console.log("Define it before proceeding.");
process.exit(1);
}
if (typeof STAGING_QUEUE === 'undefined') {
console.log("Environment variable STAGING_QUEUE cannot be empty!");
console.log("Define it before proceeding.");
process.exit(1);
}
var marathon = new Marathon(MARATHON_URL);
var app = sqsConsumer.create({
queueUrl: STAGING_QUEUE,
region: DEFAULT_REGION,
messageAttributeNames: ['appHash', 'imageName', 'route'],
handleMessage: function (message, done) {
console.log("message received");
var route = message.MessageAttributes.route.StringValue;
var marathonAppId = route.replace(/\//g, "");
var app = {
id: marathonAppId,
cpus: 0.01,
mem: 30,
instances: 1,
container: {
type: "DOCKER",
docker: {
"image": message.MessageAttributes.imageName.StringValue,
"network": "BRIDGE",
"portMappings": [{
"containerPort": DEFAULT_CONTAINER_PORT,
"hostPort": 0,
"protocol": "tcp"
}]
},
volumes: [
{
containerPath: "/data",
hostPath: "/code",
mode: "RW"
}
]
}
};
var promise = marathon.createApp(app);
promise
.then(function (result) {
var script = 'script/ansible.sh';
var args = [
MARATHON_URL,
marathonAppId,
route,
SLAVE_URL
];
execFile(script, args, function(err, stdout, stderr) {
console.log(err);
console.log(stdout);
console.log(stderr);
done();
});
}, function (err) {
console.log(err);
});
}
});
app.on('error', function (err) {
console.log(err);
});
app.start();