-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_stack.rb
226 lines (213 loc) · 6.27 KB
/
create_stack.rb
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# gem install aws-sdk --pre
require "aws-sdk"
require "aws-sdk-v1"
require "getoptlong"
require "json"
options = {
asg_max_size: 4,
asg_min_size: 2,
availability_zones: ["us-west-1a"],
cidr_block: "10.1.0.0/16",
environment: "dev",
instance_ami_id: "ami-076e6542",
instance_count: 2,
key_name: "aws_omb_us_west_1",
nat_ami_id: "ami-2b2b296e",
peer_vpc_cidr_blocks: nil,
peer_vpc_ids: nil,
regions: ["us-west-1"],
version: nil
}
opts = GetoptLong.new(
["--asg_max_size", GetoptLong::OPTIONAL_ARGUMENT],
["--asg_min_size", GetoptLong::OPTIONAL_ARGUMENT],
["--availability_zones", GetoptLong::OPTIONAL_ARGUMENT],
["--cidr_block", GetoptLong::OPTIONAL_ARGUMENT],
["--environment", GetoptLong::OPTIONAL_ARGUMENT],
["--instance_ami_id", GetoptLong::OPTIONAL_ARGUMENT],
["--instance_count", GetoptLong::OPTIONAL_ARGUMENT],
["--key_name", GetoptLong::OPTIONAL_ARGUMENT],
["--nat_ami_id", GetoptLong::OPTIONAL_ARGUMENT],
["--peer_vpc_cidr_blocks", GetoptLong::OPTIONAL_ARGUMENT],
["--peer_vpc_ids", GetoptLong::OPTIONAL_ARGUMENT],
["--regions", GetoptLong::OPTIONAL_ARGUMENT],
["--version", GetoptLong::REQUIRED_ARGUMENT]
)
opts.each do |opt, arg|
case opt
when "--asg_max_size"
options[:asg_max_size] = arg
when "--asg_min_size"
options[:asg_min_size] = arg
when "--availability_zones"
options[:availability_zones] = arg.split(",")
when "--cidr_block"
options[:cidr_block] = arg
when "--environment"
options[:environment] = arg
when "--instance_ami_id"
options[:instance_ami_id] = arg
when "--instance_count"
options[:instance_count] = arg
when "--key_name"
options[:key_name] = arg
when "--nat_ami_id"
options[:nat_ami_id] = arg
when "--peer_vpc_cidr_blocks"
options[:peer_vpc_cidr_blocks] = arg.split(",")
when "--peer_vpc_ids"
options[:peer_vpc_ids] = arg.split(",")
when "--regions"
options[:regions] = arg.split(",")
when "--version"
options[:version] = arg
end
end
if options[:peer_vpc_cidr_blocks].nil? && options[:peer_vpc_ids].nil?
options[:peer_vpc_cidr_blocks] = ["0"]
options[:peer_vpc_ids] = ["0"]
elsif options[:peer_vpc_cidr_blocks].nil? || options[:peer_vpc_ids].nil?
if options[:peer_vpc_cidr_blocks].nil?
puts "Missing argument: peer_vpc_cidr_blocks"
else
puts "Missing argument: peer_vpc_ids"
end
exit 0
end
if options[:version].nil?
puts "Missing argument: version"
exit 0
end
if options[:asg_max_size].to_i < options[:asg_min_size].to_i
puts "Invalid arguments: asg_max_size cannot be larger than asg_min_size"
exit 0
end
puts "-" * 60
puts "Parameters"
puts "-" * 30
options.each do |key, value|
puts "#{key}: #{value}"
end
# Functions
def string_from_json_file(file_name)
folder = "dev"
name = "#{folder}/#{file_name}.json"
if File.exists? name
file = File.open name
data = file.read
file.close
data.to_str
else
""
end
end
# Load all other JSON files into 1 JSON template
template_body = string_from_json_file "template"
["parameters", "mappings", "conditions", "resources", "outputs"].each do |key|
template_body.gsub! Regexp.new("{{#{key}}}"), string_from_json_file(key)
end
version_string = options[:version].split(".").join("-")
# Insert all variables into JSON string
[
{
key: "description",
value: "\"OnMyBlock #{options[:environment].capitalize} Stack\""
}
].each do |hash|
template_body.gsub! Regexp.new("{{#{hash[:key]}}}"), hash[:value]
end
# AWS Credentials used for instantiating AWS client and resources
credentials = Aws::Credentials.new(
ENV["AWS_ACCESS_KEY_ID"],
ENV["AWS_SECRET_ACCESS_KEY"]
)
# Loop through all regions and create stack for the region
options[:regions].each do |region|
# CloudFormation Client
cloudformation_client = Aws::CloudFormation::Client.new(
credentials: credentials,
region: region
)
# CloudFormation Resource
cloudformation_resource = Aws::CloudFormation::Resource.new(
client: cloudformation_client
)
begin
cloudformation_client.validate_template(template_body: template_body)
stack_name = "stack-#{options[:environment]}-#{version_string}"
stack = cloudformation_resource.create_stack(
parameters: [
{
parameter_key: "AutoScalingGroupMaxSize",
parameter_value: options[:asg_max_size].to_s
},
{
parameter_key: "AutoScalingGroupMinSize",
parameter_value: options[:asg_min_size].to_s
},
{
parameter_key: "AvailabilityZones",
parameter_value: options[:availability_zones].join(",")
},
{
parameter_key: "Environment",
parameter_value: options[:environment]
},
{
parameter_key: "InstanceAmiId",
parameter_value: options[:instance_ami_id]
},
{
parameter_key: "InstanceCount",
parameter_value: options[:instance_count].to_s
},
{
parameter_key: "KeyName",
parameter_value: options[:key_name]
},
{
parameter_key: "NatAmiId",
parameter_value: options[:nat_ami_id]
},
{
parameter_key: "PeerVpcCidrBlocks",
parameter_value: options[:peer_vpc_cidr_blocks].join(",")
},
{
parameter_key: "PeerVpcIds",
parameter_value: options[:peer_vpc_ids].join(",")
},
{
parameter_key: "Version",
parameter_value: options[:version]
},
{
parameter_key: "VersionString",
parameter_value: version_string
},
{
parameter_key: "VpcCidrBlock",
parameter_value: options[:cidr_block]
}
],
stack_name: stack_name,
tags: [
{
key: "Environment",
value: options[:environment]
},
{
key: "Version",
value: options[:version]
}
],
template_body: template_body
)
puts "-" * 30
puts "Stack: #{stack.name}"
rescue Exception => e
puts "Error: #{e}"
end
# Find VPC with particular tags
# ec2_resource.vpcs.to_a.select { |v| v.tags.to_a.detect { |tag| tag.key == "version" && tag.value == "0.0.0" } }.size
end