-
-
Save sandrods/349459 to your computer and use it in GitHub Desktop.
Setup Rails apps on the production server as a git repo and creates the Apache virtual host.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/ruby | |
| require 'fileutils' | |
| require 'rubygems' | |
| require 'commander/import' | |
| require 'ostruct' | |
| require 'yaml' | |
| class AppSetuper | |
| DEPLOY_TO = "/var/www" | |
| VHOSTS_PATH = "/etc/apache2/vhosts.d" | |
| DB_FILE = "production" | |
| DOMAIN = "tre-rs.gov.br" | |
| OWNER = "apache" | |
| def initialize(app_name, opts = {}) | |
| @app_name = app_name | |
| end | |
| def setup | |
| log "\nSetting Up...\n" | |
| create_git_repo | |
| create_hook_script | |
| create_vhost | |
| end | |
| def create_git_repo | |
| FileUtils.cd DEPLOY_TO | |
| FileUtils.mkdir_p @app_name | |
| FileUtils.cd @app_name | |
| %x{ git init } | |
| FileUtils.chown_R OWNER, OWNER, "#{DEPLOY_TO}/#{@app_name}" | |
| say "=> Created git repo in #{DEPLOY_TO}/#{@app_name}" | |
| end | |
| def create_hook_script | |
| @post_update = <<-PU | |
| #!/bin/sh | |
| exit_with_error() { | |
| echo "!!!! An error has occurred !!!!" | |
| exit 1 | |
| } | |
| # Initial directory is .git, so go to the working copy directory | |
| cd .. | |
| export RAILS_ENV="production" | |
| echo "************************" | |
| echo "Deploying application..." | |
| echo "************************" | |
| # Add everything to the index and then reset hard to both sweep changed files (like cached pages) | |
| # and update the working copy. | |
| echo " * Updating application working tree" | |
| env -i git add . | |
| env -i git reset --hard || exit_with_error# | |
| echo " * Updating database configuration" | |
| cp config/database.#{DB_FILE}.yml config/database.yml || exit_with_error | |
| echo " * Restarting app" | |
| mkdir -p log | |
| mkdir -p tmp | |
| touch tmp/restart.txt | |
| echo "*************************" | |
| echo "Successfully deployed app" | |
| echo "*************************" | |
| PU | |
| FileUtils.cd DEPLOY_TO | |
| FileUtils.cd @app_name | |
| File.open(".git/hooks/post-receive", 'w') do |f| | |
| f.write(@post_update) | |
| end | |
| FileUtils.chmod 0755, ".git/hooks/post-receive" | |
| FileUtils.chown OWNER, OWNER, ".git/hooks/post-receive" | |
| %x{ git config receive.denyCurrentBranch ignore } | |
| say "=> Updated post-receive script in .git/hooks/post-receive" | |
| end | |
| def create_vhost | |
| @vhost_text = <<-VH | |
| <VirtualHost *:80> | |
| ServerName #{@app_name}.#{DOMAIN} | |
| ServerAlias #{@app_name} | |
| DocumentRoot #{DEPLOY_TO}/#{@app_name}/public | |
| SetOutputFilter DEFLATE | |
| </VirtualHost> | |
| <Directory "#{DEPLOY_TO}/#{@app_name}/public"> | |
| Options FollowSymLinks | |
| AllowOverride None | |
| Order allow,deny | |
| Allow from all | |
| </Directory> | |
| <Directory "/"> | |
| Options FollowSymLinks | |
| AllowOverride None | |
| Order allow,deny | |
| Allow from all | |
| </Directory> | |
| VH | |
| FileUtils.cd VHOSTS_PATH | |
| File.open("#{@app_name}.conf", 'w') do |f| | |
| f.write(@vhost_text) | |
| end | |
| say "=> Created apache vhost in #{VHOSTS_PATH}/#{@app_name}.conf" | |
| say "=> Restarting apache..." | |
| %x{ /etc/init.d/apache2 restart } | |
| say "=> Done" | |
| end | |
| end | |
| program :name, 'RailsAppSetuper' | |
| program :version, '1.0.0' | |
| program :description, 'Setup Rails apps on the production server as a git repo and creates the Apache virtual host.' | |
| #program :help_formatter, Commander::HelpFormatter::TerminalCompact | |
| command :setup do |c| | |
| c.syntax = 'app setup [app_name1, app_name2, ...]' | |
| c.description = 'do all the work (create git repo, create hook script and create apache vhost)' | |
| c.option "--[no-]vhost", "Skip vhost creation" | |
| c.option "--[no-]hook", "Skip git hook script creation" | |
| c.option "--[no-]git", "Skip git repository creation" | |
| c.action do |args, opts| | |
| opts.default :vhost=>true, :hook=>true, :git=>true | |
| unless args[0] | |
| say "ERROR: Please inform at least ONE [app_name]" | |
| exit | |
| end | |
| args.each do |app| | |
| a = AppSetuper.new(app) | |
| a.create_git_repo if opts.git | |
| a.create_hook_script if opts.hook | |
| a.create_vhost if opts.vhost | |
| say "=> To deploy to this server, create a remote on your git workin tree, using the command: \n\n" | |
| say " git remote add deploy #{AppSetuper::OWNER}@#{%x{ hostname }.chomp}.#{AppSetuper::DOMAIN}:#{app}/.git" | |
| say " git push deploy master \n\n" | |
| end | |
| end | |
| end | |
| default_command :setup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment