Statistics
| Branch: | Tag: | Revision:

one / share / hooks / host_error.rb @ 2fc0f5b5

History | View | Annotate | Download (3.02 KB)

1
#!/usr/bin/env ruby 
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
#--------------------------------------------------------------------------- #
18

    
19
####################################################
20
# Script to implement host failure tolerance
21
#   It can be set to 
22
#           -r resubmit VMs running in the host
23
#           -d delete VMs running in the host 
24
####################################################
25

    
26
ONE_LOCATION=ENV["ONE_LOCATION"]
27

    
28
if !ONE_LOCATION
29
    RUBY_LIB_LOCATION="/usr/lib/one/ruby"
30
    VMDIR="/var/lib/one"
31
else
32
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
33
    VMDIR=ONE_LOCATION+"/var"
34
end
35

    
36
$: << RUBY_LIB_LOCATION
37

    
38
require 'OpenNebula'
39
include OpenNebula
40

    
41
if !(host_id=ARGV[0])
42
    exit -1
43
end
44

    
45
if !(mode=ARGV[1]) # By default, resubmit VMs
46
    mode = "-r"
47
end
48

    
49
if !(force=ARGV[2]) # By default, don't resubmit/finalize suspended VMs
50
    force = "n"
51
end
52

    
53
begin
54
    client = Client.new()
55
rescue Exception => e
56
    puts "Error: #{e}"
57
    exit -1
58
end
59

    
60
# Retrieve hostname
61
host  =  OpenNebula::Host.new_with_id(host_id, client)
62
exit -1 if OpenNebula.is_error?(host)
63
host.info
64
host_name = host.name
65

    
66
# Loop through all vms
67
vms = VirtualMachinePool.new(client, -2)
68
exit -1 if OpenNebula.is_error?(vms)
69

    
70
vms.info
71

    
72
vm_ids_array = vms.retrieve_elements("/VM_POOL/VM[STATE=3]/HISTORY[HOSTNAME=\"#{host_name}\"]/../ID")
73

    
74

    
75
if vm_ids_array
76
    vm_ids_array.each do |vm_id| 
77
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, client)
78
        vm.info
79

    
80
        if mode == "-r"
81
            vm.resubmit
82
        elsif mode == "-d"
83
            vm.finalize
84
        end
85
    end
86
end
87

    
88
if force == "y" 
89
    vm_ids_array = vms.retrieve_elements("/VM_POOL/VM[STATE=5]/HISTORY[HOSTNAME=\"#{host_name}\"]/../ID")
90

    
91
    if vm_ids_array
92
        vm_ids_array.each do |vm_id| 
93
            vm=OpenNebula::VirtualMachine.new_with_id(vm_id, client)
94
            vm.info
95

    
96
            if mode == "-r"
97
                vm.resubmit
98
            elsif mode == "-d"
99
                vm.finalize
100
            end
101
        end
102
    end
103
end
104

    
105