1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 from VMBuilder import register_plugin, Plugin, VMBuilderUserError
21 from VMBuilder.util import run_cmd
22
23 import logging
24 import os
25 import shutil
26 import VMBuilder
27 import VMBuilder.util as util
28
29 -class postinst(Plugin):
30 """
31 Plugin to provide --exec and --copy post install capabilities
32 """
33 name ='Post install plugin'
34
36 group = self.vm.setting_group('Post install actions')
37 group.add_option('--copy', metavar='FILE', help="Read 'source dest' lines from FILE, copying source files from host to dest in the guest's file system.")
38 group.add_option('--execscript', '--exec', metavar='SCRIPT', help="Run SCRIPT after distro installation finishes. Script will be called with the guest's chroot as first argument, so you can use 'chroot $1 <cmd>' to run code in the virtual machine.")
39 self.vm.register_setting_group(group)
40
42 if self.vm.copy:
43 logging.debug("Checking if --copy PATH exists: %s" % self.vm.copy)
44 if not(os.path.isfile(self.vm.copy)):
45 raise VMBuilderUserError('The path to the --copy directives is invalid: %s. Make sure you are providing a full path.' % self.vm.copy)
46
47 if self.vm.execscript:
48 logging.debug("Checking if --exec PATH exists: %s" % self.vm.execscript)
49 if not(os.path.isfile(self.vm.execscript)):
50 raise VMBuilderUserError('The path to the --execscript file is invalid: %s. Make sure you are providing a full path.' % self.vm.execscript)
51
52 - def post_install(self):
53 if self.vm.copy:
54 logging.info("Copying files specified by --copy in: %s" % self.vm.copy)
55 try:
56 for line in file(self.vm.copy):
57 pair = line.strip().split(' ')
58 util.run_cmd('cp', '-LpR', pair[0], '%s%s' % (self.vm.installdir, pair[1]))
59
60 except IOError, (errno, strerror):
61 raise VMBuilderUserError("%s executing --copy directives: %s" % (errno, strerror))
62
63 if self.vm.execscript:
64 logging.info("Executing script: %s" % self.vm.execscript)
65 util.run_cmd(self.vm.execscript, self.vm.installdir)
66
67 return True
68
69 register_plugin(postinst)
70