This repository has been archived by the owner on Dec 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarnish.js
128 lines (100 loc) · 2.72 KB
/
varnish.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
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
if (process.env['AIRBRAKE_KEY']) {
var airbrake = require('airbrake');
airbrake = airbrake.createClient(process.env['AIRBRAKE_KEY']);
airbrake.handleExceptions();
}
var Handlebars = require('handlebars')
, Http = require('http')
, Fs = require('fs')
, ChildProcess = require('child_process')
;
var _fetch_endpoints
, _render_config
, _update_config
, _reload_varnish
;
var vcl_template
, varnish_host
, alice_host
, alice_port
;
varnish_host = process.env['VARNISH_HOST'] || 'localhost';
alice_host = process.env['ALICE_HOST'] || 'localhost';
alice_port = process.env['ALICE_PORT'] || '5000';
alice_port = parseInt(alice_port, 10);
_fetch_endpoints = function(){
var buffer
, options
, endpoints
, report
;
buffer = "";
options = {
host: alice_host,
port: alice_port,
path: '/api_v1/machines/'+varnish_host+'/routers.json'
};
Http.get(options, function(res) {
if (res.statusCode == 200) {
res.setEncoding('utf8');
res.on('data', function(chunk){
buffer += chunk;
});
res.on('end', function(){
endpoints = JSON.parse(buffer);
_render_config(endpoints);
});
res.on('close', function(e){
console.log("Got error: " + e.message);
});
} else {
console.log("Got error: " + res.statusCode);
}
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
};
_render_config = function(endpoints){
var ctx
, new_vcl
;
ctx = {
routers: endpoints['routers']
};
new_vcl = vcl_template(ctx);
_update_config(new_vcl);
};
_update_config = function(new_vcl){
Fs.readFile(process.env['ALICE_VARNISH_VCL'], 'utf8', function(err, data){
if (err) { throw err; }
if (data == new_vcl) { return; }
Fs.writeFile(process.env['ALICE_VARNISH_VCL'], new_vcl, 'utf8', function(err){
if (err) { throw err; }
_reload_varnish();
});
});
};
_reload_varnish = function(){
var reload
;
reload = ChildProcess.spawn('sh', ['-c', process.env['ALICE_VARNISH_RELOAD']]);
reload.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
reload.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
reload.on('exit', function (code) {
console.log('child process ('
+process.env['ALICE_VARNISH_RELOAD']
+') exited with code '
+code);
});
};
process.env['ALICE_VARNISH_VCL'] = process.env['ALICE_VARNISH_VCL'] || '/etc/varnish/default.vcl';
Fs.readFile(__dirname+'/templates/varnish.vcl', 'utf8', function(err, data){
if (err) throw err;
vcl_template = Handlebars.compile(data);
_fetch_endpoints();
setInterval(_fetch_endpoints, 60000);
});