cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
action.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.ec2.base.action
22 #
23 # Superclass for EC2 API actions
24 #
25 # @author Oleksandr Gituliar <oleksandr@gituliar.org>
26 # @copyright: Copyright (c) 2012 IFJ PAN <http://www.ifj.edu.pl/>
27 #
28 
29 
30 from ec2 import lookup
31 from ec2.error import AuthFailure, InvalidAction, MissingParameter
32 
33 
34 class CLMException(Exception):
35 
36  def __init__(self, status, routine_name, routine_args=None):
37  self.routine_args = routine_args
38  self.routine_name = routine_name
39  self.status = status
40 
41  def __str__(self):
42  return "'%s' raised by '%s(%s)'" % \
43  (self.status, self.routine_name, self.routine_args)
44 
45 
46 ##
47 # Superclass for EC2 API actions.
48 class Action(object):
49 
50  def __init__(self, parameters, cluster_manager):
51  self.cluster_manager = cluster_manager
52  self.parameters = parameters
53 
54  ##
55  # Return an object of a concrete EC2 action class.
56  #
57  # Args:
58  # parameters <dict> of the action
59  # cluster_manager <ClusterManager> the action will be run at
60  #
61  def __new__(cls, parameters, cluster_manager):
62  if cls == Action:
63  try:
64  action = parameters['Action']
65  except KeyError:
66  raise MissingParameter(parameter='Action')
67 
68  found = False
69  for concrete_class in cls.__subclasses__():
70  if concrete_class.__name__ == action:
71  found = True
72  break
73  if not found:
74  raise InvalidAction(action=action)
75  else:
76  concrete_class = cls
77  action = super(Action, cls).__new__(concrete_class, parameters, cluster_manager)
78  return action
79 
80  def _get_template(self):
81  name = '%s.xml' % self.parameters.get('Action')
82  return lookup.get_template(name)
83 
84  ##
85  # Execute EC2 action.
86  #
87  # This call is a wrapper around @attr{self.execute_internal} defined in the
88  # subclass of ect.base.action.Action.
89  #
90  def execute(self):
91 
92  try:
93  context = self._execute() or {}
94  except CLMException, error:
95  if error.status == 'user_get':
96  raise AuthFailure()
97  raise
98  template = self._get_template()
99  response = template.render(**context)
100  return response
101