cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
farm.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.wi.forms.farm
22 #
23 # @author Piotr W√≥jcik
24 # @date 14.11.2011
25 #
26 
27 from django import forms
28 from django.utils.translation import ugettext_lazy as _
29 
30 from wi.forms.vm import EditVMForm
31 from wi.utils import parsing
32 from wi.utils.forms import NumberChoice
33 from wi.utils.views import make_request
34 from wi.utils.widgets import SelectWithDisabled, CheckboxSelectMultipleWithDisabled
35 
36 
37 ##
38 #
39 # First step of form for <b>creating a farm</b>.
40 #
41 class CreateFarmForm1(forms.Form):
42  image_id = forms.CharField(widget=forms.HiddenInput())
43 
44  def __init__(self, *args, **kwargs):
45  super(CreateFarmForm1, self).__init__(*args, **kwargs)
46 
47  ##
48  #
49  # Cast 'image_id' to int.
50  #
51  def clean_image_id(self):
52  if int(self.cleaned_data['image_id']) < 0:
53  raise forms.ValidationError(_("Please select an image."))
54  else:
55  return int(self.cleaned_data['image_id'])
56 
57 
58 ##
59 #
60 # Second step of form for <b>creating a farm</b>.
61 #
62 class CreateFarmForm2(forms.Form):
63  def __init__(self, *args, **kwargs):
64  rest_data = kwargs.pop('rest_data')
65  self.session = kwargs.pop('session')
66  super(CreateFarmForm2, self).__init__(*args, **kwargs)
67 
68  self.fields['head_template_id'] = forms.ChoiceField(choices=parsing.parse_template_names(rest_data),
69  initial=0,
70  widget=SelectWithDisabled(attrs=dict()),
71  label=_('Head template'))
72 
73  self.fields['worker_template_id'] = forms.ChoiceField(choices=parsing.parse_template_names(rest_data),
74  initial=0,
75  widget=SelectWithDisabled(attrs=dict()),
76  label=_('Worker node template'),
77  help_text=_('Template of worker nodes - worker nodes parameters'))
78 
79  self.fields['count'] = forms.ChoiceField(choices=NumberChoice(24),
80  initial=0,
81  widget=forms.Select(attrs=dict({'class': 'xsmall'})),
82  label=_('Number of worker nodes'),
83  help_text=_('Number of worker nodes - (other than head)'))
84 
85  ##
86  #
87  # Cast 'head_template_id' to int.
88  #
90  if int(self.cleaned_data['head_template_id']) < 0:
91  raise forms.ValidationError(_("Please select a template."))
92  else:
93  return int(self.cleaned_data['head_template_id'])
94 
95  ##
96  #
97  # Cast 'worker_template_id' to int.
98  #
100  if int(self.cleaned_data['worker_template_id']) < 0:
101  raise forms.ValidationError(_("Please select a template."))
102  else:
103  return int(self.cleaned_data['worker_template_id'])
104 
105  ##
106  #
107  # Cast 'count' to int.
108  #
109  def clean_count(self):
110  return int(self.cleaned_data['count'])
111 
112  ##
113  #
114  # Checks if there is enough space on CM.
115  #
116  def clean(self):
117  if not self.cleaned_data.get('count') or not self.cleaned_data.get('worker_template_id') or not self.cleaned_data.get('head_template_id'):
118  return None
119 
120  response = make_request('user/farm/check_resources/',
121  {'count': self.cleaned_data.get('count'),
122  'template_id': self.cleaned_data.get('worker_template_id'),
123  'head_template_id': self.cleaned_data.get('head_template_id')
124  }, user=self.session['user'])
125 
126  if response['status'] == 'ok' and response['data'] == False:
127  raise forms.ValidationError(_("Not enough resources. Choose smaller farm or try again later."))
128  else:
129  return self.cleaned_data
130 
131 
132 ##
133 #
134 # Third step of form for <b>creating a farm</b>.
135 #
136 class CreateFarmForm3(forms.Form):
137  def __init__(self, *args, **kwargs):
138  rest_data = kwargs.pop('rest_data')
139  super(CreateFarmForm3, self).__init__(*args, **kwargs)
140 
141  self.fields['public_ip_id'] = forms.ChoiceField(choices=parsing.parse_ips(rest_data),
142  required=False,
143  widget=SelectWithDisabled(attrs=dict()),
144  label=_('Assign IP address'),
145  help_text=_('Public IP address - To request new IP address go to: VM Resources -&gt; Elastic IP addresses'))
146 
147  self.fields['disk_list'] = forms.MultipleChoiceField(choices=parsing.parse_disks(rest_data, False),
148  required=False,
149  widget=CheckboxSelectMultipleWithDisabled,
150  label=_('Attach disk volume'),
151  help_text=_('Virtual disk - '))
152 
153  self.fields['iso_list'] = forms.ChoiceField(choices=parsing.parse_iso(rest_data),
154  required=False,
155  widget=SelectWithDisabled(attrs=dict()),
156  label=_("Attach ISO image"))
157 
158  self.fields['vnc'] = forms.BooleanField(required=False,
159  label=_('VNC'),
160  widget=forms.CheckboxInput(attrs={'class': 'checkbox'}),
161  help_text=_('VNC - Enable/Disable VNC redirection'))
162 
163  self.fields.keyOrder = ['public_ip_id', 'disk_list', 'iso_list', 'vnc']
164 
165  ##
166  #
167  # Cast 'public_ip_id' to int.
168  #
170  return int(self.cleaned_data['public_ip_id'])
171 
172  ##
173  #
174  # Cast each item in 'dist_list' to int.
175  #
176  def clean_disk_list(self):
177  for i in self.cleaned_data['disk_list']:
178  if int(i) < 0:
179  return []
180  return [int(a) for a in self.cleaned_data['disk_list']]
181 
182  ##
183  #
184  # Cast each item in 'iso_list' to int.
185  #
186  def clean_iso_list(self):
187  if int(self.cleaned_data['iso_list']) < 0:
188  return []
189  return [int(self.cleaned_data['iso_list'])]
190 
191 
192 ##
193 #
194 # Final step of form for <b>creating a farm</b>.
195 #
196 class CreateFarmForm4(EditVMForm):
197  def __init__(self, *args, **kwargs):
198  super(CreateFarmForm4, self).__init__(*args, **kwargs)
199  self.fields.keyOrder = ['name', 'description']
200