/
Storage.rb
222 lines (183 loc) · 9.85 KB
/
Storage.rb
1
2
3
4
5
6
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
##############################################################################
# Copyright 2011 Service Computing group, TU Dortmund
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
##############################################################################
# Description: OpenNebula Backend
# Author(s): Hayati Bice, Florian Feldhaus, Piotr Kasprzak
##############################################################################
require 'occi/backend/opennebula/StorageERB'
module OCCI
module Backend
module OpenNebula
# ---------------------------------------------------------------------------------------------------------------------
module Storage
TEMPLATESTORAGERAWFILE = 'occi_one_template_storage.erb'
# ---------------------------------------------------------------------------------------------------------------------
# private
# ---------------------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------------
def storage_parse_backend_object(backend_object)
if backend_object['TEMPLATE/OCCI_ID'].nil?
raise "no backend ID found" if backend_object.id.nil?
occi_id = self.generate_occi_id(OCCI::Infrastructure::Storage::KIND, backend_object.id.to_s)
else
occi_id = backend_object['TEMPLATE/OCCI_ID']
end
attributes = {}
mixins = []
# backend_object.info
attributes = {}
# parse all parameters from OpenNebula to OCCI
attributes['occi.core.id'] = occi_id
attributes['occi.core.title'] = backend_object['NAME']
attributes['occi.core.summary'] = backend_object['TEMPLATE/DESCRIPTION']
attributes['opennebula.image.type'] = backend_object['TEMPLATE/TYPE']
attributes['opennebula.image.public'] = backend_object['TEMPLATE/PUBLIC']
attributes['opennebula.image.persistent'] = backend_object['TEMPLATE/PERSISTENT']
attributes['opennebula.image.dev_prefix'] = backend_object['TEMPLATE/DEV_PREFIX']
attributes['opennebula.image.bus'] = backend_object['TEMPLATE/BUS']
if backend_object['TEMPLATE/SIZE'] != nil
attributes['occi.storage.size'] = backend_object['TEMPLATE/SIZE']
end
if backend_object['TEMPLATE/FSTYPE'] != nil
attributes['occi.storage.fstype']
end
mixins = [OCCI::Backend::ONE::Image::MIXIN]
# check if object already exists
occi_object = OCCI::Rendering::HTTP::LocationRegistry.get_object_by_location('/storage/' + occi_id)
if occi_object.nil?
occi_object = OCCI::Infrastructure::Storage.new(attributes, mixins)
occi_object.backend[:id] = backend_object.id
OCCI::Rendering::HTTP::LocationRegistry.register(occi_object.get_location, occi_object)
else
occi_object.attributes.merge!(attributes)
end
return occi_object
end
# ---------------------------------------------------------------------------------------------------------------------
public
# ---------------------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------------
def storage_deploy(storage)
backend_object = Image.new(Image.build_xml, @one_client)
storage_erb = StorageERB.new
storage_erb.storage = storage
storagelink = nil
if storage.links != nil
storage.links.each do |link|
if link.kind.term == 'storagelink'
$image_path = link.attributes['occi.core.target']
end
end
end
# check creation of images
raise "No image or storagelink provided" if $image_path == ""
template_raw = $config["TEMPLATE_LOCATION"] + TEMPLATESTORAGERAWFILE
template = ERB.new(File.read(template_raw)).result(storage_erb.get_binding)
$log.debug("Parsed template #{template}")
rc = backend_object.allocate(template)
check_rc(rc)
$log.debug("OpenNebula ID of image: #{storage.backend[:id]}")
end
# ---------------------------------------------------------------------------------------------------------------------
def storage_update_state(storage)
backend_object = Image.new(Image.build_xml(storage.backend[:id]), @one_client)
# backend_object.info
$log.debug("current Image state is: #{backend_object.state_str}")
state = case backend_object.state_str
when "READY" , "USED" , "LOCKED" then OCCI::Infrastructure::Storage::STATE_ONLINE
else OCCI::Infrastructure::Storage::STATE_OFFLINE
end
storage.state_machine.set_state(state)
storage.attributes['occi.storage.state'] = storage.state_machine.current_state.name
end
# ---------------------------------------------------------------------------------------------------------------------
def storage_delete(storage)
backend_object = Image.new(Image.build_xml(storage.backend[:id]), @one_client)
rc = backend_object.delete
check_rc(rc)
end
# ---------------------------------------------------------------------------------------------------------------------
def storage_refresh(storage)
backend_object = Image.new(Image.build_xml(storage.backend[:id]), @one_client)
# backend_object.info
occi_object = storage_parse_backend_object(backend_object)
if occi_object.nil? then
$log.warn("Problem refreshing storage with backend id #{storage.backend[:id]}")
else
# merge new attributes with existing attributes, by overwriting existing attributes with refreshed values
storage.attributes.merge!(occi_object.attributes)
# concat mixins and remove duplicate mixins
storage.mixins.concat(occi_object.mixins).uniq!
# update state
storage_update_state(storage)
end
end
# ---------------------------------------------------------------------------------------------------------------------
def storage_register_all_instances
occi_objects = []
backend_object_pool=ImagePool.new(@one_client, OCCI::Backend::OpenNebula::OpenNebula::INFO_ACL)
backend_object_pool.info
backend_object_pool.each do |backend_object|
occi_object = storage_parse_backend_object(backend_object)
if occi_object.nil?
$log.debug("Error creating storage from backend")
else
occi_object.backend[:id] = backend_object.id
$log.debug("Backend ID: #{occi_object.backend[:id]}")
occi_objects << occi_object
end
end
return occi_objects
end
# ---------------------------------------------------------------------------------------------------------------------
# STORAGE ACTIONS
# ---------------------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------------------
def storage_action_dummy(storage, parameters)
end
# ---------------------------------------------------------------------------------------------------------------------
# Action online
def storage_online(network, parameters)
backend_object = Image.new(Image.build_xml(network.backend[:id]), @one_client)
rc = backend_object.enable
check_rc(rc)
end
# ---------------------------------------------------------------------------------------------------------------------
# Action offline
def storage_offline(network, parameters)
backend_object = Image.new(Image.build_xml(network.backend[:id]), @one_client)
rc = backend_object.disable
check_rc(rc)
end
# ---------------------------------------------------------------------------------------------------------------------
# Action backup
def storage_backup(network, parameters)
$log.debug("not yet implemented")
end
# ---------------------------------------------------------------------------------------------------------------------
# Action snapshot
def storage_snapshot(network, parameters)
$log.debug("not yet implemented")
end
# ---------------------------------------------------------------------------------------------------------------------
# Action resize
def storage_resize(network, parameters)
$log.debug("not yet implemented")
end
end
end
end
end