Commit 0dbe809a by Czémán Arnold

dashboard, vm, storage, settings: fix DataStore queries with get_default_datastore()

parent b02d4a32
......@@ -84,6 +84,8 @@ ADMINS = (
EMAIL_SUBJECT_PREFIX = get_env_variable('DJANGO_SUBJECT_PREFIX', '[CIRCLE] ')
DEFAULT_DATASTORE = get_env_variable('DEFAULT_DATASTORE', 'default')
########## END MANAGER CONFIGURATION
......
......@@ -22,6 +22,7 @@ from optparse import make_option
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
from django.conf import settings
from firewall.models import Vlan, VlanGroup, Domain, Firewall, Rule
from storage.models import DataStore
......@@ -73,7 +74,8 @@ class Command(BaseCommand):
admin.set_password(options['admin_pass'])
admin.save()
self.create(DataStore, 'path', path='/datastore', name='default',
self.create(DataStore, 'path', path='/datastore',
name=settings.DEFAULT_DATASTORE,
hostname=options['datastore_queue'])
# leases
......
......@@ -37,13 +37,13 @@ class StorageDetail(SuperuserRequiredMixin, UpdateView):
template_name = "dashboard/storage/detail.html"
def get_object(self):
return DataStore.objects.get()
return DataStore.get_default_datastore()
def get_context_data(self, **kwargs):
context = super(StorageDetail, self).get_context_data(**kwargs)
ds = self.get_object()
try:
ds = self.get_object()
context['stats'] = self._get_stats()
context['missing_disks'] = ds.get_missing_disks()
context['orphan_disks'] = ds.get_orphan_disks()
......
......@@ -37,7 +37,7 @@ except Exception as e:
else:
env.roledefs['node'] = [unicode(n.host.ipv4)
for n in _Node.objects.filter(enabled=True)]
env.roledefs['storage'] = [_DataStore.objects.get().hostname]
env.roledefs['storage'] = [_DataStore.get_default_datastore().hostname]
def update_all():
......
......@@ -24,6 +24,7 @@ from os.path import join
import uuid
import re
from django.conf import settings
from celery.contrib.abortable import AbortableAsyncResult
from django.db.models import (Model, BooleanField, CharField, DateTimeField,
ForeignKey)
......@@ -110,6 +111,16 @@ class DataStore(Model):
disks = Disk.objects.filter(destroyed__isnull=True, is_ready=True)
return disks.exclude(filename__in=files)
@classmethod
def get_default_datastore(cls):
try:
datastore_name = settings.DEFAULT_DATASTORE
if datastore_name:
return cls.objects.get(name=datastore_name)
except cls.DoesNotExist:
pass
return cls.objects.all()[0] # TODO
class Disk(TimeStampedModel):
......@@ -414,7 +425,7 @@ class Disk(TimeStampedModel):
@classmethod
def __create(cls, user, params):
datastore = params.pop('datastore', DataStore.objects.get())
datastore = params.pop('datastore', DataStore.get_default_datastore())
filename = params.pop('filename', str(uuid.uuid4()))
disk = cls(filename=filename, datastore=datastore, **params)
return disk
......
......@@ -493,7 +493,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
datastore = self.disks.all()[0].datastore
except IndexError:
from storage.models import DataStore
datastore = DataStore.objects.get()
datastore = DataStore.get_default_datastore()
path = datastore.path + '/' + self.vm_name + '.dump'
return {'datastore': datastore, 'path': path}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment