Class: Tengine::ResourceWakame::Provider

Inherits:
Tengine::Resource::Provider
  • Object
show all
Defined in:
lib/tengine/resource_wakame/provider.rb

Overview

1.0.x ではTengine::Resource::Provider::Ec2を継承していましたが、 1.1.x ではクラス構造を見直して継承する必要はないと判断しTengine::Resource::Providerを継承するように変更しました。

Defined Under Namespace

Classes: PhysicalServerSynchronizer, Synchronizer, VirtualServerImageSynchronizer, VirtualServerSynchronizer, VirtualServerTypeSynchronizer

Constant Summary collapse

PHYSICAL_SERVER_STATES =
[:online, :offline].freeze
VIRTUAL_SERVER_STATES =
[
:scheduling, :pending, :starting, :running,
:failingover, :shuttingdown, :terminated].freeze
CONNECTION_TEST_ATTRIBUTES =
[:describe_instances_file, :describe_images_file, :run_instances_file,
:terminate_instances_file, :describe_host_nodes_file, :describe_instance_specs_file].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#retry_on_errorObject

Returns the value of attribute retry_on_error.



16
17
18
# File 'lib/tengine/resource_wakame/provider.rb', line 16

def retry_on_error
  @retry_on_error
end

Instance Method Details

#capacities(keep_cache = false) ⇒ Object



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
# File 'lib/tengine/resource_wakame/provider.rb', line 93

def capacities(keep_cache = false)
  reload unless keep_cache
  server_type_ids = virtual_server_types.map(&:provided_id)
  server_type_to_cpu = virtual_server_types.map_to_hash(:provided_id, &:cpu_cores)
  server_type_to_mem = virtual_server_types.map_to_hash(:provided_id, &:memory_size)
  physical_servers.inject({}) do |result, physical_server|
    if physical_server.status == 'online'
      active_guests = physical_server.guest_servers.reject do |i|
        i.status.to_s == "terminated" or
          server_type_to_cpu[i.provided_type_id].nil? or
          server_type_to_mem[i.provided_type_id].nil?
      end
      cpu_free = physical_server.cpu_cores - active_guests.map{|s| server_type_to_cpu[s.provided_type_id]}.sum
      mem_free = physical_server.memory_size - active_guests.map{|s| server_type_to_mem[s.provided_type_id]}.sum
      result[physical_server.provided_id] = server_type_ids.map_to_hash do |server_type_id|
        [ cpu_free / server_type_to_cpu[server_type_id],
          mem_free / server_type_to_mem[server_type_id]
        ].min
      end
    else
      result[physical_server.provided_id] = server_type_ids.map_to_hash{|server_type_id| 0}
    end
    result
  end
end

#connect(&block) ⇒ Object



184
185
186
# File 'lib/tengine/resource_wakame/provider.rb', line 184

def connect(&block)
  send(retry_on_error ? :connect_with_retry : :connect_without_retry, &block)
end

#connect_with_retry(&block) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/tengine/resource_wakame/provider.rb', line 211

def connect_with_retry(&block)
  retry_count = 1
  begin
    return connect_without_retry(&block)
  rescue Exception => e
    if retry_count > self.retry_count
      Tengine.logger.error "#{e.class.name} #{e.message}"
      raise e
    else
      Tengine.logger.warn "retry[#{retry_count}]: #{e.message}"
      sleep self.retry_interval
      retry_count += 1
      retry
    end
  end
end

#connect_without_retryObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/tengine/resource_wakame/provider.rb', line 191

def connect_without_retry
  connection = nil
  if self.connection_settings[:test] || self.connection_settings["test"]
    # テスト用
    connection = ::Tama::Controllers::ControllerFactory.create_controller(:test)
    options = self.connection_settings[:options] || self.connection_settings["options"]
    if options
      options.symbolize_keys!
      CONNECTION_TEST_ATTRIBUTES.each do |key|
        connection.send("#{key}=", File.expand_path(options[key])) if options[key]
      end
    end
  else
    options = self.connection_settings.symbolize_keys
    args = [:account, :ec2_host, :ec2_port, :ec2_protocol, :wakame_host, :wakame_port, :wakame_protocol].map{|key| options[key]}
    connection = ::Tama::Controllers::ControllerFactory.create_controller(*args)
  end
  return yield(connection)
end

#create_virtual_servers(name, image, type, physical, description = "", count = 1) ⇒ Array<Tengine::Resource::VirtualServer>

Parameters:

  • name (String)

    Name template for created virtual servers

  • image (Tengine::Resource::VirtualServerImage)

    Virtual server image object

  • type (Tengine::Resource::VirtualServerType)

    Virtual server type object

  • physical (String)

    Data center name to put virtual machines (availability zone)

  • description (String) (defaults to: "")

    What this virtual server is

  • count (Numeric) (defaults to: 1)

    number of vortial servers to boot

Returns:

  • (Array<Tengine::Resource::VirtualServer>)


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
# File 'lib/tengine/resource_wakame/provider.rb', line 33

def create_virtual_servers(name, image, type, physical, description = "", count = 1)
  physical_provided_id = physical.respond_to?(:provided_id) ? physical.provided_id : physical
  connect {|conn|
    results = conn.run_instances(
      image.provided_id,
      count,
      count,
      [],
      self.properties['key_name'] || self.properties[:key_name],
      "",
      nil, # <- addressing_type
      type.provided_id,
      nil,
      nil,
      physical_provided_id,
      nil  # <- block_device_mappings
    )
    yield if block_given? # テスト用のブロックの呼び出し
    results.map.with_index {|hash, idx|
      provided_id = hash.delete(:aws_instance_id)
      if server = self.virtual_servers.where({:provided_id => provided_id}).first
        server
      else
        # findではなくfirstで検索しているので、もしhost_server_provided_idで指定されるサーバが見つからなくても
        # host_serverがnilとして扱われるが、仮想サーバ自身の登録は行われます
        host_server = Tengine::Resource::PhysicalServer.by_provided_id(
          [hash[:aws_availability_zone], physical_provided_id].detect{|i| !i.blank?})
        self.find_virtual_server_on_duplicaion_error(provided_id) do
          self.virtual_servers.create!(
            :name                 => sprintf("%s%03d", name, idx + 1), # 1 origin
            :address_order        => address_order.dup,
            :description          => description,
            :provided_id          => provided_id,
            :provided_image_id    => hash.delete(:aws_image_id),
            :provided_type_id     => hash.delete(:aws_instance_type),
            :host_server_id       => host_server ? host_server.id : nil,
            :status               => hash.delete(:aws_state),
            :properties           => hash,
            :addresses            => {
  #             :dns_name           => hash.delete(:dns_name),
  #             :ip_address         => hash.delete(:ip_address),
  #             :private_dns_name   => hash.delete(:private_dns_name),
  #             :private_ip_address => hash.delete(:private_ip_address),
            })
        end
      end
    }
  }
end

#describe_host_nodes_for_api(uuids = [], options = {}) ⇒ Object



129
130
131
# File 'lib/tengine/resource_wakame/provider.rb', line 129

def describe_host_nodes_for_api(uuids = [], options = {})
  call_api_with_conversion(:describe_host_nodes, uuids, options)
end

#describe_images_for_api(uuids = [], options = {}) ⇒ Object



146
147
148
# File 'lib/tengine/resource_wakame/provider.rb', line 146

def describe_images_for_api(uuids = [], options = {})
  call_api_with_conversion(:describe_images, uuids, options)
end

#describe_instance_specs_for_api(uuids = [], options = {}) ⇒ Object

wakame api for tama



125
126
127
# File 'lib/tengine/resource_wakame/provider.rb', line 125

def describe_instance_specs_for_api(uuids = [], options = {})
  call_api_with_conversion(:describe_instance_specs, uuids, options)
end

#describe_instances_for_api(uuids = [], options = {}) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/tengine/resource_wakame/provider.rb', line 133

def describe_instances_for_api(uuids = [], options = {})
  result = call_api_with_conversion(:describe_instances, uuids, options)
  result.each do |r|
    replace_value_of_hash(r, :private_ip_address) do |v|
      v.first if v.is_a?(Array)
    end
    replace_value_of_hash(r, :ip_address) do |v|
      "nw-data\=#{$1}" if (v =~ /^nw\-data\=\[\"(.+)\"\]$/)
    end
  end
  result
end

#run_instances_for_api(uuids = [], options = {}) ⇒ Object



150
151
152
# File 'lib/tengine/resource_wakame/provider.rb', line 150

def run_instances_for_api(uuids = [], options = {})
  call_api_with_conversion(:run_instances, uuids, options)
end

#synchronize_physical_serversObject

物理サーバの監視



119
# File 'lib/tengine/resource_wakame/provider.rb', line 119

def synchronize_physical_servers     ; synchronize_by(:physical_servers     ); end

#synchronize_virtual_server_imagesObject

仮想サーバイメージの監視



121
# File 'lib/tengine/resource_wakame/provider.rb', line 121

def synchronize_virtual_server_images; synchronize_by(:virtual_server_images); end

#synchronize_virtual_server_typesObject

仮想サーバタイプの監視



120
# File 'lib/tengine/resource_wakame/provider.rb', line 120

def synchronize_virtual_server_types ; synchronize_by(:virtual_server_types ); end

#synchronize_virtual_serversObject

仮想サーバの監視



122
# File 'lib/tengine/resource_wakame/provider.rb', line 122

def synchronize_virtual_servers      ; synchronize_by(:virtual_servers      ); end

#terminate_instances_for_api(uuids = [], options = {}) ⇒ Object



154
155
156
# File 'lib/tengine/resource_wakame/provider.rb', line 154

def terminate_instances_for_api(uuids = [], options = {})
  call_api_with_conversion(:terminate_instances, uuids, options)
end

#terminate_virtual_servers(servers) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/tengine/resource_wakame/provider.rb', line 84

def terminate_virtual_servers(servers)
  connect do |conn|
    conn.terminate_instances(servers.map {|i| i.provided_id }).map do |hash|
      serv = self.virtual_servers.where(:provided_id => hash[:aws_instance_id]).first
      serv
    end
  end
end