Package VMBuilder :: Package plugins :: Package postinst
[frames] | no frames]

Source Code for Package VMBuilder.plugins.postinst

 1  # 
 2  #    Uncomplicated VM Builder 
 3  #    Copyright (C) 2007-2008 Canonical Ltd. 
 4  #     
 5  #    See AUTHORS for list of contributors 
 6  # 
 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. 
11  # 
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. 
16  # 
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/>. 
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
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)
40
41 - def preflight_check(self):
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