Class: Kumogata2::Client

Inherits:
Object
  • Object
show all
Includes:
Logger::Helper
Defined in:
lib/kumogata2/client.rb

Instance Method Summary collapse

Methods included from Logger::Helper

log

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



4
5
6
7
8
9
# File 'lib/kumogata2/client.rb', line 4

def initialize(options)
  @options = options.kind_of?(Hashie::Mash) ? options : Hashie::Mash.new(options)
  @client = Aws::CloudFormation::Client.new
  @resource = Aws::CloudFormation::Resource.new(client: @client)
  @plugin_by_ext = {}
end

Instance Method Details

#convert(path_or_url) ⇒ Object



73
74
75
76
# File 'lib/kumogata2/client.rb', line 73

def convert(path_or_url)
  template = open_template(path_or_url)
  convert0(template)
end

#create(path_or_url, stack_name = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kumogata2/client.rb', line 18

def create(path_or_url, stack_name = nil)
  stack_name = normalize_stack_name(stack_name)
  validate_stack_name(stack_name) if stack_name
  template = open_template(path_or_url)
  update_deletion_policy(template, delete_stack: !stack_name)

  outputs = create_stack(template, stack_name)

  unless @options.detach?
    post_process(path_or_url, outputs)
  end
end

#delete(stack_name) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/kumogata2/client.rb', line 44

def delete(stack_name)
  stack_name = normalize_stack_name(stack_name)
  validate_stack_name(stack_name)
  @resource.stack(stack_name).stack_status

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

#describe(stack_name) ⇒ Object



11
12
13
14
15
16
# File 'lib/kumogata2/client.rb', line 11

def describe(stack_name)
  stack_name = normalize_stack_name(stack_name)
  validate_stack_name(stack_name)
  stack = describe_stack(stack_name)
  JSON.pretty_generate(stack).colorize_as(:json)
end

#diff(path_or_url1, path_or_url2) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/kumogata2/client.rb', line 78

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

    template = Kumogata2::Utils.stringify(template)
    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

#dry_run(path_or_url, stack_name = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/kumogata2/client.rb', line 102

def dry_run(path_or_url, stack_name = nil)
  stack_name = normalize_stack_name(stack_name)
  validate_stack_name(stack_name) if stack_name
  template = open_template(path_or_url)
  update_deletion_policy(template, delete_stack: !stack_name)
  changes = show_change_set(template, stack_name)
  changes = JSON.pretty_generate(changes).colorize_as(:json) if changes
  changes
end

#export(stack_name) ⇒ Object



66
67
68
69
70
71
# File 'lib/kumogata2/client.rb', line 66

def export(stack_name)
  stack_name = normalize_stack_name(stack_name)
  validate_stack_name(stack_name)
  template = export_template(stack_name)
  convert0(template)
end

#list(stack_name = nil) ⇒ Object



59
60
61
62
63
64
# File 'lib/kumogata2/client.rb', line 59

def list(stack_name = nil)
  stack_name = normalize_stack_name(stack_name)
  validate_stack_name(stack_name) if stack_name
  stacks = describe_stacks(stack_name)
  JSON.pretty_generate(stacks).colorize_as(:json)
end

#show_events(stack_name) ⇒ Object



112
113
114
115
116
117
# File 'lib/kumogata2/client.rb', line 112

def show_events(stack_name)
  stack_name = normalize_stack_name(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



119
120
121
122
123
124
# File 'lib/kumogata2/client.rb', line 119

def show_outputs(stack_name)
  stack_name = normalize_stack_name(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



126
127
128
129
130
131
# File 'lib/kumogata2/client.rb', line 126

def show_resources(stack_name)
  stack_name = normalize_stack_name(stack_name)
  validate_stack_name(stack_name)
  resources = describe_resources(stack_name)
  JSON.pretty_generate(resources).colorize_as(:json)
end

#template_summary(path_or_url) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/kumogata2/client.rb', line 133

def template_summary(path_or_url)
  params = {}

  if path_or_url =~ %r|\Astack://(.*)|
    stack_name = $1 || ''
    validate_stack_name(stack_name)
    params[:stack_name] = stack_name
  else
    template = open_template(path_or_url)
    params[:template_body] = JSON.pretty_generate(template)
  end

  summary = describe_template_summary(params)
  JSON.pretty_generate(summary).colorize_as(:json)
end

#update(path_or_url, stack_name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kumogata2/client.rb', line 31

def update(path_or_url, stack_name)
  stack_name = normalize_stack_name(stack_name)
  validate_stack_name(stack_name)
  template = open_template(path_or_url)
  update_deletion_policy(template, update_metadate: true)

  outputs = update_stack(template, stack_name)

  unless @options.detach?
    post_process(path_or_url, outputs)
  end
end

#validate(path_or_url) ⇒ Object



54
55
56
57
# File 'lib/kumogata2/client.rb', line 54

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