Class: MPW::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/mpw/sync.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, local, password = nil) ⇒ Sync

Constructor raise an exception if there is a bad parameter



20
21
22
23
24
25
26
27
# File 'lib/mpw/sync.rb', line 20

def initialize(config, local, password=nil)
  @error_msg = nil
  @config    = config
  @local     = local
  @password  = password

  raise I18n.t('error.class') if not @local.instance_of?(MPW)
end

Instance Attribute Details

#error_msgObject

Returns the value of attribute error_msg.



16
17
18
# File 'lib/mpw/sync.rb', line 16

def error_msg
  @error_msg
end

Instance Method Details

#get_remoteObject

Get the data on remote host @rtrn: true if get the date, else false



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
61
62
63
64
65
# File 'lib/mpw/sync.rb', line 31

def get_remote
  case @config.sync_type
  when 'mpw'
    require 'mpw/sync/mpw'
    @sync = SyncMPW.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
  when 'sftp', 'scp', 'ssh'
    require 'mpw/sync/ssh'
    @sync = SyncSSH.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
  when 'ftp'
    require 'mpw/sync/ftp'
    @sync = SyncFTP.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
  else
    @error_msg =  I18n.t('error.unknown_type')
    return false
  end

  if not @sync.connect
    @error_msg = @sync.error_msg
    return false
  end

  
  file_tmp = Tempfile.new('mpw-')
  raise @sync.error_msg if not @sync.get(file_tmp.path)  

  @remote = MPW.new(file_tmp.path, @config.key)
  raise @remote.error_msg if not @remote.decrypt(@password)

  file_tmp.close(true)
  return true
rescue Exception => e
  @error_msg = "#{I18n.t('error.sync.download')} #{e}"
  file_tmp.close(true)
  return false
end

#syncObject

Sync remote data and local data raise an exception if there is a problem



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mpw/sync.rb', line 69

def sync
  
  if not @remote.to_s.empty?
    @local.list.each do |item|
      update = false
      @remote.list.each do |r|

        # Update item
        if item.id == r.id
          if item.last_edit < r.last_edit
            raise item.error_msg if not item.update(name:      r.name,
                                                    group:     r.group,
                                                    host:      r.host,
                                                    protocol:  r.protocol,
                                                    user:      r.user,
                                                    password:  r.password,
                                                    port:      r.port,
                                                    comment:   r.comment
                                                   )
          end

          r.delete
          update = true

          break
        end
      end

      # Remove an old item
      if not update and item.last_sync.to_i < @config.last_sync and item.last_edit < @config.last_sync
        item.delete
      end
    end
  end
  
  # Add item
  @remote.list.each do |r|
    if r.last_edit > @config.last_sync
      item = Item.new(id:        r.id,
                      name:      r.name,
                      group:     r.group,
                      host:      r.host,
                      protocol:  r.protocol,
                      user:      r.user,
                      password:  r.password,
                      port:      r.port,
                      comment:   r.comment,
                      created:   r.created,
                      last_edit: r.last_edit
                     )
      raise @local.error_msg if not @local.add(item)
    end
  end

  @local.list.each do |item|
    item.set_last_sync
  end

  raise @mpw.error_msg  if not @local.encrypt 
  raise @sync.error_msg if not @sync.update(@config.file_gpg)
  
  @config.set_last_sync

  return true
rescue Exception => e
  @error_msg = "#{I18n.t('error.sync.unknown')} #{e}"
  return false
end