-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (113 loc) · 3.8 KB
/
index.js
File metadata and controls
143 lines (113 loc) · 3.8 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*jslint white: true, forin: false, node: true, indent: 4 */
(function (module) {
"use strict";
var fs = require('fs'),
Batch = require('batch'),
http = require('http');
function getHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}
function loadConfigFile(config, cb) {
var matched = config.match(/(~)?([^#]+)(?:#(\w+))?/);
if (!matched) {
return cb(new Error("not a valid file name " + config));
}
var home = matched[1],
file = matched[2],
qualifier = matched[3];
if (home) {
file = getHome() + file;
}
return fs.readFile(file, "utf-8", function (err, content) {
if (err) {
return cb(); // ignore readfile errors
}
var parsed = JSON.parse(content);
if (qualifier) {
parsed = parsed[qualifier];
}
return cb(undefined, parsed);
});
}
function merge(result, content, cb) {
var batch = new Batch(),
keys = Object.keys(content);
keys.forEach(function (key) {
var value = content[key];
if (value) {
if (value.__isEC2InstanceData__) {
return batch.push(function (done) {
value(function (err, value) {
if (err) {
return done(err);
}
result[key] = value;
return done();
});
});
}
return result[key] = value;
}
return value;
});
return batch.end(function (err) {
return cb(err, result);
});
}
function loadConfigs(args, result, cb) {
if (!args.length) {
return cb(undefined, result);
}
var config = args.shift();
if (Object.prototype.toString.call(config) == '[object String]') {
return loadConfigFile(config, function (err, content) {
if (err) {
return cb(err, result);
}
if (content) {
return merge(result, content, function (err, result) {
if (err) {
return cb(err, result);
}
return loadConfigs(args, result, cb);
});
}
return loadConfigs(args, result, cb);
});
}
return merge(result, config, function (err, result) {
if (err) {
return cb(err, result);
}
return loadConfigs(args, result, cb);
});
}
module.exports = function () {
var args = Array.prototype.slice.call(arguments),
cb = args.pop();
return loadConfigs(args, {}, cb);
};
// http://169.254.169.254/latest/meta-data/instance-id
module.exports.ec2instance = function (key) {
var value = function (cb) {
return http.request({
host:'169.254.169.254',
port:80,
path:'/latest/' + key
},function (res) {
var data = "";
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
return res.on('end', function () {
return cb(undefined, data);
});
}).on('error', function (e) {
return cb(e);
});
};
value.__isEC2InstanceData__ = true;
return value;
};
})(module);