class Astrails::Safe::Ftp

Protected Instance Methods

active?() click to toggle source
# File lib/astrails/safe/ftp.rb, line 7
def active?
  host && user
end
cleanup() click to toggle source
# File lib/astrails/safe/ftp.rb, line 57
def cleanup
  return if local_only? || dry_run?

  return unless keep = config[:keep, :ftp]

  puts "listing files: #{host}:#{base}*" if verbose?
  if !port
     port = 21
  end
  Net::FTP.open(host) do |ftp|
    ftp.connect(host, port)
    ftp.login(user, password)
    files = ftp.nlst(path)
    pattern = File.basename("#{base}")
    files = files.reject{ |x| !x.start_with?(pattern)}
    puts files.collect {|x| x} if verbose?

    files = files.
      collect {|x| x }.
      sort

    cleanup_with_limit(files, keep) do |f|
      file = File.path(f)
      puts "removing ftp file #{host}:#{file}" if dry_run? || verbose?
      ftp.delete(file) unless dry_run? || local_only?
    end
  end
end
host() click to toggle source
# File lib/astrails/safe/ftp.rb, line 86
def host
  config[:ftp, :host]
end
password() click to toggle source
# File lib/astrails/safe/ftp.rb, line 94
def password
  config[:ftp, :password]
end
path() click to toggle source
# File lib/astrails/safe/ftp.rb, line 11
def path
  @path ||= expand(config[:ftp, :path] || config[:local, :path] || ":kind/:id")
end
port() click to toggle source
# File lib/astrails/safe/ftp.rb, line 98
def port
  config[:ftp, :port]
end
save() click to toggle source
# File lib/astrails/safe/ftp.rb, line 15
def save
  raise RuntimeError, "pipe-streaming not supported for FTP." unless @backup.path

  puts "Uploading #{host}:#{full_path} via FTP" if verbose? || dry_run?

  unless dry_run? || local_only?
    if !port
      port = 21
    end
    Net::FTP.open(host) do |ftp|
      ftp.connect(host, port)
      ftp.login(user, password)

      dir = File.dirname(full_path)
      parts = dir.split("/")
      growing_path = ""
      for part in parts
        next if part == ""
        if growing_path == ""
          growing_path = part
        else
          growing_path = File.join(growing_path, part)
        end
        puts "Trying to create remote directory (#{growing_path})" if verbose?
        begin
          ftp.mkdir(growing_path)
        rescue Net::FTPPermError
          puts "Remote directory (#{growing_path}) exists, or no enough permissions" if verbose?
        end
      end

      puts "Sending #{@backup.path} to #{full_path}" if verbose?
      begin
        ftp.put(@backup.path, full_path)
      rescue Net::FTPPermError
        puts "Ensuring remote path (#{path}) exists" if verbose?
      end
    end
    puts "...done" if verbose?
  end
end
user() click to toggle source
# File lib/astrails/safe/ftp.rb, line 90
def user
  config[:ftp, :user]
end