-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostinstall.sh
214 lines (166 loc) · 6.33 KB
/
postinstall.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# **postinstall.sh** is a script executed after Debian/Ubuntu has been
# installed and restarted. There is no user interaction so all commands must
# be able to run in a non-interactive mode.
#
# If any package install time questions need to be set, you can use
# `preeseed.cfg` to populate the settings.
### Setup Variables
# The version of Ruby to be installed supporting the Chef and Puppet gems
ruby_ver="1.8.7-p358"
# The base path to the Ruby used for the Chef and Puppet gems
ruby_home="/opt/vagrant_ruby"
# The non-root user that will be created. By vagrant conventions, this should
# be `"vagrant"`.
account="vagrant"
# Enable truly non interactive apt-get installs
export DEBIAN_FRONTEND=noninteractive
# Determine the platform (i.e. Debian or Ubuntu) and platform version
platform="$(lsb_release -i -s)"
platform_version="$(lsb_release -s -r)"
# Run the script in debug mode
set -x
### Customize Sudoers
# The main user (`vagrant` in our case) needs to have **password-less** sudo
# access as described in the Vagrant base box
# [documentation](http://vagrantup.com/docs/base_boxes.html#setup_permissions).
# This user belongs to the `admin`/`sudo` group, so we'll change that line.
sed -i -e '/Defaults\s\+env_reset/a Defaults\texempt_group=admin' /etc/sudoers
case "$platform" in
Debian)
sed -i -e 's/%sudo ALL=(ALL) ALL/%sudo ALL=(ALL) NOPASSWD:ALL/g' /etc/sudoers
;;
Ubuntu)
groupadd -r admin || true
usermod -a -G admin $account
sed -i -e 's/%admin ALL=(ALL) ALL/%admin ALL=(ALL) NOPASSWD:ALL/g' /etc/sudoers
;;
esac
### Other setup
# Set the LC_CTYPE so that auto-completion works and such.
echo "LC_ALL=\"en_US\"" > /etc/default/locale
### Installing Ruby
#### Compiling Ruby
# The choice was made by the Vagrant and VeeWee authors to compile a Ruby from
# source so that the user of this base box can install their own Rubies using
# packages, RVM, source, etc. It will be installed into $ruby_home so as not
# to collide with /usr/local.
#
# Currently we must install Ruby 1.8 since Puppet doesn't fully support Ruby
# 1.9 yet.
# Install packages necessary to compile Ruby from source
case "$platform" in
Debian)
apt-get -y install build-essential zlib1g-dev libssl-dev \
libreadline5-dev make curl git-core
;;
Ubuntu)
apt-get -y install build-essential zlib1g-dev libssl-dev \
libreadline-dev make curl git-core
;;
esac
# Use ruby-build to install Ruby
clone_dir=/tmp/ruby-build-$$
git clone https://github.com/sstephenson/ruby-build.git $clone_dir
$clone_dir/bin/ruby-build "$ruby_ver" "$ruby_home"
rm -rf $clone_dir
unset clone_dir
### Installing Chef and Puppet Gems
# Install prerequisite gems used by Chef and Puppet
${ruby_home}/bin/gem install polyglot net-ssh-gateway mime-types --no-ri --no-rdoc
# The Vagrant base box
# [documentation](http://vagrantup.com/docs/base_boxes.html#boot_and_setup_basic_software)
# specifies that both the Puppet and Chef gems should be installed to qualify
# as a complete Vagrant base box.
${ruby_home}/bin/gem install chef --no-ri --no-rdoc
${ruby_home}/bin/gem install puppet --no-ri --no-rdoc
# Add the Puppet group so Puppet runs without issue
groupadd puppet
# If a packaged Ruby or RVM is installed then the path to the Chef and Puppet
# binaries will be "lost". To guard against this, we'll add the compiled Ruby's
# bin directory to PATH.
echo "PATH=\$PATH:${ruby_home}/bin" >/etc/profile.d/vagrant_ruby.sh
### Vagrant SSH Keys
# Since Vagrant only supports key-based authentication for SSH, we must
# set up the vagrant user to use key-based authentication. We can get the
# public key used by the Vagrant gem directly from its Github repository.
vssh="/home/${account}/.ssh"
mkdir -p $vssh
chmod 700 $vssh
(cd $vssh &&
wget --no-check-certificate \
'https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub' \
-O $vssh/authorized_keys)
chmod 0600 $vssh/authorized_keys
chown -R ${account}:vagrant $vssh
unset vssh
### VirtualBox Guest Additions
# The netboot installs the VirtualBox support (old) so we have to remove it
apt-get -y remove virtualbox-ose-guest-dkms
apt-get -y remove virtualbox-ose-guest-utils
# The Guest Additions installer will require the use of the linux headers, so
# we'll install it for the moment
apt-get -y install linux-headers-$(uname -r)
# Now use the current VirtualBox guest additions that VeeWee uploaded, mount,
# and install it
VBOX_VERSION=$(cat /home/${account}/.vbox_version)
mount -o loop /home/$account/VBoxGuestAdditions_$VBOX_VERSION.iso /mnt
yes|sh /mnt/VBoxLinuxAdditions.run
umount /mnt
# Cleanup the downloaded ISO
rm -f /home/$account/VBoxGuestAdditions_$VBOX_VERSION.iso
# Remove the linux headers to keep things pristine
apt-get -y remove linux-headers-$(uname -r)
### Misc. Tweaks
# Install NFS client
apt-get -y install nfs-common
# Tweak sshd to prevent DNS resolution (speed up logins)
echo 'UseDNS no' >> /etc/ssh/sshd_config
# Customize the message of the day
case "$platform" in
Debian)
echo 'Welcome to your Vagrant-built virtual machine.' > /var/run/motd
;;
Ubuntu)
echo 'Welcome to your Vagrant-built virtual machine.' > /etc/motd.tail
;;
esac
# Record when the basebox was built
date > /etc/vagrant_box_build_time
# Networking - This works around issue #391
cat <<EOF > /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Make sure eth0 is working. This works around Vagrant issue #391
dhclient eth0
exit 0
EOF
### Clean up
# Remove the build tools to keep things pristine
apt-get -y remove build-essential make curl git-core
apt-get -y autoremove
apt-get -y clean
# Removing leftover leases and persistent rules
rm -f /var/lib/dhcp3/*
# Make sure Udev doesn't block our network, see: http://6.ptmc.org/?p=164
rm /etc/udev/rules.d/70-persistent-net.rules
mkdir /etc/udev/rules.d/70-persistent-net.rules
rm -rf /dev/.udev/
rm /lib/udev/rules.d/75-persistent-net-generator.rules
# Remove any temporary work files, including the postinstall.sh script
rm -f /home/${account}/{*.iso,postinstall*.sh}
### Compress Image Size
# Zero out the free space to save space in the final image
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
exit
# And we're done.