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



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/awful/launch_config.rb', line 32

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/awful/launch_config.rb', line 73

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



27
28
29
# File 'lib/awful/launch_config.rb', line 27

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

#dump(*names) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/awful/launch_config.rb', line 47

def dump(*names)
  paginate(:launch_configurations) do |token|
    autoscaling.describe_launch_configurations(launch_configuration_names: names, next_token: token)
  end.tap do |lcs|
    lcs.select! { |lc| lc.launch_configuration_name.match(options[:match]) } if options[:match]
  end.output do |lcs|
    lcs.each do |lc|
      lc[:user_data] = Base64.decode64(lc[:user_data])
      puts YAML.dump(stringify_keys(lc.to_hash))
    end
  end
end

#latest(name) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/awful/launch_config.rb', line 61

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(*names) ⇒ Object



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

def ls(*names)
  paginate(:launch_configurations) do |token|
    autoscaling.describe_launch_configurations(launch_configuration_names: names, next_token: token)
  end.tap do |lcs|
    lcs.select! { |lc| lc.launch_configuration_name.match(options[:match]) } if options[:match]
  end.output do |lcs|
    if options[:long]
      print_table lcs.map { |lc|
        [lc.launch_configuration_name, lc.image_id, lc.instance_type, lc.created_time]
      }
    else
      puts lcs.map(&:launch_configuration_name)
    end
  end
end