This repository was archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (90 loc) · 2.89 KB
/
Copy pathindex.js
File metadata and controls
99 lines (90 loc) · 2.89 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
'use strict';
var watch = require('watch');
var readYaml = require('read-yaml');
var axios = require('axios');
var glob = require('glob');
var path = require('path');
var vaultUrl = process.env.VAULT_URL || 'http://vault:8200';
var token = process.env.VAULT_TOKEN_ID || 'token';
var axiosClient = axios.create({
"baseURL": vaultUrl + "/v1/secret/",
"headers": {"X-Vault-Token": token}
});
// load initial config values
try {
console.log('Starting initial load...');
var axiosRequests = [];
var files = glob.sync('/config/*.yml', {"nodir": true});
for (var i=0; i<files.length; i++) {
var f = files[i];
var data = readYaml.sync(f);
if (data) {
axiosRequests.push(axiosClient.post(getContext(f), data).then(function(response) {
var name = path.parse(response.request.path).name;
console.log('loading initial file: /config/' + name + '.yml');
}));
} else {
console.error('no data: ' + f);
}
}
axios.all(axiosRequests).then(axios.spread(function() {
console.log('Initial load done.');
}));
} catch (err) {
console.error(err);
}
var watchOptions = {
'filter': function(f, stat) {
return !stat.isDirectory() && stat.isFile() && path.extname(f) === '.yml';
}
};
// monitor config files for updates, new files and deletions
watch.createMonitor('/config', watchOptions, function(monitor) {
monitor.on('changed', function (f) {
try {
var data = readYaml.sync(f);
if (data) {
axiosClient.post(getContext(f), data).then(function() {
console.log('update detected: ' + f);
}).catch(function(err) {
if (err) throw err;
});
} else {
console.error('no data: ' + f);
}
} catch (err) {
console.error(err);
}
});
monitor.on('created', function (f) {
try {
var data = readYaml.sync(f);
if (data) {
axiosClient.post(getContext(f), data).then(function() {
console.log('new config file detected: ' + f);
}).catch(function(err) {
if (err) throw err;
});
} else {
console.error('no data: ' + f);
}
} catch (err) {
console.error(err);
}
});
monitor.on('removed', function (f) {
try {
axiosClient.delete(getContext(f)).then(function() {
console.log('config file was deleted: ' + f);
}).catch(function(err) {
if (err) throw err;
});
} catch (err) {
console.error(err);
}
});
});
function getContext(f) {
var context = path.parse(f).name;
return context;
}