Skip to content

Instantly share code, notes, and snippets.

@dwayne
Created June 11, 2012 08:27
Show Gist options
  • Save dwayne/2909075 to your computer and use it in GitHub Desktop.
Save dwayne/2909075 to your computer and use it in GitHub Desktop.

Revisions

  1. dwayne revised this gist Nov 21, 2012. 2 changed files with 40 additions and 21 deletions.
    7 changes: 7 additions & 0 deletions .excludes
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # Exclude files that don't need to be on the server
    # Used by rsync when deploying code to the server
    .excludes
    .git
    .gitignore
    log/
    tmp/
    54 changes: 33 additions & 21 deletions Rakefile
    Original file line number Diff line number Diff line change
    @@ -14,13 +14,19 @@ namespace :thin do
    desc 'Stop the app'
    task :stop do
    puts 'Stopping...'
    Dir.new(File.join(File.dirname(__FILE__), 'tmp/pids')).each do |file|
    prefix = file.to_s
    if prefix[0, 4] == 'thin'
    puts "Stopping the server on port #{file[/\d+/]}..."
    system "bundle exec thin stop -Ptmp/pids/#{file}"

    pids = File.join(File.dirname(__FILE__), 'tmp/pids')

    if File.directory?(pids)
    Dir.new(pids).each do |file|
    prefix = file.to_s
    if prefix[0, 4] == 'thin'
    puts "Stopping the server on port #{file[/\d+/]}..."
    system "bundle exec thin stop -Ptmp/pids/#{file}"
    end
    end
    end

    puts 'Stopped!'
    end

    @@ -34,26 +40,32 @@ namespace :thin do

    end

    # FIXME: Refactor the deployment code
    user = 'username'

    app_name = 'app'
    app_dir = "/home/#{user}/webapps/#{app_name}"

    desc "Deploy to server"
    desc 'Deploy to server'
    task :deploy, :password do |t, args|
    puts "Deploying to server..."
    puts 'Deploying to server...'

    system "rsync --exclude 'log/*.log' --exclude 'tmp/**/*' --exclude '.git' --exclude '.gitignore' -rltvz -e ssh . [email protected]:/home/user/webapps/app_name"
    # http://linux.die.net/man/1/rsync
    # Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
    success = system "rsync --exclude-from .excludes -rltvz -e ssh . #{user}@#{user}.webfactional.com:#{app_dir}"

    require "net/ssh"
    Net::SSH.start("user.webfactional.com", "user", :password => args[:password]) do |ssh|
    commands = [
    "cd /home/user/webapps/app_name",
    "rvm use 1.9.3-p194@app_name --create", # assumes, rvm is already setup on the server
    "bundle install --without=development",
    # "bundle exec whenever --update-crontab", optionally, schedule cron jobs using whenever
    "bundle exec rake thin:restart"
    ].join " && "
    if success
    require 'net/ssh'
    Net::SSH.start("#{user}.webfactional.com", user, :password => args[:password]) do |ssh|
    commands = [
    'export RACK_ENV=production',

    ssh.exec commands
    end
    "cd #{app_dir}",
    'bundle install --without=development',

    'rake thin:restart'
    ].join ' && '

    puts "Finished!"
    ssh.exec commands
    end
    end
    end
  2. dwayne revised this gist Nov 20, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions config-production.yml
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    # Server options:
    port: 27017
    port: 12345

    # Adapter options:
    environment: production

    # Daemon options:
    daemonize: true
    user: dwaynecrooks
    group: dwaynecrooks
    user: user
    group: group

    # See `thin -h` for more options
  3. dwayne revised this gist Nov 20, 2012. 6 changed files with 51 additions and 30 deletions.
    3 changes: 3 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # These directories are created by Thin when `rake:thin start` is invoked
    log/
    tmp/
    31 changes: 19 additions & 12 deletions Rakefile
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,41 @@
    require 'rake'

    ENV['RACK_ENV'] ||= 'development'

    namespace :thin do

    desc "Start the application"
    desc 'Start the app'
    task :start do
    puts "Starting the application..."
    system "thin -s 1 -C config/config.yml -R config/config.ru start"
    puts 'Starting...'
    system "bundle exec thin -s 1 -C config/config-#{ENV['RACK_ENV']}.yml -R config/config.ru start"
    puts 'Started!'
    end

    desc "Stop the application"
    desc 'Stop the app'
    task :stop do
    puts "Stopping the application..."
    Dir.new("/home/user/webapps/app_name/tmp/pids").each do |file|
    puts 'Stopping...'
    Dir.new(File.join(File.dirname(__FILE__), 'tmp/pids')).each do |file|
    prefix = file.to_s
    if prefix[0, 4] == 'thin'
    puts "Stopping server on port #{file[/\d+/]}..."
    system "thin stop -Ptmp/pids/#{file}"
    puts "Stopping the server on port #{file[/\d+/]}..."
    system "bundle exec thin stop -Ptmp/pids/#{file}"
    end
    end
    puts 'Stopped!'
    end

    desc "Restart the application"
    desc 'Restart the application'
    task :restart do
    puts "Restarting the application..."
    Rake::Task["thin:stop"].invoke
    Rake::Task["thin:start"].invoke
    puts 'Restarting...'
    Rake::Task['thin:stop'].invoke
    Rake::Task['thin:start'].invoke
    puts 'Restarted!'
    end

    end

    # FIXME: Refactor the deployment code

    desc "Deploy to server"
    task :deploy, :password do |t, args|
    puts "Deploying to server..."
    8 changes: 8 additions & 0 deletions config-development.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # Server options:
    address: 127.0.0.1
    port: 5000

    # Adapter options:
    environment: development

    # See `thin -h` for more options
    12 changes: 12 additions & 0 deletions config-production.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # Server options:
    port: 27017

    # Adapter options:
    environment: production

    # Daemon options:
    daemonize: true
    user: dwaynecrooks
    group: dwaynecrooks

    # See `thin -h` for more options
    14 changes: 9 additions & 5 deletions config.ru
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,11 @@
    # config/config.ru
    # Assuming a classic style app
    require './app'
    run Sinatra::Application

    require "rubygems"
    require "bundler/setup"
    # Modular style app
    # require './awesome-app'
    # run AwesomeApp

    require './app'
    run Sinatra::Application
    # or

    # map('/end/point') { run AwesomeApp }
    13 changes: 0 additions & 13 deletions config.yml
    Original file line number Diff line number Diff line change
    @@ -1,13 +0,0 @@
    # config/config.yml

    environment: production
    chdir: /home/user/webapps/app_name
    address: 127.0.0.1
    user: user
    group: user
    port: 12345 # you get this from the WebFaction Control Panel after creating the custom application
    rackup: /home/user/webapps/app_name/config/config.ru
    max_conns: 1024
    timeout: 30
    max_persistent_conns: 512
    daemonize: true
  4. dwayne revised this gist Jun 11, 2012. 1 changed file with 7 additions and 9 deletions.
    16 changes: 7 additions & 9 deletions Rakefile
    Original file line number Diff line number Diff line change
    @@ -11,13 +11,11 @@ namespace :thin do
    desc "Stop the application"
    task :stop do
    puts "Stopping the application..."
    Dir.new("/home/user/webapps/app_name/tmp/pids").each do |file|
    Thread.new do
    prefix = file.to_s
    if prefix[0, 4] == 'thin'
    puts "Stopping server on port #{file[/\d+/]}..."
    system "thin stop -Ptmp/pids/#{file}"
    end
    Dir.new("/home/user/webapps/app_name/tmp/pids").each do |file|
    prefix = file.to_s
    if prefix[0, 4] == 'thin'
    puts "Stopping server on port #{file[/\d+/]}..."
    system "thin stop -Ptmp/pids/#{file}"
    end
    end
    end
    @@ -35,13 +33,13 @@ desc "Deploy to server"
    task :deploy, :password do |t, args|
    puts "Deploying to server..."

    system "rsync --exclude 'log/*.log' --exclude 'tmp/**/*' --exclude '.rvmrc' --exclude '.git' --exclude '.gitignore' -rltvz -e ssh . [email protected]:/home/user/webapps/app_name"
    system "rsync --exclude 'log/*.log' --exclude 'tmp/**/*' --exclude '.git' --exclude '.gitignore' -rltvz -e ssh . [email protected]:/home/user/webapps/app_name"

    require "net/ssh"
    Net::SSH.start("user.webfactional.com", "user", :password => args[:password]) do |ssh|
    commands = [
    "cd /home/user/webapps/app_name",
    "rvm use 1.9.3@app_name --create", # assumes, rvm is already setup on the server
    "rvm use 1.9.3-p194@app_name --create", # assumes, rvm is already setup on the server
    "bundle install --without=development",
    # "bundle exec whenever --update-crontab", optionally, schedule cron jobs using whenever
    "bundle exec rake thin:restart"
  5. dwayne created this gist Jun 11, 2012.
    54 changes: 54 additions & 0 deletions Rakefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    require 'rake'

    namespace :thin do

    desc "Start the application"
    task :start do
    puts "Starting the application..."
    system "thin -s 1 -C config/config.yml -R config/config.ru start"
    end

    desc "Stop the application"
    task :stop do
    puts "Stopping the application..."
    Dir.new("/home/user/webapps/app_name/tmp/pids").each do |file|
    Thread.new do
    prefix = file.to_s
    if prefix[0, 4] == 'thin'
    puts "Stopping server on port #{file[/\d+/]}..."
    system "thin stop -Ptmp/pids/#{file}"
    end
    end
    end
    end

    desc "Restart the application"
    task :restart do
    puts "Restarting the application..."
    Rake::Task["thin:stop"].invoke
    Rake::Task["thin:start"].invoke
    end

    end

    desc "Deploy to server"
    task :deploy, :password do |t, args|
    puts "Deploying to server..."

    system "rsync --exclude 'log/*.log' --exclude 'tmp/**/*' --exclude '.rvmrc' --exclude '.git' --exclude '.gitignore' -rltvz -e ssh . [email protected]:/home/user/webapps/app_name"

    require "net/ssh"
    Net::SSH.start("user.webfactional.com", "user", :password => args[:password]) do |ssh|
    commands = [
    "cd /home/user/webapps/app_name",
    "rvm use 1.9.3@app_name --create", # assumes, rvm is already setup on the server
    "bundle install --without=development",
    # "bundle exec whenever --update-crontab", optionally, schedule cron jobs using whenever
    "bundle exec rake thin:restart"
    ].join " && "

    ssh.exec commands
    end

    puts "Finished!"
    end
    7 changes: 7 additions & 0 deletions config.ru
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # config/config.ru

    require "rubygems"
    require "bundler/setup"

    require './app'
    run Sinatra::Application
    13 changes: 13 additions & 0 deletions config.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    # config/config.yml

    environment: production
    chdir: /home/user/webapps/app_name
    address: 127.0.0.1
    user: user
    group: user
    port: 12345 # you get this from the WebFaction Control Panel after creating the custom application
    rackup: /home/user/webapps/app_name/config/config.ru
    max_conns: 1024
    timeout: 30
    max_persistent_conns: 512
    daemonize: true