cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
cluster.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.clm.models.cluster
22 # @author Miłosz Zdybał <milosz.zdybal@ifj.edu.pl>
23 #
24 
25 from django.db import models
26 from clm.utils.exception import CLMException
27 from common.states import cluster_states
28 
29 
30 ##
31 #
32 # @model{CLUSTER}
33 #
34 # Cluster is set of virtualization Nodes connected within single network.
35 #
36 class Cluster(models.Model):
37  ## network address of the Cluster @field
38  address = models.CharField(max_length=20)
39  ## port on which Cluster is running @field
40  port = models.IntegerField()
41  ## human-readable name of the Cluster (one that is displayed in web interface) @field
42  name = models.CharField(max_length=40, unique=True)
43  ## whether cluster is available or locked, @seealso{src.common.states.cluster_states} @field
44  state = models.IntegerField()
45 
46  class Meta:
47  app_label = 'clm'
48 
49  def __unicode__(self):
50  return self.name
51 
52  @property
53  ##
54  #
55  # @returns{dict} this Cluster's data
56  # \n fields:
57  # @dictkey{cluster_id,int} id of this Cluster
58  # @dictkey{address,string} address of the this Cluster
59  # @dictkey{port,int} port on which this Cluster works
60  # @dictkey{name,string} name of the this Cluster
61  # @dictkey{state,int} state of the this Cluster, whether it's available or locked, @seealso{src.common.states.cluster_states}
62  #
63  def dict(self):
64  d = {}
65  d['cluster_id'] = self.id
66  d['address'] = self.address
67  d['port'] = self.port
68  d['name'] = self.name
69  d['state'] = self.state
70  return d
71 
72  @property
73  ##
74  #
75  # @returns{dict} this Cluster's shortened data
76  # \n fields:
77  # @dictkey{cluster_id,int} id of this Cluster
78  # @dictkey{name,string} name of the this Cluster
79  # @dictkey{state,int} state of the this Cluster, @seealso{src.common.states.cluster_states}
80  #
81  def short_dict(self):
82  d = {}
83  d['cluster_id'] = self.id
84  d['name'] = self.name
85  d['state'] = self.state
86  return d
87 
88  @staticmethod
89  ##
90  #
91  # @parameter{cluster_id,int} id of the requested Cluster
92  # @returns{Cluster} instance of the requested Cluster
93  #
94  # @raises{cluster_get,CLMException} no such Cluster
95  # @raises{cluster_locked,CLMException} Cluster is locked
96  #
97  def get(cluster_id):
98  try:
99  cluster = Cluster.objects.get(pk=cluster_id)
100  except Cluster.DoesNotExist:
101  raise CLMException('cluster_get')
102  if cluster.state == cluster_states['locked']:
103  raise CLMException('cluster_locked')
104 
105  return cluster
106