def __init__(self, request, vm, *args, **kwargs): super(AdminServerDiskSettingsForm, self).__init__(request, vm, *args, **kwargs) if vm.is_deployed(): self.fields['image'].widget.attrs['disabled'] = 'disabled' self.fields['zpool'].widget.attrs['class'] += ' disable_created2' # Bug #chili-625 img_inc = [disk['image_uuid'] for disk in vm.json_active_get_disks() if 'image_uuid' in disk] else: img_inc = None if vm.is_kvm(): images = [('', _('(none)'))] self.max_disks = DISK_ID_MAX self.fields['zpool'].help_text = _('Setting first disk storage to different value than ' 'server settings storage (%s) is not recommended.') % vm.zpool else: del self.fields['boot'] self.fields['size'].required = False images = [] if vm.is_deployed(): self.max_disks = 0 # Disable creating of new disks if zone is deployed else: self.max_disks = DISK_ID_MAX_OS images.extend([(i.name, i) for i in get_images(request, ostype=vm.ostype, include=img_inc)]) self.fields['image'].choices = images self.fields['zpool'].choices = get_zpools(request).values_list('zpool', 'storage__alias').distinct()
def __init__(self, request, obj, *args, **kwargs): self._disable_globals = kwargs.pop('disable_globals', False) self.table = kwargs.pop('table', False) if self.table: self._field_text_class = '' super(DcSettingsForm, self).__init__(request, obj, *args, **kwargs) from pdns.models import Domain self.fields['VMS_VM_DOMAIN_DEFAULT'].choices = Domain.objects.exclude(Domain.QServerExclude)\ .values_list('name', 'name').order_by('name') self.fields['VMS_DISK_IMAGE_DEFAULT'].choices = [ ('', _('(none)')) ] + list( get_images(request, ostype=None).values_list('name', 'alias')) self.fields['VMS_DISK_IMAGE_ZONE_DEFAULT'].choices = list( get_images(request, ostype=Image.SUNOS_ZONE).values_list('name', 'alias')) self.fields['VMS_DISK_IMAGE_LX_ZONE_DEFAULT'].choices = list( get_images(request, ostype=Image.LINUX_ZONE).values_list('name', 'alias')) self.fields['VMS_NET_DEFAULT'].choices = get_subnets( request).values_list('name', 'alias') self.fields['VMS_STORAGE_DEFAULT'].choices = get_zpools(request).values_list('zpool', 'storage__alias')\ .distinct() self.fields['VMS_VM_SNAPSHOT_SIZE_LIMIT'].addon = ' MB' self.fields['VMS_VM_SNAPSHOT_SIZE_LIMIT_DEFAULT'].addon = ' MB' self.fields['VMS_VM_SNAPSHOT_DC_SIZE_LIMIT'].addon = ' MB' self.fields['VMS_VM_BACKUP_DC_SIZE_LIMIT'].addon = ' MB'
def validate(self, attrs): try: zpool = attrs['zpool'] except KeyError: zpool = self.object.zpool try: node = attrs['node'] except KeyError: node = self.object.node try: attrs['zpool'] = get_zpools(self.request).get(node=node, zpool=zpool) except NodeStorage.DoesNotExist: self._errors['zpool'] = s.ErrorList( [_('Zpool does not exist on node.')]) # Check total number of existing backup definitions - Issue #chili-447 if self.request.method == 'POST': limit = self.request.dc.settings.VMS_VM_BACKUP_DEFINE_LIMIT if limit is not None: total = self._model_.objects.filter(vm=self.object.vm).count() if int(limit) <= total: raise s.ValidationError( _('Maximum number of backup definitions reached.')) return attrs
def __init__(self, request, vm, *args, **kwargs): super(AdminServerSettingsForm, self).__init__(request, vm, *args, **kwargs) dc_settings = request.dc.settings self.is_kvm = is_kvm(vm, self.data, prefix='opt-') # Set choices self.vm_nodes = get_nodes(request, is_compute=True) # TODO: node.color self.fields['node'].choices = [('', _('(auto)'))] + [ (i.hostname, i.hostname) for i in self.vm_nodes ] self.fields['owner'].choices = get_owners(request).values_list( 'username', 'username') self.fields['zpool'].choices = get_zpools(request).values_list( 'zpool', 'storage__alias').distinct() if not request.user.is_staff: self.fields['cpu_shares'].widget.attrs['disabled'] = 'disabled' self.fields['cpu_shares'].widget.attrs[ 'class'] += ' uneditable-input' self.fields['zfs_io_priority'].widget.attrs[ 'disabled'] = 'disabled' self.fields['zfs_io_priority'].widget.attrs[ 'class'] += ' uneditable-input' if dc_settings.MON_ZABBIX_TEMPLATES_VM_RESTRICT: self.fields[ 'monitoring_templates'].widget.tag_choices = dc_settings.MON_ZABBIX_TEMPLATES_VM_ALLOWED if dc_settings.MON_ZABBIX_HOSTGROUPS_VM_RESTRICT: self.fields[ 'monitoring_hostgroups'].widget.tag_choices = dc_settings.MON_ZABBIX_HOSTGROUPS_VM_ALLOWED if vm: empty_template_data = {} self.fields['ostype'].widget.attrs['disabled'] = 'disabled' if vm.is_deployed(): self.fields['node'].widget.attrs[ 'class'] += ' disable_created2' self.fields['zpool'].widget.attrs[ 'class'] += ' disable_created2' else: empty_template_data = self.initial ostype = Vm.OSTYPE # Disable zone support _only_ when adding new VM (zone must be available in edit mode) - Issue #chili-461 if not dc_settings.VMS_ZONE_ENABLED: # Remove SunOS Zone support ostype = [i for i in ostype if i[0] not in Vm.ZONE] self.fields['ostype'].choices = ostype empty_template = AttrDict({ 'alias': _('(none)'), 'desc': '', 'web_data': empty_template_data }) self.fields['template'].choices = [('', empty_template)] + [ (i.name, i) for i in get_templates(request) ]
def generate_storages(self, column_index, row_index=1, label='Storage'): self.update_cell(self.sheet_data, column_index, row_index, label, color=self.header_color, bgcolor=self.header_bgcolor) for row_index, ns in enumerate(get_zpools(self.request).distinct('zpool'), start=row_index + 1): self.update_cell(self.sheet_data, column_index, row_index, ns.zpool) self.add_validation(column_index, row_index, self.get_column_index(label))
def __init__(self, request, vm, *args, **kwargs): super(BackupDefineForm, self).__init__(request, vm, *args, **kwargs) self.fields['retention'].help_text = _( 'Maximum number of backups to keep.') self.fields['node'].choices = get_nodes(request, is_backup=True).values_list( 'hostname', 'hostname') self.fields['zpool'].choices = get_zpools(request).filter(node__is_backup=True) \ .values_list('zpool', 'storage__alias').distinct()
def __init__(self, request, instance, *args, **kwargs): vm_template = kwargs.pop('vm_template', False) self._update_fields_ = list(self._update_fields_) super(BackupDefineSerializer, self).__init__(request, instance, *args, **kwargs) if not kwargs.get('many', False): dc_settings = request.dc.settings backup_nodes = get_nodes(request, is_backup=True) self.fields['node'].queryset = backup_nodes self.fields['zpool'].default = dc_settings.VMS_STORAGE_DEFAULT self.fields[ 'compression'].default = dc_settings.VMS_VM_BACKUP_COMPRESSION_DEFAULT # Set first backup node and backup node storage available in DC # (used only when called by VmDefineBackup.create_from_template()) if vm_template: try: self.fields['node'].default = first_node = backup_nodes[0] except IndexError: pass else: first_node_zpools = get_zpools(request).filter( node=first_node).values_list('zpool', flat=True) if first_node_zpools and dc_settings.VMS_STORAGE_DEFAULT not in first_node_zpools: self.fields['zpool'].default = first_node_zpools[0] if request.method != 'POST': self.fields['type'].read_only = True # Limit maximum number of backups - Issue #chili-447 if dc_settings.VMS_VM_BACKUP_LIMIT is None: min_count, max_count = RETENTION_MIN, RETENTION_MAX else: min_count, max_count = 1, int(dc_settings.VMS_VM_BACKUP_LIMIT) self.fields['retention'].validators.append( validators.MinValueValidator(min_count)) self.fields['retention'].validators.append( validators.MaxValueValidator(max_count)) if instance.vm.is_kvm(): self._update_fields_.append('fsfreeze')
def generate_disks(self): disks = [] images = get_images(self.request, ostype=1) zpools = get_zpools(self.request).distinct('zpool') for i in range(0, random.randint(1, 2)): if len(zpools) > 0: random_zpool = random.randint(0, len(zpools) - 1) zpool = zpools[random_zpool].zpool else: zpool = '' disk = { 'zpool': zpool, 'size': random.choice((10245, 15365, 20485, 25605)), 'image': '', } if i == 0 and len(images) > 0: random_image = random.randint(0, len(images) - 1) disk['image'] = images[random_image].name disks.append(disk) return disks