Skip to content

Instantly share code, notes, and snippets.

@jancorg
Last active April 30, 2020 13:33
Show Gist options
  • Save jancorg/9d9d6779873f3c48e769c172c4465bd8 to your computer and use it in GitHub Desktop.
Save jancorg/9d9d6779873f3c48e769c172c4465bd8 to your computer and use it in GitHub Desktop.
install a rails 4 stack (ruby + nginx + unicorn + mysql) just using bash and system packages. not the best way, but just for fun
#!/usr/bin/env bash
set -e
#### Adding everything INLINE just for sending you an unique gist
## Test: Run an amazon ubuntu 14.04 image, copy and run the script.
## Run `wget localhost` or connect to aws server IP for getting your greeting :)
#source config.variables
RUBY_PACKAGE="ruby2.0"
MYSQL_SERVER_PACKAGE="percona-server-server-5.5"
MYSQL_CLIENT_PACKAGE="percona-server-client-5.5"
RAILS_PACKAGE="ruby-rails-4.0"
RAILS_DEPENDENCIES="libmysqlclient-dev nodejs"
UNICORN_PACKAGE="unicorn"
APP_NAME="koding"
WEB_APP_PATH="/var/www/${APP_NAME}"
UNICORN_CONFIG_RB="${WEB_APP_PATH}/config/unicorn.conf.rb"
UNICORN_CONFIG_DEFAULT="/etc/default/unicorn"
NGINX_SITE_FILE="/etc/nginx/sites-available/default"
DB_USER="koding"
DB_PASSWD="koding"
export DEBIAN_FRONTEND=noninteractive
#source helper.functions
update_package_list () {
apt-key update
apt-get update
}
install_percona_repository () {
wget https://repo.percona.com/apt/percona-release_0.1-3.$(lsb_release -sc)_all.deb
dpkg -i percona-release_0.1-3.$(lsb_release -sc)_all.deb
}
install_mysql_server () {
server_package=$1
client_package=$2
debconf-set-selections <<< "${server_package} $(cut -d "-" -f1-3 <<< $server_package)/root_password password "
debconf-set-selections <<< "${server_package} $(cut -d "-" -f1-3 <<< $server_package)/root_password_again password "
apt-get install -y $client_package $server_package
}
install_percona () {
packages="${@}"
install_percona_repository
update_package_list
install_mysql_server $packages
}
create_rails_app () {
path=$1
rails new $path --database mysql
}
create_db_users () {
db_user=$1
db_passwd=$2
mysql <<< "CREATE USER '${db_user}'@'localhost' IDENTIFIED BY '${db_passwd}';"
mysql <<< "GRANT ALL PRIVILEGES ON * . * TO '${db_user}'@'localhost';"
mysql <<< "FLUSH PRIVILEGES;"
}
configure_rails_app () {
path=$1
db_user=$2
db_passwd=$3
shift 3
deps="${@}"
apt-get install -y $deps
cd $path
echo "gem 'bundler'" >> Gemfile
sed -i "s~\(gem 'mysql2'\).*~\1, '\~> 0.3.18'~" Gemfile
bundle install
cat > $path/config/databases.yml << EOF
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: $(basename $path)_development
pool: 5
username: ${db_user}
password: ${db_password}
host: localhost
EOF
export RAILS_ENV=development
bundle exec rake db:create
bundle exec rake db:migrate
bundle exec rake assets:precompile
}
add_hello_world () {
path=$1
cd $path
bundle exec rails generate controller welcome index
cat > app/controllers/welcome_controller.rb << EOF
class WelcomeController < ApplicationController
def index
@greeting = "Hi Koding!"
end
end
EOF
cat > app/views/welcome/index.html.erb << EOF
<h1><%= @greeting %></h1>
EOF
cat > config/routes.rb << EOF
Koding::Application.routes.draw do
get 'welcome/index'
root 'welcome#index'
end
EOF
}
configure_unicorn () {
app_root=$1
unicorn_config=$2
unicorn_default_config=$3
sed -i 's/\(CONFIGURED=\)no/\1yes/' $unicorn_default_config
sed -i "s~\(APP_ROOT=\).*~\1${app_root}~" $unicorn_default_config
sed -i "s~\(CONFIG_RB=\).*~\1${unicorn_config}~" $unicorn_default_config
sed -i "s~\(PID=\).*~\1${app_root}/shared/pids/unicorn.pid~" $unicorn_default_config
cat > $unicorn_config << EOF
# set path to application
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
working_directory app_dir
# Set unicorn options
worker_processes 2
preload_app true
timeout 30
# Set up socket location
listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64
# Logging
stderr_path "#{shared_dir}/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/log/unicorn.stdout.log"
# Set master PID location
pid "#{shared_dir}/pids/unicorn.pid"
EOF
mkdir -p $app_root/shared/pids $app_root/shared/sockets $app_root/shared/log
}
configure_nginx () {
app_root=$1
nginx_site_file=$2
cat > $nginx_site_file << EOF
upstream app {
# Path to Unicorn SOCK file, as defined previously
server unix:${app_root}/shared/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name localhost;
root ${app_root}/public;
try_files \$uri/index.html \$uri @app;
location @app {
proxy_pass http://app;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header Host \$http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
EOF
}
#### main Script
install_percona $MYSQL_SERVER_PACKAGE $MYSQL_CLIENT_PACKAGE
apt-get install -y $RUBY_PACKAGE $RAILS_PACKAGE
create_rails_app $WEB_APP_PATH
configure_rails_app $WEB_APP_PATH $DB_USER $DB_PASSWD $RAILS_DEPENDENCIES
create_db_users $DB_USER $DB_PASSWD
add_hello_world $WEB_APP_PATH
apt-get install -y $UNICORN_PACKAGE
configure_unicorn $WEB_APP_PATH $UNICORN_CONFIG_RB $UNICORN_CONFIG_DEFAULT
service unicorn start
apt-get install -y nginx
configure_nginx $WEB_APP_PATH $NGINX_SITE_FILE
service nginx restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment