Mostly aesthetic changes and fix a bug in codemux.
[nodemanager.git] / sliver_libvirt.py
1 """LibVirt slivers"""
2
3 import accounts
4 import logger
5 import subprocess
6 import os
7 import os.path
8 import libvirt
9 import sys
10 import shutil
11 import bwlimit
12 import cgroups
13 import pprint
14
15 from string import Template
16
17 STATES = {
18     libvirt.VIR_DOMAIN_NOSTATE:  'no state',
19     libvirt.VIR_DOMAIN_RUNNING:  'running',
20     libvirt.VIR_DOMAIN_BLOCKED:  'blocked on resource',
21     libvirt.VIR_DOMAIN_PAUSED:   'paused by user',
22     libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
23     libvirt.VIR_DOMAIN_SHUTOFF:  'shut off',
24     libvirt.VIR_DOMAIN_CRASHED:  'crashed',
25 }
26
27 connections = dict()
28
29 # Helper methods
30
31 def getConnection(sliver_type):
32     # TODO: error checking
33     # vtype is of the form sliver.[LXC/QEMU] we need to lower case to lxc/qemu
34     vtype = sliver_type.split('.')[1].lower()
35     uri = vtype + '://'
36     return connections.setdefault(uri, libvirt.open(uri))
37
38 def debuginfo(dom):
39     ''' Helper method to get a "nice" output of the info struct for debug'''
40     [state, maxmem, mem, ncpu, cputime] = dom.info()
41     return '%s is %s, maxmem = %s, mem = %s, ncpu = %s, cputime = %s' % (dom.name(), STATES.get(state, state), maxmem, mem, ncpu, cputime)
42
43 # Common Libvirt code
44
45 class Sliver_Libvirt(accounts.Account):
46
47     def __init__(self, rec):
48         self.name = rec['name']
49         logger.verbose ('sliver_libvirt: %s init'%(self.name))
50
51         # Assume the directory with the image and config files
52         # are in place
53
54         self.keys = ''
55         self.rspec = {}
56         self.slice_id = rec['slice_id']
57         self.enabled = True
58         self.conn = getConnection(rec['type'])
59         self.xid = bwlimit.get_xid(self.name)
60
61         try:
62             self.dom = self.conn.lookupByName(self.name)
63         except:
64             logger.verbose('sliver_libvirt: Domain %s does not exist UNEXPECTED: %s'%(self.name, sys.exc_info()[0]))
65
66
67     def start(self, delay=0):
68         ''' Just start the sliver '''
69         logger.verbose('sliver_libvirt: %s start'%(self.name))
70
71         # Check if it's running to avoid throwing an exception if the
72         # domain was already running, create actually means start
73         if not self.is_running():
74             self.dom.create()
75         else:
76             logger.verbose('sliver_libvirt: sliver %s already started'%(self.name))
77
78         # After the VM is started... we can play with the virtual interface
79         # Create the ebtables rule to mark the packets going out from the virtual
80         # interface to the actual device so the filter canmatch against the mark
81         bwlimit.ebtables("-A INPUT -i veth%d -j mark --set-mark %d" % \
82             (self.xid, self.xid))
83
84     def stop(self):
85         logger.verbose('sliver_libvirt: %s stop'%(self.name))
86
87         # Remove the ebtables rule before stopping 
88         bwlimit.ebtables("-D INPUT -i veth%d -j mark --set-mark %d" % \
89             (self.xid, self.xid))
90
91         try:
92             self.dom.destroy()
93         except:
94             logger.verbose('sliver_libvirt: Domain %s not running UNEXPECTED: %s'%(self.name, sys.exc_info()[0]))
95             print 'sliver_libvirt: Domain %s not running UNEXPECTED: %s'%(self.name, sys.exc_info()[0])
96
97     def is_running(self):
98         ''' Return True if the domain is running '''
99         logger.verbose('sliver_libvirt: %s is_running'%self.name)
100         try:
101             [state, _, _, _, _] = self.dom.info()
102             if state == libvirt.VIR_DOMAIN_RUNNING:
103                 logger.verbose('sliver_libvirt: %s is RUNNING'%self.name)
104                 return True
105             else:
106                 info = debuginfo(self.dom)
107                 logger.verbose('sliver_libvirt: %s is NOT RUNNING...\n%s'%(self.name, info))
108                 return False
109         except:
110             logger.verbose('sliver_libvirt: UNEXPECTED ERROR in %s...\n%s'%(self.name, sys.exc_info[0]))
111             print 'sliver_libvirt: UNEXPECTED ERROR in %s...\n%s'%(self.name, sys.exc_info[0])
112
113     def configure(self, rec):
114
115         #sliver.[LXC/QEMU] tolower case
116         #sliver_type = rec['type'].split('.')[1].lower() 
117
118         #BASE_DIR = '/cgroup/libvirt/%s/%s/'%(sliver_type, self.name)
119
120         # Disk allocation
121         # No way through cgroups... figure out how to do that with user/dir quotas.
122         # There is no way to do quota per directory. Chown-ing would create
123         # problems as username namespaces are not yet implemented (and thus, host
124         # and containers share the same name ids
125
126         # Btrfs support quota per volumes
127
128         # It will depend on the FS selection
129         if rec.has_key('disk_max'):
130             disk_max = rec['disk_max']
131             if disk_max == 0:
132                 # unlimited 
133                 pass
134             else:
135                 # limit to certain number
136                 pass
137
138         # Memory allocation
139         if rec.has_key('memlock_hard'):
140             mem = rec['memlock_hard'] * 1024 # hard limit in bytes
141             cgroups.write(self.name, 'memory.limit_in_bytes', mem)
142         if rec.has_key('memlock_soft'):
143             mem = rec['memlock_soft'] * 1024 # soft limit in bytes
144             cgroups.write(self.name, 'memory.soft_limit_in_bytes', mem)
145
146         # CPU allocation
147         # Only cpu_shares until figure out how to provide limits and guarantees
148         # (RT_SCHED?)
149         if rec.has_key('cpu_share'):
150             cpu_share = rec['cpu_share']
151             cgroups.write(self.name, 'cpu.shares', cpu_share)
152
153         # Call the upper configure method (ssh keys...)
154         accounts.Account.configure(self, rec)
155