28 from cm.utils.exception
import CMException
29 from cm.models.storage
import Storage
30 from cm.models.node
import Node
31 from cm.utils
import log
32 from common.states
import storage_states
33 from cm.utils.decorators
import admin_cm_log
37 from django.template
import loader, Context
38 from django.conf
import settings
55 def create(caller_id, name, address, directory, capacity):
58 if Storage.objects.filter(name__exact=name).exists():
59 raise CMException(
'storage_already_exist')
66 st.capacity = capacity
67 st.state = storage_states[
'ok']
70 log.debug(caller_id,
'Cannot register storage - missing element: %s' % str(e))
71 raise CMException(
'storage_create')
76 log.debug(caller_id,
'Cannot save storage in database: %s' % str(e))
77 raise CMException(
'storage_create')
89 return [st.dict
for st
in Storage.objects.all()]
100 def lock(caller_id, storage_id):
102 st = Storage.objects.get(pk=storage_id)
103 st.state = storage_states[
'locked']
106 log.debug(caller_id,
'Cannot lock storage: %s' % str(e))
107 raise CMException(
'storage_lock')
120 st = Storage.objects.get(pk=storage_id)
121 st.state = storage_states[
'ok']
124 log.debug(caller_id,
'Cannot unlock storage: %s' % str(e))
125 raise CMException(
'storage_unlock')
139 def mount(caller_id, storage_id=None, node_id=None):
142 nodes = Node.objects.filter(id__exact=node_id)
144 nodes = Node.objects.all()
148 storages = Storage.objects.filter(id__exact=storage_id)
150 storages = Storage.objects.all()
154 log.debug(caller_id,
"Mounting node: %d" % node.id)
155 storage_response = {}
157 conn = libvirt.open(node.conn_string)
159 log.debug(caller_id,
'Cannot connect to libvirt: %s' % str(e))
162 raise CMException(
'storage_libvirt')
164 for storage
in storages:
166 st_template = loader.get_template(
"storage_%s.xml" % storage.transport)
167 log.info(caller_id,
"Rendered template: %s" % st_template)
169 raise CMException(
'cm_storage_mount')
174 context = Context({
'storage': storage,
'cc_userid': settings.CC_USERID,
'cc_groupid': settings.CC_GROUPID})
175 t = st_template.render(context)
176 log.info(caller_id, t)
179 pool = conn.storagePoolDefineXML(t, 0)
181 log.debug(caller_id,
"Cannot define storage: %s" % str(e))
182 pool = conn.storagePoolLookupByName(storage.name)
186 storage_response[str(storage.id)] =
'ok'
188 log.debug(caller_id,
'Cannot mount storage %d on node %d: %s' % (storage_id, node_id, str(e)))
189 storage_response[str(storage.id)] =
'failed'
190 node_response[str(node.id)] = storage_response
211 node = Node.objects.get(pk=n)
212 conn = libvirt.open(node.conn_string)
213 pools = conn.listStoragePools()
217 if Storage.objects.filter(name=pool).exists():
221 unmounted = Storage.objects.all().exclude(name__in=mounted).values_list(
'name', flat=
True)
222 result[
'%d' % n] = {
'mounted': mounted,
'unmounted': unmounted}