From 93ed42de9c6d3a4c630c9cb33407e89b8d32bd72 Mon Sep 17 00:00:00 2001 From: Nicolas Bally Date: Tue, 19 Aug 2014 10:02:08 +0200 Subject: [PATCH] deploy files --- Capfile | 4 ++ Gemfile | 4 +- Gemfile.lock | 26 ++++++++ config/deploy.rb | 138 ++++++++++++++++++++++++++++++++++++++++++ config/nginx.conf | 24 ++++++++ config/unicorn.rb | 10 +++ config/unicorn_init_d | 64 ++++++++++++++++++++ 7 files changed, 268 insertions(+), 2 deletions(-) create mode 100644 Capfile create mode 100644 config/deploy.rb create mode 100644 config/nginx.conf create mode 100644 config/unicorn.rb create mode 100644 config/unicorn_init_d diff --git a/Capfile b/Capfile new file mode 100644 index 0000000..d04de11 --- /dev/null +++ b/Capfile @@ -0,0 +1,4 @@ +load 'deploy' if respond_to?(:namespace) # cap2 differentiator +Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) } + +load 'config/deploy' # remove this line to skip loading any of the default tasks \ No newline at end of file diff --git a/Gemfile b/Gemfile index c04cdba..edd8008 100644 --- a/Gemfile +++ b/Gemfile @@ -32,10 +32,10 @@ gem 'haml-rails' # gem 'bcrypt', '~> 3.1.7' # Use unicorn as the app server -# gem 'unicorn' + gem 'unicorn' # Use Capistrano for deployment -# gem 'capistrano-rails', group: :development + gem 'capistrano-rails', group: :development # Use debugger # gem 'debugger', group: [:development, :test] diff --git a/Gemfile.lock b/Gemfile.lock index 9b297ba..768c04a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -29,6 +29,16 @@ GEM tzinfo (~> 1.1) arel (5.0.1.20140414130214) builder (3.2.2) + capistrano (3.2.1) + i18n + rake (>= 10.0.0) + sshkit (~> 1.3) + capistrano-bundler (1.1.3) + capistrano (~> 3.1) + sshkit (~> 1.2) + capistrano-rails (1.1.1) + capistrano (~> 3.1) + capistrano-bundler (~> 1.1) coffee-rails (4.0.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) @@ -36,6 +46,7 @@ GEM coffee-script-source execjs coffee-script-source (1.7.1) + colorize (0.7.3) erubis (2.7.0) execjs (2.2.1) haml (4.0.5) @@ -54,12 +65,16 @@ GEM railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) json (1.8.1) + kgio (2.9.2) mail (2.5.4) mime-types (~> 1.16) treetop (~> 1.4.8) mime-types (1.25.1) minitest (5.4.0) multi_json (1.10.1) + net-scp (1.2.1) + net-ssh (>= 2.6.5) + net-ssh (2.9.1) polyglot (0.3.5) rack (1.5.2) rack-test (0.6.2) @@ -79,6 +94,7 @@ GEM activesupport (= 4.1.4) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) + raindrops (0.13.0) rake (10.3.2) rdoc (4.1.1) json (~> 1.4) @@ -102,6 +118,10 @@ GEM activesupport (>= 3.0) sprockets (~> 2.8) sqlite3 (1.3.9) + sshkit (1.5.1) + colorize + net-scp (>= 1.1.2) + net-ssh (>= 2.8.0) thor (0.19.1) thread_safe (0.3.4) tilt (1.4.1) @@ -115,11 +135,16 @@ GEM uglifier (2.5.3) execjs (>= 0.3.0) json (>= 1.8.0) + unicorn (4.8.3) + kgio (~> 2.6) + rack + raindrops (~> 0.7) PLATFORMS ruby DEPENDENCIES + capistrano-rails coffee-rails (~> 4.0.0) haml-rails jbuilder (~> 2.0) @@ -131,3 +156,4 @@ DEPENDENCIES sqlite3 turbolinks uglifier (>= 1.3.0) + unicorn diff --git a/config/deploy.rb b/config/deploy.rb new file mode 100644 index 0000000..36d7e42 --- /dev/null +++ b/config/deploy.rb @@ -0,0 +1,138 @@ + + +set :application, 'ads_app' + set :domain, '62.210.187.249' + +set :user, 'web' +set :scm_user, "git" +set :scm_host, "gitlab.bally.me" + + + +set :rvm_ruby_string, 'ruby-2.1.2@ads' +set :rvm_autolibs_flag, "read-only" + +set :bundle_cmd, "/home/#{user}/.rvm/gems/#{rvm_ruby_string}@global/bin/bundle" +set :bundle_dir, "/home/#{user}/.rvm/gems/#{rvm_ruby_string}" +set :use_sudo, false + + + + + +set :deploy_to, "/home/#{user}/#{application}/" +set :deploy_via, :remote_cache + +set :normalize_asset_timestamps, false + +set :rails_env, "production" + +role :app, domain +role :web, domain +role :db, domain, :primary => true + +set :scm, :git + +set :repository, "#{scm_user}@#{scm_host}:root/#{application}.git" + +set :branch, "master" + +default_run_options[:pty] = true + + +require "rvm/capistrano" +require 'bundler/capistrano' + + + + +load 'deploy/assets' + +namespace :deploy do + +# namespace :assets do +# task :precompile, :roles => :web, :except => { :no_release => true } do +# from = source.next_revision(current_revision) +# if releases.length <= 1 || capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0 +# run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile} +# else +# logger.info "Skipping asset pre-compilation because there were no asset changes" +# end +# end +# end + + + + desc "Symlink shared config files" + task :symlink_config_files do + + + run "ln -s #{deploy_to}shared/storage/private #{current_path}/private_medias" + + run "ln -s #{deploy_to}shared/storage/public #{current_path}/public/public_medias" + + run "ln -s #{deploy_to}shared/production.sqlite3 #{current_path}/db/production.sqlite3" + + sudo "cp #{current_path}/config/unicorn_init_d /etc/init.d/#{application}" + + sudo "chmod +x /etc/init.d/#{application}" + sudo "update-rc.d #{application} defaults " + end + + + + task :start do + sudo "service #{application} start" + end + task :stop do + run "service #{application} stop" + end + task :restart do + sudo "service #{application} restart" + end + + task :reload_nginx do + + sudo "rm -rf /etc/nginx/sites-enabled/#{application}" + sudo "ln -s #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application} " + + sudo "service nginx reload" + end + + + + task :seeds do + run "cd #{deploy_to}current && rake db:seed RAILS_ENV=#{rails_env}" + + end + + + task :create_files_folders do + + run "mkdir #{deploy_to}shared/storage" + run "mkdir #{deploy_to}shared/storage/private" + run "mkdir #{deploy_to}shared/storage/public" + end + + + +end + + +#before 'deploy:setup', 'rvm:install_rvm' # install RVM +#before 'deploy:setup', 'rvm:install_ruby' # install Ruby and create gemset, OR: +#before 'deploy:setup', 'rvm:create_gemset' # only create gemset + + + +after "deploy:setup", "deploy:create_files_folders" + +#after "deploy:restart", "deploy:cleanup" + + +before "deploy:restart", "deploy:symlink_config_files" + +after "deploy", "deploy:cleanup" + + + \ No newline at end of file diff --git a/config/nginx.conf b/config/nginx.conf new file mode 100644 index 0000000..b4028c6 --- /dev/null +++ b/config/nginx.conf @@ -0,0 +1,24 @@ +upstream blog_eft_app_unicorn { + server unix:/home/web/ads_app/shared/unicorn.sock fail_timeout=0; +} + +server { + listen 80; + server_name ads.quartz.xyz; + root /home/web/ads_app/current/public; + try_files $uri/index.html $uri @unicorn; + + location @unicorn { + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_redirect off; + proxy_pass http://ads_app_unicorn; + proxy_read_timeout 6000; + } + + error_page 500 502 503 504 /500.html; + client_max_body_size 4G; + keepalive_timeout 10; +} + + diff --git a/config/unicorn.rb b/config/unicorn.rb new file mode 100644 index 0000000..06eab67 --- /dev/null +++ b/config/unicorn.rb @@ -0,0 +1,10 @@ +@rails_app = "ads_app" + +working_directory "/home/web/#{@rails_app}/current" +pid "/home/web/#{@rails_app}/shared/unicorn.pid" +stderr_path "/home/web/#{@rails_app}/shared/unicorn.log" +stdout_path "/home/web/#{@rails_app}/shared/unicorn.log" + +listen "/home/web/#{@rails_app}/shared/unicorn.sock" +worker_processes 2 +timeout 600 \ No newline at end of file diff --git a/config/unicorn_init_d b/config/unicorn_init_d new file mode 100644 index 0000000..b84b3e0 --- /dev/null +++ b/config/unicorn_init_d @@ -0,0 +1,64 @@ +#!/bin/bash + + +set -u +set -e + +# Change these to match your app: +APP_NAME=ads_app +APP_RUBY=2.1.2@ads +APP_ROOT="/home/web/ads_app/current" +PID="/home/web/ads_app/shared/unicorn.pid" +ENV=production + +GEM_HOME="/home/web/.rvm/gems/ruby-$APP_RUBY" + +UNICORN_OPTS="-D -E $ENV -c $APP_ROOT/config/unicorn.rb" + +SET_PATH="cd $APP_ROOT; rvm $APP_RUBY" +CMD="$SET_PATH; unicorn $UNICORN_OPTS" + +old_pid="$PID.oldbin" + +cd $APP_ROOT || exit 1 + +sig () { + test -s "$PID" && kill -$1 `cat $PID` +} + +oldsig () { + test -s $old_pid && kill -$1 `cat $old_pid` +} + +case ${1-help} in + start) + sig 0 && echo >&2 "Already running" && exit 0 + su - web -c "$CMD" + ;; + stop) + sig QUIT && exit 0 + echo >&2 "Not running" + ;; + force-stop) + sig TERM && exit 0 + echo >&2 "Not running" + ;; + restart|reload) + sig HUP && echo reloaded OK && exit 0 + echo >&2 "Couldn't reload, starting '$CMD' instead" + su - web -c "$CMD" + ;; + upgrade) + sig USR2 && exit 0 + echo >&2 "Couldn't upgrade, starting '$CMD' instead" + su - web -c "$CMD" + ;; + rotate) + sig USR1 && echo rotated logs OK && exit 0 + echo >&2 "Couldn't rotate logs" && exit 1 + ;; + *) + echo >&2 "Usage: $0 " + exit 1 + ;; +esac \ No newline at end of file