From 7633baad1bf2fe75bd0fc1c45229680ab3c486e6 Mon Sep 17 00:00:00 2001 From: Junv Zhao Date: Thu, 12 Nov 2015 16:46:44 +0800 Subject: [PATCH] #28 capstrano deploy --- Capfile | 33 ++++++++++++++ Gemfile | 7 +++ Gemfile.lock | 29 ++++++++++++ config/deploy.rb | 88 +++++++++++++++++++++++++++++++++++++ config/deploy/production.rb | 61 +++++++++++++++++++++++++ config/deploy/uat.rb | 61 +++++++++++++++++++++++++ config/nginx.conf | 31 +++++++++++++ 7 files changed, 310 insertions(+) create mode 100644 Capfile create mode 100644 config/deploy.rb create mode 100644 config/deploy/production.rb create mode 100644 config/deploy/uat.rb create mode 100644 config/nginx.conf diff --git a/Capfile b/Capfile new file mode 100644 index 0000000..074e147 --- /dev/null +++ b/Capfile @@ -0,0 +1,33 @@ +# Load DSL and set up stages +require 'capistrano/setup' + +# Include default deployment tasks +require 'capistrano/deploy' + +# Include tasks from other gems included in your Gemfile +# +# For documentation on these, see for example: +# +# https://github.com/capistrano/rvm +# https://github.com/capistrano/rbenv +# https://github.com/capistrano/chruby +# https://github.com/capistrano/bundler +# https://github.com/capistrano/rails +# https://github.com/capistrano/passenger +# +# require 'capistrano/rvm' +# require 'capistrano/rbenv' +# require 'capistrano/chruby' +# require 'capistrano/bundler' +# require 'capistrano/rails/assets' +# require 'capistrano/rails/migrations' +# require 'capistrano/passenger' + +require 'capistrano/rails' +require 'capistrano/bundler' +require 'capistrano/rvm' +require 'capistrano/puma' + + +# Load custom tasks from `lib/capistrano/tasks` if you have any defined +Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r } diff --git a/Gemfile b/Gemfile index be0eadb..67f2119 100644 --- a/Gemfile +++ b/Gemfile @@ -75,6 +75,13 @@ gem 'newrelic_rpm' #group data by day, week, month gem 'groupdate' +group :development do + gem 'capistrano' + gem 'capistrano-rails' + gem 'capistrano-rvm' + gem 'capistrano-bundler', require: false + gem 'capistrano3-puma', require: false +end group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index f9c1647..841f457 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -84,6 +84,22 @@ GEM thor (~> 0.19) builder (3.2.2) byebug (6.0.2) + capistrano (3.4.0) + i18n + rake (>= 10.0.0) + sshkit (~> 1.3) + capistrano-bundler (1.1.4) + capistrano (~> 3.1) + sshkit (~> 1.2) + capistrano-rails (1.1.5) + capistrano (~> 3.1) + capistrano-bundler (~> 1.1) + capistrano-rvm (0.1.2) + capistrano (~> 3.0) + sshkit (~> 1.2) + capistrano3-puma (1.2.1) + capistrano (~> 3.0) + puma (>= 2.6) celluloid (0.16.0) timers (~> 4.0.0) climate_control (0.0.3) @@ -98,6 +114,7 @@ GEM coffee-script-source execjs coffee-script-source (1.9.1.1) + colorize (0.7.7) commander (4.3.5) highline (~> 1.7.2) connection_pool (2.2.0) @@ -185,6 +202,9 @@ GEM multi_json (1.11.2) multi_xml (0.5.5) multipart-post (2.0.0) + net-scp (1.2.1) + net-ssh (>= 2.6.5) + net-ssh (3.0.1) netrc (0.10.3) newrelic_rpm (3.14.0.305) nokogiri (1.6.6.2) @@ -340,6 +360,10 @@ GEM actionpack (>= 3.0) activesupport (>= 3.0) sprockets (>= 2.8, < 4.0) + sshkit (1.7.1) + colorize (>= 0.7.0) + net-scp (>= 1.1.2) + net-ssh (>= 2.8.0) thor (0.19.1) thread_safe (0.3.5) tilt (2.0.1) @@ -383,6 +407,11 @@ DEPENDENCIES better_errors bootstrap-sass (~> 3.3.5) byebug + capistrano + capistrano-bundler + capistrano-rails + capistrano-rvm + capistrano3-puma coffee-rails (~> 4.1.0) devise devise-bootstrap-views diff --git a/config/deploy.rb b/config/deploy.rb new file mode 100644 index 0000000..e047eb3 --- /dev/null +++ b/config/deploy.rb @@ -0,0 +1,88 @@ +# config valid only for current version of Capistrano +lock '3.4.0' + + +# Change these +server '123.57.95.237', port: 22, roles: [:web, :app, :db], primary: true + +set :application, 'libr' +set :repo_url, 'git@github.com:Librcn/Libr.git' +set :user, 'deployer' +set :puma_threads, [4, 16] +set :puma_workers, 0 + +# Don't change these unless you know what you're doing +set :pty, true +set :use_sudo, false +set :stage, :production +set :deploy_via, :remote_cache +set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}" +set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock" +set :puma_state, "#{shared_path}/tmp/pids/puma.state" +set :puma_pid, "#{shared_path}/tmp/pids/puma.pid" +set :puma_access_log, "#{release_path}/log/puma.error.log" +set :puma_error_log, "#{release_path}/log/puma.access.log" +set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) } +set :puma_preload_app, true +set :puma_worker_timeout, nil +set :puma_init_active_record, true # Change to false when not using ActiveRecord + +## Defaults: +# set :scm, :git +# set :branch, :master +# set :format, :pretty +# set :log_level, :debug +# set :keep_releases, 5 + +## Linked Files & Directories (Default None): +# set :linked_files, %w{config/database.yml} +# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system} + +namespace :puma do + desc 'Create Directories for Puma Pids and Socket' + task :make_dirs do + on roles(:app) do + execute "mkdir #{shared_path}/tmp/sockets -p" + execute "mkdir #{shared_path}/tmp/pids -p" + end + end + + before :start, :make_dirs +end + +namespace :deploy do + desc "Make sure local git is in sync with remote." + task :check_revision do + on roles(:app) do + unless `git rev-parse HEAD` == `git rev-parse origin/master` + puts "WARNING: HEAD is not the same as origin/master" + puts "Run `git push` to sync changes." + exit + end + end + end + + desc 'Initial Deploy' + task :initial do + on roles(:app) do + before 'deploy:restart', 'puma:start' + invoke 'deploy' + end + end + + desc 'Restart application' + task :restart do + on roles(:app), in: :sequence, wait: 5 do + invoke 'puma:restart' + end + end + + before :starting, :check_revision + after :finishing, :compile_assets + after :finishing, :cleanup + after :finishing, :restart +end + +# ps aux | grep puma # Get puma pid +# kill -s SIGUSR2 pid # Restart puma +# kill -s SIGTERM pid # Stop puma diff --git a/config/deploy/production.rb b/config/deploy/production.rb new file mode 100644 index 0000000..4fc06fa --- /dev/null +++ b/config/deploy/production.rb @@ -0,0 +1,61 @@ +# server-based syntax +# ====================== +# Defines a single server with a list of roles and multiple properties. +# You can define all roles on a single server, or split them: + +# server 'example.com', user: 'deploy', roles: %w{app db web}, my_property: :my_value +# server 'example.com', user: 'deploy', roles: %w{app web}, other_property: :other_value +# server 'db.example.com', user: 'deploy', roles: %w{db} + + + +# role-based syntax +# ================== + +# Defines a role with one or multiple servers. The primary server in each +# group is considered to be the first unless any hosts have the primary +# property set. Specify the username and a domain or IP for the server. +# Don't use `:all`, it's a meta role. + +# role :app, %w{deploy@example.com}, my_property: :my_value +# role :web, %w{user1@primary.com user2@additional.com}, other_property: :other_value +# role :db, %w{deploy@example.com} + + + +# Configuration +# ============= +# You can set any configuration variable like in config/deploy.rb +# These variables are then only loaded and set in this stage. +# For available Capistrano configuration variables see the documentation page. +# http://capistranorb.com/documentation/getting-started/configuration/ +# Feel free to add new variables to customise your setup. + + + +# Custom SSH Options +# ================== +# You may pass any option but keep in mind that net/ssh understands a +# limited set of options, consult the Net::SSH documentation. +# http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start +# +# Global options +# -------------- +# set :ssh_options, { +# keys: %w(/home/rlisowski/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(password) +# } +# +# The server-based syntax can be used to override options: +# ------------------------------------ +# server 'example.com', +# user: 'user_name', +# roles: %w{web app}, +# ssh_options: { +# user: 'user_name', # overrides user setting above +# keys: %w(/home/user_name/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(publickey password) +# # password: 'please use keys' +# } diff --git a/config/deploy/uat.rb b/config/deploy/uat.rb new file mode 100644 index 0000000..4fc06fa --- /dev/null +++ b/config/deploy/uat.rb @@ -0,0 +1,61 @@ +# server-based syntax +# ====================== +# Defines a single server with a list of roles and multiple properties. +# You can define all roles on a single server, or split them: + +# server 'example.com', user: 'deploy', roles: %w{app db web}, my_property: :my_value +# server 'example.com', user: 'deploy', roles: %w{app web}, other_property: :other_value +# server 'db.example.com', user: 'deploy', roles: %w{db} + + + +# role-based syntax +# ================== + +# Defines a role with one or multiple servers. The primary server in each +# group is considered to be the first unless any hosts have the primary +# property set. Specify the username and a domain or IP for the server. +# Don't use `:all`, it's a meta role. + +# role :app, %w{deploy@example.com}, my_property: :my_value +# role :web, %w{user1@primary.com user2@additional.com}, other_property: :other_value +# role :db, %w{deploy@example.com} + + + +# Configuration +# ============= +# You can set any configuration variable like in config/deploy.rb +# These variables are then only loaded and set in this stage. +# For available Capistrano configuration variables see the documentation page. +# http://capistranorb.com/documentation/getting-started/configuration/ +# Feel free to add new variables to customise your setup. + + + +# Custom SSH Options +# ================== +# You may pass any option but keep in mind that net/ssh understands a +# limited set of options, consult the Net::SSH documentation. +# http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start +# +# Global options +# -------------- +# set :ssh_options, { +# keys: %w(/home/rlisowski/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(password) +# } +# +# The server-based syntax can be used to override options: +# ------------------------------------ +# server 'example.com', +# user: 'user_name', +# roles: %w{web app}, +# ssh_options: { +# user: 'user_name', # overrides user setting above +# keys: %w(/home/user_name/.ssh/id_rsa), +# forward_agent: false, +# auth_methods: %w(publickey password) +# # password: 'please use keys' +# } diff --git a/config/nginx.conf b/config/nginx.conf new file mode 100644 index 0000000..529ced3 --- /dev/null +++ b/config/nginx.conf @@ -0,0 +1,31 @@ +upstream puma { + server unix:///home/deployerer/apps/libr/shared/tmp/sockets/libr-puma.sock; +} + +server { + listen 80 default_server deferred; + # server_name example.com; + + root /home/deployer/apps/libr/current/public; + access_log /home/deployer/apps/libr/current/log/nginx.access.log; + error_log /home/deployer/apps/libr/current/log/nginx.error.log info; + + location ^~ /assets/ { + gzip_static on; + expires max; + add_header Cache-Control public; + } + + try_files $uri/index.html $uri @puma; + location @puma { + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_redirect off; + + proxy_pass http://puma; + } + + error_page 500 502 503 504 /500.html; + client_max_body_size 10M; + keepalive_timeout 10; +}