Class: Tengine::ResourceEc2::LaunchOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/tengine/resource_ec2/launch_options.rb

Constant Summary collapse

LAUNCH_OPTIONS_KEYS =
%w(current_region regions availability_zones key_pairs
security_groups images instance_types kernel_ids ramdisk_ids)
DEFAULT_REGION_CAPTIONS =
{
  "us-east-1"      => "US East(Northern Virginia)" ,
  "us-west-1"      => "US West(Northern California)" ,
  "eu-west-1"      => "EU West(Ireland)" ,
  "ap-southeast-1" => "Asia Pacific(Singapore)",
  "ap-northeast-1" => "Asia Pacific(Tokyo)",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credential) ⇒ LaunchOptions



6
7
8
# File 'lib/tengine/resource_ec2/launch_options.rb', line 6

def initialize(credential)
  @credential = credential
end

Instance Attribute Details

#current_regionObject (readonly)

Returns the value of attribute current_region.



20
21
22
# File 'lib/tengine/resource_ec2/launch_options.rb', line 20

def current_region
  @current_region
end

Instance Method Details

#availability_zonesObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/tengine/resource_ec2/launch_options.rb', line 38

def availability_zones
  # [
  #   {:region_name=>"us-east-1", :zone_name=>"us-east-1a", :zone_state=>"available"},
  #   {:region_name=>"us-east-1", :zone_name=>"us-east-1b", :zone_state=>"available"},
  #   {:region_name=>"us-east-1", :zone_name=>"us-east-1c", :zone_state=>"available"},
  #   {:region_name=>"us-east-1", :zone_name=>"us-east-1d", :zone_state=>"available"}
  # ]
  raw = @connection.describe_availability_zones
  raw.map{|h| h[:zone_name]}.sort
end

#imagesObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tengine/resource_ec2/launch_options.rb', line 66

def images
  # [
  #   {
  #     :aws_id=>"ami-5189d814",
  #     :aws_architecture=>"i386", :root_device_type=>"instance-store",
  #     :root_device_name=>"/dev/sda1",
  #     :aws_location=>"akm2000-us-west-2/dev-20100521-01.manifest.xml",
  #     :aws_image_type=>"machine", :aws_state=>"available",
  #     :aws_owner=>"892601002221", :aws_is_public=>false,
  #     :aws_kernel_id=>"aki-773c6d32", :aws_ramdisk_id=>"ari-c12e7f84",
  #   },
  # ]
  saved_images = Tengine::Resource::VirtualServerImage.all
  # raw_images = @connection.describe_images_by_owner('self')
  raw_images = @connection.describe_images(saved_images.map(&:provided_id).uniq.compact) #クラスタに登録されているAMI
  # raw_images += @connection.describe_images_by_executable_by("self") # 実行可能なAMI
  amiid_to_hash = raw_images.inject({}){|d, hash| d[hash[:aws_id]] = hash; d}
  result = saved_images.map do |saved_image|
    if ami = amiid_to_hash[saved_image.provided_id]
      {
        'id' => saved_image.id,
        'name' => ami[:aws_id],
        'caption' => saved_image.description,
        'aws_architecture' => ami[:aws_architecture],
        'aws_arch_root_dev' => to_aws_arch_root_dev(ami),
      }
    else
      nil
    end
  end
  result.compact.uniq
end

#instance_typesObject



99
100
101
102
# File 'lib/tengine/resource_ec2/launch_options.rb', line 99

def instance_types
  # rawなし
  INSTANCE_TYPES
end

#kernel_idsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tengine/resource_ec2/launch_options.rb', line 104

def kernel_ids
  # [
  #   {
  #     :aws_id=>"aki-233c6d66",
  #     :aws_architecture=>"i386", :root_device_type=>"instance-store",
  #     :aws_location=>"ec2-paid-ibm-images-us-west-1/vmlinuz-2.6.16.60-0.29-xenpae.i386.manifest.xml",
  #     :aws_image_type=>"kernel", :aws_state=>"available", :aws_owner=>"470254534024",
  #     :aws_is_public=>true, :image_owner_alias=>"amazon",
  #   },
  # ]
  raw = amazon_images.select{|img| img[:aws_image_type] == 'kernel'}
  raw.inject({}) do |dest, hash|
    key = hash[:aws_architecture]
    dest[key] ||= []
    dest[key] << hash[:aws_id]
    dest
  end
end

#key_pairsObject



49
50
51
52
53
# File 'lib/tengine/resource_ec2/launch_options.rb', line 49

def key_pairs
  # [{:aws_key_name=>"west-dev01", :aws_fingerprint=>"7c:89:2f:c9:4a:1c:02:65:1b:14:dc:a5:c9:a0:da:fb:46:08:4a:99"}]
  raw = @connection.describe_key_pairs
  raw.map{|h| h[:aws_key_name]}
end

#launch_options(connection, current_region) ⇒ Object



13
14
15
16
17
18
# File 'lib/tengine/resource_ec2/launch_options.rb', line 13

def launch_options(connection, current_region)
  @connection, @current_region = connection, current_region
  LAUNCH_OPTIONS_KEYS.map_to_hash{|m| send(m)}
ensure
  @connection, @current_region = nil, nil
end

#ramdisk_idsObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/tengine/resource_ec2/launch_options.rb', line 123

def ramdisk_ids
  # [
  #   {
  #     :aws_id=>"ari-2d3c6d68",
  #     :aws_architecture=>"i386", :root_device_type=>"instance-store",
  #     :aws_location=>"ec2-paid-ibm-images-us-west-1/initrd-2.6.16.60-0.29-xenpae.i386.manifest.xml",
  #     :aws_image_type=>"ramdisk", :aws_state=>"available", :aws_owner=>"470254534024",
  #     :aws_is_public=>true, :image_owner_alias=>"amazon"
  #   },
  # ]
  raw = amazon_images.select{|img| img[:aws_image_type] == 'ramdisk'}
  raw.inject({}) do |dest, hash|
    key = hash[:aws_architecture]
    dest[key] ||= []
    dest[key] << hash[:aws_id]
    dest
  end
end

#regionsObject



30
31
32
33
34
35
36
# File 'lib/tengine/resource_ec2/launch_options.rb', line 30

def regions
  # ["eu-west-1", "us-east-1", "us-west-1", "ap-southeast-1"]
  raw = @connection.describe_regions
  raw.inject([]) do |dest, region|
    dest << {"name" => region, "caption" => DEFAULT_REGION_CAPTIONS[region]}
  end
end

#security_groupsObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/tengine/resource_ec2/launch_options.rb', line 55

def security_groups
  # [
  #   { :aws_owner=>"892601002221", :aws_group_name=>"default", :aws_description=>"default group",
  #     :aws_perms=>[{:owner=>"892601002221", :group=>"default"}, {:from_port=>"22", :to_port=>"22", :cidr_ips=>"0.0.0.0/0", :protocol=>"tcp"}]},
  #   { :aws_owner=>"892601002221", :aws_group_name=>"ruby-dev", :aws_description=>"for developmewnt with ruby",
  #     :aws_perms=>[{:from_port=>"80", :to_port=>"80", :cidr_ips=>"0.0.0.0/0", :protocol=>"tcp"}]}
  # ]
  raw = @connection.describe_security_groups
  raw.map{|h| h[:aws_group_name]}
end