Skip to content

Commit

Permalink
add deploy script
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Aug 6, 2018
1 parent 1ddd0f8 commit 7ed11a5
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 34 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ gem 'google-protobuf', '~> 3.6'

gem 'ciri-crypto', '0.1.1'

# Deployment
gem 'mina', require: false
gem 'mina-puma', require: false
gem 'mina-multistage', require: false

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand Down
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ GEM
mimemagic (~> 0.3.2)
method_source (0.9.0)
mimemagic (0.3.2)
mina (1.2.3)
open4 (~> 1.3.4)
rake
mina-multistage (1.0.3)
mina (~> 1.0)
mina-puma (1.1.0)
mina (~> 1.2.0)
puma (>= 2.13)
mini_mime (1.0.0)
mini_portile2 (2.3.0)
minitest (5.11.3)
Expand All @@ -161,6 +169,7 @@ GEM
nokogiri (1.8.4)
mini_portile2 (~> 2.3.0)
oj (3.6.3)
open4 (1.3.4)
pg (1.0.0)
polyamorous (1.3.3)
activerecord (>= 3.0)
Expand Down Expand Up @@ -309,6 +318,9 @@ DEPENDENCIES
jbuilder (~> 2.5)
kaminari
listen (>= 3.0.5, < 3.2)
mina
mina-multistage
mina-puma
oj (~> 3.6, >= 3.6.2)
pg (>= 0.18, < 2.0)
pry
Expand Down
103 changes: 103 additions & 0 deletions config/deploy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
require 'mina/multistage'
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
# require 'mina/rbenv' # for rbenv support. (https://rbenv.org)
require 'mina/rvm' # for rvm support. (https://rvm.io)
require 'mina/puma'

# Basic settings:
# domain - The hostname to SSH to.
# deploy_to - Path to deploy into.
# repository - Git repo to clone from. (needed by mina/git)
# branch - Branch name to deploy. (needed by mina/git)

set :application_name, 'ReBirth'
set :domain, '47.97.171.140'
set :deploy_to, '/home/deploy/ReBirth'
set :repository, 'https://github.com/cryptape/ReBirth.git'
set :branch, 'develop'
set :rails_env, 'production'
set :user, 'deploy'

# Optional settings:
# set :user, 'foobar' # Username in the server to SSH to.
# set :port, '30000' # SSH port number.
# set :forward_agent, true # SSH forward_agent.

# Shared dirs and files will be symlinked into the app-folder by the 'deploy:link_shared_paths' step.
# Some plugins already add folders to shared_dirs like `mina/rails` add `public/assets`, `vendor/bundle` and many more
# run `mina -d` to see all folders and files already included in `shared_dirs` and `shared_files`
# set :shared_dirs, fetch(:shared_dirs, []).push('public/assets')
# set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'config/secrets.yml')
set :shared_dirs, fetch(:shared_dirs, []).push(
'log',
'tmp/pids',
'tmp/sockets',
'public/upload',
'public/packs',
'public/packs-test',
'storage'
)

set :shared_files, fetch(:shared_files, []).push(
'config/puma.rb',
'config/master.key',
'.env.local'
)

# This task is the environment that is loaded for all remote run commands, such as
# `mina deploy` or `mina rake`.
task :remote_environment do
# If you're using rbenv, use this to load the rbenv environment.
# Be sure to commit your .ruby-version or .rbenv-version to your repository.
# invoke :'rbenv:load'

# For those using RVM, use this to load an RVM version@gemset.
# invoke :'rvm:use', 'ruby-1.9.3-p125@default'
invoke :'rvm:use', 'ruby-2.5.1'
end

# Put any custom commands you need to run at setup
# All paths in `shared_dirs` and `shared_paths` will be created on their own.
task :setup do
# command %{rbenv install 2.3.0 --skip-existing}
command %[touch "#{fetch(:shared_path)}/config/puma.rb"]
command %[touch "#{fetch(:shared_path)}/config/master.key"]
command %[touch "#{fetch(:shared_path)}/.env.local"]
comment color_str("Be sure to edit config files")
end

desc "Deploys the current version to the server."
task :deploy do
# uncomment this line to make sure you pushed your local branch to the remote origin
# invoke :'git:ensure_pushed'
deploy do
# Put things that will set up an empty directory into a fully set-up
# instance of your project.
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
invoke :'deploy:cleanup'

on :launch do
in_path(fetch(:current_path)) do
invoke :'puma:phased_restart'
end
end
end

# you can use `run :local` to run tasks on local machine before of after the deploy scripts
# run(:local){ say 'done' }
end

# For help in making your deploy script, see the Mina documentation:
#
# - https://github.com/mina-deploy/mina/tree/master/docs

# color comment
def color_str(str)
"\x1b[0;33m#{str}\x1b[0m"
end
5 changes: 5 additions & 0 deletions config/deploy/dev.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set :domain, '47.97.171.140'
set :deploy_to, '/home/deploy/ReBirth'
set :branch, 'develop'
set :user, 'root'
set :rvm_use_path, '/usr/share/rvm/scripts/rvm'
31 changes: 31 additions & 0 deletions config/nginx.conf.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
upstream re-birth {
server unix:///home/deploy/ReBirth/shared/tmp/sockets/puma.sock;
}

server {
listen 80 default_server deferred;
server_name example.com;

root /home/deploy/ReBirth/current/public;
access_log /home/deploy/ReBirth/current/log/nginx.access.log;
error_log /home/deploy/ReBirth/current/log/nginx.error.log info;

location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}

try_files $uri/index.html $uri @re-birth;
location @re-birth {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

proxy_pass http://re-birth;
}

error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
34 changes: 0 additions & 34 deletions config/puma.rb

This file was deleted.

78 changes: 78 additions & 0 deletions config/puma.rb.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
# threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
# threads threads_count, threads_count

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
# port ENV.fetch("PORT") { 3000 }

# Specifies the `environment` that Puma will run in.
#
# environment ENV.fetch("RAILS_ENV") { "development" }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory. If you use this option
# you need to make sure to reconnect any threads in the `on_worker_boot`
# block.
#
# preload_app!

# If you are preloading your application and using Active Record, it's
# recommended that you close any connections to the database before workers
# are forked to prevent connection leakage.
#
# before_fork do
# ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
# end

# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted, this block will be run. If you are using the `preload_app!`
# option, you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, as Ruby
# cannot share connections between processes.
#
# on_worker_boot do
# ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
# end
#

# Allow puma to be restarted by `rails restart` command.
# plugin :tmp_restart

# sample for puma production env
environment 'production'
workers 2
threads 1, 6

app_name = "ReBirth"
application_path = "/home/deploy/#{app_name}"
directory "#{application_path}/current"

pidfile "#{application_path}/shared/tmp/pids/puma.pid"
state_path "#{application_path}/shared/tmp/sockets/puma.state"
stdout_redirect "#{application_path}/shared/log/puma.stdout.log", "#{application_path}/shared/log/puma.stderr.log"
bind "unix://#{application_path}/shared/tmp/sockets/puma.sock"
activate_control_app "unix://#{application_path}/shared/tmp/sockets/pumactl.sock"

daemonize true
on_restart do
puts 'On restart...'
end
preload_app!

0 comments on commit 7ed11a5

Please sign in to comment.