cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
storage.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # @cond LICENSE
4 #
5 # Copyright [2010-2013] Institute of Nuclear Physics PAN, Krakow, Poland
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # @endcond LICENSE
20 ##
21 #
22 # @author Maciej Nabozny <mn@mnabozny.pl>
23 #
24 import traceback
25 import libvirt
26 import sys
27 import os
28 
29 try:
30  sys.path.append('/usr/lib/cc1/')
31  os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cm.settings")
32 
33  from cm.models.storage import Storage
34  from common.states import storage_states
35 except Exception, e:
36  print str(e)
37  traceback.print_exc()
38  print "Cannot import CM database model. Probably unconfigured!"
39  sys.exit(1)
40 
41 
42 def mount():
43  conn = libvirt.open('qemu:///system')
44 
45  # Get local libvirt storages
46  lv_storages = []
47  for storage_name in conn.listStoragePools():
48  lv_storages.append(conn.storagePoolLookupByName(storage_name))
49 
50  # Get storage list from cm
51  cm_storages = [storage.name for storage in Storage.objects.filter(state=storage_states['ok'])]
52 
53  # Cleanup old storages and remount if necessary
54  for lv_storage in lv_storages:
55  # Stop and undefine storages, which are not listed in CM or not running
56  if lv_storage.name() in cm_storages and lv_storage.info()[0] != libvirt.VIR_STORAGE_POOL_RUNNING:
57  try:
58  lv_storage.destroy()
59  print "SUCCESS: Storage %s destroyed" % lv_storage.name()
60  except Exception, e:
61  print "ERROR: Cannot destroy storage %s: %s" % (lv_storage.name(), str(e))
62 
63  try:
64  lv_storage.undefine()
65  print "SUCCESS: Storage %s undefined" % lv_storage.name()
66  except Exception, e:
67  print "ERROR: Cannot undefine storage %s"
68  elif lv_storage.name() not in cm_storages and lv_storage.name() != 'images':
69  print "WARNING: Storage %s not exists in ClusterManager!" % lv_storage.name()
70 
71  # Start if not running (and undefine was failed)
72  try:
73  if lv_storage.info()[0] != libvirt.VIR_STORAGE_POOL_RUNNING:
74  try:
75  lv_storage.build(0)
76  except:
77  print "WARNING: Cannot build storage"
78 
79  lv_storage.create(0)
80  except Exception, e:
81  print "ERROR: Cannot start storage pool %s: %s" % (lv_storage.name(), str(e))
82 
83  # Mount new storages
84  for storage in cm_storages:
85  s = Storage.objects.get(name=storage)
86  if storage in conn.listStoragePools() and conn.storagePoolLookupByName(storage).info()[0] == libvirt.VIR_STORAGE_POOL_RUNNING:
87  print "INFO: Storage %s is running" % storage
88  continue
89 
90  try:
91  conn.storagePoolDefineXML(s.libvirt_template(), 0)
92  except Exception, e:
93  print "WARNING: Storage already defined"
94 
95  try:
96  pool = conn.storagePoolLookupByName(s.name)
97  except Exception, e:
98  print "ERROR: Cannot find defined storage: %s" % str(e)
99  s.state = storage_states['locked']
100  s.save()
101  continue
102 
103  try:
104  pool.build(0)
105  pool.create(0)
106  except Exception, e:
107  print "ERROR: Cannot create storage pool: %s" % str(e)
108  s.state = storage_states['locked']
109  s.save()
110 
111  conn.close()
112  return 0
113 
114 
115 def umount():
116  conn = libvirt.open('qemu:///system')
117 
118  # Get local libvirt storages
119  lv_storages = []
120  for storage_name in conn.listStoragePools():
121  lv_storages.append(conn.storagePoolLookupByName(storage_name))
122 
123  # Get storage list from cm
124  cm_storages = [storage.name for storage in Storage.objects.filter(state=storage_states['ok'])]
125 
126  for lv_storage in lv_storages:
127  # Stop and undefine storages, which are not listed in CM or not running
128  if lv_storage.name() in cm_storages:
129  try:
130  lv_storage.destroy()
131  print "SUCCESS: Storage %s destroyed" % lv_storage.name()
132  except Exception, e:
133  print "ERROR: Cannot destroy storage %s: %s" % (lv_storage.name(), str(e))
134 
135  try:
136  lv_storage.undefine()
137  print "SUCCESS: Storage %s undefined" % lv_storage.name()
138  except Exception, e:
139  print "ERROR: Cannot undefine storage %s"
140  elif lv_storage.name() not in cm_storages and lv_storage.name() != 'images':
141  print "WARNING: Storage %s not exists in ClusterManager!" % lv_storage.name()
142 
143  conn.close()
144  return 0
145