mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-27 07:33:05 +00:00
fix sync via ssh
This commit is contained in:
parent
b71f98d238
commit
1043b5c21c
2 changed files with 26 additions and 13 deletions
|
@ -9,8 +9,10 @@ module MPW
|
||||||
|
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'i18n'
|
require 'i18n'
|
||||||
|
require 'net/ssh'
|
||||||
|
require 'net/scp'
|
||||||
|
|
||||||
class MPW
|
class SSH
|
||||||
|
|
||||||
attr_accessor :error_msg
|
attr_accessor :error_msg
|
||||||
attr_accessor :enable
|
attr_accessor :enable
|
||||||
|
@ -33,11 +35,12 @@ module MPW
|
||||||
@user = user
|
@user = user
|
||||||
@password = password
|
@password = password
|
||||||
@path = path
|
@path = path
|
||||||
@port = port.nil? || port.empty? ? 22 : port.to_i
|
@port = port.instance_of?(Integer) ? 22 : port
|
||||||
|
|
||||||
Net::SSH.start(@host, @user, :password => @password, :port => @port) do
|
Net::SSH.start(@host, @user, :password => @password, :port => @port) do
|
||||||
|
@enable = true
|
||||||
end
|
end
|
||||||
rescue
|
rescue Exception => e
|
||||||
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
|
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
|
||||||
@enable = false
|
@enable = false
|
||||||
else
|
else
|
||||||
|
@ -53,12 +56,8 @@ module MPW
|
||||||
end
|
end
|
||||||
|
|
||||||
tmp_file = tmpfile
|
tmp_file = tmpfile
|
||||||
Net::SCP.start(@host, @user, :password => @password, :port => @port) do |ssh|
|
Net::SCP.start(@host, @user, :password => @password, :port => @port) do |scp|
|
||||||
ssh.scp.download(@path, tmp_file)
|
scp.download!(@path, tmp_file)
|
||||||
end
|
|
||||||
|
|
||||||
File.open(tmp_file, 'w') do |file|
|
|
||||||
file << msg['data']
|
|
||||||
end
|
end
|
||||||
|
|
||||||
mpw = MPW.new(tmp_file)
|
mpw = MPW.new(tmp_file)
|
||||||
|
@ -83,8 +82,12 @@ module MPW
|
||||||
end
|
end
|
||||||
|
|
||||||
tmp_file = tmpfile
|
tmp_file = tmpfile
|
||||||
Net::SCP.start(@host, @user, :password => @password, :port => @port) do |ssh|
|
File.open(tmp_file, "w") do |file|
|
||||||
ssh.scp.upload(tmp_file, @path)
|
file << data
|
||||||
|
end
|
||||||
|
|
||||||
|
Net::SCP.start(@host, @user, :password => @password, :port => @port) do |scp|
|
||||||
|
scp.upload!(tmp_file, @path)
|
||||||
end
|
end
|
||||||
|
|
||||||
File.unlink(tmp_file)
|
File.unlink(tmp_file)
|
||||||
|
|
|
@ -12,6 +12,7 @@ require 'yaml'
|
||||||
|
|
||||||
require "#{APP_ROOT}/MPW/MPW"
|
require "#{APP_ROOT}/MPW/MPW"
|
||||||
require "#{APP_ROOT}/MPW/Sync/MPW"
|
require "#{APP_ROOT}/MPW/Sync/MPW"
|
||||||
|
require "#{APP_ROOT}/MPW/Sync/SSH"
|
||||||
|
|
||||||
class Cli
|
class Cli
|
||||||
|
|
||||||
|
@ -50,7 +51,9 @@ class Cli
|
||||||
if @sync.enable
|
if @sync.enable
|
||||||
if !@mpw.sync(@sync.get(@passwd), @config.last_update)
|
if !@mpw.sync(@sync.get(@passwd), @config.last_update)
|
||||||
puts "#{I18n.t('display.error')}: #{@sync.error_msg}"
|
puts "#{I18n.t('display.error')}: #{@sync.error_msg}"
|
||||||
elsif !@sync.update(File.open(@config.file_gpg).read)
|
end
|
||||||
|
|
||||||
|
if !@sync.update(File.open(@config.file_gpg).read)
|
||||||
puts "#{I18n.t('display.error')}: #{@sync.error_msg}"
|
puts "#{I18n.t('display.error')}: #{@sync.error_msg}"
|
||||||
elsif !@config.set_last_update
|
elsif !@config.set_last_update
|
||||||
puts "#{I18n.t('display.error')}: #{@config.error_msg}"
|
puts "#{I18n.t('display.error')}: #{@config.error_msg}"
|
||||||
|
@ -60,6 +63,9 @@ class Cli
|
||||||
end
|
end
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
puts "#{I18n.t('display.error')}: #{e}"
|
puts "#{I18n.t('display.error')}: #{e}"
|
||||||
|
puts @sync.error_msg
|
||||||
|
puts @config.error_msg
|
||||||
|
puts @mpw.error_msg
|
||||||
else
|
else
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
@ -73,19 +79,23 @@ class Cli
|
||||||
key = ask(I18n.t('form.setup.gpg_key')).to_s
|
key = ask(I18n.t('form.setup.gpg_key')).to_s
|
||||||
file_gpg = ask(I18n.t('form.setup.gpg_file', :home => Dir.home())).to_s
|
file_gpg = ask(I18n.t('form.setup.gpg_file', :home => Dir.home())).to_s
|
||||||
timeout_pwd = ask(I18n.t('form.setup.timeout')).to_s
|
timeout_pwd = ask(I18n.t('form.setup.timeout')).to_s
|
||||||
|
sync_type = ask(I18n.t('form.setup.sync_type')).to_s
|
||||||
sync_host = ask(I18n.t('form.setup.sync_host')).to_s
|
sync_host = ask(I18n.t('form.setup.sync_host')).to_s
|
||||||
sync_port = ask(I18n.t('form.setup.sync_port')).to_s
|
sync_port = ask(I18n.t('form.setup.sync_port')).to_s
|
||||||
|
sync_user = ask(I18n.t('form.setup.sync_user')).to_s
|
||||||
sync_pwd = ask(I18n.t('form.setup.sync_pwd')).to_s
|
sync_pwd = ask(I18n.t('form.setup.sync_pwd')).to_s
|
||||||
sync_path = ask(I18n.t('form.setup.sync_path')).to_s
|
sync_path = ask(I18n.t('form.setup.sync_path')).to_s
|
||||||
|
|
||||||
I18n.locale = language.to_sym
|
I18n.locale = language.to_sym
|
||||||
|
|
||||||
|
sync_type = sync_type.empty? ? nil : sync_type
|
||||||
sync_host = sync_host.empty? ? nil : sync_host
|
sync_host = sync_host.empty? ? nil : sync_host
|
||||||
sync_port = sync_port.empty? ? nil : sync_port.to_i
|
sync_port = sync_port.empty? ? nil : sync_port.to_i
|
||||||
|
sync_user = sync_user.empty? ? nil : sync_user
|
||||||
sync_pwd = sync_pwd.empty? ? nil : sync_pwd
|
sync_pwd = sync_pwd.empty? ? nil : sync_pwd
|
||||||
sync_path = sync_path.empty? ? nil : sync_path
|
sync_path = sync_path.empty? ? nil : sync_path
|
||||||
|
|
||||||
if @config.setup(key, language, file_gpg, timeout_pwd, sync_host, sync_port, sync_pwd, sync_path)
|
if @config.setup(key, language, file_gpg, timeout_pwd, sync_type, sync_host, sync_port, sync_user, sync_pwd, sync_path)
|
||||||
puts I18n.t('form.setup.valid')
|
puts I18n.t('form.setup.valid')
|
||||||
else
|
else
|
||||||
puts "#{I18n.t('display.error')}: #{@config.error_msg}"
|
puts "#{I18n.t('display.error')}: #{@config.error_msg}"
|
||||||
|
|
Loading…
Reference in a new issue