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.views.admin_cm.farm
22 # @author Piotr W√≥jcik
23 # @date 26.03.2012
24 #
25 from colorsys import hsv_to_rgb
26 
27 from django.contrib import messages
28 from django.shortcuts import render_to_response
29 from django.template import RequestContext
30 from django.template.loader import render_to_string
31 
32 from common.states import vm_states, farm_states
33 from wi.utils import messages_ajax
34 from wi.utils.decorators import admin_cm_permission
35 from wi.utils.decorators import django_view
36 from wi.utils.exceptions import RestErrorException
37 from wi.utils.formatters import time_from_sec
38 from wi.utils.messages_ajax import ajax_request
39 from wi.utils.states import farm_states_reversed, vm_states_reversed
40 from wi.utils.views import prep_data
41 
42 
43 @django_view
44 @admin_cm_permission
45 ##
46 #
47 # CM admin farms list view.
48 #
49 def cma_farms(request, template_name='admin_cm/show_farms.html'):
50  all_users = []
51 
52  try:
53  all_users = prep_data(('admin_cm/user/get_list/', {'short': True}), request.session)
54  except RestErrorException as ex:
55  messages.error(request, ex.value)
56 
57  return render_to_response(template_name, {'all_users': all_users}, context_instance=RequestContext(request))
58 
59 
60 @django_view
61 @ajax_request
62 @admin_cm_permission
63 ##
64 #
65 # Ajax view for fetching farms list.
66 #
67 def cma_farms_ajax_get_table(request, user_id):
68  if request.method == 'GET':
69  farms = prep_data(('admin_cm/farm/get_list/', {'user_id': int(user_id)}), request.session)
70 
71  for farm in farms:
72  farm['stateName'] = unicode(farm_states_reversed[farm['state']])
73 
74  for vm in farm['vms']:
75  vm['pub_ip'] = []
76  for i in vm['leases']:
77  if i['public_ip'] != "":
78  vm['pub_ip'].append(i['public_ip']['address'])
79 
80  vm['priv_ip'] = []
81  for i in vm['leases']:
82  vm['priv_ip'].append(i['address'])
83 
84  farm['stringIP'] = ', '.join(farm['vms'][0]['priv_ip'])
85  farm['stringPubIP'] = ', '.join(farm['vms'][0]['pub_ip'])
86  farm['stringDisk'] = ', '.join([disk['name'] for disk in farm['vms'][0]['storage_images']])
87  farm['stringISO'] = ', '.join([iso['name'] for iso in farm['vms'][0]['iso_images']])
88 
89  return messages_ajax.success(farms)
90 
91 
92 @django_view
93 @ajax_request
94 @admin_cm_permission
95 ##
96 #
97 # Ajax view for fetching farm details.
98 #
99 def cma_farms_ajax_details(request, id1, template_name='admin_cm/ajax/farm_details.html'):
100  if request.method == 'POST':
101  owner = None
102 
103  farm = prep_data(('admin_cm/farm/get_by_id/', {'farm_id': id1}), request.session)
104  owner = prep_data(('admin_cm/user/get_by_id/', {'user_id': farm['user_id']}), request.session)
105 
106  farm['uptime'] = time_from_sec(farm['uptime'])
107  farm['status'] = farm['state']
108 
109  for vm in farm['vms']:
110  vm['stateName'] = unicode(vm_states_reversed[vm['state']])
111  vm['pub_ip'] = []
112  for i in vm['leases']:
113  if i['public_ip'] != "":
114  vm['pub_ip'].append(i['public_ip']['address'])
115 
116  vm['priv_ip'] = []
117  for i in vm['leases']:
118  vm['priv_ip'].append(i['address'])
119 
120  vm['cpuLoadPercent'] = int(min(float(vm['cpu_load'].get('60') or 0) * 100, 100))
121  vm['cpuLoadColor'] = "#%02x%02x%02x" % tuple(i * 255 for i in hsv_to_rgb(float(vm['cpuLoadPercent']) / 300, 1.0, 0.8))
122 
123  return messages_ajax.success(
124  render_to_string(template_name,
125  {'farm_id': id1,
126  'item': farm,
127  'farm_states_reverse': farm_states_reversed,
128  'farm_states': farm_states,
129  'vm_states': vm_states,
130  'owner': owner},
131  context_instance=RequestContext(request)))
132