Top Level Namespace
Defined Under Namespace
Modules: Clouds
Classes: Hash
Instance Method Summary
collapse
Instance Method Details
#clone_stack(stack, new_stack, force = false, commit = false) ⇒ Object
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/clouds.rb', line 216
def clone_stack(stack, new_stack, force=false, commit=false)
if File.exist?(get_stack_directory(new_stack)) and force == false
raise 'The stack already exists, use --force to override'
elsif force == true
FileUtils.rm_rf(get_stack_directory(new_stack))
end
FileUtils.cp_r(get_stack_directory(stack), get_stack_directory(new_stack))
if commit
puts 'Committing changes to AWS'
update_stack(new_stack, true)
else
puts 'Only copied the stack template and parameters locally, use the --commit flag in order to do commit the changes to AWS'
end
end
|
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
|
# File 'lib/clouds.rb', line 23
def configure(profile=nil)
if profile.nil? && ENV['AWS_ACCESS_KEY_ID'] && ENV['AWS_SECRET_ACCESS_KEY']
@aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
@aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
@aws_session_token = ENV['AWS_SECURITY_TOKEN']
@region = ENV['AWS_DEFAULT_REGION'] || 'us-east-1'
else
profile = profile || ENV['AWS_DEFAULT_PROFILE']
aws_config_file = File.join(File.expand_path('~'), '.aws', 'config')
if profile.nil?
profile = "default"
puts "Using the default profile"
else
profile = "profile #{profile}"
puts "Using the profile '#{profile}'"
end
aws_config = IniFile.load(aws_config_file)[profile]
@aws_access_key_id = aws_config['aws_access_key_id']
@aws_secret_access_key = aws_config['aws_secret_access_key']
@region = aws_config['region']
end
proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
AWS.config({:access_key_id => @aws_access_key_id,
:secret_access_key => @aws_secret_access_key,
:session_token => @aws_session_token,
:region => @region,
:proxy_uri => proxy,
})
@cfn = AWS::CloudFormation.new
end
|
#create_stack_directory(stack_name) ⇒ Object
80
81
82
|
# File 'lib/clouds.rb', line 80
def create_stack_directory(stack_name)
FileUtils.mkdir_p(get_stack_directory(stack_name))
end
|
#delete_stack(stack_name) ⇒ Object
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/clouds.rb', line 231
def delete_stack(stack_name)
configure()
FileUtils.rm_rf(get_stack_directory(stack_name))
if @cfn.stacks[stack_name].exists?
puts "Deleting stack #{stack_name}"
@cfn.stacks[stack_name].delete
else
puts "Stack #{stack_name} does not exist, nothing to delete here..."
end
end
|
#dump_all_stacks(force = false) ⇒ Object
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/clouds.rb', line 146
def dump_all_stacks(force=false)
configure()
begin
@cfn.stacks.each do |stack|
dump_stacks([stack.name],force)
end
rescue => e
puts e
end
end
|
#dump_stacks(stack_list, force = false) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/clouds.rb', line 128
def dump_stacks(stack_list, force=false)
stack_list.each do |stack_name|
configure()
begin
stack = @cfn.stacks[stack_name]
puts "Dumping stack #{stack.name}"
template_content = stack.template
parameters = stack.parameters
create_stack_directory(stack_name)
write_file(get_template_path(stack_name), template_content, force)
write_file(get_parameters_path(stack_name), parameters.to_yaml, force)
rescue => e
puts "dump failed: #{e}"
end
end
end
|
#get_parameters_path(stack_name) ⇒ Object
89
90
91
|
# File 'lib/clouds.rb', line 89
def get_parameters_path(stack_name)
File.join(get_stack_directory(stack_name), 'parameters.yaml')
end
|
#get_stack_directory(stack_name) ⇒ Object
76
77
78
|
# File 'lib/clouds.rb', line 76
def get_stack_directory(stack_name)
File.join(File.expand_path('.'),'stacks', stack_name)
end
|
#get_template_path(stack_name) ⇒ Object
85
86
87
|
# File 'lib/clouds.rb', line 85
def get_template_path(stack_name)
File.join(get_stack_directory(stack_name), 'template.json')
end
|
#list_local_stacks ⇒ Object
118
119
120
121
122
123
124
125
126
|
# File 'lib/clouds.rb', line 118
def list_local_stacks()
list = []
return [] unless File.directory?('stacks')
Dir.foreach('stacks') do |item|
next if item == '.' or item == '..'
list << item
end
list
end
|
#list_stacks ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/clouds.rb', line 93
def list_stacks()
configure()
max_stack_length = 0
stacks = Hash.new()
local_stacks = list_local_stacks()
local_stacks.each do |s|
max_stack_length = s.length if s.length > max_stack_length
stacks[s] = 'LOCAL-ONLY'
end
begin
@cfn.stacks.each do |stack|
stacks[stack.name] = stack.status
max_stack_length = stack.name.length if stack.name.length > max_stack_length
end
rescue => e
puts e
end
puts 'Stack list and stack status:'
stacks.keys.sort.each do |key|
printf("%#{max_stack_length}s %s\n",key, stacks[key])
end
end
|
#read_file(file_name) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/clouds.rb', line 68
def read_file(file_name)
begin
File.open(file_name,'rb').read
rescue => e
puts e
end
end
|
#update_all_stacks ⇒ Object
208
209
210
211
212
213
|
# File 'lib/clouds.rb', line 208
def update_all_stacks()
Dir.foreach('stacks') do |stack|
next if item == '.' or item == '..'
update_stack(stack)
end
end
|
#update_stack(stack_name, create_if_missing = false) ⇒ Object
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
|
# File 'lib/clouds.rb', line 157
def update_stack(stack_name, create_if_missing=false)
configure()
stack=nil
template_content = read_file(get_template_path(stack_name))
parameters_content = read_file(get_parameters_path(stack_name))
begin
parameters_hash = YAML.load(parameters_content)
rescue => e
puts e
raise e
end
raise 'Empty stack template' if template_content.nil? || template_content.empty?
template_validation = @cfn.validate_template(template_content)
raise template_validation[:message] unless template_validation[:message].nil?
begin
if @cfn.stacks[stack_name].exists?
puts "Updating stack #{stack_name}"
stack = @cfn.stacks[stack_name]
stack_capabilities = stack.capabilities
stack.update(:template => template_content,
:parameters => parameters_hash,
:capabilities => stack_capabilities)
elsif create_if_missing
puts "Creating stack #{stack_name}"
stack = @cfn.stacks.create(stack_name,
template_content,
{ :parameters => parameters_hash,
:capabilities => ['CAPABILITY_IAM']})
else
puts "Skipping stack #{stack_name} since it's not defined in this AWS account, if the stack exists locally you might use the -c flag"
end
unless stack.nil?
estimated_cost = stack.estimate_template_cost()
puts "Estimated costs for the stack #{stack_name} is #{estimated_cost}"
end
rescue => e
puts e
end
end
|
#update_stacks(stack_list, create_if_missing = false) ⇒ Object
202
203
204
205
206
|
# File 'lib/clouds.rb', line 202
def update_stacks(stack_list, create_if_missing=false)
stack_list.each do |stack|
update_stack(stack, create_if_missing)
end
end
|
#write_file(file_name, content, force) ⇒ Object
59
60
61
62
63
64
65
66
|
# File 'lib/clouds.rb', line 59
def write_file(file_name, content, force)
puts "Creating #{file_name}"
if File.exist?(file_name) and force == false
raise 'The file already exists, use --force to override'
end
File.open(file_name, 'w') {|f| f.write(content)}
puts "Created #{file_name}"
end
|