Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

change the way yads works. separate in two commands: setup and deploy

  • Loading branch information...
commit 820c2c31c02309d3af0bf22a0f7f2e8349ce7cff 1 parent 6331da4
@rafaelss authored
View
7 Rakefile
@@ -1,2 +1,9 @@
require 'bundler'
Bundler::GemHelper.install_tasks
+
+require "rake/testtask"
+Rake::TestTask.new do |t|
+ t.libs << "test"
+ t.test_files = FileList['test/test*.rb']
+ t.verbose = true
+end
View
16 bin/yads 100644 → 100755
@@ -1,4 +1,16 @@
#!/usr/bin/env ruby
+require "slop"
-require "yads"
-Yads::Deployer.new(STDOUT).deploy
+opts = Slop.new(:strict => true)
+opts.parse do |action|
+ require "yads"
+ deployer = Yads::Deployer.new(STDOUT)
+
+ action = action.to_sym
+ if !action.empty? && deployer.respond_to?(action)
+ deployer.send(action)
+ else
+ puts "Unknow command \"#{action}\""
+ puts "Usage: yads [setup|deploy]"
+ end
+end
View
4 examples/deploy.yml
@@ -3,10 +3,10 @@ user: username
forward_agent: true/false
path: /path/on/remote/server
+repository: git@repohost.com:myproject.git
commands:
- clone: git clone --depth 1 git@repohost.com:myproject.git . # mandatory, other commands below are optional
reset: git reset --hard
pull: git pull origin master
bundle: bundle install --deployment --binstubs --without development test
- migrate: ENV=production bin/rake db:migrate --silent
+ migrate: RAILS_ENV=production bin/rake db:migrate --silent
restart: touch tmp/restart.txt
View
52 lib/yads.rb
@@ -1,54 +1,6 @@
-require "net/ssh"
-require "yaml"
-
module Yads
-
class ConfigNotFound < Errno::ENOENT; end
- class Deployer
- def initialize(logger = STDOUT)
- @logger = logger
- end
-
- def deploy
- begin
- config = YAML.load(File.open("config/deploy.yml"))
- rescue Errno::ENOENT
- raise Yads::ConfigNotFound, "config/deploy.yml not found"
- end
-
- Net::SSH.start(config["host"], config["user"], :forward_agent => config["forward_agent"]) do |ssh|
- check_path(ssh, config["path"], config["commands"].delete("clone"))
-
- commands = config["commands"].values.unshift("cd #{config['path']}").join(" && ")
- @logger.puts "> #{commands}"
- @logger.puts
-
- ssh.exec(commands) do |ch, stream, data|
- print data
- end
-
- ssh.loop
- end
- end
-
- def check_path(ssh, path, clone)
- @logger.puts "> cd #{path}/.git"
- ssh.exec("cd #{path}/.git") do |ch, stream, data|
- if stream == :stderr
- @logger.puts "> cd #{path}"
- ssh.exec("mkdir -p #{path}; cd #{path}") do |ch1, stream1, data1|
- @logger.puts "> #{clone}"
- ssh.exec(clone) do |ch1, stream1, data1|
- abort data1 if stream1 == :stderr
-
- @logger.puts "> cd #{path}"
- ssh.exec("cd #{path}")
- end
- end
- end
- end
- ssh.loop
- end
- end
+ autoload :SSH, "yads/ssh"
+ autoload :Deployer, "yads/deployer"
end
View
41 lib/yads/deployer.rb
@@ -0,0 +1,41 @@
+require "yaml"
+
+module Yads
+ class Deployer
+
+ def initialize(logger = STDOUT)
+ @logger = logger
+ end
+
+ def setup
+ command = "mkdir -p #{config["path"]} && cd #{config["path"]} && git clone --depth 1 #{config['repository']} ."
+ @logger.puts("> #{command}")
+ @logger.puts(connection.execute(command))
+ end
+
+ def deploy
+ commands = config["commands"].values
+ commands.unshift("cd #{config["path"]}")
+ commands = commands.join(" && ")
+
+ @logger.puts("> #{commands}")
+ connection.execute(commands) do |output|
+ @logger.puts(output)
+ end
+ end
+
+ private
+
+ def config
+ @config ||= begin
+ YAML.load(File.open("config/deploy.yml"))
+ rescue Errno::ENOENT
+ raise Yads::ConfigNotFound, "config/deploy.yml not found"
+ end
+ end
+
+ def connection
+ @connection ||= SSH.new(:host => config["host"], :user => config["user"], :forward_agent => config["forward_agent"])
+ end
+ end
+end
View
25 lib/yads/ssh.rb
@@ -0,0 +1,25 @@
+require "net/ssh"
+
+module Yads
+
+ class SSH
+
+ def initialize(config)
+ @ssh = Net::SSH.start(config[:host], config[:user], :forward_agent => config[:forward_agent])
+ end
+
+ def execute(cmd)
+ if block_given?
+ @ssh.exec(cmd) do |ch, stream, data|
+ yield data
+ end
+ else
+ @ssh.exec!(cmd)
+ end
+ end
+
+ def close
+ @ssh.close
+ end
+ end
+end
View
5 test/fixtures/config/deploy.yml
@@ -1,7 +1,8 @@
host: rafaelss.com
user: deploy
forward_agent: true
-path: /Users/rafael/projects/yads/test/fixtures
+path: /tmp/yads
+repository: git@repohost.com:myrepo.git
commands:
- chdir: cd /tmp/yads
+ migrate: rake db:migrate
touch: touch test
View
50 test/test_ssh.rb
@@ -0,0 +1,50 @@
+require "bundler"
+Bundler.require
+
+require "minitest/autorun"
+require "mocha"
+require "yads/ssh"
+
+class TestSSH < MiniTest::Unit::TestCase
+
+ def test_connect
+ connection_mock
+
+ Yads::SSH.new(:host => "example.org", :user => "deploy", :forward_agent => true)
+ end
+
+ def test_execute
+ session = mock
+ session.expects(:exec!).with("mkdir -p /tmp/yads")
+ connection_mock(session)
+
+ s = Yads::SSH.new(:host => "example.org", :user => "deploy", :forward_agent => true)
+ s.execute("mkdir -p /tmp/yads")
+ end
+
+ def test_execute_with_block
+ session = mock
+ session.expects(:exec).with("echo $PATH").yields(nil, nil, "/usr/bin:/usr/local/bin")
+ connection_mock(session)
+
+ s = Yads::SSH.new(:host => "example.org", :user => "deploy", :forward_agent => true)
+ s.execute("echo $PATH") do |output|
+ assert_equal "/usr/bin:/usr/local/bin", output
+ end
+ end
+
+ def test_close
+ session = mock
+ session.expects(:close)
+ connection_mock(session)
+
+ s = Yads::SSH.new(:host => "example.org", :user => "deploy", :forward_agent => true)
+ s.close
+ end
+
+ private
+
+ def connection_mock(session = nil)
+ Net::SSH.expects(:start).with("example.org", "deploy", :forward_agent => true).returns(session)
+ end
+end
View
59 test/test_yads.rb
@@ -7,47 +7,52 @@
class TestYads < MiniTest::Unit::TestCase
def setup
- @deployer = Yads::Deployer.new(File.open("/dev/null", "w"))
+ @log_file = File.open("/dev/null", "w")
end
- def test_check_path_abort
- ssh = mock
- ssh.expects(:exec).with("cd /tmp/yads/.git").yields("ch", :stderr, "data")
- ssh.expects(:exec).with("git clone git@repohost.com:myrepo.git .").yields("ch1", :stderr, "data1")
-
- assert_raises(SystemExit) do
- @deployer.check_path(ssh, "/tmp/yads", "git clone git@repohost.com:myrepo.git .")
+ def test_try_loading_not_found_config_file_on_setup
+ assert_raises(Yads::ConfigNotFound) do
+ Dir.chdir("/tmp") do
+ deployer = Yads::Deployer.new(@log_file)
+ deployer.setup
+ end
end
end
- def test_check_path
- ssh = mock
- ssh.expects(:exec).with("cd /tmp/yads/.git").yields("ch", :stderr, "data")
- ssh.expects(:exec).with("git clone git@repohost.com:myrepo.git .").yields("ch1", :stdout, "data1")
- ssh.expects(:exec).with("cd /tmp/yads")
- ssh.expects(:loop)
-
- @deployer.check_path(ssh, "/tmp/yads", "git clone git@repohost.com:myrepo.git .")
- end
-
- def test_load_not_found_config_file
+ def test_try_loading_not_found_config_file_on_deploy
assert_raises(Yads::ConfigNotFound) do
Dir.chdir("/tmp") do
- @deployer.deploy
+ deployer = Yads::Deployer.new(@log_file)
+ deployer.deploy
end
end
end
- def test_load_config_file
- Dir.chdir(File.expand_path("../fixtures", __FILE__)) do
+ def test_setup
+ inside_project_root do
ssh = mock
- ssh.expects(:exec).with("cd /tmp/yads && touch test")
- ssh.expects(:loop)
+ ssh.expects(:execute).with("mkdir -p /tmp/yads && cd /tmp/yads && git clone --depth 1 git@repohost.com:myrepo.git .")
+ Yads::SSH.expects(:new).with(:host => "rafaelss.com", :user => "deploy", :forward_agent => true).returns(ssh)
- @deployer.expects(:check_path).with(ssh)
- Net::SSH.expects(:start).with("rafaelss.com", "deploy", :forward_agent => true).yields(ssh)
+ deployer = Yads::Deployer.new(@log_file)
+ deployer.setup
+ end
+ end
+
+ def test_deploy
+ inside_project_root do
+ ssh = mock
+ ssh.expects(:execute).with("cd /tmp/yads && rake db:migrate && touch test")
+ Yads::SSH.expects(:new).with(:host => "rafaelss.com", :user => "deploy", :forward_agent => true).returns(ssh)
- @deployer.deploy
+ deployer = Yads::Deployer.new(@log_file)
+ deployer.deploy
end
end
+
+ private
+
+ def inside_project_root(&block)
+ Dir.chdir(File.expand_path("../fixtures", __FILE__), &block)
+ end
end
View
1  yads.gemspec
@@ -13,6 +13,7 @@ Gem::Specification.new do |s|
s.description = %q{Simple deployment tool that uses a single YAML file to put your code live}
s.add_runtime_dependency "net-ssh", "~> 2.1.0"
+ s.add_runtime_dependency "slop", "~> 1.5.2"
s.add_development_dependency "minitest", "~> 2.0.2"
s.add_development_dependency "mocha", "~> 0.9.10"
Please sign in to comment.
Something went wrong with that request. Please try again.