mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-27 07:33:05 +00:00
rewrite class sync ssh
This commit is contained in:
parent
ec982ce4d8
commit
e1de7b7704
1 changed files with 45 additions and 68 deletions
|
@ -2,79 +2,56 @@
|
|||
# author: nishiki
|
||||
# mail: nishiki@yaegashi.fr
|
||||
|
||||
require 'rubygems'
|
||||
require 'i18n'
|
||||
require 'net/ssh'
|
||||
require 'net/sftp'
|
||||
|
||||
module MPW
|
||||
class SyncSSH
|
||||
|
||||
attr_accessor :error_msg
|
||||
attr_accessor :enable
|
||||
|
||||
# Constructor
|
||||
# @args: host -> the server host
|
||||
# port -> ther connection port
|
||||
# gpg_key -> the gpg key
|
||||
# password -> the remote password
|
||||
def initialize(host, user, password, path, port=nil)
|
||||
@error_msg = nil
|
||||
@enable = false
|
||||
|
||||
@host = host
|
||||
@user = user
|
||||
@password = password
|
||||
@path = path
|
||||
@port = port.instance_of?(Integer) ? 22 : port
|
||||
end
|
||||
|
||||
# Connect to server
|
||||
# @rtrn: false if the connection fail
|
||||
def connect
|
||||
Net::SSH.start(@host, @user, password: @password, port: @port) do |ssh|
|
||||
@enable = true
|
||||
end
|
||||
rescue Exception => e
|
||||
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
|
||||
@enable = false
|
||||
else
|
||||
return @enable
|
||||
end
|
||||
|
||||
# Get data on server
|
||||
# @args: gpg_password -> the gpg password
|
||||
# @rtrn: nil if nothing data or error
|
||||
def get(file_tmp)
|
||||
return false if not @enable
|
||||
|
||||
Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp|
|
||||
sftp.lstat(@path) do |response|
|
||||
sftp.download!(@path, file_tmp) if response.ok?
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
rescue Exception => e
|
||||
@error_msg = "#{I18n.t('error.sync.download')}\n#{e}"
|
||||
return false
|
||||
end
|
||||
|
||||
# Update the remote data
|
||||
# @args: file_gpg -> the data to send on server
|
||||
# @rtrn: false if there is a problem
|
||||
def update(file_gpg)
|
||||
return true if not @enable
|
||||
|
||||
Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp|
|
||||
sftp.upload!(file_gpg, @path)
|
||||
end
|
||||
|
||||
return true
|
||||
rescue Exception => e
|
||||
@error_msg = "#{I18n.t('error.sync.upload')}\n#{e}"
|
||||
return false
|
||||
end
|
||||
class SyncSSH
|
||||
|
||||
# Constructor
|
||||
# @args: config -> the config
|
||||
def initialize(config)
|
||||
@host = config['host']
|
||||
@user = config['user']
|
||||
@password = config['password']
|
||||
@path = config['path']
|
||||
@port = config['port'].instance_of?(Integer) ? 22 : config['port']
|
||||
end
|
||||
|
||||
# Connect to server
|
||||
# @rtrn: false if the connection fail
|
||||
def connect
|
||||
Net::SSH.start(@host, @user, password: @password, port: @port) do
|
||||
break
|
||||
end
|
||||
rescue Exception => e
|
||||
raise "#{I18n.t('error.sync.connection')}\n#{e}"
|
||||
end
|
||||
|
||||
# Get data on server
|
||||
# @args: gpg_password -> the gpg password
|
||||
# @rtrn: nil if nothing data or error
|
||||
def get(file_tmp)
|
||||
Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp|
|
||||
sftp.lstat(@path) do |response|
|
||||
sftp.download!(@path, file_tmp) if response.ok?
|
||||
end
|
||||
end
|
||||
rescue Exception => e
|
||||
raise "#{I18n.t('error.sync.download')}\n#{e}"
|
||||
end
|
||||
|
||||
# Update the remote data
|
||||
# @args: file_gpg -> the data to send on server
|
||||
# @rtrn: false if there is a problem
|
||||
def update(file_gpg)
|
||||
Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp|
|
||||
sftp.upload!(file_gpg, @path)
|
||||
end
|
||||
rescue Exception => e
|
||||
raise "#{I18n.t('error.sync.upload')}\n#{e}"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue