Class: Wakame::Actions::ShutdownVM

Inherits:
Wakame::Action show all
Defined in:
lib/wakame/actions/shutdown_vm.rb

Constant Summary

Constants included from AttributeHelper

AttributeHelper::CLASS_TYPE_KEY, AttributeHelper::CONVERT_CLASSES, AttributeHelper::PRIMITIVE_CLASSES

Instance Attribute Summary

Attributes inherited from Wakame::Action

#action_manager

Instance Method Summary collapse

Methods inherited from Wakame::Action

#acquire_lock, #actor_request, #agent_monitor, #all_subactions_complete?, #flush_subactions, #master, #notes, #notify, #on_canceled, #on_failed, #service_cluster, #status=, #subactions, #sync_actor_request, #trigger_action, #walk_subactions

Methods included from ThreadImmutable

#bind_thread, included, #target_thread, #target_thread?, #thread_check

Methods included from AttributeHelper

#dump_attrs, #retrieve_attr_attribute

Constructor Details

#initialize(agent) ⇒ ShutdownVM

Returns a new instance of ShutdownVM.

Raises:

  • (ArgumentError)


4
5
6
7
# File 'lib/wakame/actions/shutdown_vm.rb', line 4

def initialize(agent)
  raise ArgumentError unless agent.is_a?(Service::Agent)
  @agent = agent
end

Instance Method Details

#runObject



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
# File 'lib/wakame/actions/shutdown_vm.rb', line 9

def run
  if @agent.mapped?
    cloud_host = @agent.cloud_host
    raise "The VM has running service(s)." if cloud_host.assigned_services.any? {|svc_id|
      Wakame::Service::ServiceInstance.find(svc_id).monitor_status == Wakame::Service::STATUS_ONLINE
    }
  end

  require 'uri'
  require 'socket'
  Master.instance.cluster_manager.clusters.each { |cluster_id|
    cluster = Service::ServiceCluster.find(cluster_id)
    next if cluster.advertised_amqp_servers.nil?
    amqp_uri = URI.parse(cluster.advertised_amqp_servers)
    amqp_svr_ip = IPSocket.getaddress(amqp_uri.host)
    
    [@agent.vm_attr[:dns_name], @agent.vm_attr[:private_dns_name]].each { |hostname|
      if IPSocket.getaddress(hostname) == amqp_svr_ip
        Wakame.log.info("Skip to shutdown the VM as the master is running on this node: #{@agent.id}")
        return
      end
    }
  }

  StatusDB.barrier {
    @agent.update_status(Service::Agent::STATUS_TERMINATING)
  }
  shutdown_ec2_instance

end