Sha256: 79c54033c3e6e245c66c123691b7fd3e0b7c79355a6dafa210a7867f48b7d016

Contents?: true

Size: 1.53 KB

Versions: 5

Compression:

Stored size: 1.53 KB

Contents

require 'net/sftp'
require 'ptools'

module Middleman
  module Deploy
    module Methods
      class Sftp < Ftp

        def process
          puts "## Deploying via sftp to #{self.user}@#{self.host}:#{path}"

          # `nil` is a valid value for user and/or pass.
          Net::SFTP.start(self.host, self.user, :password => self.pass, :port => self.port) do |sftp|
            sftp.mkdir(self.path)

            Dir.chdir(self.server_instance.build_dir) do
              filtered_files.each do |filename|
                if File.directory?(filename)
                  upload_directory(sftp, filename)
                else
                  upload_file(sftp, filename)
                end
              end
            end
          end
        end

      protected

        def handle_exception(exception)
          reply     = exception.message
          err_code  = reply[0,3].to_i

          if err_code == 550
            sftp.upload(filename, file_path)
          end
        end

        def upload_directory(sftp, filename)
          file_path = "#{self.path}/#{filename}"

          begin
            sftp.mkdir(file_path)
            puts "Created directory #{filename}"
          rescue
          end
        end

        def upload_file(sftp, filename)
          file_path = "#{self.path}/#{filename}"

          begin
            sftp.upload(filename, file_path)
          rescue Exception => exception
            handle_exception(exception, file_path)
          end

          puts "Copied #{filename}"
        end

      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
middleman-deploy-0.2.4 lib/middleman-deploy/methods/sftp.rb
middleman-deploy-0.2.3 lib/middleman-deploy/methods/sftp.rb
middleman-deploy-0.2.2 lib/middleman-deploy/methods/sftp.rb
middleman-deploy-0.2.1 lib/middleman-deploy/methods/sftp.rb
middleman-deploy-0.2.0 lib/middleman-deploy/methods/sftp.rb