Class: Stackr::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/stackr/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



45
46
47
# File 'lib/stackr/cli.rb', line 45

def self.source_root
  File.join(File.dirname(__FILE__),'..','..')
end

Instance Method Details

#create_project(name) ⇒ Object



50
51
52
53
54
55
# File 'lib/stackr/cli.rb', line 50

def create_project(name)
  @project_name = name

  say "Creating stackr project: #{name}\n"
  directory 'templates/project', name
end

#create_stack(template_name) ⇒ Object

TODO: log parameters and their values



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/stackr/cli.rb', line 115

def create_stack(template_name)
  return if !validate_template(template_name)

  template = load_template(template_name)
  if !template
    say "There is no template named \'#{template_name}\'."
    return
  end

  name = options[:name] || template.name
  launcher = Stackr::CloudFormation.new
  say "Creating CloudFormation stack #{name} from template #{template.name}\n"
  begin
    launcher.create_stack(template, options)
  rescue Stackr::StackAlreadyExistsError => e
    say e.message
  end
end

#create_template(name) ⇒ Object



59
60
61
62
63
# File 'lib/stackr/cli.rb', line 59

def create_template(name)
  @template_name = name
  say "Creating template generator #{name}\n"
  template 'templates/generator.rb.tt', File.join(templates_path, "#{name}.rb")
end

#delete_stack(stack_name) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/stackr/cli.rb', line 166

def delete_stack(stack_name)
  say "Deleting CloudFormation stack #{stack_name}\n"
  launcher = Stackr::CloudFormation.new
  begin
    launcher.delete_stack(stack_name)
  rescue Stackr::StackMissingError => e
    say e.message
  end
end

#generate_template(template_name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/stackr/cli.rb', line 67

def generate_template(template_name)
  template = load_template(template_name)
  if !template
    say "There is no template named \'#{template_name}\'."
    return
  end

  json_file = json_template_path(template_name)

  say "Writing #{template_name} to #{json_file}\n"
  File.open(json_file, 'w') do |f|
    f.write(template.body)
  end
end

#list_stacksObject



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/stackr/cli.rb', line 178

def list_stacks
  launcher = Stackr::CloudFormation.new
  rows = []
  launcher.list_stacks.each do |stack|
    rows << [
      stack.name,
      stack.stack_status,
      stack.creation_time ? stack.creation_time.iso8601 : '',
      stack.last_updated_time ? stack.last_updated_time.iso8601 : ''
    ]
  end
  print_table rows
end

#update_stack(template_name) ⇒ Object

TODO: log parameters and their values



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/stackr/cli.rb', line 141

def update_stack(template_name)
  return if !validate_template(template_name)

  template = load_template(template_name)
  if !template
    say "There is no template named \'#{template_name}\'."
    return
  end

  name = options[:name] || template.name
  say "Updating CloudFormation stack #{name} from template #{template.name}\n"
  launcher = Stackr::CloudFormation.new
  begin
    launcher.update_stack(template, options)
  rescue Stackr::StackMissingError => e
    say e.message
  rescue Stackr::StackUpdateNotRequiredError => e
    say e.message
  rescue Stackr::InsufficientCapabilitiesError => e
    say e.message
  end
end

#validate_template(template_name) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/stackr/cli.rb', line 84

def validate_template(template_name)
  template = load_template(template_name)
  if !template
    say "There is no template named \'#{template_name}\'."
    return
  end

  launcher = Stackr::CloudFormation.new
  begin
    launcher.validate_template(template)
  rescue Aws::S3::Errors::ServiceError => e
    say e.message
    return false
  else
    say "Template #{template_name} validates."
    return true
  end
end

#versionObject

version



38
# File 'lib/stackr/cli.rb', line 38

desc 'version', 'show version'