From a1ae241553ccce7869cd51b6ac0a66c2bdc63390 Mon Sep 17 00:00:00 2001 From: tobiaswerner Date: Sat, 8 Jun 2013 22:46:43 +0200 Subject: [PATCH] Added SFTP deployment --- README.md | 16 +++++++++++++ lib/middleman-deploy/commands.rb | 39 ++++++++++++++++++++++++++++++++ middleman-deploy.gemspec | 1 + 3 files changed, 56 insertions(+) diff --git a/README.md b/README.md index 02cbcfe..54e0635 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,22 @@ Edit `config.rb`, and add: Adjust these values accordingly. +## Step 3d - SFTP setup + +**These settings are required.** + +Edit `config.rb`, and add: + + activate :deploy do |deploy| + deploy.method = :sftp + deploy.host = "ftp.example.com" + deploy.user = "tvaughan" + deploy.password = "secret" + deploy.path = "/srv/www/site" + end + +Adjust these values accordingly. + ## Step 4 middleman build [--clean] diff --git a/lib/middleman-deploy/commands.rb b/lib/middleman-deploy/commands.rb index 460d86e..4852f9a 100644 --- a/lib/middleman-deploy/commands.rb +++ b/lib/middleman-deploy/commands.rb @@ -220,6 +220,45 @@ EOF ftp.close end + def deploy_sftp + require 'net/sftp' + require 'ptools' + + host = self.deploy_options.host + user = self.deploy_options.user + pass = self.deploy_options.password + path = self.deploy_options.path + + puts "## Deploying via sftp to #{user}@#{host}:#{path}" + + Net::SFTP.start(host, user, :password => pass) do |sftp| + sftp.mkdir(path) + Dir.chdir(self.inst.build_dir) do + files = Dir.glob('**/*', File::FNM_DOTMATCH) + files.reject { |a| a =~ Regexp.new('\.$') }.each do |f| + if File.directory?(f) + begin + sftp.mkdir("#{path}/#{f}") + puts "Created directory #{f}" + rescue + end + else + begin + sftp.upload(f, "#{path}/#{f}") + rescue Exception => e + reply = e.message + err_code = reply[0,3].to_i + if err_code == 550 + sftp.upload(f, "#{path}/#{f}") + end + end + puts "Copied #{f}" + end + end + end + end + end + end # Alias "d" to "deploy" diff --git a/middleman-deploy.gemspec b/middleman-deploy.gemspec index dc23293..bca343a 100644 --- a/middleman-deploy.gemspec +++ b/middleman-deploy.gemspec @@ -23,4 +23,5 @@ Gem::Specification.new do |s| # Additional dependencies s.add_runtime_dependency("ptools") + s.add_runtime_dependency("net-sftp") end