Class: CloudManager::Configuration::Service::EC2Machine

Inherits:
Machine
  • Object
show all
Defined in:
lib/cloud_manager/configuration/service/ec2.rb

Overview

This class represents an EC2 machine along with its settings.

Since:

  • 0.1.0

Instance Attribute Summary collapse

Attributes inherited from Machine

#alias, #config, #parent_service

Instance Method Summary collapse

Constructor Details

#initialize(machine_alias, config, parent_service) ⇒ EC2Machine

Returns a new instance of EC2Machine.

Parameters:

  • machine_alias (String)

    the machine alias

  • config (Hash)

    the machine configuration

  • parent_service (EC2)

    the initiator service

Since:

  • 0.1.0



127
128
129
130
131
132
133
134
135
# File 'lib/cloud_manager/configuration/service/ec2.rb', line 127

def initialize(machine_alias, config, parent_service)
  super(machine_alias, config, parent_service)
  @instances = []

  raise "services.ec2.#{@alias} configuration is missing" unless @config.is_a?(Hash)
  raise "services.ec2.#{@alias}.image_id must be configured" unless @config.include?('image_id') && @config['image_id'].is_a?(String)
  raise "services.ec2.#{@alias}.key_name must be configured" unless @config.include?('key_name') && @config['key_name'].is_a?(String)
  raise "services.ec2.#{@alias}.instance_type must be configured" unless @config.include?('instance_type') && @config['instance_type'].is_a?(String)
end

Instance Attribute Details

#instancesArray<Aws::EC2::Instance> (readonly)

Returns the EC2 instances of this machine – docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Instance.html.

Returns:

Since:

  • 0.1.0



120
121
122
# File 'lib/cloud_manager/configuration/service/ec2.rb', line 120

def instances
  @instances
end

Instance Method Details

#dns_namesArray<String>

Returns the DNS name list for the machine.

Returns:

  • (Array<String>)

    DNS names of the machine

Since:

  • 0.1.0



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/cloud_manager/configuration/service/ec2.rb', line 185

def dns_names
  dns_names = []

  unless @instances.empty?
    @instances.each do |instance|
      instance.load # need to refresh data to get dns names
      dns_names.push(instance.public_dns_name) unless instance.public_dns_name.nil?
    end
  end

  dns_names
end

#ipsArray<String>

Returns the IP list for the machine.

Returns:

  • (Array<String>)

    IPs of the machine

Since:

  • 0.1.0



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cloud_manager/configuration/service/ec2.rb', line 169

def ips
  ips = []

  unless @instances.empty?
    @instances.each do |instance|
      instance.load # need to refresh data to get ip address
      ips.push(instance.public_ip_address) unless instance.public_ip_address.nil?
    end
  end

  ips
end

#startvoid

TODO:

Available instances with the same alias could be checked before start. If there is any, the #instances property could be filled with them.

This method returns an undefined value.

Starts the machine in the EC2 cloud.

The used aws-sdk methods:

Since:

  • 0.1.0



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cloud_manager/configuration/service/ec2.rb', line 145

def start
  if @instances.empty?
    @instances = @parent_service.resource.create_instances({
      image_id: @config['image_id'],
      min_count: @config.include?('min_count') ? @config['min_count'] : 1,
      max_count: @config.include?('max_count') ? @config['max_count'] : 1,
      key_name: @config['key_name'],
      instance_type: @config['instance_type'],
    })

    @instances.each do |instance|
      @parent_service.resource.create_tags({
        resources: [instance.id],
        tags: [key: 'Name', value: @alias]
      })

      puts "      > instance started: #{instance.id} (#{@alias})"
    end
  end
end