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-2009 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 version 3, as 
 9  #    published by the Free Software Foundation. 
10  # 
11  #    This program is distributed in the hope that it will be useful, 
12  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  #    GNU General Public License for more details. 
15  # 
16  #    You should have received a copy of the GNU General Public License 
17  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
18  # 
19  from VMBuilder import register_distro_plugin, Plugin, VMBuilderUserError 
20   
21  import logging 
22  import os 
23  import VMBuilder.util as util 
24   
25 -class postinst(Plugin):
26 """ 27 Plugin to provide --exec and --copy post install capabilities 28 """ 29 name ='Post install plugin' 30
31 - def register_options(self):
32 group = self.setting_group('Post install actions') 33 group.add_setting('copy', metavar='FILE', help="Read 'source dest' lines from FILE, copying source files from host to dest in the guest's file system.") 34 group.add_setting('execscript', extra_args=['--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.")
35
36 - def preflight_check(self):
37 copy = self.context.get_setting('copy') 38 if copy: 39 logging.debug("Checking if copy PATH exists: %s" % copy) 40 if not(os.path.isfile(copy)): 41 raise VMBuilderUserError('The path to the copy directive is invalid: %s. Make sure you are providing a full path.' % copy) 42 43 execscript = self.context.get_setting('execscript') 44 if execscript: 45 logging.debug("Checking if exec PATH exists: %s" % execscript) 46 if not(os.path.isfile(execscript)): 47 raise VMBuilderUserError('The path to the execscript file is invalid: %s. Make sure you are providing a full path.' % execscript) 48 49 logging.debug("Checking permissions of exec PATH: %s" % execscript) 50 if not os.access(execscript, os.X_OK|os.R_OK): 51 raise VMBuilderUserError('The path to the execscript file has invalid permissions: %s. Make sure the path is readable and executable.' % execscript)
52
53 - def post_install(self):
54 copy = self.context.get_setting('copy') 55 execscript = self.context.get_setting('execscript') 56 if copy: 57 logging.info("Copying files specified by copy in: %s" % copy) 58 try: 59 for line in file(copy): 60 pair = line.strip().split(' ') 61 if len(pair) < 2: # skip blank and incomplete lines 62 continue 63 directory = '%s%s' % (self.context.chroot_dir, os.path.dirname(pair[1])) 64 if not os.path.exists(directory): 65 os.makedirs(directory) 66 util.run_cmd('cp', '-LpR', pair[0], '%s%s' % (self.context.chroot_dir, pair[1])) 67 68 except IOError, (errno, strerror): 69 raise VMBuilderUserError("%s executing copy directives: %s" % (errno, strerror)) 70 71 if execscript: 72 logging.info("Executing script: %s" % execscript) 73 util.run_cmd(execscript, self.context.chroot_dir) 74 75 return True
76 77 register_distro_plugin(postinst) 78