/
rm.py
45 lines (37 loc) · 1.01 KB
/
rm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import logging
from ConfigParser import ConfigParser
LOG = logging.getLogger(__name__)
vman_config = 'vman.cfg'
def rm(args):
p = ConfigParser()
vm_cfg = args.name + '.conf'
p.read(vm_cfg)
for section in p.sections():
if section.startswith('disk'):
LOG.debug('Delete VM %s', args.name)
try:
f = p.get(section, 'file')
LOG.info('Delete VM disk %s', f)
os.unlink(f)
except OSError as e:
raise RuntimeError(e)
LOG.debug('Delete VM config %s', vm_cfg)
try:
os.unlink(vm_cfg)
except OSError:
LOG.warn('%s is not exist' % vm_cfg)
LOG.debug('Delete VM item from config file')
p = ConfigParser()
p.read(vman_config)
p.remove_section(args.name)
p.write(open(vman_config, 'w'))
def make(parser):
"""
Delete a virtual machine
"""
parser.add_argument(
'name',
help='name of this VM'
)
parser.set_defaults(func=rm)