Statistics
| Branch: | Tag: | Revision:

one / src / oca / ruby / OpenNebula / Host.rb @ 6329d8b3

History | View | Annotate | Download (4.74 KB)

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

    
17
require 'OpenNebula/Pool'
18

    
19
module OpenNebula
20
    class Host < PoolElement
21
        #######################################################################
22
        # Constants and Class Methods
23
        #######################################################################
24
        HOST_METHODS = {
25
            :info     => "host.info",
26
            :allocate => "host.allocate",
27
            :delete   => "host.delete",
28
            :enable   => "host.enable"
29
        }
30

    
31
        HOST_STATES=%w{INIT MONITORING MONITORED ERROR DISABLED}
32

    
33
        SHORT_HOST_STATES={
34
            "INIT"          => "on",
35
            "MONITORING"    => "on",
36
            "MONITORED"     => "on",
37
            "ERROR"         => "err",
38
            "DISABLED"      => "off"
39
        }
40

    
41
        # Creates a Host description with just its identifier
42
        # this method should be used to create plain Host objects.
43
        # +id+ the id of the host
44
        #
45
        # Example:
46
        #   host = Host.new(Host.build_xml(3),rpc_client)
47
        #
48
        def Host.build_xml(pe_id=nil)
49
            if pe_id
50
                host_xml = "<HOST><ID>#{pe_id}</ID></HOST>"
51
            else
52
                host_xml = "<HOST></HOST>"
53
            end
54

    
55
            XMLElement.build_xml(host_xml, 'HOST')
56
        end
57

    
58
        #######################################################################
59
        # Class constructor
60
        #######################################################################
61
        def initialize(xml, client)
62
            super(xml,client)
63

    
64
            @client = client
65
            @pe_id  = self['ID'].to_i if self['ID']
66
        end
67

    
68
        #######################################################################
69
        # XML-RPC Methods for the Host
70
        #######################################################################
71
        
72
        # Retrieves the information of the given Host.
73
        def info()
74
            super(HOST_METHODS[:info], 'HOST')
75
        end
76

    
77
        # Allocates a new Host in OpenNebula
78
        #
79
        # +hostname+ A string containing the name of the new Host.
80
        #
81
        # +im+ A string containing the name of the im_driver
82
        #
83
        # +vmm+ A string containing the name of the vmm_driver
84
        #
85
        # +tm+ A string containing the name of the tm_driver
86
        def allocate(hostname,im,vmm,tm)
87
            super(HOST_METHODS[:allocate],hostname,im,vmm,tm)
88
        end
89

    
90
        # Deletes the Host
91
        def delete()
92
            super(HOST_METHODS[:delete])
93
        end
94

    
95
        # Enables the Host
96
        def enable()
97
            set_enabled(true)
98
        end
99

    
100
        # Disables the Host
101
        def disable()
102
            set_enabled(false)
103
        end
104

    
105
        #######################################################################
106
        # Helpers to get Host information
107
        #######################################################################
108

    
109
        # Returns the state of the Host (numeric value)
110
        def state
111
            self['STATE'].to_i
112
        end
113

    
114
        # Returns the state of the Host (string value)
115
        def state_str
116
            HOST_STATES[state]
117
        end
118

    
119
        # Returns the state of the Host (string value)
120
        def short_state_str
121
            SHORT_HOST_STATES[state_str]
122
        end
123

    
124
        # Returns the cluster ID of the Host
125
        def cluster_id
126
            self['CID']
127
        end
128

    
129

    
130
    private
131
        def set_enabled(enabled)
132
            return Error.new('ID not defined') if !@pe_id
133

    
134
            rc = @client.call(HOST_METHODS[:enable], @pe_id, enabled)
135
            rc = nil if !OpenNebula.is_error?(rc)
136

    
137
            return rc
138
        end
139
    end
140
end