Class: Comodule::Deployment::Helper::Aws::CloudFormation::Service

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/comodule/deployment/helper/aws/cloud_formation.rb

Instance Method Summary collapse

Methods included from Base

included

Instance Method Details

#cfnObject



16
17
18
# File 'lib/comodule/deployment/helper/aws/cloud_formation.rb', line 16

def cfn
  @cfn ||= aws.cloud_formation
end

#cloud_formation_templateObject



135
136
137
138
139
140
141
142
143
144
# File 'lib/comodule/deployment/helper/aws/cloud_formation.rb', line 135

def cloud_formation_template
  if block_given?
    yield config
  end

  file = File.join(owner.cloud_formation_dir, 'template.json.erb')
  common_file = File.join(owner.common_cloud_formation_dir, 'template.json.erb')

  owner.render( File.file?(file) ? file : common_file )
end

#create_stack(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/comodule/deployment/helper/aws/cloud_formation.rb', line 36

def create_stack(&block)
  if config.upload_secret_files
    puts 'Upload secret files'
    owner.upload_secret_files
  end

  if config.upload_project
    puts 'Upload project'
    owner.upload_project
  end

  stack_name = [stack_basename, Time.now.strftime("%Y%m%d")].join(?-)

  template = validate_template(&block)

  stack = cfn.stacks.create(stack_name, template)

  puts "Progress of creation stack: #{stack.name}"

  status = stack_status_watch(stack)

  puts "\n!!! #{status} !!!\n"
end

#delete_stackObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/comodule/deployment/helper/aws/cloud_formation.rb', line 60

def delete_stack
  stack = latest_stack

  if !stack || !stack.exists?
    puts "Stack:/#{stack_basename}-[0-9]*/ is not found.\n"
    exit
  end

  print "You are going to delete stack #{stack.name}. Are you sure? [N/y] "
  confirm = STDIN.gets
  unless confirm =~ /^y(es)?$/
    puts "\nAbort!\n"
    exit
  end

  stack.delete

  puts "Progress of deletion stack: #{stack.name}"

  status = stack_status_watch(stack)

  puts "\n!!! #{status} !!!\n"
end

#latest_stackObject



31
32
33
34
# File 'lib/comodule/deployment/helper/aws/cloud_formation.rb', line 31

def latest_stack
  filter = -> stack { stack.name.match(/[0-9]*$/)[0].to_i }
  own_stacks.max { |a,b| filter[a] <=> filter[b] }
end

#own_stacksObject



27
28
29
# File 'lib/comodule/deployment/helper/aws/cloud_formation.rb', line 27

def own_stacks
  cfn.stacks.find_all { |stack| stack.name =~ /#{stack_basename}/ }
end

#stack_basenameObject



20
21
22
23
24
25
# File 'lib/comodule/deployment/helper/aws/cloud_formation.rb', line 20

def stack_basename
  stack_name = []
  stack_name << (config.stack_name_prefix || owner.project_name)
  stack_name << owner.name
  stack_name.join(?-)
end

#stack_status_watch(stack, interval = 10) ⇒ Object



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
# File 'lib/comodule/deployment/helper/aws/cloud_formation.rb', line 84

def stack_status_watch(stack, interval=10)
  begin
    status = stack.status
  rescue
    return 'Missing stack'
  end

  first_status = status
  before_status = ""

  while status == first_status
    if status == before_status
      before_status, status = status, ?.
    else
      before_status = status
    end

    print status

    sleep interval

    begin
      status = stack.status
    rescue
      status = "Missing stack"
      break
    end
  end

  status
end

#validate_template(&block) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/comodule/deployment/helper/aws/cloud_formation.rb', line 116

def validate_template(&block)
  template = cloud_formation_template(&block)

  template_path = File.join(owner.test_cloud_formation_dir, 'template.json')

  File.open(template_path, 'w') do |file|
    file.write template
  end

  result = cfn.validate_template(template)

  puts "Validation result:"
  result.each do |key, msg|
    puts "  #{key}: #{msg}"
  end

  template
end