Class: Autosftp::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/autosftp/connection.rb

Class Method Summary collapse

Class Method Details

.check?(sftp) ⇒ Boolean

username@host:port形式かどうかチェックする。

Returns:

  • (Boolean)


9
10
11
12
13
14
15
# File 'lib/autosftp/connection.rb', line 9

def self.check? sftp
  if /.*\@.*|.*\@.*\:.*/ =~ sftp
    true
  else
    false
  end
end

.create(ssh_hash, local_file, remote_file, permission) ⇒ Object

ファイルの新規作成+更新



36
37
38
39
40
41
42
43
44
# File 'lib/autosftp/connection.rb', line 36

def self.create ssh_hash, local_file, remote_file, permission
  Net::SSH.start(ssh_hash[:host], ssh_hash[:user], {:password => ssh_hash[:password], :port => ssh_hash[:port]}) do |ssh|
    ssh_dir(ssh, File.dirname(remote_file), permission)
    if File.exist? local_file
      ssh.sftp.upload!(local_file, remote_file)
      ssh.exec! "chmod #{permission[:file]} #{remote_file}"
    end
  end
end

.delete(ssh_hash, remote_file) ⇒ Object

ファイルの削除



47
48
49
50
51
# File 'lib/autosftp/connection.rb', line 47

def self.delete ssh_hash, remote_file
  Net::SSH.start(ssh_hash[:host], ssh_hash[:user], {:password => ssh_hash[:password], :port => ssh_hash[:port]}) do |ssh|
    ssh.sftp.remove!(remote_file)
  end
end

.explode(sftp) ⇒ Object

username@host:port形式を分解する。



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/autosftp/connection.rb', line 18

def self.explode sftp
  ssh_hash = {}
  if /.*\@.*/ =~ sftp
    user_host_ary = sftp.split("@")
    ssh_hash[:user] = user_host_ary[0]
    if /.*\:.*/ =~ user_host_ary[1]
      host_port_ary = user_host_ary[1].split(":")
      ssh_hash[:host] = host_port_ary[0]
      ssh_hash[:port] = host_port_ary[1].to_i
    else
      ssh_hash[:host] = user_host_ary[1]
      ssh_hash[:port] = 22
    end
  end
  ssh_hash
end

.ssh_dir(ssh, path, permission) ⇒ Object

ディレクトリの作成



54
55
56
57
58
59
60
61
# File 'lib/autosftp/connection.rb', line 54

def self.ssh_dir ssh, path, permission
  ssh.sftp.stat!(path)
rescue
  parent = File::dirname(path);
  ssh_dir(ssh, parent, permission)
  ssh.sftp.mkdir!(path)
  ssh.exec! "chmod #{permission[:dir]} #{path}"
end