Class: Awful::LaunchConfig

Inherits:
Cli show all
Defined in:
lib/awful/launch_config.rb

Instance Method Summary collapse

Methods inherited from Cli

#initialize

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#clean(name, num = 1) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/awful/launch_config.rb', line 26

def clean(name, num = 1)
  autoscaling.describe_launch_configurations.map(&:launch_configurations).flatten.select do |lc|
    lc.launch_configuration_name.match(name)
  end.sort_by(&:created_time).first(num.to_i).map(&:launch_configuration_name).tap do |names|
    puts names
    if yes? 'delete these launch configs?', :yellow
      names.each do |n|
        autoscaling.delete_launch_configuration(launch_configuration_name: n)
      end
    end
  end
end

#create(file = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/awful/launch_config.rb', line 59

def create(file = nil)
  opt = load_cfg(options, file)

  whitelist = %i[launch_configuration_name image_id key_name security_groups classic_link_vpc_id classic_link_vpc_security_groups user_data
                 instance_id instance_type kernel_id ramdisk_id block_device_mappings instance_monitoring spot_price iam_instance_profile
                 ebs_optimized associate_public_ip_address placement_tenancy]

  if options[:timestamp]
    opt[:launch_configuration_name] += "-#{Time.now.utc.strftime('%Y%m%d%H%M%S')}"
  end

  opt[:user_data] = Base64.encode64(opt[:user_data]) # encode user data
  opt = remove_empty_strings(opt)
  opt = only_keys_matching(opt, whitelist)
  autoscaling.create_launch_configuration(opt)
  opt[:launch_configuration_name].tap do |name|
    puts name
  end
end

#delete(name) ⇒ Object



21
22
23
# File 'lib/awful/launch_config.rb', line 21

def delete(name)
  autoscaling.delete_launch_configuration(launch_configuration_name: name)
end

#dump(name) ⇒ Object



40
41
42
43
44
# File 'lib/awful/launch_config.rb', line 40

def dump(name)
  lc = autoscaling.describe_launch_configurations(launch_configuration_names: Array(name)).map(&:launch_configurations).flatten.first.to_hash
  lc[:user_data] = Base64.decode64(lc[:user_data])
  puts YAML.dump(stringify_keys(lc))
end

#latest(name) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/awful/launch_config.rb', line 47

def latest(name)
  autoscaling.describe_launch_configurations.map(&:launch_configurations).flatten.select do |lc|
    lc.launch_configuration_name.match(/^#{name}/)
  end.sort_by(&:created_time).last.launch_configuration_name.tap do |latest|
    puts latest
  end
end

#ls(name = /./) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/awful/launch_config.rb', line 9

def ls(name = /./)
  fields = options[:long] ? %i[launch_configuration_name image_id instance_type created_time] : %i[launch_configuration_name]
  autoscaling.describe_launch_configurations.map(&:launch_configurations).flatten.select do |lc|
    lc.launch_configuration_name.match(name)
  end.map do |lc|
    fields.map { |field| lc.send(field) }
  end.tap do |list|
    print_table list
  end
end