Fixed directory strings
[nodemanager.git] / sliver_lxc.py
1 #
2
3 """LXC slivers"""
4
5 import accounts
6 import logger
7 import subprocess
8 import os
9
10 class Sliver_LXC(accounts.Account):
11     """This class wraps LXC commands"""
12
13     SHELL = '/bin/sh' 
14     # Using /bin/bash triggers destroy root/site_admin (?!?)
15     TYPE = 'sliver.LXC'
16     # Need to add a tag at myplc to actually use this account
17     # type = 'sliver.LXC'
18
19     def __init__(self, rec):
20         self.name = rec['name']
21         print "LXC __init__ %s"%(self.name)
22         logger.verbose ('sliver_lxc: %s init'%self.name)
23     
24         self.dir = '/vservers/%s'%(self.name)
25         
26         # Assume the directory with the image and config files
27         # are in place
28         
29         self.config = '%s/config'%(self.dir)
30         self.fstab  = '%s/fstab'%(self.dir)
31         self.lxc_log  = '%s/lxc.log'%(self.dir)
32         self.keys = ''
33         self.rspec = {}
34         self.slice_id = rec['slice_id']
35         self.disk_usage_initialized = False
36         self.initscript = ''
37         self.enabled = True
38         self.configure(rec)
39
40     @staticmethod
41     def create(name, rec = None):
42         ''' Create dirs, copy fs image, lxc_create '''
43         print "LXC create %s"%(name)
44         logger.verbose ('sliver_lxc: %s create'%name)
45         dir = '/vservers/%s'%(name)
46         config = '%s/config'%(dir)
47         lxc_log = '%s/lxc.log'%(dir)
48         
49         if not (os.path.isdir(dir) and 
50             os.access(dir, os.R_OK | os.W_OK | os.X_OK)):
51             print 'lxc_create: directory %s does not exist or wrong perms'%(dir)
52             return
53         # Assume for now that the directory is there and with a FS
54         command=[]
55         # be verbose
56         command += ['/bin/bash','-x',]
57         command += ['/usr/bin/lxc-create', '-n', name, '-f', config, '&']
58         print command
59         subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT, shell=False)
60         
61     @staticmethod
62     def destroy(name):
63         ''' lxc_destroy '''
64         print "LXC destroy %s"%(name)
65         dir = '/vservers/%s'%(name)
66         lxc_log = '%s/lxc.log'%(dir)
67         command=[]
68         command += ['/usr/bin/lxc-destroy', '-n', name]
69
70         subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT, shell=False)
71         print "LXC destroy DONE"
72
73     def configure(self, rec):
74         print "LXC configure %s"%(self.name) 
75
76     def start(self, delay=0):
77         ''' Check existence? lxc_start '''
78         print "LXC start %s"%(self.name)
79         command=[]
80         command += ['/usr/bin/lxc-start', '-n', self.name, '-d']
81         print command
82         subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT, shell=False)
83
84     def stop(self):
85         ''' lxc_stop '''
86         print "LXC stop %s"%(self.name)
87     
88     def is_running(self):
89         print "LXC is_running %s"%(self.name)
90         command = []
91         command += ['/usr/bin/lxc-info -n %s'%(self.name)]
92         print command
93         p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
94         state = p.communicate()[0].split(' ')[2]
95         print state
96         if state == 'RUNNING': return True
97         else: return False
98
99