Class: Wakame::Command::ShutdownVm

Inherits:
Object
  • Object
show all
Includes:
Wakame::Command
Defined in:
lib/wakame/command/shutdown_vm.rb

Instance Method Summary collapse

Methods included from Wakame::Command

included, #options=, #params

Instance Method Details

#runObject



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

def run
  agent = Wakame::Models::AgentPool.instance.find_agent(@options['agent_id'])
  if agent.cloud_host_id.nil?
    trigger_action(Wakame::Actions::ShutdownVM.new(agent))
  else
    cloud_host = agent.cloud_host
    # Check if the agent has the running service(s).
    live_svcs = cloud_host.assigned_services.find_all {|svc_id|
      Wakame::Service::ServiceInstance.find(svc_id).monitor_status == Wakame::Service::STATUS_ONLINE
    }
    if live_svcs.empty?
      cloud_host.unmap_agent
      trigger_action(Wakame::Actions::ShutdownVM.new(agent))
    else
      raise "Service(s) are still running on #{agent.id}" unless @options['force']
      
      trigger_action { |action|
        live_svcs.each {|svc_id|
          svc = Wakame::Service::ServiceInstance.find(svc_id)
          action.trigger_action(Wakame::Actions::StopService.new(svc))
        }
        action.flush_subactions

        Wakame::StatusDB.barrier {
          cluster = Wakame::Service::ServiceCluster.find(cloud_host.cluster_id)
          live_svcs.each {|svc_id|
            cluster.destroy(svc_id)
          }
          
          cloud_host.unmap_agent
          cluster.remove_cloud_host(cloud_host.id)
        }

        action.trigger_action(Wakame::Actions::ShutdownVM.new(agent))
      }
    end
  end
    
end