Class: Kumogata::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kumogata/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



2
3
4
5
6
7
8
# File 'lib/kumogata/client.rb', line 2

def initialize(options)
  @options = options
  @options = Hashie::Mash.new(@options) unless @options.kind_of?(Hashie::Mash)
  @cloud_formation = AWS::CloudFormation.new
  @outputs_filter = Kumogata::OutputsFilter.new(@options)
  @post_processing = Kumogata::PostProcessing.new(@options)
end

Instance Method Details

#convert(path_or_url) ⇒ Object



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
# File 'lib/kumogata/client.rb', line 34

def convert(path_or_url)
  template = open_template(path_or_url)
  output_format = @options.output_format

  unless output_format
    output_format = case @options.format || guess_format(path_or_url)
                    when :ruby then :json
                    when :json then :ruby
                    when :yaml then :json
                    when :js then :json
                    when :json5 then :json
                    end
  end

  case output_format
  when :ruby
    devaluate_template(template).chomp.colorize_as(:ruby)
  when :json, :json5
    JSON.pretty_generate(template).colorize_as(:json)
  when :yaml
    YAML.dump(template).colorize_as(:yaml)
  when :js
    '(' + JSON.pretty_generate(template).colorize_as(:json) + ')'
  when :coffee
    raise 'Output to CoffeeScript is not implemented'
  end
end

#create(path_or_url, stack_name = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/kumogata/client.rb', line 10

def create(path_or_url, stack_name = nil)
  validate_stack_name(stack_name)

  @options.delete_stack = false if stack_name
  template = open_template(path_or_url)
  update_deletion_policy(template)
  add_encryption_password(template)

  outputs = create_stack(template, stack_name)

  unless @options.detach?
    @outputs_filter.filter!(outputs)
    @post_processing.run(:create, outputs)
    outputs
  end
end

#delete(stack_name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kumogata/client.rb', line 79

def delete(stack_name)
  validate_stack_name(stack_name)

  if @options.force? or agree("Are you sure you want to delete `#{stack_name}`? ".yellow)
    delete_stack(stack_name)
  end

  unless @options.detach?
    true
  end
end

#diff(path_or_url1, path_or_url2) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/kumogata/client.rb', line 135

def diff(path_or_url1, path_or_url2)
  templates = [path_or_url1, path_or_url2].map do |path_or_url|
    template = nil

    if path_or_url =~ %r|\Astack://(.*)|
      stack_name = $1 || ''
      validate_stack_name(stack_name)
      template = export_template(stack_name)
    else
      template = open_template(path_or_url)
    end

    JSON.pretty_generate(template)
  end

  diff_opts = @options.ignore_all_space? ? '-uw' : '-u'
  opts = {:include_diff_info => true, :diff => diff_opts}
  diff = Diffy::Diff.new(*templates, opts).to_s

  diff.sub(/^(\e\[\d+m)?\-\-\-(\s+)(\S+)/m) { "#{$1}---#{$2}#{path_or_url1}"}
      .sub(/^(\e\[\d+m)?\+\+\+(\s+)(\S+)/m) { "#{$1}+++#{$2}#{path_or_url2}"}
end

#export(stack_name) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kumogata/client.rb', line 98

def export(stack_name)
  validate_stack_name(stack_name)

  template = export_template(stack_name)
  format = @options.format || :ruby

  case format
  when :ruby
    devaluate_template(template).chomp.colorize_as(:ruby)
  when :json
    JSON.pretty_generate(template).colorize_as(:json)
  else
    raise "Unknown format: #{format}"
  end
end

#list(stack_name = nil) ⇒ Object



91
92
93
94
95
96
# File 'lib/kumogata/client.rb', line 91

def list(stack_name = nil)
  validate_stack_name(stack_name)

  stacks = describe_stacks(stack_name)
  JSON.pretty_generate(stacks).colorize_as(:json)
end

#show_events(stack_name) ⇒ Object



114
115
116
117
118
119
# File 'lib/kumogata/client.rb', line 114

def show_events(stack_name)
  validate_stack_name(stack_name)

  events = describe_events(stack_name)
  JSON.pretty_generate(events).colorize_as(:json)
end

#show_outputs(stack_name) ⇒ Object



121
122
123
124
125
126
# File 'lib/kumogata/client.rb', line 121

def show_outputs(stack_name)
  validate_stack_name(stack_name)

  outputs = describe_outputs(stack_name)
  JSON.pretty_generate(outputs).colorize_as(:json)
end

#show_resources(stack_name) ⇒ Object



128
129
130
131
132
133
# File 'lib/kumogata/client.rb', line 128

def show_resources(stack_name)
  validate_stack_name(stack_name)

  resources = describe_resources(stack_name)
  JSON.pretty_generate(resources).colorize_as(:json)
end

#update(path_or_url, stack_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kumogata/client.rb', line 62

def update(path_or_url, stack_name)
  validate_stack_name(stack_name)

  @options.delete_stack = false
  template = open_template(path_or_url)
  update_deletion_policy(template, :update_metadate => true)
  add_encryption_password(template)

  outputs = update_stack(template, stack_name)

  unless @options.detach?
    @outputs_filter.filter!(outputs)
    @post_processing.run(:update, outputs)
    outputs
  end
end

#validate(path_or_url) ⇒ Object



27
28
29
30
31
32
# File 'lib/kumogata/client.rb', line 27

def validate(path_or_url)
  template = open_template(path_or_url)
  add_encryption_password_for_validation(template)
  validate_template(template)
  true
end