Skip to content

Commit

Permalink
fixes, hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Paxa committed Mar 9, 2010
1 parent 3b3eafa commit bc80126
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
nbproject
test
File renamed without changes.
26 changes: 26 additions & 0 deletions bin/sunraise
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby

require 'rubygems'
require 'rainbow'

require File.join(File.dirname(__FILE__), '..', 'lib', 'config')
require File.join(File.dirname(__FILE__), '..', 'lib', 'deployer')

include SunRaise::PubConf


if File.expand_path(__FILE__) == File.expand_path(File.join(Dir.pwd, 'sunraise'))
puts "Run it from you app folder, containing sunraise file with deploy instructions"
exit
end

unless File.file?(File.join Dir.pwd, 'sunraise')
puts "No sunraise file found at #{Dir.pwd}"
exit
end


at_exit { SunRaise::Deployer.new.go! }


load File.join Dir.pwd, 'sunraise'
32 changes: 29 additions & 3 deletions lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module SunRaise
class Config

include Singleton
attr_accessor :conf
attr_accessor :conf, :callbacks

class << self
attr_accessor :config_methods
Expand All @@ -24,7 +24,11 @@ class << self
:verbose,
:force,
:release,
:remake
:remake,
:test_rails_app,
:auto_migrate,
:help,
:destroy
]

@config_methods.each do |method_name|
Expand All @@ -35,7 +39,16 @@ class << self
end

def initialize
@conf = {:verbose => false, :local_project_path => '.', :release => true}
@callbacks = {:after => ''}
@conf = {
:verbose => false,
:local_project_path => '.',
:release => true,
:test_rails_app => true,
:auto_migrate => true,
:help => false
}

cl_options
end

Expand All @@ -56,6 +69,15 @@ def cl_options
opts.on("--remake", "Delete current deploy and make initial deploy") do |remake|
@conf[:remake] = remake
end

opts.on("-d", "--destroy", "Delete current deploy and make initial deploy") do |destroy|
@conf[:destroy] = destroy
end

opts.on("-h", "--help", "Show this help") do |help|
@conf[:help] = true
puts opts
end
end.parse!
end

Expand All @@ -67,5 +89,9 @@ module PubConf
SunRaise::Config.instance.send method_name, value
end
end

def after_deploy &block
SunRaise::Config.instance.callbacks[:after] = block
end
end
end
87 changes: 74 additions & 13 deletions lib/deployer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ module SunRaise
class Deployer

def go!
return if conf[:help]
return destroy_existen! if conf[:destroy]
if !conf[:remake] && initiated?
update
else
init_deploy
end

callbacks :after

puts "SSH OUT: \n" + @ssh_out.join("\n") if conf[:verbose]
end

Expand All @@ -29,13 +33,17 @@ def init_deploy
"mkdir -p log shared tmp",
"git clone #{conf[:git_url]} current"
]
log_ok ""
log_ok "Created dir and cloned repo"

test_rails_app! 'current' if conf[:test_rails_app]
auto_migrate! 'current' if conf[:auto_migrate]

log_ok "Maked new dirs and cloned repo"
log_ok "Made new dirs and cloned repo"
make_links 'current'
end

def update

last_commit = ssh_exec ["cd #{current_path}", "git log -1 --pretty=format:\"%H\""]
last_repo_commit = (`cd #{conf[:local_project_path]} && git ls-remote #{conf[:git_url]}`).split("\t").first

Expand All @@ -61,9 +69,11 @@ def update
log_ok "Forked, git updated"

make_links @new_dir

test_rails_app! @new_dir if conf[:test_rails_app]
auto_migrate! @new_dir if conf[:auto_migrate]


release!
release! if conf[:release]
end

def release!
Expand Down Expand Up @@ -102,14 +112,67 @@ def make_links dist_dir
end

ssh_exec ["cd #{File.join deploy_path, dist_dir}"] + links
@ssh_out.each {|m| puts m}
log_ok "Made links"
end

def initiated?
ssh_exec "cd #{current_path}"
last_msg = @ssh_out.pop
!(last_msg && last_msg =~ /No such file or directory/)
end

def destroy_existen!
log_ok "Removing existen deploy dir"
ssh_exec "rm #{deploy_path} -rf"
end

def test_rails_app! dir
@app_about ||= rails_app_about dir
if !@app_about.index('Application root')
if !@app_about.index('Database schema version')
log_error "Rails app test fail"
else
log_error "Rails app test: Database didn't configurated"
end
puts @app_about
log_error "Deploy aborted, use " + "ssh #{conf[:remote_user]}@#{conf[:remote_host]}".italic + " to fix it"
exit
else
log_ok "Rails app successfully tested"
end
end

def auto_migrate! dir
@app_about ||= rails_app_about dir
matches = @app_about.match(/Database schema version\s+ ([0-9]+)/)
remote_magration_version = matches && matches.size > 0 && matches[1] || 0
local_about = `#{File.join conf[:local_project_path], 'script', 'about'}`
local_migration_version = local_about.match(/Database schema version\s+ ([0-9]+)/)[1]
if remote_magration_version == local_migration_version
msg_ok "No new migrations"
else
log_ok "Rinning rake db:migrate"
puts ssh_exec ["cd #{File.join deploy_path, dir}", "rake db:migrate RAILS_ENV=production"] # run migrations
end
end

log_ok "Maked links"
def rails_app_about dir
ssh_exec "RAILS_ENV=production " + File.join(deploy_path, dir, 'script', 'about') # run script/about
end

def conf
SunRaise::Config.instance.conf
end

def callbacks name
c = SunRaise::Config.instance.callbacks

if c[name].class == Proc
instance_eval &c[name]
end
end

def current_path
File.join conf[:deploy_to], 'current'
end
Expand All @@ -118,19 +181,17 @@ def deploy_path
conf[:deploy_to]
end

def app_path
File.join deploy_path, (conf[:release] ? 'current' : 'pre_release' )
end

def log_ok msg
puts ":: ".color(:green) + "#{msg}".bright
end

def initiated?
ssh_exec "cd #{current_path}"
last_msg = @ssh_out.pop
!(last_msg && last_msg =~ /No such file or directory/)
def log_error msg
puts "!! ".bright.color(:red) + "#{msg}".bright
end

def destroy_existen!
log_ok "Removing existen deploy dir"
ssh_exec "rm #{deploy_path} -rf"
end
end
end
23 changes: 0 additions & 23 deletions sunraise

This file was deleted.

12 changes: 12 additions & 0 deletions sunraise.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
remote_host 'twews.com'
remote_user 'root'

git_url 'git://github.com/Paxa/sunraise_test_app.git'

deploy_to '/tmp/deploy/suntest'
then_link_to '/tmp/www/suntest'

shared_dirs ['lib']
linked_dirs ['logs', 'tmp']

local_project_path "/home/paxa/Dropbox/sunraise/test/suntest"
13 changes: 13 additions & 0 deletions sunraise.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Gem::Specification.new do |s|
s.name = "sunraise"
s.version = "0.1"
s.summary = "Super-fast and simple rails deployment."
s.description = "Super-fast and simple rails deployment"
s.author = "Pavel Evstigneev"
s.email = "pavel.evst@gmail.com"
s.homepage = "http://github.com/Paxa/sunraise"
s.has_rdoc = false
s.executables = ["sunraise"]
s.rubyforge_project = "sunraise"
s.files = [ "bin/sunraise", "lib/config.rb", "lib/deployer.rb", "README.md", "sunraise.gemspec", 'sunraise.example']
end
18 changes: 0 additions & 18 deletions sunraise~

This file was deleted.

1 change: 0 additions & 1 deletion test/configs
Submodule configs deleted from b6de9f
1 change: 0 additions & 1 deletion test/newrepo
Submodule newrepo deleted from b6de9f

0 comments on commit bc80126

Please sign in to comment.