From d5adbc3f104dfd1af5aaf2602f7f95324d31b575 Mon Sep 17 00:00:00 2001 From: Aaron Bull Schaefer Date: Sun, 7 Feb 2016 22:38:39 -0800 Subject: [PATCH] Remove duplication in installation scripts Instead of having mostly duplicate scripts for the entire installation process, we can split them up into the base installation steps and then run shell provisioners for anything specific to each of the types after the machine has rebooted. This structure will mean less chance of error when making changes to machines, and will also allow for expansion of the scripts to be run at the end, such as adding a minimization script to remove history and clear out space on the drive before compression. --- arch-template.json | 63 +++++++----- .../install-parallels.sh | 7 +- scripts/install-virtualbox.sh | 16 +++ srv/{install-vmware.sh => install-base.sh} | 0 srv/install-parallels.sh | 89 ----------------- srv/install-virtualbox.sh | 97 ------------------- 6 files changed, 59 insertions(+), 213 deletions(-) rename srv/parallels-tools.sh => scripts/install-parallels.sh (76%) create mode 100755 scripts/install-virtualbox.sh rename srv/{install-vmware.sh => install-base.sh} (100%) delete mode 100755 srv/install-parallels.sh delete mode 100755 srv/install-virtualbox.sh diff --git a/arch-template.json b/arch-template.json index 3c4c130..f3f19a2 100644 --- a/arch-template.json +++ b/arch-template.json @@ -6,6 +6,28 @@ "ssh_timeout": "20m" }, "builders": [ + { + "type": "parallels-iso", + "parallels_tools_flavor": "lin", + "parallels_tools_mode": "attach", + "guest_os_type": "linux-2.6", + "iso_url": "{{user `iso_url`}}", + "iso_checksum": "{{user `iso_checksum`}}", + "iso_checksum_type": "{{user `iso_checksum_type`}}", + "http_directory": "srv", + "boot_wait": "5s", + "boot_command": [ + "", + "/usr/bin/curl -O http://{{.HTTPIP}}:{{.HTTPPort}}/install-base.sh", + "/usr/bin/curl -O http://{{.HTTPIP}}:{{.HTTPPort}}/poweroff.timer", + "/usr/bin/bash ./install-base.sh" + ], + "disk_size": 20480, + "ssh_username": "vagrant", + "ssh_password": "vagrant", + "ssh_timeout": "{{user `ssh_timeout`}}", + "shutdown_command": "sudo systemctl start poweroff.timer" + }, { "type": "virtualbox-iso", "iso_url": "{{user `iso_url`}}", @@ -17,9 +39,9 @@ "boot_wait": "5s", "boot_command": [ "", - "/usr/bin/curl -O http://{{.HTTPIP}}:{{.HTTPPort}}/install-virtualbox.sh", + "/usr/bin/curl -O http://{{.HTTPIP}}:{{.HTTPPort}}/install-base.sh", "/usr/bin/curl -O http://{{.HTTPIP}}:{{.HTTPPort}}/poweroff.timer", - "/usr/bin/bash ./install-virtualbox.sh" + "/usr/bin/bash ./install-base.sh" ], "disk_size": 20480, "hard_drive_interface": "sata", @@ -37,38 +59,29 @@ "boot_wait": "5s", "boot_command": [ "", - "/usr/bin/curl -O http://{{.HTTPIP}}:{{.HTTPPort}}/install-vmware.sh", + "/usr/bin/curl -O http://{{.HTTPIP}}:{{.HTTPPort}}/install-base.sh", "/usr/bin/curl -O http://{{.HTTPIP}}:{{.HTTPPort}}/poweroff.timer", - "/usr/bin/bash ./install-vmware.sh" + "/usr/bin/bash ./install-base.sh" ], "disk_size": 20480, "ssh_username": "vagrant", "ssh_password": "vagrant", "ssh_timeout": "{{user `ssh_timeout`}}", "shutdown_command": "sudo systemctl start poweroff.timer" + } + ], + "provisioners": [ + { + "only": ["parallels-iso"], + "type": "shell", + "execute_command": "{{.Vars}} sudo -E -S bash '{{.Path}}'", + "script": "scripts/install-parallels.sh" }, { - "type": "parallels-iso", - "parallels_tools_flavor": "lin", - "parallels_tools_mode": "attach", - "guest_os_type": "linux-2.6", - "iso_url": "{{user `iso_url`}}", - "iso_checksum": "{{user `iso_checksum`}}", - "iso_checksum_type": "{{user `iso_checksum_type`}}", - "http_directory": "srv", - "boot_wait": "5s", - "boot_command": [ - "", - "/usr/bin/curl -O http://{{.HTTPIP}}:{{.HTTPPort}}/install-parallels.sh", - "/usr/bin/curl -O http://{{.HTTPIP}}:{{.HTTPPort}}/poweroff.timer", - "/usr/bin/curl -O http://{{.HTTPIP}}:{{.HTTPPort}}/parallels-tools.sh", - "/usr/bin/bash ./install-parallels.sh" - ], - "disk_size": 20480, - "ssh_username": "vagrant", - "ssh_password": "vagrant", - "ssh_timeout": "{{user `ssh_timeout`}}", - "shutdown_command": "sudo /parallels_tools.sh && sudo rm /parallels_tools.sh && sudo systemctl start poweroff.timer" + "only": ["virtualbox-iso"], + "type": "shell", + "execute_command": "{{.Vars}} sudo -E -S bash '{{.Path}}'", + "script": "scripts/install-virtualbox.sh" } ], "post-processors": [ diff --git a/srv/parallels-tools.sh b/scripts/install-parallels.sh similarity index 76% rename from srv/parallels-tools.sh rename to scripts/install-parallels.sh index d789961..e9da565 100755 --- a/srv/parallels-tools.sh +++ b/scripts/install-parallels.sh @@ -1,4 +1,7 @@ -# Install parallels tools (https://wiki.archlinux.org/index.php/Parallels) +#!/usr/bin/bash -x + +# Parallels Tools +# https://wiki.archlinux.org/index.php/Parallels mount /dev/sr1 /mnt ln -sf /usr/lib/systemd/scripts/ /etc/init.d export def_sysconfdir=/etc/init.d @@ -7,7 +10,7 @@ pacman -S --noconfirm python2 linux-headers ln -sf /usr/bin/python2 /usr/local/bin/python /mnt/install --install-unattended -# Cleanup after parallels tools installation +# clean up umount /dev/sr1 /usr/bin/pacman -Rcns --noconfirm python2 linux-headers rm -f /etc/init.d diff --git a/scripts/install-virtualbox.sh b/scripts/install-virtualbox.sh new file mode 100755 index 0000000..37aa130 --- /dev/null +++ b/scripts/install-virtualbox.sh @@ -0,0 +1,16 @@ +#!/usr/bin/bash -x + +# VirtualBox Guest Additions +# https://wiki.archlinux.org/index.php/VirtualBox +/usr/bin/pacman -S --noconfirm linux-headers virtualbox-guest-utils virtualbox-guest-dkms nfs-utils +echo -e 'vboxguest\nvboxsf\nvboxvideo' > /etc/modules-load.d/virtualbox.conf +guest_version=$(/usr/bin/pacman -Q virtualbox-guest-dkms | awk '{ print $2 }' | cut -d'-' -f1) +kernel_version="$(/usr/bin/pacman -Q linux | awk '{ print $2 }')-ARCH" +/usr/bin/dkms install "vboxguest/${guest_version}" -k "${kernel_version}/x86_64" +/usr/bin/systemctl enable dkms.service +/usr/bin/systemctl enable vboxservice.service +/usr/bin/systemctl enable rpcbind.service + +# Add groups for VirtualBox folder sharing +/usr/bin/groupadd vagrant +/usr/bin/usermod --append --groups vagrant,vboxsf vagrant diff --git a/srv/install-vmware.sh b/srv/install-base.sh similarity index 100% rename from srv/install-vmware.sh rename to srv/install-base.sh diff --git a/srv/install-parallels.sh b/srv/install-parallels.sh deleted file mode 100755 index 70f7587..0000000 --- a/srv/install-parallels.sh +++ /dev/null @@ -1,89 +0,0 @@ -#!/usr/bin/env bash - -DISK='/dev/sda' -FQDN='vagrant-arch.vagrantup.com' -KEYMAP='us' -LANGUAGE='en_US.UTF-8' -PASSWORD=$(/usr/bin/openssl passwd -crypt 'vagrant') -TIMEZONE='UTC' - -CONFIG_SCRIPT='/usr/local/bin/arch-config.sh' -ROOT_PARTITION="${DISK}1" -TARGET_DIR='/mnt' - -echo "==> clearing partition table on ${DISK}" -/usr/bin/sgdisk --zap ${DISK} - -echo "==> destroying magic strings and signatures on ${DISK}" -/usr/bin/dd if=/dev/zero of=${DISK} bs=512 count=2048 -/usr/bin/wipefs --all ${DISK} - -echo "==> creating /root partition on ${DISK}" -/usr/bin/sgdisk --new=1:0:0 ${DISK} - -echo "==> setting ${DISK} bootable" -/usr/bin/sgdisk ${DISK} --attributes=1:set:2 - -echo '==> creating /root filesystem (ext4)' -/usr/bin/mkfs.ext4 -F -m 0 -q -L root ${ROOT_PARTITION} - -echo "==> mounting ${ROOT_PARTITION} to ${TARGET_DIR}" -/usr/bin/mount -o noatime,errors=remount-ro ${ROOT_PARTITION} ${TARGET_DIR} - -echo '==> bootstrapping the base installation' -/usr/bin/pacstrap ${TARGET_DIR} base base-devel -/usr/bin/arch-chroot ${TARGET_DIR} pacman -S --noconfirm gptfdisk openssh syslinux -/usr/bin/arch-chroot ${TARGET_DIR} syslinux-install_update -i -a -m -/usr/bin/sed -i 's/sda3/sda1/' "${TARGET_DIR}/boot/syslinux/syslinux.cfg" -/usr/bin/sed -i 's/TIMEOUT 50/TIMEOUT 10/' "${TARGET_DIR}/boot/syslinux/syslinux.cfg" - -echo '==> generating the filesystem table' -/usr/bin/genfstab -p ${TARGET_DIR} >> "${TARGET_DIR}/etc/fstab" - -echo '==> generating the system configuration script' -/usr/bin/install --mode=0755 /dev/null "${TARGET_DIR}${CONFIG_SCRIPT}" - -cat <<-EOF > "${TARGET_DIR}${CONFIG_SCRIPT}" - echo '${FQDN}' > /etc/hostname - /usr/bin/ln -s /usr/share/zoneinfo/${TIMEZONE} /etc/localtime - echo 'KEYMAP=${KEYMAP}' > /etc/vconsole.conf - /usr/bin/sed -i 's/#${LANGUAGE}/${LANGUAGE}/' /etc/locale.gen - /usr/bin/locale-gen - /usr/bin/mkinitcpio -p linux - /usr/bin/usermod --password ${PASSWORD} root - # https://wiki.archlinux.org/index.php/Network_Configuration#Device_names - /usr/bin/ln -s /dev/null /etc/udev/rules.d/80-net-setup-link.rules - /usr/bin/ln -s '/usr/lib/systemd/system/dhcpcd@.service' '/etc/systemd/system/multi-user.target.wants/dhcpcd@eth0.service' - /usr/bin/sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config - /usr/bin/systemctl enable sshd.service - - # Vagrant-specific configuration - /usr/bin/useradd --password ${PASSWORD} --comment 'Vagrant User' --create-home --gid users vagrant - echo 'Defaults env_keep += "SSH_AUTH_SOCK"' > /etc/sudoers.d/10_vagrant - echo 'vagrant ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.d/10_vagrant - /usr/bin/chmod 0440 /etc/sudoers.d/10_vagrant - /usr/bin/install --directory --owner=vagrant --group=users --mode=0700 /home/vagrant/.ssh - /usr/bin/curl --output /home/vagrant/.ssh/authorized_keys --location https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub - /usr/bin/chown vagrant:users /home/vagrant/.ssh/authorized_keys - /usr/bin/chmod 0600 /home/vagrant/.ssh/authorized_keys - - # clean up - /usr/bin/pacman -Rcns --noconfirm gptfdisk - /usr/bin/yes | /usr/bin/pacman -Scc -EOF - -echo '==> entering chroot and configuring system' -/usr/bin/arch-chroot ${TARGET_DIR} ${CONFIG_SCRIPT} -rm "${TARGET_DIR}${CONFIG_SCRIPT}" - -# http://comments.gmane.org/gmane.linux.arch.general/48739 -echo '==> adding workaround for shutdown race condition' -/usr/bin/install --mode=0644 poweroff.timer "${TARGET_DIR}/etc/systemd/system/poweroff.timer" - -echo '==> Include parallels tools' -/usr/bin/install --mode=0755 parallels_tools.sh "${TARGET_DIR}/parallels_tools.sh" - -echo '==> installation complete!' -/usr/bin/sleep 3 -/usr/bin/umount ${TARGET_DIR} -/usr/bin/systemctl reboot diff --git a/srv/install-virtualbox.sh b/srv/install-virtualbox.sh deleted file mode 100755 index f8c7758..0000000 --- a/srv/install-virtualbox.sh +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env bash - -DISK='/dev/sda' -FQDN='vagrant-arch.vagrantup.com' -KEYMAP='us' -LANGUAGE='en_US.UTF-8' -PASSWORD=$(/usr/bin/openssl passwd -crypt 'vagrant') -TIMEZONE='UTC' - -CONFIG_SCRIPT='/usr/local/bin/arch-config.sh' -ROOT_PARTITION="${DISK}1" -TARGET_DIR='/mnt' - -echo "==> clearing partition table on ${DISK}" -/usr/bin/sgdisk --zap ${DISK} - -echo "==> destroying magic strings and signatures on ${DISK}" -/usr/bin/dd if=/dev/zero of=${DISK} bs=512 count=2048 -/usr/bin/wipefs --all ${DISK} - -echo "==> creating /root partition on ${DISK}" -/usr/bin/sgdisk --new=1:0:0 ${DISK} - -echo "==> setting ${DISK} bootable" -/usr/bin/sgdisk ${DISK} --attributes=1:set:2 - -echo '==> creating /root filesystem (ext4)' -/usr/bin/mkfs.ext4 -F -m 0 -q -L root ${ROOT_PARTITION} - -echo "==> mounting ${ROOT_PARTITION} to ${TARGET_DIR}" -/usr/bin/mount -o noatime,errors=remount-ro ${ROOT_PARTITION} ${TARGET_DIR} - -echo '==> bootstrapping the base installation' -/usr/bin/pacstrap ${TARGET_DIR} base base-devel -/usr/bin/arch-chroot ${TARGET_DIR} pacman -S --noconfirm gptfdisk openssh syslinux -/usr/bin/arch-chroot ${TARGET_DIR} syslinux-install_update -i -a -m -/usr/bin/sed -i 's/sda3/sda1/' "${TARGET_DIR}/boot/syslinux/syslinux.cfg" -/usr/bin/sed -i 's/TIMEOUT 50/TIMEOUT 10/' "${TARGET_DIR}/boot/syslinux/syslinux.cfg" - -echo '==> generating the filesystem table' -/usr/bin/genfstab -p ${TARGET_DIR} >> "${TARGET_DIR}/etc/fstab" - -echo '==> generating the system configuration script' -/usr/bin/install --mode=0755 /dev/null "${TARGET_DIR}${CONFIG_SCRIPT}" - -cat <<-EOF > "${TARGET_DIR}${CONFIG_SCRIPT}" - echo '${FQDN}' > /etc/hostname - /usr/bin/ln -s /usr/share/zoneinfo/${TIMEZONE} /etc/localtime - echo 'KEYMAP=${KEYMAP}' > /etc/vconsole.conf - /usr/bin/sed -i 's/#${LANGUAGE}/${LANGUAGE}/' /etc/locale.gen - /usr/bin/locale-gen - /usr/bin/mkinitcpio -p linux - /usr/bin/usermod --password ${PASSWORD} root - # https://wiki.archlinux.org/index.php/Network_Configuration#Device_names - /usr/bin/ln -s /dev/null /etc/udev/rules.d/80-net-setup-link.rules - /usr/bin/ln -s '/usr/lib/systemd/system/dhcpcd@.service' '/etc/systemd/system/multi-user.target.wants/dhcpcd@eth0.service' - /usr/bin/sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config - /usr/bin/systemctl enable sshd.service - - # VirtualBox Guest Additions - /usr/bin/pacman -S --noconfirm linux-headers virtualbox-guest-utils virtualbox-guest-dkms nfs-utils - echo -e 'vboxguest\nvboxsf\nvboxvideo' > /etc/modules-load.d/virtualbox.conf - guest_version=\$(/usr/bin/pacman -Q virtualbox-guest-dkms | awk '{ print \$2 }' | cut -d'-' -f1) - kernel_version="\$(/usr/bin/pacman -Q linux | awk '{ print \$2 }')-ARCH" - /usr/bin/dkms install "vboxguest/\${guest_version}" -k "\${kernel_version}/x86_64" - /usr/bin/systemctl enable dkms.service - /usr/bin/systemctl enable vboxservice.service - /usr/bin/systemctl enable rpcbind.service - - # Vagrant-specific configuration - /usr/bin/groupadd vagrant - /usr/bin/useradd --password ${PASSWORD} --comment 'Vagrant User' --create-home --gid users --groups vagrant,vboxsf vagrant - echo 'Defaults env_keep += "SSH_AUTH_SOCK"' > /etc/sudoers.d/10_vagrant - echo 'vagrant ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.d/10_vagrant - /usr/bin/chmod 0440 /etc/sudoers.d/10_vagrant - /usr/bin/install --directory --owner=vagrant --group=users --mode=0700 /home/vagrant/.ssh - /usr/bin/curl --output /home/vagrant/.ssh/authorized_keys --location https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub - /usr/bin/chown vagrant:users /home/vagrant/.ssh/authorized_keys - /usr/bin/chmod 0600 /home/vagrant/.ssh/authorized_keys - - # clean up - /usr/bin/pacman -Rcns --noconfirm gptfdisk - /usr/bin/yes | /usr/bin/pacman -Scc -EOF - -echo '==> entering chroot and configuring system' -/usr/bin/arch-chroot ${TARGET_DIR} ${CONFIG_SCRIPT} -rm "${TARGET_DIR}${CONFIG_SCRIPT}" - -# http://comments.gmane.org/gmane.linux.arch.general/48739 -echo '==> adding workaround for shutdown race condition' -/usr/bin/install --mode=0644 poweroff.timer "${TARGET_DIR}/etc/systemd/system/poweroff.timer" - -echo '==> installation complete!' -/usr/bin/sleep 3 -/usr/bin/umount ${TARGET_DIR} -/usr/bin/systemctl reboot