2
# Uncomplicated VM Builder
3
# Copyright (C) 2007-2008 Canonical Ltd.
5
# See AUTHORS for list of contributors
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
from VMBuilder import register_plugin, Plugin, VMBuilderUserError
21
from VMBuilder.util import run_cmd
27
import VMBuilder.util as util
29
class postinst(Plugin):
31
Plugin to provide --exec and --copy post install capabilities
33
name ='Post install plugin'
35
def register_options(self):
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)
41
def preflight_check(self):
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)
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)
52
def post_install(self):
54
logging.info("Copying files specified by --copy in: %s" % self.vm.copy)
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]))
60
except IOError, (errno, strerror):
61
raise VMBuilderUserError("%s executing --copy directives: %s" % (errno, strerror))
63
if self.vm.execscript:
64
logging.info("Executing script: %s" % self.vm.execscript)
65
util.run_cmd(self.vm.execscript, self.vm.installdir)
69
register_plugin(postinst)