File: launch_configuration_collection.rb

package info (click to toggle)
ruby-aws-sdk 1.67.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,840 kB
  • sloc: ruby: 28,436; makefile: 7
file content (177 lines) | stat: -rw-r--r-- 6,502 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

require 'base64'

module AWS
  class AutoScaling
    class LaunchConfigurationCollection

      include Core::Collection::WithLimitAndNextToken

      # Creates an Auto Scaling launch configuration.
      #
      #     auto_scaling.launch_configurations.create('name', 'ami-12345', 'm1.small')
      #
      # @param [String] name The name of the launch configuration to create.
      #
      # @param [EC2::Image,String] image An {EC2::Image} or image id string.
      #
      # @param [String] instance_type The type of instance (e.g.
      #   't1.micro', 'm1.small', 'm2.4xlarge', etc).
      #
      # @param [Hash] options
      #
      # @option options [Array<Hash>] :block_device_mappings
      #
      # @option options [Boolean] :detailed_instance_monitoring (true)
      #   When enabled, CloudWatch will generate metrics every minute
      #   and your account will be charged a fee. When you disable
      #   detailed monitoring, by specifying False, Cloudwatch will
      #   generate metrics every 5 minutes.
      #
      # @option options [String] :kernel_id The ID of the kernel to
      #   launch the instances with.
      #
      # @option options [KeyPair,String] :key_pair The keypair to launch
      #   instances with.  This may be an {EC2::KeyPair} object or
      #   or key pair name string.
      #
      # @option options [String] :ramdisk_id The ID of the ramdisk to
      #   launch the instances with.
      #
      # @option options [Array<EC2::SecurityGroup>,Array<String>] :security_groups
      #   A list of security groups to associate with the instances.
      #   For both EC2 Classic and VPC, this option can be an array of {EC2::SecurityGroup} objects
      #   or security group ids. For EC2 Classic, this option can also be an array of
      #   security group names.
      #   Note: The VPC is derived from the security groups.
      #
      # @option options [String] :user_data The user data available to
      #   the launched Amazon EC2 instances.
      #
      # @option options [String] :iam_instance_profile
      #
      # @option options [String] :spot_price
      #
      # @option options [Boolean] :associate_public_ip_address
      #   Used for Auto Scaling groups that launch instances into an
      #   Amazon Virtual Private Cloud (Amazon VPC). Specifies whether
      #   to assign a public IP address to each instance launched in a Amazon VPC.
      #
      # @option options [String] :placement_tenancy
      #
      # @option options [String] :classic_link_vpc_id
      #   The ID of a ClassicLink-enabled VPC to link EC2 Classic instances to.
      #
      # @option options [Array<EC2::SecurityGroup>,Array<String>] :classic_link_vpc_security_groups
      #   The list of security groups for the specified VPC to associate
      #   with the instances. This may be an array of {EC2::SecurityGroup}
      #   objects or security group ids. VPC security groups cannot be
      #   referenced by name.
      #
      # @return [LaunchConfiguration]
      #
      def create name, image, instance_type, options = {}

        client_opts = {}
        client_opts[:launch_configuration_name] = name
        client_opts[:image_id] = image_id_opt(image)
        client_opts[:instance_type] = instance_type
        client_opts[:instance_monitoring] = instance_monitoring_opt(options) if
          options.key?(:detailed_instance_monitoring)
        client_opts[:key_name] = key_name_opt(options) if options[:key_pair]
        client_opts[:security_groups] = security_groups_opt(options[:security_groups]) if
          options.key?(:security_groups)
        client_opts[:classic_link_vpc_security_groups] = security_groups_opt(options[:classic_link_vpc_security_groups]) if
          options.key?(:classic_link_vpc_security_groups)
        client_opts[:user_data] = user_data_opt(options) if options[:user_data]

        [
          :classic_link_vpc_id,
          :iam_instance_profile,
          :spot_price,
          :kernel_id,
          :ramdisk_id,
          :block_device_mappings,
          :associate_public_ip_address,
          :placement_tenancy,
        ].each do |opt|
          client_opts[opt] = options[opt] if options.key?(opt)
        end

        client.create_launch_configuration(client_opts)

        LaunchConfiguration.new(name,
          :image_id => client_opts[:image_id],
          :instance_type => client_opts[:instance_type],
          :config => config)

      end

      # @param [String] name The name of a launch configuration.
      # @return [LaunchConfiguration]
      def [] name
        LaunchConfiguration.new(name, :config => config)
      end

      protected

      def _each_item next_token, limit, options = {}, &block

        options[:next_token] = next_token if next_token
        options[:max_records] = limit if limit

        resp = client.describe_launch_configurations(options)
        resp.launch_configurations.each do |details|

          launch_configuration = LaunchConfiguration.new_from(
            :describe_launch_configurations,
            details,
            details.launch_configuration_name,
            :config => config)

          yield(launch_configuration)

        end
        resp.data[:next_token]
      end

      def image_id_opt image
        image.is_a?(EC2::Image) ? image.image_id : image
      end

      def instance_monitoring_opt options
        options[:detailed_instance_monitoring] == true ?
          { :enabled => true } :
          { :enabled => false }
      end

      def key_name_opt options
        key_pair = options[:key_pair]
        key_pair.is_a?(EC2::KeyPair) ? key_pair.name : key_pair
      end

      def security_groups_opt security_groups
        security_groups.collect do |sg|
          sg.is_a?(EC2::SecurityGroup) ? sg.id : sg
        end
      end

      def user_data_opt options
        Base64.encode64(options[:user_data])
      end

    end
  end
end