Class: Ftpeter::Transport::Lftp

Inherits:
Object
  • Object
show all
Defined in:
lib/ftpeter/transport/lftp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, changes) ⇒ Lftp

Returns a new instance of Lftp.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ftpeter/transport/lftp.rb', line 9

def initialize(connection, changes)
  @changes = changes
  @lftp_script = []
  @script = Pathname.new('./lftp_script').expand_path

  @host = connection.host
  @credentials = connection.credentials
  @dir = connection.dir
  @commands = connection.commands

  prepare
end

Instance Attribute Details

#scriptObject (readonly)

Returns the value of attribute script.



7
8
9
# File 'lib/ftpeter/transport/lftp.rb', line 7

def script
  @script
end

Instance Method Details

#cleanupObject



73
74
75
# File 'lib/ftpeter/transport/lftp.rb', line 73

def cleanup
  script.delete
end

#commandsObject



54
55
56
# File 'lib/ftpeter/transport/lftp.rb', line 54

def commands
  @lftp_script
end

#executeObject



69
70
71
# File 'lib/ftpeter/transport/lftp.rb', line 69

def execute
  system("lftp -f #{script}")
end

#informObject



58
59
60
# File 'lib/ftpeter/transport/lftp.rb', line 58

def inform
  @lftp_script.join("\n")
end

#persistObject



62
63
64
65
66
67
# File 'lib/ftpeter/transport/lftp.rb', line 62

def persist
  script.open('w') do |f|
    f << @lftp_script.flatten.join("\n")
    f << "\n"
  end
end

#prepareObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ftpeter/transport/lftp.rb', line 22

def prepare
  # lftp connection header
  @lftp_script << "open #{@host}"
  if @credentials
    @lftp_script << "user #{@credentials['user']} #{@credentials['pass']}"
  end
  @lftp_script << "cd #{@dir}"

  # lftp file commands
  @lftp_script << @changes.deleted.map do |fn|
    "rm #{fn}"
  end
  @lftp_script << @changes.newdirs.map do |fn|
    "mkdir -p #{fn}"
  end
  @lftp_script << @changes.added.map do |fn|
    "put #{fn} -o #{fn}"
  end
  @lftp_script << @changes.changed.map do |fn|
    "put #{fn} -o #{fn}"
  end
  @lftp_script << @commands.split("\n").map do |cmd|
    "!#{cmd}"
  end if @commands
  @lftp_script << '!echo ""'
  @lftp_script << '!echo "Deployment complete"'

  @lftp_script.flatten!.compact!

  @lftp_script
end