| 1 | #!/bin/sh |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | THIS_NAME=ror-farm-image |
|---|
| 22 | THIS_VERSION=1 |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | VSERVER_HOME=/var/lib/vservers |
|---|
| 28 | RORFARM_LIB="$PWD"/`dirname $0` |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | template=template |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | help() { |
|---|
| 36 | cat << EOF |
|---|
| 37 | Usage: $THIS_NAME [options] image-file |
|---|
| 38 | |
|---|
| 39 | Creates a new RoR-ready vserver instance. |
|---|
| 40 | |
|---|
| 41 | Options: |
|---|
| 42 | --template vserver filesystem template (default: '$template') |
|---|
| 43 | --help, -h this help |
|---|
| 44 | --version, -v this script version |
|---|
| 45 | EOF |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | version() { |
|---|
| 49 | echo "$THIS_NAME $THIS_VERSION" |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | error() { |
|---|
| 53 | echo "$THIS_NAME: error: $*" |
|---|
| 54 | exit 1 |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | parse_opt=run |
|---|
| 61 | while [ $parse_opt != "done" ] ; do |
|---|
| 62 | case "$1" in |
|---|
| 63 | |
|---|
| 64 | --template) shift; template="$1"; shift;; |
|---|
| 65 | |
|---|
| 66 | --help|-h) shift; help; exit 0;; |
|---|
| 67 | --version|-v) shift; version; exit 0;; |
|---|
| 68 | |
|---|
| 69 | --) shift; parse_opt=done;; |
|---|
| 70 | -*) echo "$THIS_NAME: unknown option '$1'"; exit 1;; |
|---|
| 71 | *) parse_opt=done;; |
|---|
| 72 | esac |
|---|
| 73 | done |
|---|
| 74 | image="$1"; shift |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | if [ -z "$image" ]; then |
|---|
| 80 | error "missing image file name" |
|---|
| 81 | fi |
|---|
| 82 | if [ -e "$image" ]; then |
|---|
| 83 | error "image file '$image' exists" |
|---|
| 84 | fi |
|---|
| 85 | |
|---|
| 86 | if [ -z "$template" ]; then |
|---|
| 87 | error "missing --template" |
|---|
| 88 | fi |
|---|
| 89 | if [ x`echo "$template" | sed -ne '/\//p'` != x ]; then |
|---|
| 90 | error "unsane template name ('$template')" |
|---|
| 91 | fi |
|---|
| 92 | if [ ! -d "$VSERVER_HOME/$template" ]; then |
|---|
| 93 | error "filesystem template '$template' not found (in $VSERVER_HOME)" |
|---|
| 94 | fi |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | cleanup() { |
|---|
| 98 | if [ ! -z "$image_mnt" ]; then |
|---|
| 99 | umount "$image_mnt" |
|---|
| 100 | fi |
|---|
| 101 | if [ ! -z "$image_loop" ]; then |
|---|
| 102 | losetup -d $image_loop |
|---|
| 103 | fi |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | bug() { |
|---|
| 109 | echo "** $0: error, exiting" >&2 |
|---|
| 110 | cleanup |
|---|
| 111 | exit 1 |
|---|
| 112 | } |
|---|
| 113 | set -e |
|---|
| 114 | trap bug ERR |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | image_path=`dirname $image` |
|---|
| 121 | image_name=`basename $image` |
|---|
| 122 | cd "$image_path" |
|---|
| 123 | exec >"$image_name".log 2>&1 |
|---|
| 124 | tar xzf $RORFARM_LIB/disk-1gb.img.tar |
|---|
| 125 | mv disk-1gb.img "$image_name" |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | image_mnt=/mnt/"$image_name".mnt |
|---|
| 141 | mkdir -p "$image_mnt" |
|---|
| 142 | mount -o loop,offset=$((63*512)) "$image_name" "$image_mnt" |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | cp -a $VSERVER_HOME/"$template"/* "$image_mnt"/ |
|---|
| 148 | cd "$image_mnt" |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | sed -i \ |
|---|
| 154 | -e 's/^root:[^:]\+/root:/' \ |
|---|
| 155 | -e 's/^ror:[^:]\+/ror:/' \ |
|---|
| 156 | etc/shadow |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | cat <<EOF > etc/fstab |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | proc /proc proc defaults 0 0 |
|---|
| 165 | /dev/hda1 / ext3 defaults,noatime,errors=remount-ro 0 1 |
|---|
| 166 | /dev/hda2 none swap sw 0 0 |
|---|
| 167 | /dev/hdc /media/cdrom0 udf,iso9660 user,noauto 0 0 |
|---|
| 168 | EOF |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | cat <<EOF > etc/network/interfaces |
|---|
| 176 | |
|---|
| 177 | auto lo |
|---|
| 178 | iface lo inet loopback |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | auto eth0 |
|---|
| 182 | iface eth0 inet dhcp |
|---|
| 183 | EOF |
|---|
| 184 | cat <<EOF > etc/hosts |
|---|
| 185 | 127.0.0.1 localhost.localdomain localhost |
|---|
| 186 | EOF |
|---|
| 187 | echo ror-vm > etc/hostname |
|---|
| 188 | echo ror-vm.my.lan > etc/mailname |
|---|
| 189 | chroot "$image_mnt" update-rc.d -f networking remove |
|---|
| 190 | chroot "$image_mnt" update-rc.d networking start 40 S . start 35 0 6 . |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | cat <<EOF >>etc/issue |
|---|
| 195 | |
|---|
| 196 | -- |
|---|
| 197 | |
|---|
| 198 | Welcome to 'ror-vm', a ready-to-play RoR virtual machine. |
|---|
| 199 | |
|---|
| 200 | * The 'root' and 'ror' accounts have an empty password |
|---|
| 201 | |
|---|
| 202 | * Reach your application via http://localhost:8080/ |
|---|
| 203 | (use: qemu -nographic -redir tcp:8080::8080 <image>) |
|---|
| 204 | |
|---|
| 205 | This image has been mastered from the same template used on the |
|---|
| 206 | hosting facility provided by the Bearstech RoR farm. See |
|---|
| 207 | http://my.ror.bearstech.com/ for more information. |
|---|
| 208 | |
|---|
| 209 | Have fun ! The Bearstech RoR team. |
|---|
| 210 | |
|---|
| 211 | -- |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | EOF |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | PACKAGES="linux-image-2.6-amd64 grub mysql-server-5.0 mysql-client-5.0" |
|---|
| 221 | echo do_initrd = Yes > etc/kernel-img.conf |
|---|
| 222 | export DEBIAN_FRONTEND=noninteractive |
|---|
| 223 | chroot "$image_mnt" apt-get update |
|---|
| 224 | chroot "$image_mnt" apt-get --yes --force-yes install $PACKAGES |
|---|
| 225 | chroot "$image_mnt" apt-get clean |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | sed -i \ |
|---|
| 230 | -e 's/^\( |
|---|
| 231 | -e 's/^\( |
|---|
| 232 | boot/grub/menu.lst |
|---|
| 233 | cat <<EOF >>etc/inittab |
|---|
| 234 | T0:23:respawn:/sbin/getty -L ttyS0 9600 vt100 |
|---|
| 235 | EOF |
|---|
| 236 | chroot "$image_mnt" update-grub -y |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | db_name="myapp" |
|---|
| 242 | db_pass=`pwgen 8 1` |
|---|
| 243 | cat <<EOF | chroot "$image_mnt" mysql |
|---|
| 244 | CREATE DATABASE \`${db_name}_dev\` CHARSET utf8; |
|---|
| 245 | CREATE DATABASE \`${db_name}_prod\` CHARSET utf8; |
|---|
| 246 | GRANT ALL PRIVILEGES ON \`${db_name}\_dev\`.* TO '$db_name'@'%'; |
|---|
| 247 | GRANT ALL PRIVILEGES ON \`${db_name}\_prod\`.* TO '$db_name'@'%'; |
|---|
| 248 | SET PASSWORD FOR '$db_name'@'%'=PASSWORD('$db_pass'); |
|---|
| 249 | FLUSH PRIVILEGES; |
|---|
| 250 | EOF |
|---|
| 251 | cat <<EOF > .my.cnf |
|---|
| 252 | [client] |
|---|
| 253 | user = $db_name |
|---|
| 254 | pass = $db_pass |
|---|
| 255 | EOF |
|---|
| 256 | |
|---|
| 257 | chroot "$image_mnt" /etc/init.d/mysql stop |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | find var/backups var/cache var/lock var/log var/mail var/run var/spool var/tmp -type f -exec rm {} \; |
|---|
| 263 | |
|---|
| 264 | touch var/log/dmesg |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | cd home/ror |
|---|
| 269 | rm -rf .aptitude |
|---|
| 270 | rm -f .bash_history .lesshst .viminfo |
|---|
| 271 | rm -f .ssh/known_hosts .ssh/id_* |
|---|
| 272 | rm -f gem/source_cache |
|---|
| 273 | rm -f gem/cache/* |
|---|
| 274 | rm -rf rubygems-* |
|---|
| 275 | rm -f http/tmp/* |
|---|
| 276 | rm -f http/log/* |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | cd "$image_path" |
|---|
| 282 | cleanup |
|---|
| 283 | tar cSzf "$image_name".tar.gz "$image_name" |
|---|