cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
vm.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.ci.vm
22 #
23 # @author Maciej Nabozny <mn@mnabozny.pl>
24 # @alldecoratedby{src.cm.utils.decorators.user_log}
25 #
26 
27 from common import log
28 from common.states import vm_states
29 
30 from cm.utils.decorators import ci_log
31 from cm.utils.exception import CMException
32 from cm.utils.threads.vm import VMThread
33 from cm.models.vm import VM
34 from cm.models.node import Node
35 
36 
37 @ci_log(log=True)
38 ##
39 #
40 # @cmview_ci
41 # @param_post{remote_ip,string}
42 # @param_post{vm_name}
43 # @param_post{action}
44 # @param_post{state}
45 #
46 def update_state(remote_ip, vm_name, action, state):
47  try:
48  Node.objects.get(address=remote_ip)
49  except:
50  raise CMException('node_not_found')
51 
52  try:
53  vm_id = int(vm_name.split('-')[1])
54  user_id = int(vm_name.split('-')[2])
55  except:
56  log.debug(0, "Unknown vm from hook: %s" % vm_name)
57  raise CMException('vm_not_found')
58 
59  if action != "stopped":
60  log.debug(user_id, "Not updating vm state: action is %s" % str(action))
61  return ''
62 
63 # try:
64 # conn = libvirt.open(node.conn_string)
65 # dom = conn.lookupByName(vm_name)
66 # log.error(user_id, 'Libvirt domain %s exists!' % vm_name)
67 # raise CMException('vm_exists')
68 # except:
69 # log.debug(user_id, 'Libvirt domain not found. Closing...')
70 
71  try:
72  VM.objects.update()
73  vm = VM.objects.get(id=vm_id)
74  except:
75  log.error(user_id, 'Cannot find vm in database!')
76  raise CMException('vm_not_found')
77 
78  if not vm.state in [vm_states['running ctx'], vm_states['running']]:
79  log.error(user_id, 'VM is not running!')
80  raise CMException('vm_not_running')
81 
82  if vm.state == vm_states['restart']:
83  raise CMException('vm_restart')
84 
85  thread = VMThread(vm, 'delete')
86  thread.start()
87 
88  return ''
89