cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
libvirt.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # @cond LICENSE
4 #
5 # Copyright [2010-2013] Institute of Nuclear Physics PAN, Krakow, Poland
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # @endcond LICENSE
20 
21 ##
22 # @package src.node.tools.libvirt
23 # @author Maciej Nabozny <mn@mnabozny.pl>
24 #
25 
26 import os
27 import sys
28 import stat
29 import subprocess
30 
31 sys.path.append('/usr/lib/cc1/')
32 
33 from common.tools import config
34 
35 
36 def configure(address, username, transport, driver, suffix):
37  print "NODE: Updating libvirt config"
38  qemu = open('/etc/libvirt/qemu.conf', 'r')
39  qemu_conf = qemu.readlines()
40  qemu.close()
41 
42  qemu = open('/etc/libvirt/qemu.conf', 'w')
43  for line in qemu_conf:
44  if '#user = "root"' in line:
45  qemu.write('user = "root"\n')
46  elif '#group = "root"' in line:
47  qemu.write('group = "root"\n')
48  elif '#dynamic_ownership = 1' in line:
49  qemu.write('dynamic_ownership = 0')
50  else:
51  qemu.write(line)
52  qemu.close()
53  os.chmod('/etc/libvirt/qemu.conf', stat.S_IROTH | stat.S_IRUSR | stat.S_IRGRP | stat.S_IWUSR)
54 
55  config.update('node', 'lv_conn_string', '%s+%s://%s@%s/%s' % (driver, transport, username, address, suffix))
56 
57  print "NODE: Adding cc1 user to libvirt groups"
58  subprocess.call("usermod -a -G libvirt,kvm cc1", shell=True)
59 
60  print "NODE: Restarting libvirt"
61  subprocess.call('/etc/init.d/libvirt-bin restart', shell=True)
62 
63  return 0
64 
65 
66 def remove():
67  print "NODE: Removing package changes from libvirt config"
68  qemu = open('/etc/libvirt/qemu.conf', 'r')
69  qemu_conf = qemu.readlines()
70  qemu.close()
71 
72  qemu = open('/etc/libvirt/qemu.conf', 'w')
73  for line in qemu_conf:
74  if 'user = "root"' in line:
75  qemu.write('#user = "root"\n')
76  elif 'group = "root"' in line:
77  qemu.write('#group = "root"\n')
78  elif 'dynamic_ownership = 0' in line:
79  qemu.write('#dynamic_ownership = 1')
80  else:
81  qemu.write(line)
82  qemu.close()
83 
84 
85 def purge():
86  remove()
87  return 0
88