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

Source Code for Package VMBuilder.plugins.firstscripts

 1  # 
 2  #    Uncomplicated VM Builder 
 3  #    Copyright (C) 2007-2010 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   
24 -class Firstscripts(Plugin):
25 """ 26 Plugin to provide --firstboot and --firstlogin scripts capabilities 27 """ 28 name = 'First-Scripts plugin' 29
30 - def register_options(self):
31 group = self.setting_group('Scripts') 32 group.add_setting('firstboot', metavar='PATH', help='Specify a script that will be copied into the guest and executed the first time the machine boots. This script must not be interactive.') 33 group.add_setting('firstlogin', metavar='PATH', help='Specify a script that will be copied into the guest and will be executed the first time the user logs in. This script can be interactive.')
34
35 - def preflight_check(self):
36 firstboot = self.context.get_setting('firstboot') 37 if firstboot: 38 logging.debug("Checking if firstboot script %s exists" % (firstboot,)) 39 if not(os.path.isfile(firstboot) and firstboot.startswith('/')): 40 raise VMBuilderUserError('The path to the first-boot script is invalid: %s. Make sure you are providing a full path.' % firstboot) 41 42 firstlogin = self.context.get_setting('firstlogin') 43 if firstlogin: 44 logging.debug("Checking if first login script %s exists" % (firstlogin,)) 45 if not(os.path.isfile(firstlogin) and firstlogin.startswith('/')): 46 raise VMBuilderUserError('The path to the first-login script is invalid: %s. Make sure you are providing a full path.' % firstlogin)
47
48 - def post_install(self):
49 firstboot = self.context.get_setting('firstboot') 50 if firstboot: 51 logging.debug("Installing firstboot script %s" % (firstboot,)) 52 self.context.install_file('/root/firstboot.sh', source=firstboot, mode=0700) 53 os.rename('%s/etc/rc.local' % self.context.chroot_dir, '%s/etc/rc.local.orig' % self.context.chroot_dir) 54 self.install_from_template('/etc/rc.local', 'firstbootrc', mode=0755) 55 56 firstlogin = self.context.get_setting('firstlogin') 57 if firstlogin: 58 logging.debug("Installing first login script %s" % (firstlogin,)) 59 self.context.install_file('/root/firstlogin.sh', source=firstlogin, mode=0755) 60 os.rename('%s/etc/bash.bashrc' % self.context.chroot_dir, '%s/etc/bash.bashrc.orig' % self.context.chroot_dir) 61 self.install_from_template('/etc/bash.bashrc', 'firstloginrc') 62 63 return True
64 65 register_distro_plugin(Firstscripts) 66