1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 from VMBuilder import register_plugin, Plugin, VMBuilderUserError
20 from VMBuilder.util import run_cmd
21
22 import logging
23 import os
24 import shutil
25 import VMBuilder
26
28 """
29 Plugin to provide --firstboot and --firstlogin scripts capabilities
30 """
31 name = 'First-Scripts plugin'
32
34 group = self.vm.setting_group('Scripts')
35 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.')
36 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.')
37 self.vm.register_setting_group(group)
38
40
41 if self.vm.firstboot:
42 logging.debug("Checking if firstboot script %s exists" % (self.vm.firstboot,))
43 if not(os.path.isfile(self.vm.firstboot)):
44 raise VMBuilderUserError('The path to the first-boot script is invalid: %s. Make sure you are providing a full path.' % self.vm.firstboot)
45
46 if self.vm.firstlogin:
47 logging.debug("Checking if first login script %s exists" % (self.vm.firstlogin,))
48 if not(os.path.isfile(self.vm.firstlogin)):
49 raise VMBuilderUserError('The path to the first-login script is invalid: %s. Make sure you are providing a full path.' % self.vm.firstlogin)
50
51 - def post_install(self):
52 logging.debug("Installing firstboot script %s" % (self.vm.firstboot,))
53 if self.vm.firstboot:
54 self.vm.install_file('/root/firstboot.sh', source=self.vm.firstboot, mode=0700)
55 os.rename('%s/etc/rc.local' % self.vm.installdir, '%s/etc/rc.local.orig' % self.vm.installdir)
56 self.install_from_template('/etc/rc.local', 'firstbootrc', mode=0755)
57
58 logging.debug("Installing first login script %s" % (self.vm.firstlogin,))
59 if self.vm.firstlogin:
60 self.vm.install_file('/root/firstlogin.sh', source=self.vm.firstlogin, mode=0755)
61 os.rename('%s/etc/bash.bashrc' % self.vm.installdir, '%s/etc/bash.bashrc.orig' % self.vm.installdir)
62 self.install_from_template('/etc/bash.bashrc', 'firstloginrc')
63
64 return True
65
66 register_plugin(Firstscripts)
67