Class: CloudBuilder::StackCommand

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/cloud_builder/cli.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



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
# File 'lib/cloud_builder/cli.rb', line 21

def execute
  template = CloudBuilder::Stack.new(stack)
  
  if dummy?
    template.resource "dummy" do
      type "AWS::CloudFormation::WaitConditionHandle"
    end
  end
  
  template = template.to_json + "\n"

  cf = AWS::CloudFormation.new(:cloud_formation_endpoint => 'cloudformation.%s.amazonaws.com' % region)

  s3 = AWS::S3.new

  key = File.basename(stack, '.*')

  if validate?
    result = cf.validate_template(template)
    if result.has_key?(:code)
      raise Exception, "Could not validate!\ncode=#{result[:code]}\nreason=#{result[:reason]}\n"
    end
  end

  if diff?
    remote_template = cf.stacks[key].template
    
    # b = s3.buckets[bucket]
    # o = b.objects[key]
    # remote_template = o.read
    
    t1 = Tempfile.new('stack')
    t1.write(remote_template)
    t1.close

    t2 = Tempfile.new('stack')
    t2.write(template)
    t2.close

    cmd = "%s %s %s" % [diff_tool ? diff_tool : "git diff --color", t1.path, t2.path]
    # puts cmd
    puts `#{cmd}`
    return
  end

  if bucket
    b = s3.buckets[bucket]
    o = b.objects[key]
    o.write template
    template = o.public_url
  else
    puts template
  end

  if estimate?
    puts cf.estimate_template_cost(template, :capabilities => ["CAPABILITY_IAM"] )
    return
  end

  if create?
    cf.stacks.create(key, template, :capabilities => ["CAPABILITY_IAM"] )
  elsif update?
    cf.stacks[key].update(:template => template, :capabilities => ["CAPABILITY_IAM"])
  end

end