Class: Fum::Commands::Template

Inherits:
Fum::Command show all
Defined in:
lib/fum/commands/template.rb

Instance Method Summary collapse

Methods inherited from Fum::Command

#initialize, #stage

Methods included from Util

#die

Constructor Details

This class inherits a constructor from Fum::Command

Instance Method Details

#compare_settings(options) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/fum/commands/template.rb', line 115

def compare_settings(options)
  templates = Hash.new{ |h,k| h[k] = Hash.new(&h.default_proc) }
  table = []
  all_names = {}
  columns = %w(Namespace Name)

  options[:template_names].each { |name|
    template = Fog::AWS[:beanstalk].templates.get(@application.main_decl.name, name)
    die "No configuration template named #{name}" if template.nil?
    settings = template.option_settings #.sort_by { |a| [ a["Namespace"], a["OptionName"]]}
                                        #templates[name] = settings

    settings.each { |setting|
      namespace = setting['Namespace']
      option_name = setting['OptionName']

      # Maintain a list of all unique namespace/name combinations
      names = all_names[setting['Namespace']] || []
      names << setting['OptionName'] unless names.include?(setting['OptionName'])
      all_names[setting['Namespace']] = names

      # Build Index for comparison later

      templates[name][namespace][option_name] = setting['Value']
    }

    columns << name
  }

  all_names.each { |namespace, option_names|

    option_names.each { |option_name|
      line = {
          'Namespace' => namespace,
          'Name' => option_name
      }

      values = {}
      prev_value = :init
      values_differ = false

      options[:template_names].each { |template_name|
        value = templates[template_name][namespace][option_name]

        if !values_differ && prev_value != :init
          values_differ = value != prev_value
          prev_value = value
        end
        prev_value = value if prev_value == :init

        values[template_name] = value
      }

      if values_differ
        values.each { |key, value|
          values[key] = "[bold]#{value.to_s}[/]"
        }
      end

      table << line.merge(values)
    }
  }

  table.sort_by { |a| [ a["Namespace"], a["Name"]]}

  Formatador.display_compact_table(table, columns)


end

#create_template(options) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/fum/commands/template.rb', line 216

def create_template(options)

  die "Must specify application name " if options[:application].nil?

  create_opts = {
      :name => options[:template_name],
      :application_name => options[:application]
  }

  create_opts[:solution_stack_name] = options[:stack] if options[:stack]
  create_opts[:description] = options[:description] unless options[:description].nil?
  create_opts[:source_configuration] = {
      'ApplicationName' => options[:from_application].nil? ? options[:application] : options[:from_application],
      'TemplateName' => options[:from_template]
  } if options[:from_template]

  begin
    template = Fog::AWS[:beanstalk].templates.create(create_opts)
    puts "Created template #{template.name}"
  rescue Fog::AWS::ElasticBeanstalk::InvalidParameterError => ex
    die ex
  end

end

#display_options(values, options) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/fum/commands/template.rb', line 185

def display_options(values, options)
  values = values.sort_by { |a| [ a["Namespace"], a["Name"]]}

  if options[:json]
    puts JSON.pretty_generate(values)
  else
    values.each { |value|
      constraints = ''
      if value['MinValue'] && value['MaxValue']
        constraints = "Range(#{value['MinValue']}-#{value['MaxValue']})"
      elsif value['ValueOptions']
        type = value['ValueType'] == 'List' ? 'List' : 'Scalar'
        constraints = "#{type}(#{value['ValueOptions'].join(', ')})"
      elsif value['Regex']
        constraints = "Regex(#{value['Regex']['Label']} = #{value['Regex']['Pattern']})"
      elsif value['ValueType'] == 'Boolean'
        constraints = "Boolean(true, false)"
      end

      if value['MaxLength']
        max_length_constraint = "MaxLength(#{value['MaxLength']})"
        constraints += ', ' unless constraints.empty?
        constraints += max_length_constraint
      end
      value['Constraints'] = constraints
    }
    Formatador.display_compact_table(values, %w(Namespace Name DefaultValue Constraints))
  end

end

#display_settings(values, options) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fum/commands/template.rb', line 101

def display_settings(values, options)
  values = values.sort_by { |a| [ a["Namespace"], a["OptionName"]]}
  if options[:json]
    # Prune nil values in JSON, or we can't update from JSON
    values = values.select { |v| !v["Value"].nil? }
    puts JSON.pretty_generate(values)
  else
    values.each { |value|
      value['Namespace'] = "[bold]#{value['Namespace']}[/]"
    }
    Formatador.display_compact_table(values, %w(Namespace OptionName Value))
  end
end

#execute(options) ⇒ Object

  • :type



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
# File 'lib/fum/commands/template.rb', line 46

def execute(options)
  app = @application.main_decl

  # TODO make sure only one of these is is set, for now do least destructive order.
  if options[:settings] || options[:update] || options[:options] || options[:delete]

    template = Fog::AWS[:beanstalk].templates.get(app.name, options[:template_name])
    die "No configuration template named #{options[:template_name]}" if template.nil?

    if options[:settings]
      display_settings(template.option_settings, options)
    elsif options[:options]
      display_options(template.options, options)
    elsif options[:update]
      update_template(template, options)
    elsif options[:delete]
      template.destroy
      puts "Deleted template #{template.name}."
    end
  elsif options[:create]
    create_template(options)
  elsif options[:compare]
    compare_settings(options)
  elsif options[:list]
    list_templates(options)
  elsif options[:stack]
    begin
      config_opts = Fog::AWS[:beanstalk].describe_configuration_options('SolutionStackName' => options[:stack])
      config_opts = config_opts.body['DescribeConfigurationOptionsResult']['Options']
      display_options(config_opts, options)
    rescue Fog::AWS::ElasticBeanstalk::InvalidParameterError => ex
      die ex.message
    end
  end

end

#list_templates(options) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fum/commands/template.rb', line 83

def list_templates(options)
  templates = Fog::AWS[:beanstalk].templates

  templates = templates.sort_by { |a| [ a.application_name, a.name ] }

  table = []
  templates.each { |template|
    table << {
        'name' => template.name,
        'application' => template.application_name,
        'description' => template.description,
        'created' => template.created_at,
        'updated' => template.updated_at
    }
  }
  Formatador.display_compact_table(table, %w(application name description created updated))
end

#parse_optionsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fum/commands/template.rb', line 9

def parse_options
  options = Trollop::options do
    banner "usage: template [options] <environment-id>, where options are:"
    opt :options, "Describe Configuration Options"
    opt :json, "Output in JSON format"
    opt :application, "Application name (for creating templates only)", :type => :string
    opt :description, "Description (update/create only)", :type => :string
    #opt :prompt, "Prompt for options when updating or creating"
    opt :stack, "Use the specified solution stack.", :type => :string
    opt :create, "Create the specified template"
    opt :update, "Update the specified template"
    opt :delete, "Delete the specified template"
    opt :list, "List all templates with some details"
    opt :settings, "Print the current settings of the template"
    opt :compare, "Compares two or more templates"
    opt :from_template, "Base template to use when creating a new template", :type => :string
    opt :from_json, "JSON File to use for create or update", :type => :string
    opt :from_application, "Application to use for source template", :type => :string
  end
  if options[:options] || options[:settings] || options[:create] || options[:update] || options[:delete]
    if ARGV.empty?
      die "Please specify a template name for this operation"
    else
      options[:template_name] = ARGV.shift
    end
  elsif options[:compare]
    die "Please specify two templates to compare" unless ARGV.length >= 2
    options[:template_names] = ARGV.dup
  end


  options
end

#update_template(template, options) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/fum/commands/template.rb', line 241

def update_template(template, options)

  settings = []
  if options[:from_json]
    settings = JSON.parse(File.read(options[:from_json]))
    # TODO add some sanity checks, verify array of hashes, etc.
  end
  new_attributes = {
      :option_settings => settings
  }
  new_attributes[:description] = options[:description] unless options[:description].nil?

  begin
    template.modify(new_attributes)
    puts "Updated template #{template.name}"
  rescue Fog::AWS::ElasticBeanstalk::InvalidParameterError => ex
    die ex
  end

end