Class: Awscli::As::Configurations

Inherits:
Object
  • Object
show all
Defined in:
lib/awscli/as.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Configurations

Returns a new instance of Configurations.



19
20
21
# File 'lib/awscli/as.rb', line 19

def initialize(connection)
  @conn = connection
end

Instance Method Details

#create(options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
82
83
84
85
86
87
88
# File 'lib/awscli/as.rb', line 31

def create(options)
  #validate block device mapping and parse it to a hash understandable by fog
  opts = Marshal.load(Marshal.dump(options))
  block_device_mapping = Array.new
  if options[:block_device_mappings]
    options[:block_device_mappings].each do |group|
      mapping = Hash.new
      #parse options
      abort "Invalid block device mapping format, expecting 'devicename=blockdevice' format" unless group =~ /\S=\S/
      device_name, block_device = group.split('=')
      abort "Invalid device name, expecting '/dev/sd[a-z]'" unless device_name =~ /^\/dev\/sd[a-z]$/
      abort "Invalid block device format, expecting 'ephemeral[0..3]|none|[snapshot-id]:[volume-size]:[true|false]:[standard|io1[:iops]]'" unless block_device =~ /^(snap-.*|ephemeral\w{1,3}|none|:.*)$/
      mapping['DeviceName'] = device_name
      case block_device
      when 'none'
        mapping['Ebs.NoDevice'] = 'true'
      when /ephemeral/
        mapping['VirtualName'] = block_device
      when /snap-.*|:.*/
        snapshot_id, volume_size, delete_on_termination, volume_type, iops = block_device.split(":")
        mapping['Ebs.SnapshotId'] = snapshot_id if !snapshot_id.nil? && !snapshot_id.empty?
        mapping['Ebs.VolumeSize'] = volume_size if !volume_size.nil? && !volume_size.empty?
        mapping['Ebs.DeleteOnTermination'] = delete_on_termination if !delete_on_termination.nil? && !delete_on_termination.empty?
      else
        abort 'Cannot validate block_device'
      end
      block_device_mapping << mapping
    end
  end
  if block_devices = opts.delete(:block_device_mappings)
    opts.merge!('BlockDeviceMappings' => block_device_mapping)
  end
  if key_name = opts.delete(:key_name)
    opts.merge!('KeyName' => key_name)
  end
  if sec_grps = opts.delete(:security_groups)
    opts.merge!('SecurityGroups' => sec_grps)
  end
  if spot_price = opts.delete(:spot_price)
    opts.merge!('SpotPrice' => spot_price)
  end
  if user_data = opts.delete(:user_data)
    opts.merge!('UserData' => user_data)
  end
  if instance_monitoring = opts.delete(:instance_monitoring)
    opts.merge!('InstanceMonitoring.Enabled' => instance_monitoring)
  end
  opts.reject! { |k| k == 'image_id' }
  opts.reject! { |k| k == 'instance_type' }
  opts.reject! { |k| k == 'id' }

  begin
    cfgs = @conn.create_launch_configuration(options[:image_id], options[:instance_type], options[:id], opts)
    puts "Created Launch Configuration, #{options[:id]}"
  rescue Fog::AWS::AutoScaling::IdentifierTaken
    puts "A launch configuration already exists with the name #{options[:id]}"
  end
end

#delete(cfg_name) ⇒ Object



90
91
92
93
94
95
# File 'lib/awscli/as.rb', line 90

def delete(cfg_name)
  cfg = @conn.configurations.get(cfg_name)
  abort "Cannot find launch configuration with name: #{cfg_name}" unless cfg
  cfg.destroy
  puts "Deleted Launch Configuration with name: #{cfg_name}"
end

#list(options) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/awscli/as.rb', line 23

def list(options)
  if options[:table]
    @conn.configurations.table([:id, :instance_type, :key_name, :security_groups])
  else
    puts @conn.describe_launch_configurations.body['DescribeLaunchConfigurationsResult']['LaunchConfigurations'].to_yaml
  end
end