opennebula-oca 3.8.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, 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
+ module OpenNebula
18
+ class DocumentPoolJSON < DocumentPool
19
+
20
+ TEMPLATE_TAG = "BODY"
21
+
22
+ def factory(element_xml)
23
+ doc = OpenNebula::DocumentJSON.new(element_xml, @client)
24
+ doc.load_body
25
+ doc
26
+ end
27
+
28
+ # Generates a json representing the object
29
+ #
30
+ # @param [true, false] pretty_generate
31
+ # @return [String] json representing the object
32
+ #
33
+ def to_json(pretty_generate=true)
34
+ hash = self.to_hash
35
+
36
+ if hash['DOCUMENT_POOL'] && hash['DOCUMENT_POOL']['DOCUMENT']
37
+ if !hash['DOCUMENT_POOL']['DOCUMENT'].instance_of?(Array)
38
+ array = [hash['DOCUMENT_POOL']['DOCUMENT']]
39
+ hash['DOCUMENT_POOL']['DOCUMENT'] = array.compact
40
+ end
41
+
42
+ hash['DOCUMENT_POOL']['DOCUMENT'].each { |doc|
43
+ body = doc['TEMPLATE']["#{TEMPLATE_TAG}"]
44
+ if body
45
+ b_hash = JSON.parse(body)
46
+ doc['TEMPLATE']["#{TEMPLATE_TAG}"] = b_hash
47
+ end
48
+ }
49
+ end
50
+
51
+ if pretty_generate
52
+ JSON.pretty_generate hash
53
+ else
54
+ hash.to_json
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,161 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, 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
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class Group < PoolElement
22
+ #######################################################################
23
+ # Constants and Class Methods
24
+ #######################################################################
25
+
26
+ GROUP_METHODS = {
27
+ :info => "group.info",
28
+ :allocate => "group.allocate",
29
+ :delete => "group.delete",
30
+ :quota => "group.quota"
31
+ }
32
+
33
+ # Flag for requesting connected user's group info
34
+ SELF = -1
35
+
36
+ #Default location for group ACL's
37
+ if ENV['ONE_LOCATION']
38
+ GROUP_DEFAULT = ENV['ONE_LOCATION'] + "/etc/group.default"
39
+ else
40
+ GROUP_DEFAULT = "/etc/one/group.default"
41
+ end
42
+
43
+ # Creates a Group description with just its identifier
44
+ # this method should be used to create plain Group objects.
45
+ # +id+ the id of the user
46
+ #
47
+ # Example:
48
+ # group = Group.new(Group.build_xml(3),rpc_client)
49
+ #
50
+ def Group.build_xml(pe_id=nil)
51
+ if pe_id
52
+ group_xml = "<GROUP><ID>#{pe_id}</ID></GROUP>"
53
+ else
54
+ group_xml = "<GROUP></GROUP>"
55
+ end
56
+
57
+ XMLElement.build_xml(group_xml,'GROUP')
58
+ end
59
+
60
+ # Class constructor
61
+ def initialize(xml, client)
62
+ super(xml,client)
63
+ end
64
+
65
+ #######################################################################
66
+ # Group utils
67
+ #######################################################################
68
+
69
+ # Creates ACLs for the group. The ACL rules are described in a file
70
+ def create_acls(filename = GROUP_DEFAULT)
71
+ if !File.readable?(filename)
72
+ return -1, "Cannot read deafult ACL file for group"
73
+ end
74
+
75
+ msg = String.new
76
+
77
+ File.open(filename).each_line{ |l|
78
+ next if l.match(/^#/)
79
+
80
+ rule = "@#{@pe_id} #{l}"
81
+ parse = OpenNebula::Acl.parse_rule(rule)
82
+
83
+ if OpenNebula.is_error?(parse)
84
+ return -1, "Error parsing rule #{rule}: #{parse.message}"
85
+ end
86
+
87
+ xml = OpenNebula::Acl.build_xml
88
+ acl = OpenNebula::Acl.new(xml, @client)
89
+
90
+ rc = acl.allocate(*parse)
91
+
92
+ if OpenNebula.is_error?(rc)
93
+ return -1, "Error creating rule #{rule}: #{rc.message}"
94
+ else
95
+ msg << "ACL_ID: #{acl.id}\n"
96
+ end
97
+ }
98
+
99
+ return 0, msg
100
+ end
101
+
102
+ #######################################################################
103
+ # XML-RPC Methods for the Group Object
104
+ #######################################################################
105
+
106
+ # Retrieves the information of the given Group.
107
+ def info()
108
+ super(GROUP_METHODS[:info], 'GROUP')
109
+ end
110
+
111
+ # Allocates a new Group in OpenNebula
112
+ #
113
+ # +groupname+ A string containing the name of the Group.
114
+ def allocate(groupname)
115
+ super(GROUP_METHODS[:allocate], groupname)
116
+ end
117
+
118
+ # Deletes the Group
119
+ def delete()
120
+ super(GROUP_METHODS[:delete])
121
+ end
122
+
123
+ # Sets the group quota limits
124
+ # @param quota [String] a template (XML or txt) with the new quota limits
125
+ #
126
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
127
+ # otherwise
128
+ def set_quota(quota)
129
+ return Error.new('ID not defined') if !@pe_id
130
+
131
+ rc = @client.call(GROUP_METHODS[:quota],@pe_id, quota)
132
+ rc = nil if !OpenNebula.is_error?(rc)
133
+
134
+ return rc
135
+ end
136
+
137
+ # ---------------------------------------------------------------------
138
+ # Helpers to get information
139
+ # ---------------------------------------------------------------------
140
+
141
+ # Returns whether or not the user with id 'uid' is part of this group
142
+ def contains(uid)
143
+ #This doesn't work in ruby 1.8.5
144
+ #return self["USERS/ID[.=#{uid}]"] != nil
145
+
146
+ id_array = retrieve_elements('USERS/ID')
147
+ return id_array != nil && id_array.include?(uid.to_s)
148
+ end
149
+
150
+ # Returns an array with the numeric user ids
151
+ def user_ids
152
+ array = Array.new
153
+
154
+ self.each("USERS/ID") do |id|
155
+ array << id.text.to_i
156
+ end
157
+
158
+ return array
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,54 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, 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
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class GroupPool < Pool
22
+ #######################################################################
23
+ # Constants and Class attribute accessors
24
+ #######################################################################
25
+
26
+
27
+ GROUP_POOL_METHODS = {
28
+ :info => "grouppool.info"
29
+ }
30
+
31
+ #######################################################################
32
+ # Class constructor & Pool Methods
33
+ #######################################################################
34
+
35
+ # +client+ a Client object that represents a XML-RPC connection
36
+ def initialize(client)
37
+ super('GROUP_POOL','GROUP',client)
38
+ end
39
+
40
+ # Factory method to create User objects
41
+ def factory(element_xml)
42
+ OpenNebula::Group.new(element_xml,@client)
43
+ end
44
+
45
+ #######################################################################
46
+ # XML-RPC Methods for the User Object
47
+ #######################################################################
48
+
49
+ # Retrieves all the Groups in the pool.
50
+ def info()
51
+ super(GROUP_POOL_METHODS[:info])
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,199 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, 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
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class Host < PoolElement
22
+ #######################################################################
23
+ # Constants and Class Methods
24
+ #######################################################################
25
+
26
+
27
+ HOST_METHODS = {
28
+ :info => "host.info",
29
+ :allocate => "host.allocate",
30
+ :delete => "host.delete",
31
+ :enable => "host.enable",
32
+ :update => "host.update",
33
+ :monitoring => "host.monitoring"
34
+ }
35
+
36
+ HOST_STATES=%w{INIT MONITORING_MONITORED MONITORED ERROR DISABLED MONITORING_ERROR}
37
+
38
+ SHORT_HOST_STATES={
39
+ "INIT" => "init",
40
+ "MONITORING_MONITORED" => "update",
41
+ "MONITORED" => "on",
42
+ "ERROR" => "err",
43
+ "DISABLED" => "off",
44
+ "MONITORING_ERROR" => "retry",
45
+ }
46
+
47
+ # Creates a Host description with just its identifier
48
+ # this method should be used to create plain Host objects.
49
+ # +id+ the id of the host
50
+ #
51
+ # Example:
52
+ # host = Host.new(Host.build_xml(3),rpc_client)
53
+ #
54
+ def Host.build_xml(pe_id=nil)
55
+ if pe_id
56
+ host_xml = "<HOST><ID>#{pe_id}</ID></HOST>"
57
+ else
58
+ host_xml = "<HOST></HOST>"
59
+ end
60
+
61
+ XMLElement.build_xml(host_xml, 'HOST')
62
+ end
63
+
64
+ # Class constructor
65
+ def initialize(xml, client)
66
+ super(xml,client)
67
+
68
+ @client = client
69
+ @pe_id = self['ID'].to_i if self['ID']
70
+ end
71
+
72
+ #######################################################################
73
+ # XML-RPC Methods for the Host
74
+ #######################################################################
75
+
76
+ # Retrieves the information of the given Host.
77
+ def info()
78
+ super(HOST_METHODS[:info], 'HOST')
79
+ end
80
+
81
+ # Allocates a new Host in OpenNebula
82
+ #
83
+ # @param hostname [String] Name of the new Host.
84
+ # @param im [String] Name of the im_driver (information/monitoring)
85
+ # @param vmm [String] Name of the vmm_driver (hypervisor)
86
+ # @param tm [String] Name of the vnm_driver (networking)
87
+ # @param cluster_id [String] Id of the cluster
88
+ #
89
+ # @return [Integer, OpenNebula::Error] the new ID in case of
90
+ # success, error otherwise
91
+ def allocate(hostname,im,vmm,vnm,cluster_id=ClusterPool::NONE_CLUSTER_ID)
92
+ super(HOST_METHODS[:allocate],hostname,im,vmm,vnm,cluster_id)
93
+ end
94
+
95
+ # Deletes the Host
96
+ def delete()
97
+ super(HOST_METHODS[:delete])
98
+ end
99
+
100
+ # Enables the Host
101
+ def enable()
102
+ set_enabled(true)
103
+ end
104
+
105
+ # Disables the Host
106
+ def disable()
107
+ set_enabled(false)
108
+ end
109
+
110
+ def flush()
111
+ self.disable
112
+
113
+ vm_pool = OpenNebula::VirtualMachinePool.new(@client,
114
+ VirtualMachinePool::INFO_ALL_VM)
115
+
116
+ rc = vm_pool.info
117
+ if OpenNebula.is_error?(rc)
118
+ puts rc.message
119
+ exit -1
120
+ end
121
+
122
+ vm_pool.each do |vm|
123
+ hid = vm['HISTORY_RECORDS/HISTORY[last()]/HID']
124
+ if hid == self['ID']
125
+ vm.resched
126
+ end
127
+ end
128
+ end
129
+
130
+ # Replaces the template contents
131
+ #
132
+ # +new_template+ New template contents
133
+ def update(new_template)
134
+ super(HOST_METHODS[:update], new_template)
135
+ end
136
+
137
+ # Retrieves this Host's monitoring data from OpenNebula
138
+ #
139
+ # @param [Array<String>] xpath_expressions Elements to retrieve.
140
+ #
141
+ # @return [Hash<String, Array<Array<int>>>, OpenNebula::Error] Hash with
142
+ # the requested xpath expressions, and an Array of 'timestamp, value'.
143
+ #
144
+ # @example
145
+ # host.monitoring( ['HOST_SHARE/FREE_CPU', 'HOST_SHARE/RUNNING_VMS'] )
146
+ #
147
+ # { "HOST_SHARE/RUNNING_VMS" =>
148
+ # [["1337266000", "1"],
149
+ # ["1337266044", "1"],
150
+ # ["1337266088", "3"]],
151
+ # "HOST_SHARE/FREE_CPU" =>
152
+ # [["1337266000", "800"],
153
+ # ["1337266044", "800"],
154
+ # ["1337266088", "800"]]
155
+ # }
156
+ def monitoring(xpath_expressions)
157
+ return super(HOST_METHODS[:monitoring], 'HOST',
158
+ 'LAST_MON_TIME', xpath_expressions)
159
+ end
160
+
161
+ # Retrieves this Host's monitoring data from OpenNebula, in XML
162
+ #
163
+ # @return [String] Monitoring data, in XML
164
+ def monitoring_xml()
165
+ return Error.new('ID not defined') if !@pe_id
166
+
167
+ return @client.call(HOST_METHODS[:monitoring], @pe_id)
168
+ end
169
+
170
+ #######################################################################
171
+ # Helpers to get Host information
172
+ #######################################################################
173
+
174
+ # Returns the state of the Host (numeric value)
175
+ def state
176
+ self['STATE'].to_i
177
+ end
178
+
179
+ # Returns the state of the Host (string value)
180
+ def state_str
181
+ HOST_STATES[state]
182
+ end
183
+
184
+ # Returns the state of the Host (string value)
185
+ def short_state_str
186
+ SHORT_HOST_STATES[state_str]
187
+ end
188
+
189
+ private
190
+ def set_enabled(enabled)
191
+ return Error.new('ID not defined') if !@pe_id
192
+
193
+ rc = @client.call(HOST_METHODS[:enable], @pe_id, enabled)
194
+ rc = nil if !OpenNebula.is_error?(rc)
195
+
196
+ return rc
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,91 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, 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
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class HostPool < Pool
22
+ #######################################################################
23
+ # Constants and Class attribute accessors
24
+ #######################################################################
25
+
26
+
27
+ HOST_POOL_METHODS = {
28
+ :info => "hostpool.info",
29
+ :monitoring => "hostpool.monitoring"
30
+ }
31
+
32
+ #######################################################################
33
+ # Class constructor & Pool Methods
34
+ #######################################################################
35
+
36
+
37
+ # +client+ a Client object that represents a XML-RPC connection
38
+ def initialize(client)
39
+ super('HOST_POOL','HOST',client)
40
+ end
41
+
42
+ # Factory Method for the Host Pool
43
+ def factory(element_xml)
44
+ OpenNebula::Host.new(element_xml,@client)
45
+ end
46
+
47
+ #######################################################################
48
+ # XML-RPC Methods for the Host Pool
49
+ #######################################################################
50
+
51
+ # Retrieves all the Hosts in the pool.
52
+ def info()
53
+ super(HOST_POOL_METHODS[:info])
54
+ end
55
+
56
+ # Retrieves the monitoring data for all the Hosts in the pool
57
+ #
58
+ # @param [Array<String>] xpath_expressions Elements to retrieve.
59
+ #
60
+ # @return [Hash<String, <Hash<String, Array<Array<int>>>>>,
61
+ # OpenNebula::Error] The first level hash uses the Host ID as keys,
62
+ # and as value a Hash with the requested xpath expressions,
63
+ # and an Array of 'timestamp, value'.
64
+ #
65
+ # @example
66
+ # host_pool.monitoring(
67
+ # ['HOST_SHARE/FREE_CPU',
68
+ # 'HOST_SHARE/RUNNING_VMS',
69
+ # 'TEMPLATE/CUSTOM_PROBE'] )
70
+ #
71
+ # {"1"=>
72
+ # {"TEMPLATE/CUSTOM_PROBE"=>[],
73
+ # "HOST_SHARE/FREE_CPU"=>[["1337609673", "800"]],
74
+ # "HOST_SHARE/RUNNING_VMS"=>[["1337609673", "3"]]},
75
+ # "0"=>
76
+ # {"TEMPLATE/CUSTOM_PROBE"=>[],
77
+ # "HOST_SHARE/FREE_CPU"=>[["1337609673", "800"]],
78
+ # "HOST_SHARE/RUNNING_VMS"=>[["1337609673", "3"]]}}
79
+ def monitoring(xpath_expressions)
80
+ return super(HOST_POOL_METHODS[:monitoring],
81
+ 'HOST', 'LAST_MON_TIME', xpath_expressions)
82
+ end
83
+
84
+ # Retrieves the monitoring data for all the Hosts in the pool, in XML
85
+ #
86
+ # @return [String] VM monitoring data, in XML
87
+ def monitoring_xml()
88
+ return @client.call(HOST_POOL_METHODS[:monitoring])
89
+ end
90
+ end
91
+ end