cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
functions.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.cm.views.ctx.functions
22 #
23 from cm.utils.decorators import ctx_log
24 from cm.utils import log
25 from actions import VERSION
26 import os
27 import json
28 from common.states import command_states
29 from common import response
30 from cm.models.vm import VM
31 from cm.models.command import Command
32 
33 
34 @ctx_log(log=True)
35 ##
36 #
37 # REST stub for hello function
38 #
39 # @param_post{remote_ip,string}
40 # @param_post{kw}
41 # @returns HTTP response
42 #
43 def hello(remote_ip, **kw):
44  vm = VM.get_by_ip(remote_ip)
45  log.info(vm.user_id, "vm called hello")
46  Command.hello(remote_ip)
47 
48  r = response('ok')
49  if int(kw.get('version', 0)) < VERSION:
50  f = file(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'actions.py'), 'r')
51  r['actions_file'] = f.read()
52  f.close()
53  return r
54 
55 
56 @ctx_log(log=True)
57 ##
58 #
59 # @param_post{remote_ip,string}
60 # @param_post{kw,dict} keyword params
61 # @returns{Command} next command from the que to the asking VM
62 #
63 def get_command(remote_ip, **kw):
64  vm = VM.get_by_ip(remote_ip)
65 
66  log.debug(0, "Get first command for %s" % vm.id)
67  command = vm.command_set.filter(state=command_states['pending']).order_by('id')
68  if len(command) == 0:
69  return response('ctx_no_command')
70 
71  command = command[0]
72 
73  log.debug(0, "First command is %s" % command.id)
74  command.state = command_states['executing']
75  command.save()
76 
77  d = command.dict()
78 
79  r = response('ok', d)
80  if int(kw.get('version', 0)) < VERSION:
81  f = file(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'actions.py'), 'r')
82  r['actions_file'] = f.read()
83  f.close()
84  return r
85 
86 
87 @ctx_log(log=True)
88 ##
89 #
90 # REST stub for finish_command
91 #
92 # @param_post{remote_ip,string}
93 # @param_post{command_id,string} hash string identyfing command
94 # @param_post{status,string}
95 # @param_post{returns,dict} dictionary containing VM returned values
96 # @param_post{kw,dict} keyword params
97 #
98 def finish_command(remote_ip, command_id, status, returns=None, **kw):
99  vm = VM.get_by_ip(remote_ip)
100 
101  if returns:
102  returns = json.dumps(returns)
103 
104  log.debug(0, "Select command %s %s" % (command_id, status))
105  try:
106  command = vm.command_set.get(id=command_id)
107  except Command.DoesNotExist:
108  return
109 
110  log.debug(0, "Finish command %s" % command)
111  if command is None:
112  for c in Command.objects.all():
113  log.debug(0, 'Finish - Available cmds id:%s, state:%s, name:%s, vmid:%s' % (c.id, c.state, c.name, c.vm_id))
114  return
115  log.debug(vm.user_id, "command state %s" % command.state)
116 
117  command.response = returns
118  command.state = command_states[status]
119  log.debug(vm.user_id, "Finish command %s" % command.id)
120  command.save()
121 
122  r = response('ok')
123  if int(kw.get('version', 0)) < VERSION:
124  f = file(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'actions.py'), 'r')
125  r['actions_file'] = f.read()
126  f.close()
127  return r
128