Skip to content

Instantly share code, notes, and snippets.

@kaspergrubbe
Created February 26, 2013 17:07
Show Gist options
  • Save kaspergrubbe/5040161 to your computer and use it in GitHub Desktop.
Save kaspergrubbe/5040161 to your computer and use it in GitHub Desktop.
Stackscript
#!/bin/bash
# <UDF name="HOSTNAME" Label="server hostname" default="linode"/>
###########################################################
# System
###########################################################
function system_update {
apt-get update
apt-get -y install aptitude
aptitude -y full-upgrade
}
function system_primary_ip {
# returns the primary IP assigned to eth0
echo $(ifconfig eth0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }')
}
function get_rdns {
# calls host on an IP address and returns its reverse dns
if [ ! -e /usr/bin/host ]; then
aptitude -y install dnsutils > /dev/null
fi
echo $(host $1 | awk '/pointer/ {print $5}' | sed 's/\.$//')
}
function get_rdns_primary_ip {
# returns the reverse dns of the primary IP assigned to this system
echo $(get_rdns $(system_primary_ip))
}
###########################################################
# Helpers
###########################################################
# add_line_if_not_exists ~/.bashrc 'export FAGS="-march=native -O3 -pipe -fomit-frame-pointer"'
function add_line_if_not_exists {
if [ -f $1 ];
then
grep -q "$2" $1 || echo $2 >> $1
echo A file named $1
else
echo No file named $1
fi
}
function add_varnish_config {
cat > /etc/varnish/default.vcl << EOF
backend w1 {
.host = "127.0.0.1";
.port = "4200";
}
# backend w2 {
# .host = "127.0.0.1";
# .port = "1902";
# }
director default round-robin {
{ .backend = w1; }
# { .backend = w2; }
}
sub vcl_recv {
# Don't cache POST, PUT, or DELETE requests
if (req.request == "POST" || req.request == "PUT" || req.request == "DELETE") {
return(pass);
}
# Varnish will keep two variants of the page requested due to the different Accept-Encoding headers.
# Normalizing the accept-encoding header will sure that you have as few variants as possible.
# The following VCL code will normalize the Accept-Encoding headers:
# https://www.varnish-cache.org/docs/trunk/tutorial/vary.html#tutorial-vary
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
# No point in compressing these
remove req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
# unknown algorithm
unset req.http.Accept-Encoding;
}
}
return(lookup);
}
sub vcl_fetch {
# ETag removal, if this is present ESI will not work for cached blocks
remove beresp.http.ETag;
# Strip cookies before static items are inserted into cache.
if (req.url ~ "\.(png|gif|jpg|swf|css|js|ico|html|htm|woff|eof|ttf|svg)$") {
remove beresp.http.set-cookie;
}
# Remove the Vary header, we don't treat clients differently
#remove obj.http.Vary;
# If header specifies "max-age", remove any cookie and deliver into the cache.
# The idea here is to trust the backend. If the backend set a max-age in
# the Cache-Control header, then the response should be cached even if there
# is a Set-Cookie header. The cleaner way to handle this is the not set a
# Set-Cookie header in the backend, but unfortunately Rails always sets one.
if (beresp.http.Cache-Control ~ "max-age") {
unset beresp.http.Set-Cookie;
return(deliver);
}
# Do not deliver into cache otherwise.
return(hit_for_pass);
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Varnish-Cache = "HIT (" +obj.hits+ ")";
#set resp.http.X-Varnish-Cache = "HIT";
} else {
set resp.http.X-Varnish-Cache = "MISS";
}
return(deliver);
}
#https://www.varnish-cache.org/trac/wiki/VCLExampleCachingLoggedInUsers
sub vcl_hash {
### these 2 entries are the default ones used for vcl. Below we add our own.
hash_data(req.url);
hash_data(req.http.host);
remove req.http.cookie;
return(hash);
}
EOF
}
# Do something if command DONT exist:
# if which rbenv > /dev/null; then :; else echo "Do something"; fi
# INVERSE:
# if which rbenv > /dev/null; then echo "Do something"; fi
echo $HOSTNAME > /etc/hostname
echo -e "\n127.0.0.1 $HOSTNAME $HOSTNAME.local\n" >> /etc/hosts
hostname -F /etc/hostname
# Setup needed packages for building Ruby
#apt-get update
system_update
apt-get -y install gcc make git zlib1g-dev build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev
# Imagemagick
apt-get -y install graphicsmagick
apt-get -y install libjemalloc1 # jemalloc
#apt-get -y install libtcmalloc-minimal0 # tcmalloc
# JRuby
# apt-get -y install openjdk-6-jre
# apt-get -y install java6-runtime
# inotify
# apt-get -y install inotify-tools
# top
apt-get -y install htop
# Fixing Locale errors
apt-get -y install language-pack-da-base
echo LANGUAGE=\"en_US\" >> /etc/environment
echo LC_ALL=\"da_DK.UTF-8\" >> /etc/environment
echo LANG=\"en_US\" >> /etc/environment
echo LC_TYPE=\"da_DK\" >> /etc/environment
dpkg-reconfigure locales
# Setting up SSH-keys
wget http://kaspergrubbe.dk/publickey -O key
# Setup root:
mkdir -p /root/.ssh
touch /root/.ssh/authorized_keys
cat key | cat >> /root/.ssh/authorized_keys
# Install Varnish
apt-get -y install varnish
# Changing:
# DAEMON_OPTS="-a :6081
# TO
# DAEMON_OPTS="-a :80
sed -i "s/DAEMON_OPTS=\"-a :6081/DAEMON_OPTS=\"-a :80/g" /etc/default/varnish
add_varnish_config
service varnish restart
# colors for root:
sed -i "s/#force_color_prompt=yes/force_color_prompt=yes/g" /root/.bashrc
# Add deploy user with ssh
adduser deployer --ingroup sudo --disabled-password --gecos ""
usermod -a -G sudo deployer
sudo -u deployer mkdir -p /home/deployer/.ssh
sudo -u deployer touch /home/deployer/.ssh/authorized_keys
cat key | cat >> /home/deployer/.ssh/authorized_keys
sudo -u deployer ssh-keygen -q -t rsa -N '' -f /home/deployer/.ssh/id_rsa
rm key
# enable colors for deployer
sed -i "s/#force_color_prompt=yes/force_color_prompt=yes/g" /home/deployer/.bashrc
sudo -u deployer mkdir /home/deployer/bin
sudo -u deployer sed -i '1i PATH=/home/deployer/bin:$PATH' /home/deployer/.bashrc
sudo -u deployer source /home/deployer/.bashrc
# Ruby GC-tweaks (deployer)
echo export RUBY_HEAP_MIN_SLOTS=1000000 >> /home/deployer/.bashrc
echo export RUBY_HEAP_SLOTS_INCREMENT=1000000 >> /home/deployer/.bashrc
echo export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1 >> /home/deployer/.bashrc
echo export RUBY_GC_MALLOC_LIMIT=1000000000 >> /home/deployer/.bashrc
echo export RUBY_HEAP_FREE_MIN=500000 >> /home/deployer/.bashrc
# Compile flags
echo "" >> /home/deployer/.bashrc
echo 'export CFLAGS="-march=native -O3 -pipe -fomit-frame-pointer"' >> /home/deployer/.bashrc
echo "" >> /home/deployer/.bashrc
echo 'export LD_PRELOAD=$LD_PRELOAD:/usr/lib/libjemalloc.so.1' # jemalloc
# Look into adding high performance allocators like jemalloc and tcmalloc
# https://gist.github.com/4136373
# Installing RBENV
su deployer -c "cd ~ && wget https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer && bash rbenv-installer"
echo 'export RBENV_ROOT="${HOME}/.rbenv"' >> /home/deployer/.bashrc
echo 'if [ -d "${RBENV_ROOT}" ]; then' >> /home/deployer/.bashrc
echo ' export PATH="${RBENV_ROOT}/bin:${PATH}"' >> /home/deployer/.bashrc
echo ' eval "$(rbenv init -)"' >> /home/deployer/.bashrc
echo 'fi' >> /home/deployer/.bashrc
su deployer -c "git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build"
su deployer -c "source ~/.bashrc"
su deployer -c "cd ~ && ~/.rbenv/bin/rbenv install 2.0.0-p0"
su deployer -c "cd ~ && ~/.rbenv/bin/rbenv global 2.0.0-p0"
su deployer -c "cd ~ && gem install bundler"
su deployer -c "cd ~ && ~/.rbenv/bin/rbenv rehash"
#su deployer -c "cd ~ && ~/.rbenv/bin/rbenv install jruby-1.7.1"
#su deployer -c "cd ~ && ~/.rbenv/bin/rbenv global jruby-1.7.1"
# Network interface
#echo -e "\nauto eth0:1" >> /etc/network/interfaces
#echo -e "\n address $IP" >> /etc/network/interfaces
#echo -e "\n netmask 255.255.128.0 \n" >> /etc/network/interfaces
#/etc/init.d/networking restart
reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment