kumogata-template 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (241) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +54 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +75 -0
  6. data/README.md +204 -0
  7. data/Rakefile +23 -0
  8. data/bin/kumogata-template +9 -0
  9. data/kumogata-template.gemspec +26 -0
  10. data/lib/kumogata/template.rb +17 -0
  11. data/lib/kumogata/template/autoscaling.rb +77 -0
  12. data/lib/kumogata/template/cloudwatch.rb +28 -0
  13. data/lib/kumogata/template/codedeploy.rb +85 -0
  14. data/lib/kumogata/template/const.rb +175 -0
  15. data/lib/kumogata/template/datapipeline.rb +90 -0
  16. data/lib/kumogata/template/dynamodb.rb +111 -0
  17. data/lib/kumogata/template/ec2.rb +165 -0
  18. data/lib/kumogata/template/ecs.rb +119 -0
  19. data/lib/kumogata/template/elasticbeanstalk.rb +33 -0
  20. data/lib/kumogata/template/elb.rb +132 -0
  21. data/lib/kumogata/template/emr.rb +166 -0
  22. data/lib/kumogata/template/events.rb +25 -0
  23. data/lib/kumogata/template/ext/argument_parser.rb +8 -0
  24. data/lib/kumogata/template/ext/kumogata.rb +103 -0
  25. data/lib/kumogata/template/helper.rb +211 -0
  26. data/lib/kumogata/template/iam.rb +96 -0
  27. data/lib/kumogata/template/lambda.rb +34 -0
  28. data/lib/kumogata/template/s3.rb +223 -0
  29. data/lib/kumogata/template/sns.rb +25 -0
  30. data/lib/kumogata/template/version.rb +1 -0
  31. data/template/_template.rb +25 -0
  32. data/template/autoscaling-group.rb +47 -0
  33. data/template/autoscaling-launch-configuration.rb +54 -0
  34. data/template/autoscaling-lifecycle-hook.rb +27 -0
  35. data/template/autoscaling-scaling-policy.rb +33 -0
  36. data/template/autoscaling-scheduled-action.rb +37 -0
  37. data/template/cloudtrail.rb +39 -0
  38. data/template/cloudwatch-alarm.rb +45 -0
  39. data/template/codedeploy-application.rb +15 -0
  40. data/template/codedeploy-deployment-config.rb +23 -0
  41. data/template/codedeploy-deployment-group.rb +35 -0
  42. data/template/datapipeline-pipeline.rb +28 -0
  43. data/template/dynamodb-table.rb +33 -0
  44. data/template/ec2-eip-association.rb +23 -0
  45. data/template/ec2-eip.rb +17 -0
  46. data/template/ec2-instance.rb +65 -0
  47. data/template/ec2-internet-gateway.rb +15 -0
  48. data/template/ec2-nat-gateway.rb +17 -0
  49. data/template/ec2-network-acl-entry.rb +30 -0
  50. data/template/ec2-network-acl.rb +17 -0
  51. data/template/ec2-route-table.rb +17 -0
  52. data/template/ec2-route.rb +27 -0
  53. data/template/ec2-security-group.rb +24 -0
  54. data/template/ec2-subnet-network-acl-association.rb +17 -0
  55. data/template/ec2-subnet-route-table-association.rb +17 -0
  56. data/template/ec2-subnet.rb +23 -0
  57. data/template/ec2-volume-attachment.rb +19 -0
  58. data/template/ec2-volume.rb +31 -0
  59. data/template/ec2-vpc-endpoint.rb +23 -0
  60. data/template/ec2-vpc-gateway-attachment.rb +19 -0
  61. data/template/ec2-vpc.rb +24 -0
  62. data/template/ecs-cluster.rb +11 -0
  63. data/template/ecs-service.rb +24 -0
  64. data/template/ecs-task-definition.rb +18 -0
  65. data/template/elasticache-cache-cluster.rb +63 -0
  66. data/template/elasticache-parameter-group.rb +20 -0
  67. data/template/elasticache-replication-group.rb +58 -0
  68. data/template/elasticache-subnet-group.rb +17 -0
  69. data/template/elasticbeanstalk-application-version.rb +24 -0
  70. data/template/elasticbeanstalk-application.rb +17 -0
  71. data/template/elasticbeanstalk-configuration-template.rb +31 -0
  72. data/template/elasticbeanstalk-environment.rb +44 -0
  73. data/template/elb-loadbalancer.rb +46 -0
  74. data/template/emr-cluster.rb +39 -0
  75. data/template/emr-instance-group-config.rb +33 -0
  76. data/template/emr-step.rb +22 -0
  77. data/template/events-rule.rb +28 -0
  78. data/template/iam-access-key.rb +19 -0
  79. data/template/iam-group.rb +20 -0
  80. data/template/iam-instance-profile.rb +17 -0
  81. data/template/iam-managed-policy.rb +28 -0
  82. data/template/iam-policy.rb +26 -0
  83. data/template/iam-role.rb +21 -0
  84. data/template/iam-user-to-group-addition.rb +17 -0
  85. data/template/iam-user.rb +24 -0
  86. data/template/lambda-alias.rb +21 -0
  87. data/template/lambda-event-source-mapping.rb +24 -0
  88. data/template/lambda-function.rb +34 -0
  89. data/template/lambda-permission.rb +26 -0
  90. data/template/lambda-version.rb +19 -0
  91. data/template/mappings-ec2.rb +85 -0
  92. data/template/output-access-key.rb +6 -0
  93. data/template/output-arn.rb +6 -0
  94. data/template/output-autoscaling.rb +6 -0
  95. data/template/output-az.rb +6 -0
  96. data/template/output-dynamodb.rb +6 -0
  97. data/template/output-ec2.rb +8 -0
  98. data/template/output-elasticache.rb +30 -0
  99. data/template/output-elb.rb +12 -0
  100. data/template/output-emr.rb +6 -0
  101. data/template/output-rds.rb +10 -0
  102. data/template/output-redshift.rb +10 -0
  103. data/template/output-s3.rb +8 -0
  104. data/template/output-security-group.rb +5 -0
  105. data/template/output-sqs.rb +5 -0
  106. data/template/output-topic.rb +6 -0
  107. data/template/output-vpc.rb +10 -0
  108. data/template/output.rb +29 -0
  109. data/template/parameter-ec2.rb +22 -0
  110. data/template/parameter-elasticache.rb +9 -0
  111. data/template/parameter-rds.rb +9 -0
  112. data/template/parameter-redshift.rb +9 -0
  113. data/template/parameter.rb +20 -0
  114. data/template/rds-db-cluster-parameter-group.rb +22 -0
  115. data/template/rds-db-cluster.rb +47 -0
  116. data/template/rds-db-instance.rb +82 -0
  117. data/template/rds-db-parameter-group.rb +21 -0
  118. data/template/rds-db-subnet-group.rb +19 -0
  119. data/template/rds-event-subscription.rb +42 -0
  120. data/template/rds-option-group.rb +23 -0
  121. data/template/redshift-cluster-parameter-group.rb +19 -0
  122. data/template/redshift-cluster-subnet-group.rb +17 -0
  123. data/template/redshift-cluster.rb +60 -0
  124. data/template/s3-bucket-policy.rb +20 -0
  125. data/template/s3-bucket.rb +42 -0
  126. data/template/sns-topic.rb +20 -0
  127. data/template/sqs-queue.rb +27 -0
  128. data/test/_template.rb +26 -0
  129. data/test/abstract_unit.rb +101 -0
  130. data/test/autoscaling_test.rb +109 -0
  131. data/test/codedeploy_test.rb +117 -0
  132. data/test/datapipeline_test.rb +142 -0
  133. data/test/dynamodb_test.rb +184 -0
  134. data/test/ec2_test.rb +286 -0
  135. data/test/ecs_test.rb +135 -0
  136. data/test/elasticbeanstalk_test.rb +56 -0
  137. data/test/elb_test.rb +147 -0
  138. data/test/emr_test.rb +193 -0
  139. data/test/events_test.rb +22 -0
  140. data/test/helper_test.rb +547 -0
  141. data/test/iam_test.rb +93 -0
  142. data/test/lambda_test.rb +41 -0
  143. data/test/s3_test.rb +195 -0
  144. data/test/sns_test.rb +27 -0
  145. data/test/template/autoscaling-group_test.rb +177 -0
  146. data/test/template/autoscaling-launch-configuration_test.rb +117 -0
  147. data/test/template/autoscaling-lifecycle-hook_test.rb +33 -0
  148. data/test/template/autoscaling-scaling-policy_test.rb +26 -0
  149. data/test/template/autoscaling-scheduled-action_test.rb +26 -0
  150. data/test/template/cloudtrail_test.rb +55 -0
  151. data/test/template/cloudwatch-alarm_test.rb +38 -0
  152. data/test/template/codedeploy-application_test.rb +21 -0
  153. data/test/template/codedeploy-deployment-config_test.rb +26 -0
  154. data/test/template/codedeploy-deployment-group_test.rb +48 -0
  155. data/test/template/datapipeline-pipeline_test.rb +358 -0
  156. data/test/template/dynamodb-table_test.rb +94 -0
  157. data/test/template/ec2-eip-association_test.rb +22 -0
  158. data/test/template/ec2-eip_test.rb +21 -0
  159. data/test/template/ec2-instance_test.rb +95 -0
  160. data/test/template/ec2-internet-gateway_test.rb +48 -0
  161. data/test/template/ec2-nat-gateway_test.rb +22 -0
  162. data/test/template/ec2-network-acl-entry_test.rb +61 -0
  163. data/test/template/ec2-network-acl_test.rb +51 -0
  164. data/test/template/ec2-route-table_test.rb +49 -0
  165. data/test/template/ec2-route_test.rb +21 -0
  166. data/test/template/ec2-security-group_test.rb +50 -0
  167. data/test/template/ec2-subnet-netwokr-acl-association_test.rb +26 -0
  168. data/test/template/ec2-subnet-route-table-association_test.rb +22 -0
  169. data/test/template/ec2-subnet_test.rb +51 -0
  170. data/test/template/ec2-volume-attachment_test.rb +24 -0
  171. data/test/template/ec2-volume_test.rb +52 -0
  172. data/test/template/ec2-vpc-gateway-attachment_test.rb +23 -0
  173. data/test/template/ec2-vpc_test.rb +102 -0
  174. data/test/template/ecs-cluster_test.rb +18 -0
  175. data/test/template/ecs-service_test.rb +29 -0
  176. data/test/template/ecs-task-definition_test.rb +143 -0
  177. data/test/template/elasticache-cache-cluster_test.rb +77 -0
  178. data/test/template/elasticache-parameter-group_test.rb +25 -0
  179. data/test/template/elasticache-replication-group_test.rb +45 -0
  180. data/test/template/elasticache-subnet-group_test.rb +26 -0
  181. data/test/template/elasticbeanstalk-application-version_test.rb +27 -0
  182. data/test/template/elasticbeanstalk-application_test.rb +21 -0
  183. data/test/template/elasticbeanstalk-configuration-template_test.rb +23 -0
  184. data/test/template/elasticbeanstalk-environment_test.rb +56 -0
  185. data/test/template/elasticbeanstalk-template_test.rb +33 -0
  186. data/test/template/elb-loadbalancer_test.rb +94 -0
  187. data/test/template/emr-cluster_test.rb +147 -0
  188. data/test/template/emr-instance-group-config_test.rb +65 -0
  189. data/test/template/emr-step_test.rb +82 -0
  190. data/test/template/events-rule_test.rb +34 -0
  191. data/test/template/iam-access-key_test.rb +22 -0
  192. data/test/template/iam-group_test.rb +21 -0
  193. data/test/template/iam-instance-profile_test.rb +24 -0
  194. data/test/template/iam-managed-policy_test.rb +35 -0
  195. data/test/template/iam-policy_test.rb +52 -0
  196. data/test/template/iam-role_test.rb +37 -0
  197. data/test/template/iam-user-to-group-addition_test.rb +24 -0
  198. data/test/template/iam-user_test.rb +21 -0
  199. data/test/template/lambda-alias_test.rb +46 -0
  200. data/test/template/lambda-event-source-mapping_test.rb +36 -0
  201. data/test/template/lambda-function_test.rb +34 -0
  202. data/test/template/lambda-permission_test.rb +32 -0
  203. data/test/template/lambda-version_test.rb +27 -0
  204. data/test/template/mappings-ec2_test.rb +92 -0
  205. data/test/template/output-access-key_test.rb +30 -0
  206. data/test/template/output-arn_test.rb +30 -0
  207. data/test/template/output-autoscaling_test.rb +27 -0
  208. data/test/template/output-az_test.rb +31 -0
  209. data/test/template/output-dynamodb_test.rb +21 -0
  210. data/test/template/output-ec2_test.rb +82 -0
  211. data/test/template/output-elasticache_test.rb +107 -0
  212. data/test/template/output-elb_test.rb +48 -0
  213. data/test/template/output-emr_test.rb +30 -0
  214. data/test/template/output-rds_test.rb +39 -0
  215. data/test/template/output-redshift_test.rb +39 -0
  216. data/test/template/output-s3_test.rb +52 -0
  217. data/test/template/output-security-group_test.rb +21 -0
  218. data/test/template/output-sqs_test.rb +21 -0
  219. data/test/template/output-topic_test.rb +30 -0
  220. data/test/template/output-vpc_test.rb +48 -0
  221. data/test/template/output_test.rb +87 -0
  222. data/test/template/parameter-ec2_test.rb +44 -0
  223. data/test/template/parameter-elasticache_test.rb +30 -0
  224. data/test/template/parameter-rds_test.rb +30 -0
  225. data/test/template/parameter-redshift_test.rb +30 -0
  226. data/test/template/parameter_test.rb +40 -0
  227. data/test/template/rds-db-cluster-parameter-group_test.rb +53 -0
  228. data/test/template/rds-db-cluster_test.rb +84 -0
  229. data/test/template/rds-db-instance_test.rb +93 -0
  230. data/test/template/rds-db-parameter-group_test.rb +53 -0
  231. data/test/template/rds-db-subnet-group.rb +54 -0
  232. data/test/template/rds-event-subscription_test.rb +48 -0
  233. data/test/template/rds-option-group_test.rb +56 -0
  234. data/test/template/redshift-cluster-parameter-group_test.rb +28 -0
  235. data/test/template/redshift-cluster-subnet-group_test.rb +26 -0
  236. data/test/template/redshift-cluster_test.rb +47 -0
  237. data/test/template/s3-bucket-policy_test.rb +40 -0
  238. data/test/template/s3-bucket_test.rb +127 -0
  239. data/test/template/sns-topic_test.rb +42 -0
  240. data/test/template/sqs-queue_test.rb +25 -0
  241. metadata +468 -0
@@ -0,0 +1,166 @@
1
+ #
2
+ # Helper - EMR
3
+ #
4
+ require 'kumogata/template/helper'
5
+
6
+
7
+ def _emr_applications(args)
8
+ applications = args[:applications] || []
9
+
10
+ array = []
11
+ applications.each do |application|
12
+ args = application[:args] || []
13
+ array << _{
14
+ #AdditionalInfo
15
+ Args args unless args.empty?
16
+ Name application[:name].capitalize
17
+ Version application[:version] if application.key? :version
18
+ }
19
+ end
20
+ array
21
+ end
22
+
23
+ def _emr_bootstraps(args)
24
+ actions = args[:bootstraps] || []
25
+
26
+ array = []
27
+ actions.each do |action|
28
+ array << _{
29
+ Name action[:name]
30
+ ScriptBootstrapAction _{
31
+ Args action[:script_args] if action.key? :script_args
32
+ Path action[:script_path]
33
+ }
34
+ }
35
+ end
36
+ array
37
+ end
38
+
39
+ def _emr_configurations(args)
40
+ configurations = args[:configurations] || []
41
+
42
+ array = []
43
+ configurations.each do |configuration|
44
+ classification = configuration[:classification] || ""
45
+ properties = configuration[:properties] || []
46
+ array << _{
47
+ Classification classification unless classification.empty?
48
+ ConfigurationProperties properties unless properties.empty?
49
+ #Configurations
50
+ }
51
+ end
52
+ array
53
+ end
54
+
55
+ def _emr_ebs(args)
56
+ ebs = args[:ebs] || []
57
+ return '' if ebs.empty?
58
+ ebs_block_devices = ebs.collect{|v| _emr_ebs_block_device(v) }
59
+
60
+ _{
61
+ EbsBlockDeviceConfig ebs_block_devices
62
+ #EbsOptimized
63
+ }
64
+ end
65
+
66
+ def _emr_ebs_block_device(args)
67
+ volume_spec = _emr_ebs_volume(args)
68
+ volume_per_instance = args[:per_instance] || ""
69
+
70
+ _{
71
+ VolumeSpecification volume_spec
72
+ VolumesPerInstance volume_per_instance unless volume_per_instance.empty?
73
+ }
74
+ end
75
+
76
+ def _emr_ebs_volume(args)
77
+ iops = args[:iops] || 300
78
+ size = _ref_string("size", args, "volume size")
79
+ type = _valid_values(args[:type], %w( io1 gp2 sc1 st1 ), "gp2")
80
+
81
+ _{
82
+ Iops iops if type == "io1"
83
+ SizeInGB size
84
+ VolumeType type
85
+ }
86
+ end
87
+
88
+ def _emr_job_flow(args)
89
+ job = args[:job] || {}
90
+
91
+ add_master_security_groups = _ref_array("add_master_security_groups", job, "security group")
92
+ add_slave_security_groups = _ref_array("add_slave_security_groups", job, "security group")
93
+ core_instance_group = _emr_instance_group(job[:core])
94
+ key_name = _ref_string("key_name", job, "key name")
95
+ subnet = _ref_string("subnet", job, "subnet")
96
+ emr_master_security_group = _ref_string("emr_master_security_group", job, "security group")
97
+ emr_slave_security_group = _ref_string("emr_slave_security_group", job, "security group")
98
+ hadoop_version = _valid_values("hadoop", %w( 1.0.3 2.2.0 2.4.0 ))
99
+ master = _emr_instance_group(job[:master])
100
+ placement = job[:placement] || ""
101
+ service_access_security_group = _ref_string("service_access_security_group", job, "security group")
102
+ termination = _bool("termination", job, false)
103
+
104
+ _{
105
+ AdditionalMasterSecurityGroups add_master_security_groups unless add_master_security_groups.empty?
106
+ AdditionalSlaveSecurityGroups add_slave_security_groups unless add_slave_security_groups.empty?
107
+ CoreInstanceGroup core_instance_group
108
+ Ec2KeyName key_name unless key_name.empty?
109
+ Ec2SubnetId subnet unless subnet.empty?
110
+ EmrManagedMasterSecurityGroup emr_master_security_group unless emr_master_security_group.empty?
111
+ EmrManagedSlaveSecurityGroup emr_slave_security_group unless emr_slave_security_group.empty?
112
+ HadoopVersion hadoop_version unless hadoop_version.nil?
113
+ MasterInstanceGroup master
114
+ Placement _{
115
+ AvailabilityZone placement
116
+ } unless placement.empty?
117
+ ServiceAccessSecurityGroup service_access_security_group unless service_access_security_group.empty?
118
+ TerminationProtected termination
119
+ }
120
+ end
121
+
122
+ def _emr_instance_group(args)
123
+ bid = args[:bid] || ""
124
+ configurations = _emr_configurations(args)
125
+ instance_count = args[:instance_count] || 1
126
+ instance_type = _ref_string("instance_type", args, "instance type")
127
+ instance_type = EMR_DEFAULT_INSTANCE_TYPE if instance_type.empty?
128
+ market = _valid_values("market", %w( on_demand, spot), "on_demand")
129
+ name = _ref_name("name", args)
130
+
131
+ _{
132
+ BidPrice bid unless bid.empty?
133
+ Configurations configurations unless configurations.empty?
134
+ InstanceCount instance_count
135
+ InstanceType instance_type
136
+ Market market.upcase
137
+ Name name
138
+ }
139
+ end
140
+
141
+ def _emr_hadoop_jar_step_config(args)
142
+ config_args = args[:args] || []
143
+ jar = args[:jar]
144
+ main_class = args[:main_class] || ""
145
+ step_properties = _emr_step_properties(args)
146
+
147
+ _{
148
+ Args config_args unless config_args.empty?
149
+ Jar jar
150
+ MainClass main_class unless main_class.empty?
151
+ StepProperties step_properties unless step_properties.empty?
152
+ }
153
+ end
154
+
155
+ def _emr_step_properties(args)
156
+ properties = args[:step_properties] || []
157
+
158
+ array = []
159
+ properties.each do |property|
160
+ _{
161
+ Key property[:key]
162
+ Value property[:value]
163
+ }
164
+ end
165
+ array
166
+ end
@@ -0,0 +1,25 @@
1
+ #
2
+ # Helper - Events
3
+ #
4
+ require 'kumogata/template/helper'
5
+
6
+ def _events_pattern(args)
7
+ pattern = args[:pattern] || ""
8
+ return "" if pattern.empty?
9
+
10
+ end
11
+
12
+ def _events_targets(args)
13
+ targets = args[:targets] || []
14
+
15
+ array = []
16
+ targets.each do |target|
17
+ array << _{
18
+ Arn target[:arn]
19
+ Id target[:id]
20
+ Input target[:input] if target.key? :input
21
+ InputPath target[:path] if target.key? :path
22
+ }
23
+ end
24
+ array
25
+ end
@@ -0,0 +1,8 @@
1
+ class Kumogata::ArgumentParser
2
+ Kumogata::ArgumentParser::COMMANDS.merge!(
3
+ init: {
4
+ :description => 'Generate basic template',
5
+ :arguments => [:stack_name],
6
+ }
7
+ )
8
+ end
@@ -0,0 +1,103 @@
1
+ require 'kumogata'
2
+ require 'kumogata/argument_parser'
3
+
4
+ class Kumogata::Client
5
+ def init(stack_name)
6
+ begin
7
+ base_template = ''
8
+ File.open(get_template_path("_template"), "r"){|f|
9
+ base_template = f.read
10
+ }
11
+ raise 'initialize template is empty' if base_template.empty?
12
+
13
+ new_template = "#{stack_name}.rb"
14
+ if File.exists? new_template
15
+ print "#{new_template} is already exists. Are sure want to overwrite? [y/N]: "
16
+ answer = STDIN.gets.to_s.chomp
17
+ return nil if answer.upcase != 'Y'
18
+ end
19
+
20
+ File.open(new_template, "w"){|f|
21
+ template = base_template.gsub('#{NAME}', stack_name)
22
+ f.write(template)
23
+ }
24
+ Kumogata.logger.info("Saved template to #{stack_name}.rb".green)
25
+ rescue => e
26
+ Kumogata.logger.info("Failed to template #{stack_name} - #{e}".red)
27
+ end
28
+ nil
29
+ end
30
+
31
+ def define_template_func(scope, path_or_url)
32
+ functions = ""
33
+
34
+ Dir.glob(File.join(get_template_path, "*.rb")).all? do |file|
35
+ functions << include_func(path_or_url, file)
36
+ functions << "\n\n"
37
+ end
38
+
39
+ scope.instance_eval(<<-EOS)
40
+ #{functions}
41
+
42
+ def _include(file, args = {})
43
+ path = file.dup
44
+
45
+ unless path =~ %r|\\A/| or path =~ %r|\\A\\w+://|
46
+ path = File.expand_path(File.join(File.dirname(#{path_or_url.inspect}), path))
47
+ end
48
+
49
+ open(path) {|f| instance_eval(f.read) }
50
+ end
51
+
52
+ def _path(path, value = nil, &block)
53
+ if block
54
+ value = Dslh::ScopeBlock.nest(binding, 'block')
55
+ end
56
+
57
+ @__hash__[path] = value
58
+ end
59
+
60
+ def _outputs_filter(&block)
61
+ @__hash__[:_outputs_filter] = block
62
+ end
63
+
64
+ def _post(options = {}, &block)
65
+ commands = Dslh::ScopeBlock.nest(binding, 'block')
66
+
67
+ @__hash__[:_post] = {
68
+ :options => options,
69
+ :commands => commands,
70
+ }
71
+ end
72
+ EOS
73
+ end
74
+
75
+
76
+ private ###########################################################
77
+
78
+ def include_func(path_or_url, file)
79
+ <<-EOS
80
+ def _#{get_funcname(file)}(name, args = {})
81
+ args[:name] = name unless args.key? :name
82
+
83
+ path = "#{file}"
84
+
85
+ unless path =~ %r|\\A/| or path =~ %r|\\A\\w+://|
86
+ path = File.expand_path(File.join(File.dirname(#{path_or_url.inspect}), path))
87
+ end
88
+
89
+ open(path) {|f| instance_eval(f.read) }
90
+ end
91
+ EOS
92
+ end
93
+
94
+ def get_funcname(file)
95
+ File.basename(file, '.rb').gsub('-', '_')
96
+ end
97
+
98
+ def get_template_path(file = nil)
99
+ template_path = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..","..", "template"))
100
+ template_path = File.join(template_path, "#{file}.rb") unless file.nil?
101
+ template_path
102
+ end
103
+ end
@@ -0,0 +1,211 @@
1
+ #
2
+ # Helper
3
+ #
4
+ require 'date'
5
+ require 'kumogata/template/const'
6
+
7
+ def _resource_name(name, append = nil)
8
+ name = name.to_s
9
+ name += " #{append}" unless append.nil?
10
+ name.split.map(&:capitalize).join(' ').gsub(/[-_#,.]/, '').delete(' ')
11
+ end
12
+
13
+ def _array(args)
14
+ if args.is_a? String
15
+ [ args ]
16
+ elsif args.is_a? Hash
17
+ args.values
18
+ else
19
+ args
20
+ end
21
+ end
22
+
23
+ def _bool(name, args, default = false)
24
+ args.key?(name.to_sym) ? args[name.to_sym].to_s : default.to_s
25
+ end
26
+
27
+ def _capitalize(name)
28
+ name.split(' ').map(&:capitalize).join()
29
+ end
30
+
31
+ def _valid_values(value, values, default = nil)
32
+ return default if value.nil?
33
+ values.collect{|v| return v if v =~ /^#{value}$/i }
34
+ default
35
+ end
36
+
37
+ def _valid_numbers(value, min = 0, max = 0, default = nil)
38
+ return default if value.nil?
39
+ number = value.to_i
40
+ (min <= number and number <= max) ? number : default
41
+ end
42
+
43
+ def _ref_string(name, args, ref_name = "")
44
+ return args[name.to_sym].to_s || "" unless args.key? "ref_#{name}".to_sym
45
+
46
+ _{ Ref _resource_name(args["ref_#{name}".to_sym].to_s, ref_name) }
47
+ end
48
+
49
+ def _ref_array(name, args, ref_name = "")
50
+ return _array(args[name.to_sym]) || [] unless args.key? "ref_#{name}".to_sym
51
+
52
+ array = []
53
+ if args["ref_#{name}".to_sym].is_a? String
54
+ array << _ref_string(name, args, ref_name)
55
+ else
56
+ args["ref_#{name}".to_sym].collect{|v|
57
+ array << _{ Ref _resource_name(v, ref_name) }
58
+ }
59
+ end
60
+ array
61
+ end
62
+
63
+ def _ref_attr_string(name, attr, args, ref_name = "")
64
+ if args.key? "ref_#{name}".to_sym
65
+ _{
66
+ Fn__GetAtt [ _resource_name(args["ref_#{name}".to_sym], ref_name), attr ]
67
+ }
68
+ elsif args.key? name.to_sym
69
+ args[name.to_sym]
70
+ else
71
+ ""
72
+ end
73
+ end
74
+
75
+ def _ref_name(name, args, ref_name = "")
76
+ return args["raw_#{name}".to_sym] if args.key? "raw_#{name}".to_sym
77
+ name = _ref_string(name, args, ref_name)
78
+ if name.empty?
79
+ _{ Fn__Join "-", [ _{ Ref "Service" }, _{ Ref "Name" } ] }
80
+ elsif name.is_a? Hash
81
+ _{ Fn__Join "-", [ _{ Ref "Service" }, name ] }
82
+ else
83
+ name.gsub(' ', '-')
84
+ end
85
+ end
86
+
87
+ def _attr_string(name, attr, ref_name = "")
88
+ _{ Fn__GetAtt [ _resource_name(name, ref_name), attr ] }
89
+ end
90
+
91
+ def _find_in_map(name, top_level, secondary_level)
92
+ _{ Fn__FindInMap [ name, top_level, secondary_level ] }
93
+ end
94
+
95
+ def _tag(args)
96
+ key = args[:key].to_s || ""
97
+ value = args[:value] || ""
98
+ if key =~ /^ref_.*/
99
+ key.gsub!(/^ref_/, '')
100
+ value = _{ Ref _resource_name(value) }
101
+ end
102
+
103
+ _{
104
+ Key _resource_name(key)
105
+ Value value
106
+ }
107
+ end
108
+
109
+ def _tags(args)
110
+ tags = [
111
+ _{
112
+ Key "Name"
113
+ Value _tag_name(args[:name])
114
+ },
115
+ _{
116
+ Key "Service"
117
+ Value { Ref _resource_name("service") }
118
+ },
119
+ _{
120
+ Key "Version"
121
+ Value { Ref _resource_name("version") }
122
+ },
123
+ ]
124
+ args[:tags_append].collect{|k, v| tags << _tag(key: k, value: v) } if args.key? :tags_append
125
+ tags
126
+ end
127
+
128
+ def _tag_name(name)
129
+ _{
130
+ Fn__Join [ "-", [ _{ Ref _resource_name("service") }, name.to_s.gsub(' ', '-') ] ]
131
+ }
132
+ end
133
+
134
+ def _availability_zone(args, use_subnet = true)
135
+ zone = args.dup
136
+ zone.delete(:ref_subnet) if use_subnet == false
137
+ if zone.key? :ref_subnet
138
+ _ref_attr_string("subnet", "AvailabilityZone", zone, "subnet")
139
+ elsif zone.key? :ref_az
140
+ _ref_string("az", zone)
141
+ elsif zone.key? :az
142
+ zone[:az]
143
+ else
144
+ ""
145
+ end
146
+ end
147
+
148
+ def _availability_zones(args, use_subnet = true)
149
+ zone = args.dup
150
+ if use_subnet == false
151
+ zone.delete(:ref_subnet)
152
+ zone.delete(:ref_subnets)
153
+ end
154
+ if zone.key? :ref_subnet
155
+ [ _attr_string(args[:ref_subnet], "AvailabilityZone", "subnet") ]
156
+ elsif zone.key? :ref_subnets
157
+ zone[:ref_subnets].collect{|v| _ref_attr_string("subnet", "AvailabilityZone", { ref_subnet: v }, "subnet") }
158
+ elsif args.key? :ref_azs
159
+ zone[:ref_azs].collect{|v| _ref_string("azs", { ref_azs: v }, "zone") }
160
+ elsif args.key? :ref_az
161
+ [ _ref_string("az", zone, "zone") ]
162
+ else
163
+ _{ Fn__GetAZs _{ Ref "AWS::Region" } }
164
+ end
165
+ end
166
+
167
+ def _timestamp_utc(year = nil, month = nil, day = nil, hour = 0, min = 0)
168
+ time =
169
+ if year.nil?
170
+ Time.now
171
+ else
172
+ Time.local(year, month, day, hour, min)
173
+ end
174
+ time.utc.strftime("%Y-%m-%dT%H:%M:00Z")
175
+ end
176
+
177
+ def _timestamp_utc_from_string(time)
178
+ time = Time.strptime(time, "%Y-%m-%d %H:%M")
179
+ _timestamp_utc(time.year, time.month, time.day, time.hour, time.min)
180
+ end
181
+
182
+ def _maintenance_window(service, start_time)
183
+ start_time = start_time.utc
184
+ format = "%a:%H:%M"
185
+ case service
186
+ when "elasticache"
187
+ # 60 min
188
+ end_time = start_time + (60 * 60)
189
+ when "rds"
190
+ # 30 min
191
+ end_time = start_time + (60 * 30)
192
+ when "redshift"
193
+ # 30 min
194
+ end_time = start_time + (60 * 30)
195
+ end
196
+ "#{start_time.strftime(format)}-#{end_time.strftime(format)}"
197
+ end
198
+
199
+ def _window_time(service, start_time)
200
+ start_time = start_time.utc
201
+ format = "%H:%M"
202
+ case service
203
+ when "elasticache"
204
+ # 60 min
205
+ end_time = start_time + (60 * 60)
206
+ when "rds"
207
+ # 30 min
208
+ end_time = start_time + (60 * 30)
209
+ end
210
+ "#{start_time.strftime(format)}-#{end_time.strftime(format)}"
211
+ end