class Panoramix::Plugin::CloudFormation
Attributes
dst[R]
owner[R]
params[R]
src[R]
stage[R]
version[R]
Public Class Methods
new(dst, src, parameters, stage, owner, version)
click to toggle source
# File lib/panoramix/plugin/cfn.rb, line 20 def initialize(dst, src, parameters, stage, owner, version) @dst = dst @src = src @stage = stage @owner = owner @version = version @params = TOML.load_file(parameters) end
Public Instance Methods
ask_pro()
click to toggle source
# File lib/panoramix/plugin/cfn.rb, line 40 def ask_pro choose do |menu| menu.prompt = "Would you like to delete the stack #{gen_stack_name}? " menu.choice(:No) do say("Don't do it again!") return false end menu.choices(:Yes) do resp = ask("Warning, this action can not be reversed. Do you want to continue? (Yes/No)".red, String) do |q| q.default = "No" end return resp == "Yes" end end end
created?()
click to toggle source
Has this stacks already been created
# File lib/panoramix/plugin/cfn.rb, line 82 def created? # Get a list af all created Stacks query = shell("aws cloudformation describe-stacks --query 'Stacks[*].{StackName:StackName, CreationTime:CreationTime}'", true)[:out] parsed_stacks = JSON.parse query # Check wether the stacks has already been created info = parsed_stacks.select { |s| s["StackName"] == gen_stack_name } @created = info.empty? ? nil: info.first end
delete()
click to toggle source
Action delete for this task
# File lib/panoramix/plugin/cfn.rb, line 93 def delete return unless created? return unless question cmd = "aws cloudformation delete-stack \ --stack-name #{gen_stack_name}" shell(cmd) # Wait until the stack is being deleted loop do break if !created? end end
fill_env(path)
click to toggle source
Match every parameter with an environment variable
# File lib/panoramix/plugin/cfn.rb, line 114 def fill_env path file = File.read path json = JSON.parse file env = Hash.new json["Parameters"].each do |k,v| env[k] = @params["default"][k] ? @params["default"][k] : nil env[k] = @params[@stage][k] ? @params[@stage][k] : env[k] env[k] = ENV[k] ? ENV[k] : env[k] raise "Parameter or environment variable #{k} not defined" unless env[k] end env end
gen_stack_name()
click to toggle source
Final Cloud Formation stack name
# File lib/panoramix/plugin/cfn.rb, line 31 def gen_stack_name @version ? "#{@stage}-#{@dst}-#{@version}-#{owner}" : "#{@stage}-#{@dst}-#{owner}" end
needed?(timestamps)
click to toggle source
When this instance needs to be executed
# File lib/panoramix/plugin/cfn.rb, line 108 def needed? timestamps this_time = timestamp timestamps.any? { |t| t > this_time } end
ps()
click to toggle source
# File lib/panoramix/plugin/cfn.rb, line 174 def ps if created? shell("aws cloudformation describe-stacks --stack-name #{gen_stack_name}") end end
question()
click to toggle source
# File lib/panoramix/plugin/cfn.rb, line 57 def question case @stage when "pro" return true if ENV['CFN_FORCE_DELETE'] return ask_pro when "pre" return true if ENV['CFN_FORCE_DELETE'] return ask_pro when "test" return true when "dev" return true else return false end end
run_default()
click to toggle source
Deafult task
# File lib/panoramix/plugin/cfn.rb, line 148 def run_default if created? puts "WARNING: #{gen_stack_name} already exists!".red return end env = fill_env @src params = "" env.each {|k,v| params = params + "ParameterKey=#{k},ParameterValue=#{v} "} validate_file @src template = "file://#{@src}" cmd = "aws cloudformation create-stack \ --query StackId \ --stack-name #{gen_stack_name} \ --template-body #{template} \ --disable-rollback \ --parameters #{params}" result = shell(cmd, true)[:out] stack_id = result.strip wait_for_creation stack_id end
stack_name()
click to toggle source
Return stack name
# File lib/panoramix/plugin/cfn.rb, line 36 def stack_name puts gen_stack_name end
timestamp()
click to toggle source
Return the stack creation time
# File lib/panoramix/plugin/cfn.rb, line 75 def timestamp return Time.at(0) unless @created return Time.parse(@created["CreationTime"]) end
validate_file(path)
click to toggle source
Check wether the file exists
# File lib/panoramix/plugin/cfn.rb, line 139 def validate_file path # Raise error if the file does not exist unless File.exists? path message = I18n.t('errors.cfn.template_not_found', {:path => path}) raise(Panoramix::Plugin::DockerUpExceptionError, message) end end
wait_for_creation(stack_id)
click to toggle source
Wait until the stack is being created
# File lib/panoramix/plugin/cfn.rb, line 129 def wait_for_creation stack_id loop do status = shell("aws cloudformation describe-stacks --stack #{stack_id} --query Stacks[0].StackStatus --output text", true)[:out].strip raise "Creation Failed" if status == "CREATE_FAILED" break if status != "CREATE_IN_PROGRESS" sleep 30 end end