1
0
Fork 0
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:
nishiki 2016-05-12 22:44:57 +02:00
parent ec982ce4d8
commit e1de7b7704

View file

@ -2,7 +2,6 @@
# author: nishiki # author: nishiki
# mail: nishiki@yaegashi.fr # mail: nishiki@yaegashi.fr
require 'rubygems'
require 'i18n' require 'i18n'
require 'net/ssh' require 'net/ssh'
require 'net/sftp' require 'net/sftp'
@ -10,70 +9,48 @@ require 'net/sftp'
module MPW module MPW
class SyncSSH class SyncSSH
attr_accessor :error_msg
attr_accessor :enable
# Constructor # Constructor
# @args: host -> the server host # @args: config -> the config
# port -> ther connection port def initialize(config)
# gpg_key -> the gpg key @host = config['host']
# password -> the remote password @user = config['user']
def initialize(host, user, password, path, port=nil) @password = config['password']
@error_msg = nil @path = config['path']
@enable = false @port = config['port'].instance_of?(Integer) ? 22 : config['port']
@host = host
@user = user
@password = password
@path = path
@port = port.instance_of?(Integer) ? 22 : port
end end
# Connect to server # Connect to server
# @rtrn: false if the connection fail # @rtrn: false if the connection fail
def connect def connect
Net::SSH.start(@host, @user, password: @password, port: @port) do |ssh| Net::SSH.start(@host, @user, password: @password, port: @port) do
@enable = true break
end end
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}" raise "#{I18n.t('error.sync.connection')}\n#{e}"
@enable = false
else
return @enable
end end
# Get data on server # Get data on server
# @args: gpg_password -> the gpg password # @args: gpg_password -> the gpg password
# @rtrn: nil if nothing data or error # @rtrn: nil if nothing data or error
def get(file_tmp) def get(file_tmp)
return false if not @enable
Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp| Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp|
sftp.lstat(@path) do |response| sftp.lstat(@path) do |response|
sftp.download!(@path, file_tmp) if response.ok? sftp.download!(@path, file_tmp) if response.ok?
end end
end end
return true
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.sync.download')}\n#{e}" raise "#{I18n.t('error.sync.download')}\n#{e}"
return false
end end
# Update the remote data # Update the remote data
# @args: file_gpg -> the data to send on server # @args: file_gpg -> the data to send on server
# @rtrn: false if there is a problem # @rtrn: false if there is a problem
def update(file_gpg) def update(file_gpg)
return true if not @enable
Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp| Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp|
sftp.upload!(file_gpg, @path) sftp.upload!(file_gpg, @path)
end end
return true
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.sync.upload')}\n#{e}" raise "#{I18n.t('error.sync.upload')}\n#{e}"
return false
end end
end end