class _ImageForm(SerializerForm): """ Base class used by AdminImageForm below and servers.forms.ImageForm. """ name = forms.CharField(label=_('Name'), max_length=32, required=True, widget=forms.TextInput(attrs={'class': 'input-transparent narrow disable_created', 'required': 'required', 'pattern': '[A-Za-z0-9._-]+'})) alias = forms.CharField(label=_('Alias'), required=True, max_length=32, widget=forms.TextInput(attrs={'class': 'input-transparent narrow', 'required': 'required'})) version = forms.CharField(label=_('Version'), required=False, max_length=16, widget=forms.TextInput(attrs={'class': 'input-transparent narrow', 'required': 'required'})) owner = forms.ChoiceField(label=_('Owner'), required=False, widget=forms.Select(attrs={'class': 'narrow input-select2'})) access = forms.TypedChoiceField(label=_('Access'), required=False, coerce=int, choices=Image.ACCESS, widget=forms.Select(attrs={'class': 'narrow input-select2'})) desc = forms.CharField(label=_('Description'), max_length=128, required=False, widget=forms.TextInput(attrs={'class': 'input-transparent wide', 'required': ''})) resize = forms.BooleanField(label=_('Resizable?'), required=False, help_text=_('Image is able to resize the disk during ' 'an initial start or deploy process.'), widget=forms.CheckboxInput(attrs={'class': 'normal-check'})) deploy = forms.BooleanField(label=_('Shutdown after deploy?'), required=False, help_text=_('Image is able to shut down the server after ' 'an initial start and successful deploy.'), widget=forms.CheckboxInput(attrs={'class': 'normal-check'})) tags = TagField(label=_('Tags'), required=False, help_text=_('Tags will be inherited by servers which will use this image.'), widget=TagWidget(attrs={'class': 'tags-select2 narrow'})) def __init__(self, request, img, *args, **kwargs): super(_ImageForm, self).__init__(request, img, *args, **kwargs) self.fields['owner'].choices = get_owners(request).values_list('username', 'username') self.fields['tags'].tag_choices = TagVm.objects.distinct().filter( content_object__in=Vm.objects.filter(dc=request.dc) ).order_by('tag__name').values_list('tag__name', flat=True)
class AdminServerSettingsForm(ServerSettingsForm): """ Copy of vm_define serializer (api). """ admin = True _api_call = vm_define tags = TagField(label=_('Tags'), required=False, widget=TagWidget(attrs={'class': 'tags-select2 narrow'}), help_text=_('The tag will be created in case it does not exist.')) node = forms.TypedChoiceField(label=_('Node'), required=False, coerce=str, empty_value=None, widget=forms.Select(attrs={'class': 'narrow input-select2'})) template = forms.TypedChoiceField(label=_('Template'), required=False, coerce=str, empty_value=None, help_text=_('Setting template can modify lots of server attributes, ' 'e.g. disks, nics.'), widget=DataSelect(attrs={'class': 'narrow input-select2'})) ostype = forms.TypedChoiceField(label=_('OS Type'), choices=Vm.OSTYPE, required=True, coerce=int, widget=forms.Select(attrs={'class': 'input-select2 narrow', 'required': 'required'})) vcpus = forms.IntegerField(label=_('VCPUs'), max_value=64, min_value=1, required=True, widget=NumberInput(attrs={'class': 'input-transparent narrow', 'required': 'required'})) # noinspection SpellCheckingInspection ram = forms.IntegerField(label=_('RAM'), max_value=524288, min_value=32, required=True, widget=forms.TextInput(attrs={'class': 'input-transparent narrow input-mbytes', 'required': 'required', 'pattern': '[0-9\.]+[BKMGTPEbkmgtpe]?'})) monitored = forms.BooleanField(label=_('Monitored?'), required=False, widget=forms.CheckboxInput(attrs={'class': 'normal-check'})) installed = forms.BooleanField(label=_('Installed?'), required=False, help_text=_('This field is used for informational purposes only.'), widget=forms.CheckboxInput(attrs={'class': 'normal-check'})) snapshot_limit_manual = forms.IntegerField(label=_('Snapshot count limit'), required=False, widget=NumberInput(attrs={'class': 'input-transparent narrow'}), help_text=_('Maximum number of manual server snapshots.')) snapshot_size_limit = forms.IntegerField(label=_('Snapshot size limit'), required=False, widget=NumberInput(attrs={'class': 'input-transparent narrow'}), help_text=_('Maximum size of all server snapshots.')) cpu_shares = forms.IntegerField(label=_('CPU Shares'), max_value=1048576, min_value=0, required=True, widget=NumberInput(attrs={'class': 'input-transparent narrow', 'required': 'required'})) # cpu_cap = forms.IntegerField(label=_('CPU Capping'), max_value=6400, min_value=0, required=False, # widget=NumberInput(attrs={'class': 'input-transparent narrow'})) zfs_io_priority = forms.IntegerField(label=_('IO Priority'), max_value=1024, min_value=0, required=True, widget=NumberInput(attrs={'class': 'input-transparent narrow', 'required': 'required'})) zpool = forms.ChoiceField(label=_('Storage'), required=False, widget=forms.Select(attrs={'class': 'narrow input-select2'})) owner = forms.ChoiceField(label=_('Owner'), required=False, widget=forms.Select(attrs={'class': 'narrow input-select2'})) monitoring_templates = ArrayField(label=_('Monitoring templates'), required=False, help_text=_('Comma-separated list of custom monitoring templates.'), widget=ArrayWidget(attrs={'class': 'input-transparent narrow'})) monitoring_hostgroups = ArrayField(label=_('Monitoring hostgroups'), required=False, help_text=_('Comma-separated list of custom monitoring hostgroups.'), widget=ArrayWidget(attrs={'class': 'input-transparent narrow'})) mdata = DictField(label=_('Metadata'), required=False, help_text=_('key=value string pairs.'), widget=DictWidget(attrs={ 'class': 'input-transparent small', 'rows': 5, 'data-raw_input_enabled': 'true', })) def __init__(self, request, vm, *args, **kwargs): super(AdminServerSettingsForm, self).__init__(request, vm, *args, **kwargs) 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 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 # Remove Linux Zone support (lx brand) ostype = [i for i in Vm.OSTYPE if i[0] != Vm.LINUX_ZONE] # Disable zone support _only_ when adding new VM (zone must be available in edit mode) - Issue #chili-461 if not request.dc.settings.VMS_ZONE_ENABLED: # Remove SunOS Zone support ostype = [i for i in ostype if i[0] != Vm.SUNOS_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 _initial_data(self, request, vm): fix = super(AdminServerSettingsForm, self)._initial_data(request, vm) ret = get_vm_define(request, vm) # We need string representation of tags, but vm_define returns a list if 'tags' in ret: ret['tags'] = tags_to_string(ret['tags']) # Some serializer data need to be replaced by data expected by the parent form ret.update(fix) return ret
class AdminServerSettingsForm(ServerSettingsForm): """ Copy of vm_define serializer (api). """ admin = True _api_call = vm_define tags = TagField( label=_('Tags'), required=False, widget=TagWidget(attrs={'class': 'tags-select2 narrow'}), help_text=_('The tag will be created in case it does not exist.')) node = forms.TypedChoiceField( label=_('Node'), required=False, coerce=str, empty_value=None, widget=forms.Select(attrs={'class': 'narrow input-select2'})) template = forms.TypedChoiceField( label=_('Template'), required=False, coerce=str, empty_value=None, help_text=_('Setting template can modify lots of server attributes, ' 'e.g. disks, nics.'), widget=DataSelect(attrs={'class': 'narrow input-select2'})) ostype = forms.TypedChoiceField( label=_('OS Type'), choices=Vm.OSTYPE, required=True, coerce=int, widget=forms.Select( attrs={ 'class': 'input-select2 narrow ostype-select', 'required': 'required', 'onChange': 'update_vm_form_fields_from_ostype()' })) hvm_type = forms.TypedChoiceField( label=_('Hypervisor Type'), choices=Vm.HVM_TYPE_GUI, required=False, coerce=int, widget=forms.Select( attrs={ 'class': 'input-select2 narrow hvm-type-select', 'required': 'required', 'onChange': 'update_vm_form_fields_from_hvm_type()' })) vcpus = forms.IntegerField( label=_('VCPUs'), required=False, widget=NumberInput(attrs={ 'class': 'input-transparent narrow', 'required': 'required' })) # noinspection SpellCheckingInspection ram = forms.IntegerField( label=_('RAM'), required=False, widget=forms.TextInput( attrs={ 'class': 'input-transparent narrow input-mbytes', 'required': 'required', 'pattern': '[0-9.]+[BKMGTPEbkmgtpe]?' })) note = forms.CharField( label=_('Note'), help_text=_( 'Text with markdown support, visible to every user with access ' 'to this server.'), required=False, widget=forms.Textarea(attrs={ 'class': 'input-transparent', 'rows': 5 })) monitored = forms.BooleanField( label=_('Monitored?'), required=False, widget=forms.CheckboxInput(attrs={'class': 'normal-check'})) installed = forms.BooleanField( label=_('Installed?'), required=False, help_text=_('This field is used for informational purposes only.'), widget=forms.CheckboxInput(attrs={'class': 'normal-check'})) snapshot_limit_manual = forms.IntegerField( label=_('Snapshot count limit'), required=False, widget=NumberInput(attrs={'class': 'input-transparent narrow'}), help_text=_('Maximum number of manual server snapshots.')) snapshot_size_percent_limit = forms.IntegerField( label=_('Snapshot size % limit'), required=False, widget=NumberInput(attrs={'class': 'input-transparent narrow'}), help_text=_( 'Maximum size of all server snapshots as % of all disk space ' 'of this VM (example: 200% = VM with 10GB disk(s) can have ' '20GB of snapshots).')) snapshot_size_limit = forms.IntegerField( label=_('Snapshot size limit'), required=False, widget=NumberInput(attrs={'class': 'input-transparent narrow'}), help_text=_('Maximum size of all server snapshots. ' 'If set, it takes precedence over % limit.')) cpu_shares = forms.IntegerField( label=_('CPU Shares'), max_value=1048576, min_value=0, required=True, widget=NumberInput(attrs={ 'class': 'input-transparent narrow', 'required': 'required' })) # cpu_cap = forms.IntegerField(label=_('CPU Capping'), max_value=6400, min_value=0, required=False, # widget=NumberInput(attrs={'class': 'input-transparent narrow'})) zfs_io_priority = forms.IntegerField( label=_('IO Priority'), max_value=1024, min_value=0, required=True, widget=NumberInput(attrs={ 'class': 'input-transparent narrow', 'required': 'required' })) bootrom = forms.ChoiceField( label=_('Bootrom'), required=False, widget=forms.Select(attrs={'class': 'narrow input-select2'})) zpool = forms.ChoiceField( label=_('Storage'), required=False, widget=forms.Select(attrs={'class': 'narrow input-select2'})) owner = forms.ChoiceField( label=_('Owner'), required=False, widget=forms.Select(attrs={'class': 'narrow input-select2'})) monitoring_templates = ArrayField( label=_('Monitoring templates'), required=False, tags=True, help_text=_('Comma-separated list of custom monitoring templates.'), widget=ArrayWidget(tags=True, escape_space=False, attrs={'class': 'tags-select2 narrow'})) monitoring_hostgroups = ArrayField( label=_('Monitoring hostgroups'), required=False, tags=True, validators=[ RegexValidator(regex=MonitoringBackend.RE_MONITORING_HOSTGROUPS) ], help_text=_('Comma-separated list of custom monitoring hostgroups.'), widget=ArrayWidget(tags=True, escape_space=False, attrs={'class': 'tags-select2 narrow'})) mdata = DictField(label=_('Metadata'), required=False, help_text=_('key=value string pairs.'), widget=DictWidget( attrs={ 'class': 'input-transparent small', 'rows': 5, 'data-raw_input_enabled': 'true', })) def __init__(self, request, vm, *args, **kwargs): super(AdminServerSettingsForm, self).__init__(request, vm, *args, **kwargs) dc_settings = request.dc.settings # 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() self.fields['bootrom'].choices = Vm.BHYVE_BOOTROM 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 dc_settings.MON_ZABBIX_HOSTGROUPS_VM: self.fields['monitoring_hostgroups'].help_text += _(' Automatically added hostgroups: ') \ + ', '.join(dc_settings.MON_ZABBIX_HOSTGROUPS_VM) if vm: empty_template_data = {} self.fields['ostype'].widget.attrs['disabled'] = 'disabled' self.fields['hvm_type'].widget.attrs['disabled'] = 'disabled' if not vm.is_hvm(): # for zones the only HVM choice is NO hypervisor self.fields['hvm_type'].choices = Vm.HVM_TYPE_GUI_NO_HYPERVISOR 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_OSTYPES] 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 _initial_data(self, request, vm): fix = super(AdminServerSettingsForm, self)._initial_data(request, vm) ret = get_vm_define(request, vm) # We need string representation of tags, but vm_define returns a list if 'tags' in ret: ret['tags'] = tags_to_string(ret['tags']) # Some serializer data need to be replaced by data expected by the parent form ret.update(fix) return ret