Skip to content

Commit

Permalink
Start using a unified box command
Browse files Browse the repository at this point in the history
Parse the command line arguments and forward them to respective
subroutines. Just the `./setup` side of things have been implemented
here.

Work done for GH-12
  • Loading branch information
casr committed Aug 26, 2014
1 parent fa96305 commit c7f352b
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 108 deletions.
8 changes: 4 additions & 4 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Scripts to create a lean CentOS Vagrant box.

Run:

./setup
./box initialize

and at the boot prompt press tab to gain access to the boot options.
Add the `ks=.*` string you get from the command prompt. The rest of
the installation is automated.

Finally, run the last command that `setup` spits out (it's of the
form `./cleanup && ...`). Congratulations! You have just created a
Vagrant box.
Finally, run the last command that the initialize step spits out
(it's of the form `./cleanup && ...`). Congratulations! You have
just created a Vagrant box.


Specification
Expand Down
83 changes: 83 additions & 0 deletions box
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/sh

vc_printerr () {
printf "$@" 1>&2
}

vc_usage () {
vc_printerr "usage: $0"
vc_printerr " action\n"
}

vc_get_file_dir () {
local src

src="$1"
while [ -h "${src}" ]; do src="$(readlink "${src}")"; done
src=$(cd -P $(dirname "${src}") && pwd)

printf "${src}"
}

vc_check_file_readable () {
if [ ! -r "$1" ]; then
vc_printerr "$0: Cannot read file: \"$1\".\n\n"
vc_usage
exit 2
fi
printf "$1"
}

vc_parse_options () {
. ./vars.sh
}

vc_parse_args () {
local arg
local args

args=$(getopt -n "$0" "$@")
if [ $? -ne 0 ]; then
vc_usage
exit 1
fi

set -- ${args}
for arg in "$@"; do
case "${arg}" in
--)
shift
break
;;
esac
done

case "$1" in
initialize|cleanup)
ACTION="$1"; shift
;;
*)
vc_printerr "$0: Unrecognised action: \"$1\".\n\n"
vc_usage
exit 3
;;
esac
}


ROOT_DIR=$(vc_get_file_dir $0)

. "${ROOT_DIR}/src/initialize.sh"
. "${ROOT_DIR}/src/cleanup.sh"

vc_parse_options
vc_parse_args "$@"

case "${ACTION}" in
initialize)
vc_action_initialize
;;
cleanup)
vc_action_cleanup
;;
esac
21 changes: 0 additions & 21 deletions cleanup

This file was deleted.

83 changes: 0 additions & 83 deletions setup

This file was deleted.

17 changes: 17 additions & 0 deletions src/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
vc_action_cleanup () {
VBoxManage modifyvm ${NAME} \
--boot1 disk --boot2 none --boot3 none --boot4 none \

VBoxManage storagectl ${NAME} \
--name SATA --remove

VBoxManage storagectl ${NAME} \
--name SATA --add sata --portcount 1 --bootable on

VBoxManage storageattach ${NAME} \
--storagectl SATA --port 0 --type hdd \
--medium "${HDD}"
VBoxManage storageattach ${NAME} \
--storagectl SATA --port 1 --type hdd \
--medium "${HDD_SWAP}"
}
File renamed without changes.
93 changes: 93 additions & 0 deletions src/initialize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
vc_initialize_ruby_exec () {
local embedded_dir
local ruby_exec
local src

src=$(which vagrant)
if [ $? -eq 0 ]; then
embedded_dir="$(vc_get_file_dir "${src}")/../embedded"

unset RUBYOPT
ruby_exec="${embedded_dir}/bin/ruby"
else
ruby_exec=$(which ruby)
if [ ! $? -eq 0 ]; then
vc_printerr "$0: Cannot find Ruby (needed to serve kickstart file)"
exit 4
fi
fi

printf "${ruby_exec}"
}

vc_initialize_setup_box () {
VBoxManage createvm --name ${NAME} --ostype ${TYPE} --register

VBoxManage modifyvm ${NAME} \
--vram 12 \
--accelerate3d off \
--memory 613 \
--usb off \
--audio none \
--boot1 disk --boot2 dvd --boot3 none --boot4 none \
--nictype1 virtio --nic1 nat --natnet1 "${NATNET}" \
--nictype2 virtio \
--nictype3 virtio \
--nictype4 virtio \
--acpi on --ioapic off \
--chipset piix3 \
--rtcuseutc on \
--hpet on \
--bioslogofadein off \
--bioslogofadeout off \
--bioslogodisplaytime 0 \
--biosbootmenu disabled

VBoxManage createhd --filename "${HDD}" --size 8192
VBoxManage createhd --filename "${HDD_SWAP}" --size 1226

VBoxManage storagectl ${NAME} \
--name SATA --add sata --portcount 2 --bootable on

VBoxManage storageattach ${NAME} \
--storagectl SATA --port 0 --type hdd --medium "${HDD}"
VBoxManage storageattach ${NAME} \
--storagectl SATA --port 1 --type hdd --medium "${HDD_SWAP}"
VBoxManage storageattach ${NAME} \
--storagectl SATA --port 2 --type dvddrive --medium "${INSTALLER}"
VBoxManage storageattach ${NAME} \
--storagectl SATA --port 3 --type dvddrive --medium "${GUESTADDITIONS}"

VBoxManage startvm ${NAME} --type gui
}

vc_initialize_start_httpd () {
# This only really caters for the common case. If you have
# problems, please discover your host's IP address and adjust
# accordingly.
IP=${NATNET%.*/*}

printf "At the boot prompt, hit <TAB> and then type:\n\n"
printf " ks=http://${IP}.3:"

ruby=$(vc_initialize_ruby_exec)
"${ruby}" "${ROOT_DIR}/src/httpd.rb" "${ROOT_DIR}/ks.cfg"
}

vc_initialize_cleanup_msg () {
printf "\n\n"
printf "The box has accepted the kickstart file. It will now go through\n"
printf "a lengthy install. When it is finished it will shutdown and you\n"
printf "can run:\n\n"
printf " ./box cleanup && vagrant package --base ${NAME} --output boxes/${NAME}-`date +%Y%m%d`.box\n\n"
printf "to create a Vagrant box.\n"
}

vc_action_initialize () {
local ruby

vc_initialize_setup_box
vc_initialize_start_httpd

vc_initialize_cleanup_msg
}

0 comments on commit c7f352b

Please sign in to comment.