1 |
|
2 | (function() {
|
3 | var CloudFormationTemplateContext, CoffeeScript, fs, path, vm,
|
4 | bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
|
5 | slice = [].slice;
|
6 |
|
7 | fs = require('fs');
|
8 |
|
9 | vm = require('vm');
|
10 |
|
11 | path = require('path');
|
12 |
|
13 | CoffeeScript = require('coffee-script');
|
14 |
|
15 | CloudFormationTemplateContext = (function() {
|
16 | function CloudFormationTemplateContext() {
|
17 | this.Description = bind(this.Description, this);
|
18 | this.Condition = bind(this.Condition, this);
|
19 | this.Output = bind(this.Output, this);
|
20 | this.Mapping = bind(this.Mapping, this);
|
21 | this._resourceByType = bind(this._resourceByType, this);
|
22 | this.DeclareResource = bind(this.DeclareResource, this);
|
23 | this._buildCall = bind(this._buildCall, this);
|
24 | this._paramByType = bind(this._paramByType, this);
|
25 | this._resources = {};
|
26 | this._parameters = {};
|
27 | this._mappings = null;
|
28 | this._outputs = {};
|
29 | this._description = null;
|
30 | this._conditions = null;
|
31 | this.Params = {};
|
32 | this.Resources = {};
|
33 | this.Mappings = {};
|
34 | this.Conditions = {};
|
35 | this.AWS = {
|
36 | AutoScaling: {
|
37 | AutoScalingGroup: null,
|
38 | LaunchConfiguration: null,
|
39 | ScalingPolicy: null,
|
40 | LifecycleHook: null,
|
41 | ScheduledAction: null,
|
42 | Trigger: null
|
43 | },
|
44 | CloudFormation: {
|
45 | Authentication: null,
|
46 | CustomResource: null,
|
47 | Stack: null,
|
48 | WaitCondition: null,
|
49 | WaitConditionHandle: null
|
50 | },
|
51 | CloudFront: {
|
52 | Distribution: null
|
53 | },
|
54 | CloudWatch: {
|
55 | Alarm: null
|
56 | },
|
57 | CodeDeploy: {
|
58 | Application: null,
|
59 | DeploymentConfig: null,
|
60 | DeploymentGroup: null
|
61 | },
|
62 | CodePipeline: {
|
63 | Pipeline: null,
|
64 | CustomActionType: null
|
65 | },
|
66 | Config: {
|
67 | ConfigRule: null,
|
68 | ConfigurationRecorder: null,
|
69 | DeliveryChannel: null
|
70 | },
|
71 | DirectoryService: {
|
72 | MicrosoftAD: null,
|
73 | SimpleAD: null
|
74 | },
|
75 | CloudTrail: {
|
76 | Trail: null
|
77 | },
|
78 | DataPipeline: {
|
79 | Pipeline: null
|
80 | },
|
81 | DynamoDB: {
|
82 | Table: null
|
83 | },
|
84 | EC2: {
|
85 | CustomerGateway: null,
|
86 | DHCPOptions: null,
|
87 | EIP: null,
|
88 | EIPAssociation: null,
|
89 | Instance: null,
|
90 | InternetGateway: null,
|
91 | NetworkAcl: null,
|
92 | NetworkAclEntry: null,
|
93 | NetworkInterface: null,
|
94 | PlacementGroup: null,
|
95 | Route: null,
|
96 | RouteTable: null,
|
97 | SecurityGroup: null,
|
98 | SecurityGroupIngress: null,
|
99 | SecurityGroupEgress: null,
|
100 | Subnet: null,
|
101 | SubnetNetworkAclAssociation: null,
|
102 | SubnetRouteTableAssociation: null,
|
103 | SpotFleet: null,
|
104 | Volume: null,
|
105 | VolumeAttachment: null,
|
106 | VPC: null,
|
107 | VPCDHCPOptionsAssociation: null,
|
108 | VPCEndpoint: null,
|
109 | VPCGatewayAttachment: null,
|
110 | VPNConnection: null,
|
111 | VPNGateway: null
|
112 | },
|
113 | ECS: {
|
114 | Cluster: null,
|
115 | Service: null,
|
116 | TaskDefinition: null
|
117 | },
|
118 | ElastiCache: {
|
119 | CacheCluster: null,
|
120 | ParameterGroup: null,
|
121 | ReplicationGroup: null,
|
122 | SecurityGroup: null,
|
123 | SecurityGroupIngress: null,
|
124 | SubnetGroup: null
|
125 | },
|
126 | ElasticBeanstalk: {
|
127 | Application: null,
|
128 | ApplicationVersion: null,
|
129 | Environment: null,
|
130 | ConfigurationTemplate: null
|
131 | },
|
132 | ElasticLoadBalancing: {
|
133 | LoadBalancer: null
|
134 | },
|
135 | EFS: {
|
136 | FileSystem: null,
|
137 | MountTarget: null
|
138 | },
|
139 | IAM: {
|
140 | AccessKey: null,
|
141 | Group: null,
|
142 | InstanceProfile: null,
|
143 | ManagedPolicy: null,
|
144 | Policy: null,
|
145 | Role: null,
|
146 | User: null,
|
147 | UserToGroupAddition: null
|
148 | },
|
149 | Kinesis: {
|
150 | Stream: null
|
151 | },
|
152 | KMS: {
|
153 | Key: null
|
154 | },
|
155 | Logs: {
|
156 | Destination: null,
|
157 | LogGroup: null,
|
158 | LogStream: null,
|
159 | MetricFilter: null,
|
160 | SubscriptionFilter: null
|
161 | },
|
162 | Lambda: {
|
163 | EventSourceMapping: null,
|
164 | Function: null,
|
165 | Permission: null
|
166 | },
|
167 | OpsWorks: {
|
168 | App: null,
|
169 | Instance: null,
|
170 | Layer: null,
|
171 | Stack: null
|
172 | },
|
173 | Redshift: {
|
174 | Cluster: null,
|
175 | ClusterParameterGroup: null,
|
176 | ClusterSecurityGroup: null,
|
177 | ClusterSubnetGroup: null
|
178 | },
|
179 | RDS: {
|
180 | DBCluster: null,
|
181 | DBClusterParameterGroup: null,
|
182 | DBInstance: null,
|
183 | DBParameterGroup: null,
|
184 | DBSubnetGroup: null,
|
185 | DBSecurityGroup: null,
|
186 | DBSecurityGroupIngress: null,
|
187 | EventSubscription: null,
|
188 | OptionGroup: null
|
189 | },
|
190 | Route53: {
|
191 | RecordSet: null,
|
192 | RecordSetGroup: null,
|
193 | HostedZone: null,
|
194 | HealthCheck: null
|
195 | },
|
196 | SDB: {
|
197 | Domain: null
|
198 | },
|
199 | S3: {
|
200 | Bucket: null,
|
201 | BucketPolicy: null
|
202 | },
|
203 | SNS: {
|
204 | Topic: null,
|
205 | TopicPolicy: null
|
206 | },
|
207 | SQS: {
|
208 | Queue: null,
|
209 | QueuePolicy: null
|
210 | },
|
211 | SSM: {
|
212 | Document: null
|
213 | },
|
214 | WAF: {
|
215 | ByteMatchSet: null,
|
216 | IPSet: null,
|
217 | Rule: null,
|
218 | SqlInjectionMatchSet: null,
|
219 | WebACL: null
|
220 | },
|
221 | WorkSpaces: {
|
222 | Workspace: null
|
223 | }
|
224 | };
|
225 | this.Param = {
|
226 | String: (function(_this) {
|
227 | return function(name, arg1, arg2) {
|
228 | return _this._paramByType('String', name, arg1, arg2);
|
229 | };
|
230 | })(this),
|
231 | Number: (function(_this) {
|
232 | return function(name, arg1, arg2) {
|
233 | return _this._paramByType('Number', name, arg1, arg2);
|
234 | };
|
235 | })(this),
|
236 | CommaDelimitedList: (function(_this) {
|
237 | return function(name, arg1, arg2) {
|
238 | return _this._paramByType('CommaDelimitedList', name, arg1, arg2);
|
239 | };
|
240 | })(this),
|
241 | AWS: (function(_this) {
|
242 | return function(type, name, arg1, arg2) {
|
243 | return _this._paramByType("AWS::" + type, name, arg1, arg2);
|
244 | };
|
245 | })(this),
|
246 | AWSList: (function(_this) {
|
247 | return function(type, name, arg1, arg2) {
|
248 | return _this._paramByType("List<AWS::" + type + ">", name, arg1, arg2);
|
249 | };
|
250 | })(this)
|
251 | };
|
252 | this._buildCall(null, null, 'AWS', this.AWS);
|
253 | }
|
254 |
|
255 | CloudFormationTemplateContext.prototype._paramByType = function(type, name, arg1, arg2) {
|
256 | var result;
|
257 | result = {};
|
258 | if (arg1 == null) {
|
259 | result[name] = {};
|
260 | } else if (arg2 == null) {
|
261 | result[name] = typeof arg1 === 'string' ? {
|
262 | Description: arg1
|
263 | } : arg1;
|
264 | } else {
|
265 | result[name] = arg2;
|
266 | result[name].Description = arg1;
|
267 | }
|
268 | result[name].Type = type;
|
269 | this._set(result, this._parameters);
|
270 | return this.Params[name] = {
|
271 | Ref: name
|
272 | };
|
273 | };
|
274 |
|
275 | CloudFormationTemplateContext.prototype._buildCall = function(parent, lastKey, awsType, leaf) {
|
276 | var key, val;
|
277 | if (leaf != null) {
|
278 | for (key in leaf) {
|
279 | val = leaf[key];
|
280 | this._buildCall(leaf, key, awsType + "::" + key, val);
|
281 | }
|
282 | return;
|
283 | }
|
284 | return parent[lastKey] = (function(_this) {
|
285 | return function(name, props) {
|
286 | return _this._resourceByType(awsType, name, props);
|
287 | };
|
288 | })(this);
|
289 | };
|
290 |
|
291 | CloudFormationTemplateContext.prototype.DeclareResource = function(name) {
|
292 | var base;
|
293 | return (base = this.Resources)[name] != null ? base[name] : base[name] = {
|
294 | Ref: name
|
295 | };
|
296 | };
|
297 |
|
298 | CloudFormationTemplateContext.prototype._resourceByType = function(type, name, props) {
|
299 | var result;
|
300 | result = {};
|
301 | if (((props != null ? props.Metadata : void 0) != null) || ((props != null ? props.Properties : void 0) != null) || ((props != null ? props.DependsOn : void 0) != null) || ((props != null ? props.UpdatePolicy : void 0) != null) || ((props != null ? props.CreationPolicy : void 0) != null) || ((props != null ? props.Condition : void 0) != null)) {
|
302 | result[name] = props;
|
303 | result[name].Type = type;
|
304 | } else {
|
305 | result[name] = {
|
306 | Type: type,
|
307 | Properties: props
|
308 | };
|
309 | }
|
310 | this._set(result, this._resources);
|
311 | return this.DeclareResource(name);
|
312 | };
|
313 |
|
314 | CloudFormationTemplateContext.prototype._set = function(source, target) {
|
315 | var key, results, val;
|
316 | results = [];
|
317 | for (key in source) {
|
318 | val = source[key];
|
319 | results.push(target[key] = val);
|
320 | }
|
321 | return results;
|
322 | };
|
323 |
|
324 | CloudFormationTemplateContext.prototype.Mapping = function(name, map) {
|
325 | var result;
|
326 | if (this._mappings == null) {
|
327 | this._mappings = {};
|
328 | }
|
329 | result = {};
|
330 | result[name] = map;
|
331 | return this._set(result, this._mappings);
|
332 | };
|
333 |
|
334 | CloudFormationTemplateContext.prototype.Output = function() {
|
335 | var args, name, result;
|
336 | name = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
|
337 | result = {};
|
338 | if (args.length === 1) {
|
339 | result[name] = {
|
340 | Value: args[0]
|
341 | };
|
342 | }
|
343 | if (args.length === 2) {
|
344 | result[name] = {
|
345 | Description: args[0],
|
346 | Value: args[1]
|
347 | };
|
348 | }
|
349 | return this._set(result, this._outputs);
|
350 | };
|
351 |
|
352 | CloudFormationTemplateContext.prototype.Condition = function(name, intrinsicfn) {
|
353 | var result;
|
354 | if (this._conditions == null) {
|
355 | this._conditions = {};
|
356 | }
|
357 | result = {};
|
358 | result[name] = intrinsicfn;
|
359 | return this._set(result, this._conditions);
|
360 | };
|
361 |
|
362 | CloudFormationTemplateContext.prototype.Description = function(d) {
|
363 | return this._description = d;
|
364 | };
|
365 |
|
366 | CloudFormationTemplateContext.prototype.Tag = function(key, val) {
|
367 | return {
|
368 | Key: key,
|
369 | Value: val
|
370 | };
|
371 | };
|
372 |
|
373 | CloudFormationTemplateContext.prototype.Join = function() {
|
374 | var args, delimiter;
|
375 | delimiter = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
|
376 | if (args.length === 1 && (args[0] instanceof Array)) {
|
377 | return {
|
378 | 'Fn::Join': [delimiter, args[0]]
|
379 | };
|
380 | } else {
|
381 | return {
|
382 | 'Fn::Join': [delimiter, args]
|
383 | };
|
384 | }
|
385 | };
|
386 |
|
387 | CloudFormationTemplateContext.prototype.FindInMap = function() {
|
388 | var args;
|
389 | args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
|
390 | return {
|
391 | 'Fn::FindInMap': args
|
392 | };
|
393 | };
|
394 |
|
395 | CloudFormationTemplateContext.prototype.GetAtt = function() {
|
396 | var args;
|
397 | args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
|
398 | return {
|
399 | 'Fn::GetAtt': args
|
400 | };
|
401 | };
|
402 |
|
403 | CloudFormationTemplateContext.prototype.Base64 = function(arg) {
|
404 | return {
|
405 | 'Fn::Base64': arg
|
406 | };
|
407 | };
|
408 |
|
409 | CloudFormationTemplateContext.prototype.GetAZs = function(arg) {
|
410 | return {
|
411 | 'Fn::GetAZs': arg
|
412 | };
|
413 | };
|
414 |
|
415 | CloudFormationTemplateContext.prototype.Select = function() {
|
416 | var args, index;
|
417 | index = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
|
418 | if (args.length === 1 && (args[0] instanceof Array)) {
|
419 | return {
|
420 | 'Fn::Select': [index, args[0]]
|
421 | };
|
422 | } else {
|
423 | return {
|
424 | 'Fn::Select': [index, args]
|
425 | };
|
426 | }
|
427 | };
|
428 |
|
429 | CloudFormationTemplateContext.prototype.And = function() {
|
430 | var condition, conditions;
|
431 | condition = arguments[0], conditions = 2 <= arguments.length ? slice.call(arguments, 1) : [];
|
432 | if (conditions.length === 1 && (conditions[0] instanceof Array)) {
|
433 | return {
|
434 | 'Fn::And': [condition, conditions[0]]
|
435 | };
|
436 | } else {
|
437 | return {
|
438 | 'Fn::And': [condition, conditions]
|
439 | };
|
440 | }
|
441 | };
|
442 |
|
443 | CloudFormationTemplateContext.prototype.Equals = function(value_1, value_2) {
|
444 | return {
|
445 | 'Fn::Equals': [value_1, value_2]
|
446 | };
|
447 | };
|
448 |
|
449 | CloudFormationTemplateContext.prototype.If = function(condition, value_if_true, value_if_false) {
|
450 | return {
|
451 | 'Fn::If': [condition, value_if_true, value_if_false]
|
452 | };
|
453 | };
|
454 |
|
455 | CloudFormationTemplateContext.prototype.Not = function(condition) {
|
456 | return {
|
457 | 'Fn::Not': [condition]
|
458 | };
|
459 | };
|
460 |
|
461 | CloudFormationTemplateContext.prototype.Or = function() {
|
462 | var condition, conditions;
|
463 | condition = arguments[0], conditions = 2 <= arguments.length ? slice.call(arguments, 1) : [];
|
464 | if (conditions.length === 1 && (conditions[0] instanceof Array)) {
|
465 | return {
|
466 | 'Fn::Or': [condition, conditions[0]]
|
467 | };
|
468 | } else {
|
469 | return {
|
470 | 'Fn::Or': [condition, conditions]
|
471 | };
|
472 | }
|
473 | };
|
474 |
|
475 | CloudFormationTemplateContext.prototype.AccountId = {
|
476 | Ref: 'AWS::AccountId'
|
477 | };
|
478 |
|
479 | CloudFormationTemplateContext.prototype.NotificationARNs = {
|
480 | Ref: 'AWS::NotificationARNs'
|
481 | };
|
482 |
|
483 | CloudFormationTemplateContext.prototype.NoValue = {
|
484 | Ref: 'AWS::NoValue'
|
485 | };
|
486 |
|
487 | CloudFormationTemplateContext.prototype.Region = {
|
488 | Ref: 'AWS::Region'
|
489 | };
|
490 |
|
491 | CloudFormationTemplateContext.prototype.StackId = {
|
492 | Ref: 'AWS::StackId'
|
493 | };
|
494 |
|
495 | CloudFormationTemplateContext.prototype.StackName = {
|
496 | Ref: 'AWS::StackName'
|
497 | };
|
498 |
|
499 | CloudFormationTemplateContext.prototype.InitScript = function(arg) {
|
500 | var chunks, compiled, existsSyncFunc, match, pattern, text;
|
501 | existsSyncFunc = fs.existsSync != null ? fs.existsSync : path.existsSync;
|
502 | if (!existsSyncFunc(arg)) {
|
503 | text = arg;
|
504 | } else {
|
505 | text = fs.readFileSync(arg).toString();
|
506 | }
|
507 | chunks = [];
|
508 | pattern = /((.|\n)*?)%{([^}?]+)}?((.|\n)*)/;
|
509 | match = text.match(pattern);
|
510 | while (match) {
|
511 | chunks.push(match[1]);
|
512 | compiled = CoffeeScript.compile(match[3], {
|
513 | bare: true
|
514 | });
|
515 | chunks.push(eval(compiled));
|
516 | text = match[4];
|
517 | match = text.match(pattern);
|
518 | }
|
519 | if (text && text.length > 0) {
|
520 | chunks.push(text);
|
521 | }
|
522 | return this.Base64(this.Join('', chunks));
|
523 | };
|
524 |
|
525 | return CloudFormationTemplateContext;
|
526 |
|
527 | })();
|
528 |
|
529 | module.exports.CloudFormationTemplateContext = CloudFormationTemplateContext;
|
530 |
|
531 | module.exports = function(func) {
|
532 | var context, template;
|
533 | context = new CloudFormationTemplateContext;
|
534 | func.apply(context, [context]);
|
535 | template = {
|
536 | AWSTemplateFormatVersion: '2010-09-09'
|
537 | };
|
538 | if (context._description != null) {
|
539 | template.Description = context._description;
|
540 | }
|
541 | template.Parameters = context._parameters;
|
542 | if (context._mappings != null) {
|
543 | template.Mappings = context._mappings;
|
544 | }
|
545 | template.Resources = context._resources;
|
546 | template.Outputs = context._outputs;
|
547 | if (context._conditions != null) {
|
548 | template.Conditions = context._conditions;
|
549 | }
|
550 | return template;
|
551 | };
|
552 |
|
553 | require('pkginfo')(module, 'version');
|
554 |
|
555 | }).call(this);
|