Class: Publisher::Sftp

Inherits:
Object
  • Object
show all
Defined in:
lib/depengine/publisher/sftp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#remote_hostObject

Returns the value of attribute remote_host.



3
4
5
# File 'lib/depengine/publisher/sftp.rb', line 3

def remote_host
  @remote_host
end

#remote_userObject

Returns the value of attribute remote_user.



4
5
6
# File 'lib/depengine/publisher/sftp.rb', line 4

def remote_user
  @remote_user
end

#ssh_key_fileObject

Returns the value of attribute ssh_key_file.



5
6
7
# File 'lib/depengine/publisher/sftp.rb', line 5

def ssh_key_file
  @ssh_key_file
end

Instance Method Details

#chmod(path, permissions, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/depengine/publisher/sftp.rb', line 62

def chmod(path, permissions, options = {})
  Helper.validates_presence_of remote_host, 'Remote Host not set'
  Helper.validates_presence_of remote_user, 'Remote User not set'
  Helper.validates_presence_of ssh_key_file, 'SSH-Keyfile not set'

  # check ssh keyfile
  unless File.file? ssh_key_file
    $log.writer.error "Can not find SSH keyfile #{ssh_key_file}"
    exit 1
  end

  Net::SFTP.start(remote_host, remote_user, \
                  auth_methods: ['publickey'], \
                  forward_agent: false, \
                  keys: ssh_key_file, \
                  timeout: 90) do |sftp|

    $log.writer.debug 'Set permissions'

    if options[:recursive]
      sftp.dir.entries(path).each do |entry|
        next if entry.name == '.' or entry.name == '..'
        child_path = File.join(path, entry.name)

        sftp.setstat!(child_path, permissions: permissions)
      end

      sftp.setstat!(path, permissions: permissions)

    else
      sftp.setstat!(path, permissions: permissions)
    end
  end
end

#copy(source, target, _options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
53
54
55
56
57
58
59
60
# File 'lib/depengine/publisher/sftp.rb', line 7

def copy(source, target, _options = {})
  Helper.validates_presence_of remote_host, 'Remote Host not set'
  Helper.validates_presence_of remote_user, 'Remote User not set'
  Helper.validates_presence_of ssh_key_file, 'SSH-Keyfile not set'

  # check ssh keyfile
  unless File.file? ssh_key_file
    $log.writer.error "Can not find SSH keyfile #{ssh_key_file}"
    exit 1
  end

  source_last = source.split('/').last
  target_last = target.split('/').last
  unless source_last == target_last
    if not target.end_with?('/')
      target = target + '/' + source_last
    else
      target = target + source_last
    end
  end

  begin

    ### delete directory
    if File.directory?(source)
      Net::SFTP.start(remote_host, remote_user, \
                      auth_methods: ['publickey'], \
                      forward_agent: false, \
                      keys: ssh_key_file, \
                      timeout: 90) do |sftp|

        # delete all files in target, but not the target-dir itself
        $log.writer.debug "Cleaning remote directory #{target}"
        sftp.rm_r!(target, delete_root: true)
      end
    end

    ### copy files
    Net::SFTP.start(remote_host, remote_user, \
                    auth_methods: ['publickey'], \
                    forward_agent: false, \
                    keys: ssh_key_file, \
                    timeout: 90) do |sftp|

      sftp.upload!(source, target)

    end
  rescue => e
    $log.writer.error "Can not upload files to #{target}"
    $log.writer.error e.message
    # $log.writer.error e.backtrace.join("\n")
    exit 1
  end
end