public
Created

Stackscript

  • Download Gist
gistfile1.txt
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
#!/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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.