Skip to content

Instantly share code, notes, and snippets.

@Yukaii
Created April 5, 2016 14:53
Show Gist options
  • Select an option

  • Save Yukaii/070131984193db3142d28aa095f610e5 to your computer and use it in GitHub Desktop.

Select an option

Save Yukaii/070131984193db3142d28aa095f610e5 to your computer and use it in GitHub Desktop.

Revisions

  1. @xhj xhj revised this gist Jul 6, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion deploy.rb
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,6 @@
    set :term_mode, :system

    set :shared_paths, ['config/mongoid.yml', 'config/application.yml', 'log']
    set :shared_children, shared_children << 'tmp/sockets'
    set :app_path, lambda { "#{deploy_to}/#{current_path}" }
    set :stage, 'production'

  2. @xhj xhj revised this gist Jul 6, 2013. 2 changed files with 142 additions and 0 deletions.
    72 changes: 72 additions & 0 deletions deploy.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    require 'mina/bundler'
    require 'mina/rails'
    require 'mina/git'
    require 'mina/rvm' # for rvm support. (http://rvm.io)

    set :domain, 'esdb.cn'
    set :identity_file, '/User/somebody/.ssh/somebody.pem'
    set :deploy_to, '/home/ubuntu/apps/xxx.com'
    set :repository, 'ssh://git@bitbucket.org/somebody/xxx.com.git'
    set :branch, 'master'
    set :term_mode, :system

    set :shared_paths, ['config/mongoid.yml', 'config/application.yml', 'log']
    set :shared_children, shared_children << 'tmp/sockets'
    set :app_path, lambda { "#{deploy_to}/#{current_path}" }
    set :stage, 'production'

    task :environment do
    invoke :'rvm:use[ruby-2.0.0-p247@xxx_com]'
    end

    task :setup => :environment do
    queue! %[mkdir -p "#{deploy_to}/shared/log"]
    queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]

    queue! %[mkdir -p "#{deploy_to}/shared/config"]
    queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"]

    queue! %[mkdir -p "#{deploy_to}/shared/tmp/pids"]
    queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp"]
    queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/pids"]

    queue! %[mkdir -p "#{deploy_to}/shared/tmp/sockets"]
    queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/sockets"]

    queue! %[touch "#{deploy_to}/shared/config/mongoid.yml"]
    queue! %[touch "#{deploy_to}/shared/config/application.yml"]
    end

    desc "Deploys the current version to the server."
    task :deploy => :environment do
    deploy do
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:assets_precompile'

    to :launch do
    invoke 'puma:restart'
    end
    end
    end

    namespace :puma do
    desc "Start the application"
    task :start do
    queue 'echo "-----> Start Puma"'
    queue "cd #{current_path} && RAILS_ENV=#{stage} && bin/puma.sh start", :pty => false
    end

    desc "Stop the application"
    task :stop do
    queue 'echo "-----> Stop Puma"'
    queue "cd #{current_path} && RAILS_ENV=#{stage} && bin/puma.sh stop"
    end

    desc "Restart the application"
    task :restart, :roles => :app, :except => { :no_release => true } do
    queue 'echo "-----> Restart Puma"'
    queue "cd #{current_path} && RAILS_ENV=#{stage} && bin/puma.sh restart"
    end
    end
    70 changes: 70 additions & 0 deletions puma.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    #! /bin/sh

    PUMA_CONFIG_FILE=/home/somebody/apps/xxx.com/config/puma.rb
    PUMA_PID_FILE=/home/somebody/apps/xxx.com/shared/tmp/pids/puma.pid
    PUMA_SOCKET=/home/somebody/apps/xxx.com/shared/tmp/sockets/puma.sock

    # check if puma process is running
    puma_is_running() {
    if [ -S $PUMA_SOCKET ] ; then
    if [ -e $PUMA_PID_FILE ] ; then
    if cat $PUMA_PID_FILE | xargs pgrep -P > /dev/null ; then
    return 0
    else
    echo "No puma process found"
    fi
    else
    echo "No puma pid file found"
    fi
    else
    echo "No puma socket found"
    fi

    return 1
    }

    case "$1" in
    start)
    echo "Starting puma..."
    rm -f $PUMA_SOCKET
    if [ -e $PUMA_CONFIG_FILE ] ; then
    bundle exec puma -C $PUMA_CONFIG_FILE
    else
    bundle exec puma
    fi

    echo "done"
    ;;

    stop)
    echo "Stopping puma..."
    kill -s SIGTERM `cat $PUMA_PID_FILE`
    rm -f $PUMA_PID_FILE
    rm -f $PUMA_SOCKET

    echo "done"
    ;;

    restart)
    if puma_is_running ; then
    echo "Hot-restarting puma..."
    kill -s SIGUSR2 `cat $PUMA_PID_FILE`

    echo "Doublechecking the process restart..."
    sleep 5
    if puma_is_running ; then
    echo "done"
    exit 0
    else
    echo "Puma restart failed :/"
    fi
    fi

    echo "Trying cold reboot"
    script/puma.sh start
    ;;

    *)
    echo "Usage: script/puma.sh {start|stop|restart}" >&2
    ;;
    esac
  3. @xhj xhj revised this gist Jul 6, 2013. 2 changed files with 45 additions and 4 deletions.
    42 changes: 42 additions & 0 deletions nginx.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    user ubuntu;
    worker_processes 1;

    error_log /var/log/nginx/error.log;
    pid /var/run/nginx.pid;

    events {
    worker_connections 1024;
    accept_mutex off;
    use epoll;
    }


    http {
    include mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay off;

    keepalive_timeout 45;

    gzip on;
    gzip_http_version 1.0;
    gzip_proxied any;
    gzip_min_length 500;
    gzip_disable "MSIE [1-6]\.";
    gzip_types text/plain text/xml text/css
    text/comma-separated-values
    text/javascript application/x-javascript
    application/atom+xml;

    server_names_hash_bucket_size 128;

    client_max_body_size 4G;
    client_body_buffer_size 128k;

    include /etc/nginx/conf/conf.d/*.conf;
    }
    7 changes: 3 additions & 4 deletions puma.rb
    Original file line number Diff line number Diff line change
    @@ -4,10 +4,9 @@

    daemonize true

    pidfile "/home/ubuntu/apps/xxx/shared/tmp/pids/puma.pid"
    state_path "/home/ubuntu/apps/xxx/shared/tmp/pids/puma.state"
    stdout_redirect "/home/ubuntu/apps/xxx/shared/tmp/log/stdout", "/home/ubuntu/apps/xxx/shared/tmp/log/stderr"
    pidfile "/home/ubuntu/apps/esdb.cn/shared/tmp/pids/puma.pid"
    stdout_redirect "/home/ubuntu/apps/esdb.cn/shared/tmp/log/stdout", "/home/ubuntu/apps/esdb.cn/shared/tmp/log/stderr"

    threads 0, 16

    bind "unix:///home/ubuntu/apps/xxx/shared/tmp/sockets/puma.sock"
    bind "unix:///home/ubuntu/apps/esdb.cn/shared/tmp/sockets/puma.sock"
  4. @xhj xhj created this gist Jul 6, 2013.
    13 changes: 13 additions & 0 deletions puma.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    #!/usr/bin/env puma

    environment ENV['RAILS_ENV'] || 'production'

    daemonize true

    pidfile "/home/ubuntu/apps/xxx/shared/tmp/pids/puma.pid"
    state_path "/home/ubuntu/apps/xxx/shared/tmp/pids/puma.state"
    stdout_redirect "/home/ubuntu/apps/xxx/shared/tmp/log/stdout", "/home/ubuntu/apps/xxx/shared/tmp/log/stderr"

    threads 0, 16

    bind "unix:///home/ubuntu/apps/xxx/shared/tmp/sockets/puma.sock"