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-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   
28 -class Firstscripts(Plugin):
29 """ 30 Plugin to provide --firstboot and --firstlogin scripts capabilities 31 """ 32 name = 'First-Scripts plugin' 33
34 - def register_options(self):
35 group = self.vm.setting_group('Scripts') 36 group.add_option('--firstboot', metavar='PATH', default='', 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.') 37 group.add_option('--firstlogin', metavar='PATH', default='', 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.') 38 self.vm.register_setting_group(group)
39
40 - def preflight_check(self):
41 42 if self.vm.firstboot: 43 logging.debug("Checking if firstboot script %s exists" % (self.vm.firstboot,)) 44 if not(os.path.isfile(self.vm.firstboot)): 45 raise VMBuilderUserError('The path to the first-boot script is invalid: %s. Make sure you are providing a full path.' % self.vm.firstboot) 46 47 if self.vm.firstlogin: 48 logging.debug("Checking if first login script %s exists" % (self.vm.firstlogin,)) 49 if not(os.path.isfile(self.vm.firstlogin)): 50 raise VMBuilderUserError('The path to the first-login script is invalid: %s. Make sure you are providing a full path.' % self.vm.firstlogin)
51
52 - def post_install(self):
53 logging.debug("Installing firstboot script %s" % (self.vm.firstboot,)) 54 if self.vm.firstboot: 55 self.vm.install_file('/root/firstboot.sh', source=self.vm.firstboot, mode=0700) 56 os.rename('%s/etc/rc.local' % self.vm.installdir, '%s/etc/rc.local.orig' % self.vm.installdir) 57 self.install_from_template('/etc/rc.local', 'firstbootrc', mode=0755) 58 59 logging.debug("Installing first login script %s" % (self.vm.firstlogin,)) 60 if self.vm.firstlogin: 61 self.vm.install_file('/root/firstlogin.sh', source=self.vm.firstlogin, mode=0755) 62 os.rename('%s/etc/bash.bashrc' % self.vm.installdir, '%s/etc/bash.bashrc.orig' % self.vm.installdir) 63 self.install_from_template('/etc/bash.bashrc', 'firstloginrc') 64 65 return True
66 67 register_plugin(Firstscripts) 68