oca 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,266 @@
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
+ module OpenNebula
18
+ # Abstract rules of the type USER RESOURCE RIGHTS
19
+ # which are:
20
+ # USER -> #<num>
21
+ # @<num>
22
+ # ALL
23
+ # RESOURCE -> + separated list and "/{#,@}<num>|ALL"
24
+ # VM,
25
+ # HOST
26
+ # NET
27
+ # IMAGE
28
+ # USER
29
+ # TEMPLATE
30
+ # GROUP
31
+ # ACL
32
+ # RIGHTS -> + separated list
33
+ # CREATE
34
+ # DELETE
35
+ # USE
36
+ # MANAGE
37
+ # INFO
38
+ # INFO_POOL
39
+ # INFO_POOL_MINE
40
+ # INSTANTIATE
41
+ # CHOWN
42
+ # DEPLOY
43
+ class Acl < PoolElement
44
+
45
+ USERS = {
46
+ "UID" => 0x100000000,
47
+ "GID" => 0x200000000,
48
+ "ALL" => 0x400000000
49
+ }
50
+
51
+ RESOURCES =
52
+ {
53
+ "VM" => 0x1000000000,
54
+ "HOST" => 0x2000000000,
55
+ "NET" => 0x4000000000,
56
+ "IMAGE" => 0x8000000000,
57
+ "USER" => 0x10000000000,
58
+ "TEMPLATE" => 0x20000000000,
59
+ "GROUP" => 0x40000000000
60
+ }
61
+
62
+ RIGHTS =
63
+ {
64
+ "CREATE" => 0x1, # Auth. to create an object
65
+ "DELETE" => 0x2, # Auth. to delete an object
66
+ "USE" => 0x4, # Auth. to use an object
67
+ "MANAGE" => 0x8, # Auth. to manage an object
68
+ "INFO" => 0x10, # Auth. to view an object
69
+ "INFO_POOL" => 0x20, # Auth. to view any object in the pool
70
+ "INFO_POOL_MINE"=> 0x40, # Auth. to view user and/or group objects
71
+ "INSTANTIATE" => 0x80, # Auth. to instantiate a VM from a TEMPLATE
72
+ "CHOWN" => 0x100,# Auth. to change ownership of an object
73
+ "DEPLOY" => 0x200 # Auth. to deploy a VM in a Host
74
+ }
75
+
76
+ # Constructor
77
+ #
78
+ # @param xml [String] must be an xml built with {#build_xml}
79
+ # @param client [Client] represents an XML-RPC connection
80
+ def initialize(xml, client)
81
+ super(xml,client)
82
+ end
83
+
84
+ # Creates an empty XML representation. It contains the id, if it is
85
+ # specified.
86
+ #
87
+ # @param pe_id [Integer] rule ID
88
+ # @param client [Client] represents an XML-RPC connection
89
+ #
90
+ # @return [String] an empty XML representation
91
+ def self.build_xml(pe_id=nil)
92
+ if pe_id
93
+ acl_xml = "<ACL><ID>#{pe_id}</ID></ACL>"
94
+ else
95
+ acl_xml = "<ACL></ACL>"
96
+ end
97
+
98
+ XMLElement.build_xml(acl_xml,'ACL')
99
+ end
100
+
101
+ # Creates a new ACL rule.
102
+ #
103
+ # @param user [String]
104
+ # A string containing a hex number, e.g. 0x100000001
105
+ # @param resource [String]
106
+ # A string containing a hex number, e.g. 0x2100000001
107
+ # @param rights [String]
108
+ # A string containing a hex number, e.g. 0x10
109
+ #
110
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
111
+ # otherwise
112
+ def allocate(user, resource, rights)
113
+ return super( AclPool::ACL_POOL_METHODS[:addrule],
114
+ user,
115
+ resource,
116
+ rights )
117
+ end
118
+
119
+ # Deletes the Acl rule
120
+ #
121
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
122
+ # otherwise
123
+ def delete()
124
+ super(AclPool::ACL_POOL_METHODS[:delrule])
125
+ end
126
+
127
+ # Does nothing, individual ACL rules info can't be retrieved from
128
+ # OpenNebula
129
+ #
130
+ # @return [nil] nil
131
+ def info()
132
+ return nil
133
+ end
134
+
135
+ # Parses a rule string, e.g. "#5 HOST+VM/@12 INFO+CREATE+DELETE"
136
+ #
137
+ # @param rule_str [String] an ACL rule in string format
138
+ #
139
+ # @return [Array] an Array containing 3 strings (hex 64b numbers),
140
+ # or OpenNebula::Error objects
141
+ def self.parse_rule(rule_str)
142
+ ret = Array.new
143
+
144
+ rule_str = rule_str.split(" ")
145
+
146
+ if rule_str.length != 3
147
+ return OpenNebula::Error.new(
148
+ "String needs three components: User, Resource, Rights")
149
+ end
150
+
151
+ ret << parse_users(rule_str[0])
152
+ ret << parse_resources(rule_str[1])
153
+ ret << parse_rights(rule_str[2])
154
+
155
+ errors=ret.map do |arg|
156
+ if OpenNebula.is_error?(arg)
157
+ arg.message
158
+ else
159
+ nil
160
+ end
161
+ end
162
+
163
+ errors.compact!
164
+
165
+ if errors.length>0
166
+ return OpenNebula::Error.new(errors.join(', '))
167
+ end
168
+
169
+ return ret
170
+ end
171
+
172
+ private
173
+
174
+ # Converts a string in the form [#<id>, @<id>, *] to a hex. number
175
+ #
176
+ # @param users [String] Users component string
177
+ #
178
+ # @return [String] A string containing a hex number
179
+ def self.parse_users(users)
180
+ begin
181
+ return calculate_ids(users).to_i.to_s(16)
182
+ rescue Exception => e
183
+ return OpenNebula::Error.new(e.message)
184
+ end
185
+ end
186
+
187
+ # Converts a resources string to a hex. number
188
+ #
189
+ # @param resources [String] Resources component string
190
+ #
191
+ # @return [String] A string containing a hex number
192
+ def self.parse_resources(resources)
193
+ begin
194
+ ret = 0
195
+ resources = resources.split("/")
196
+
197
+ if resources.size != 2
198
+ raise "Resource '#{resources}' malformed"
199
+ end
200
+
201
+ resources[0].split("+").each{ |resource|
202
+ if !RESOURCES[resource.upcase]
203
+ raise "Resource '#{resource}' does not exist"
204
+ end
205
+ ret += RESOURCES[resource.upcase]
206
+ }
207
+
208
+ ret += calculate_ids(resources[1])
209
+
210
+ return ret.to_i.to_s(16)
211
+ rescue Exception => e
212
+ return OpenNebula::Error.new(e.message)
213
+ end
214
+ end
215
+
216
+ # Converts a rights string to a hex. number
217
+ #
218
+ # @param rights [String] Rights component string
219
+ #
220
+ # @return [String] A string containing a hex number
221
+ def self.parse_rights(rights)
222
+ begin
223
+ ret = 0
224
+ rights = rights.split("+")
225
+
226
+ rights.each{ |right|
227
+ raise "Right '#{right}' does not exist" if !RIGHTS[right.upcase]
228
+
229
+ ret += RIGHTS[right.upcase]
230
+ }
231
+
232
+ return ret.to_i.to_s(16)
233
+ rescue Exception => e
234
+ return OpenNebula::Error.new(e.message)
235
+ end
236
+ end
237
+
238
+ # Calculates the numeric value for a String containing an individual
239
+ # (#<id>), group (@<id>) or all (*) ID component
240
+ #
241
+ # @param id_str [String] Rule Id string
242
+ #
243
+ # @return [Integer] the numeric value for the given id_str
244
+ def self.calculate_ids(id_str)
245
+ raise "ID string '#{id_str}' malformed" if
246
+ !id_str.match(/^([\#@]\d+|\*)$/)
247
+
248
+ value = 0
249
+
250
+ case id_str[0..0]
251
+ when "#"
252
+ value = USERS["UID"]
253
+ users_value = id_str[1..-1].to_i + value
254
+
255
+ when "@"
256
+ value = USERS["GID"]
257
+ users_value = id_str[1..-1].to_i + value
258
+
259
+ when "*"
260
+ users_value = USERS["ALL"]
261
+ end
262
+
263
+ return users_value
264
+ end
265
+ end
266
+ end
@@ -0,0 +1,52 @@
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 AclPool < Pool
21
+
22
+ #######################################################################
23
+ # Constants and Class Methods
24
+ #######################################################################
25
+ ACL_POOL_METHODS = {
26
+ :info => "acl.info",
27
+ :addrule => "acl.addrule",
28
+ :delrule => "acl.delrule"
29
+ }
30
+
31
+ #######################################################################
32
+ # Class constructor
33
+ #######################################################################
34
+ def initialize(client)
35
+ super('ACL_POOL','ACL',client)
36
+ end
37
+
38
+ def factory(element_xml)
39
+ OpenNebula::Acl.new(element_xml, @client)
40
+ end
41
+
42
+ #######################################################################
43
+ # XML-RPC Methods
44
+ #######################################################################
45
+
46
+ # Retrieves the ACL Pool
47
+ def info()
48
+ # Retrieves all the Acls in the pool.
49
+ super(ACL_POOL_METHODS[:info])
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,148 @@
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 Group < PoolElement
21
+ # ---------------------------------------------------------------------
22
+ # Constants and Class Methods
23
+ # ---------------------------------------------------------------------
24
+ GROUP_METHODS = {
25
+ :info => "group.info",
26
+ :allocate => "group.allocate",
27
+ :delete => "group.delete"
28
+ }
29
+
30
+ # Flag for requesting connected user's group info
31
+ SELF = -1
32
+
33
+ #Default location for group ACL's
34
+ if ENV['ONE_LOCATION']
35
+ GROUP_DEFAULT = ENV['ONE_LOCATION'] + "/etc/group.default"
36
+ else
37
+ GROUP_DEFAULT = "/etc/one/group.default"
38
+ end
39
+
40
+ # Creates a Group description with just its identifier
41
+ # this method should be used to create plain Group objects.
42
+ # +id+ the id of the user
43
+ #
44
+ # Example:
45
+ # group = Group.new(Group.build_xml(3),rpc_client)
46
+ #
47
+ def Group.build_xml(pe_id=nil)
48
+ if pe_id
49
+ group_xml = "<GROUP><ID>#{pe_id}</ID></GROUP>"
50
+ else
51
+ group_xml = "<GROUP></GROUP>"
52
+ end
53
+
54
+ XMLElement.build_xml(group_xml,'GROUP')
55
+ end
56
+
57
+ # ---------------------------------------------------------------------
58
+ # Class constructor
59
+ # ---------------------------------------------------------------------
60
+ def initialize(xml, client)
61
+ super(xml,client)
62
+
63
+ @client = client
64
+ end
65
+
66
+ # --------------------------------------------------------------------
67
+ # Group utils
68
+ # --------------------------------------------------------------------
69
+
70
+ # Creates ACLs for the group. The ACL rules are described in a file
71
+ def create_acls(filename = GROUP_DEFAULT)
72
+ if !File.readable?(filename)
73
+ return -1, "Can not read deafult ACL file for group"
74
+ end
75
+
76
+ msg = String.new
77
+
78
+ File.open(filename).each_line{ |l|
79
+ next if l.match(/^#/)
80
+
81
+ rule = "@#{@pe_id} #{l}"
82
+ parse = OpenNebula::Acl.parse_rule(rule)
83
+
84
+ if OpenNebula.is_error?(parse)
85
+ return -1, "Error parsing rule #{rule}: #{parse.message}"
86
+ end
87
+
88
+ xml = OpenNebula::Acl.build_xml
89
+ acl = OpenNebula::Acl.new(xml, @client)
90
+
91
+ rc = acl.allocate(*parse)
92
+
93
+ if OpenNebula.is_error?(rc)
94
+ return -1, "Error creating rule #{rule}: #{rc.message}"
95
+ else
96
+ msg << "ACL_ID: #{acl.id}\n"
97
+ end
98
+ }
99
+
100
+ return 0, msg
101
+ end
102
+
103
+ # ---------------------------------------------------------------------
104
+ # XML-RPC Methods for the Group Object
105
+ # ---------------------------------------------------------------------
106
+
107
+ # Retrieves the information of the given Group.
108
+ def info()
109
+ super(GROUP_METHODS[:info], 'GROUP')
110
+ end
111
+
112
+ # Allocates a new Group in OpenNebula
113
+ #
114
+ # +groupname+ A string containing the name of the Group.
115
+ def allocate(groupname)
116
+ super(GROUP_METHODS[:allocate], groupname)
117
+ end
118
+
119
+ # Deletes the Group
120
+ def delete()
121
+ super(GROUP_METHODS[:delete])
122
+ end
123
+
124
+ # ---------------------------------------------------------------------
125
+ # Helpers to get information
126
+ # ---------------------------------------------------------------------
127
+
128
+ # Returns whether or not the user with id 'uid' is part of this group
129
+ def contains(uid)
130
+ # This doesn't work in ruby 1.8.5
131
+ # return self["USERS/ID[.=#{uid}]"] != nil
132
+
133
+ id_array = retrieve_elements('USERS/ID')
134
+ return id_array != nil && id_array.include?(uid.to_s)
135
+ end
136
+
137
+ # Returns an array with the numeric user ids
138
+ def user_ids
139
+ array = Array.new
140
+
141
+ self.each("USERS/ID") do |id|
142
+ array << id.text.to_i
143
+ end
144
+
145
+ return array
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,52 @@
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 GroupPool < Pool
21
+ # ---------------------------------------------------------------------
22
+ # Constants and Class attribute accessors
23
+ # ---------------------------------------------------------------------
24
+
25
+ GROUP_POOL_METHODS = {
26
+ :info => "grouppool.info"
27
+ }
28
+
29
+ # ---------------------------------------------------------------------
30
+ # Class constructor & Pool Methods
31
+ # ---------------------------------------------------------------------
32
+
33
+ # +client+ a Client object that represents a XML-RPC connection
34
+ def initialize(client)
35
+ super('GROUP_POOL','GROUP',client)
36
+ end
37
+
38
+ # Factory method to create User objects
39
+ def factory(element_xml)
40
+ OpenNebula::Group.new(element_xml,@client)
41
+ end
42
+
43
+ # ---------------------------------------------------------------------
44
+ # XML-RPC Methods for the User Object
45
+ # ---------------------------------------------------------------------
46
+
47
+ # Retrieves all the Groups in the pool.
48
+ def info()
49
+ super(GROUP_POOL_METHODS[:info])
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,142 @@
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
+ :update => "host.update"
30
+ }
31
+
32
+ HOST_STATES=%w{INIT MONITORING MONITORED ERROR DISABLED}
33
+
34
+ SHORT_HOST_STATES={
35
+ "INIT" => "on",
36
+ "MONITORING" => "on",
37
+ "MONITORED" => "on",
38
+ "ERROR" => "err",
39
+ "DISABLED" => "off"
40
+ }
41
+
42
+ # Creates a Host description with just its identifier
43
+ # this method should be used to create plain Host objects.
44
+ # +id+ the id of the host
45
+ #
46
+ # Example:
47
+ # host = Host.new(Host.build_xml(3),rpc_client)
48
+ #
49
+ def Host.build_xml(pe_id=nil)
50
+ if pe_id
51
+ host_xml = "<HOST><ID>#{pe_id}</ID></HOST>"
52
+ else
53
+ host_xml = "<HOST></HOST>"
54
+ end
55
+
56
+ XMLElement.build_xml(host_xml, 'HOST')
57
+ end
58
+
59
+ #######################################################################
60
+ # Class constructor
61
+ #######################################################################
62
+ def initialize(xml, client)
63
+ super(xml,client)
64
+
65
+ @client = client
66
+ @pe_id = self['ID'].to_i if self['ID']
67
+ end
68
+
69
+ #######################################################################
70
+ # XML-RPC Methods for the Host
71
+ #######################################################################
72
+
73
+ # Retrieves the information of the given Host.
74
+ def info()
75
+ super(HOST_METHODS[:info], 'HOST')
76
+ end
77
+
78
+ # Allocates a new Host in OpenNebula
79
+ #
80
+ # +hostname+ A string containing the name of the new Host.
81
+ #
82
+ # +im+ A string containing the name of the im_driver
83
+ #
84
+ # +vmm+ A string containing the name of the vmm_driver
85
+ #
86
+ # +tm+ A string containing the name of the tm_driver
87
+ def allocate(hostname,im,vmm,tm)
88
+ super(HOST_METHODS[:allocate],hostname,im,vmm,tm)
89
+ end
90
+
91
+ # Deletes the Host
92
+ def delete()
93
+ super(HOST_METHODS[:delete])
94
+ end
95
+
96
+ # Enables the Host
97
+ def enable()
98
+ set_enabled(true)
99
+ end
100
+
101
+ # Disables the Host
102
+ def disable()
103
+ set_enabled(false)
104
+ end
105
+
106
+ # Replaces the template contents
107
+ #
108
+ # +new_template+ New template contents
109
+ def update(new_template)
110
+ super(HOST_METHODS[:update], new_template)
111
+ end
112
+
113
+ #######################################################################
114
+ # Helpers to get Host information
115
+ #######################################################################
116
+
117
+ # Returns the state of the Host (numeric value)
118
+ def state
119
+ self['STATE'].to_i
120
+ end
121
+
122
+ # Returns the state of the Host (string value)
123
+ def state_str
124
+ HOST_STATES[state]
125
+ end
126
+
127
+ # Returns the state of the Host (string value)
128
+ def short_state_str
129
+ SHORT_HOST_STATES[state_str]
130
+ end
131
+
132
+ private
133
+ def set_enabled(enabled)
134
+ return Error.new('ID not defined') if !@pe_id
135
+
136
+ rc = @client.call(HOST_METHODS[:enable], @pe_id, enabled)
137
+ rc = nil if !OpenNebula.is_error?(rc)
138
+
139
+ return rc
140
+ end
141
+ end
142
+ end