Module: Splash::Transfers

Includes:
Config, Helpers, Loggers
Included in:
CLISplash::Transfers
Defined in:
lib/splash/transfers.rb

Overview

Transfers module

Defined Under Namespace

Classes: TxNotifier, TxRecords

Constant Summary

Constants included from Loggers

Loggers::ALIAS, Loggers::LEVELS

Constants included from Constants

Constants::AUTHOR, Constants::BACKENDS_STRUCT, Constants::CONFIG_FILE, Constants::COPYRIGHT, Constants::DAEMON_LOGMON_SCHEDULING, Constants::DAEMON_METRICS_SCHEDULING, Constants::DAEMON_PID_FILE, Constants::DAEMON_PROCESS_NAME, Constants::DAEMON_PROCMON_SCHEDULING, Constants::DAEMON_STDERR_TRACE, Constants::DAEMON_STDOUT_TRACE, Constants::DEFAULT_RETENTION, Constants::EMAIL, Constants::EXECUTION_TEMPLATE, Constants::EXECUTION_TEMPLATE_TOKENS_LIST, Constants::LICENSE, Constants::LOGGERS_STRUCT, Constants::PID_PATH, Constants::PROMETHEUS_ALERTMANAGER_URL, Constants::PROMETHEUS_PUSHGATEWAY_URL, Constants::PROMETHEUS_URL, Constants::TRACE_PATH, Constants::TRANSPORTS_STRUCT, Constants::VERSION, Constants::WEBADMIN_IP, Constants::WEBADMIN_PID_FILE, Constants::WEBADMIN_PID_PATH, Constants::WEBADMIN_PORT, Constants::WEBADMIN_PROCESS_NAME, Constants::WEBADMIN_PROXY, Constants::WEBADMIN_STDERR_TRACE, Constants::WEBADMIN_STDOUT_TRACE

Instance Method Summary collapse

Methods included from Helpers

#check_unicode_term, #daemonize, #format_by_extensions, #format_response, #get_processes, #group_root, #install_file, #is_root?, #make_folder, #make_link, #run_as_root, #search_file_in_gem, #user_root, #verify_file, #verify_folder, #verify_link, #verify_service

Methods included from Loggers

#change_logger, #get_logger, #get_session

Methods included from Config

#get_config, #rehash_config

Methods included from ConfigUtilities

#addservice, #checkconfig, #flush_backend, #setupsplash

Instance Method Details

#prepare_tx(name) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/splash/transfers.rb', line 136

def prepare_tx(name)
  log = get_logger
  record = get_config.transfers.select { |item| item[:name] == name.to_sym }.first
  home  = Etc.getpwnam(record[:local][:user]).dir
  identity = ::File::readlines("#{home}/.ssh/id_rsa.pub").first.chomp
  folder  = {:mode => "755",
             :owner => record[:local][:user] ,
             :group => Etc.getgrgid(Etc.getpwnam(record[:local][:user]).gid).name,
             :name => record[:local][:path],
             :path => record[:local][:path]}
  log.info "Ensure local folder : #{record[:local][:path]}"
  make_folder(folder) unless verify_folder(folder).empty?
  begin
    log.info "Ensure RSA Key sharing for local user : #{record[:local][:user]} to remote user : #{record[:remote][:user]}@#{record[:remote][:host]}"
    ssh = Net::SSH.start(record[:remote][:host],record[:remote][:user])
    output = ssh.exec!(%[
      /bin/bash -cl '
      umask 077;
      mkdir #{record[:remote][:path]};
      test -d ~/.ssh || mkdir ~/.ssh;
      if [ ! -f ~/.ssh/authorized_keys -o `grep "#{identity}" ~/.ssh/authorized_keys 2> /dev/null | wc -l` -eq 0 ]; then              echo "#{identity}" >> ~/.ssh/authorized_keys
      fi'])
    log.info "Prepare remote folder : #{record[:remote][:path]}"
    log.info "Prepare data file for transfer : #{record[:name]}"
    txrec = TxRecords::new record[:name]
    txrec.add_record :status => :prepared
    log.ok "Transfer : #{record[:name]} prepared successfully"
    return {:case => :quiet_exit }
  rescue Interrupt
    splash_exit case: :interrupt, more: "Remote command exection"
  rescue TTY::Reader::InputInterrupt
    splash_exit case: :interrupt, more: "Remote command exection"
  end
end

#push(record) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/splash/transfers.rb', line 179

def push(record)
  config = get_config
  log = get_logger
  txrec = TxRecords::new record[:name]
  start = Time.now
  res = true
  count = 0
  done =[]
  start_date = DateTime.now.to_s
  list = Dir.glob("#{record[:local][:path]}/#{record[:pattern]}")
  count = list.count
  log.arrow "Transfering #{count} file(s)"

  begin
    scp = Net::SCP.start(record[:remote][:host],record[:remote][:user])
    list.each do|f|
      log.arrow "Copy file : #{f} to #{record[:remote][:user]}@#{record[:remote][:host]}:#{record[:remote][:path]}"
      scp.upload! f, record[:remote][:path]
      done.push f
      if record[:backup] then
        log.arrow "File #{f} backuped"
        FileUtils::mv(f, "#{f}.#{Time.now.getutc.to_i}")
      else
        FileUtils::unlink(f)
      end
    end

  rescue
    res = false
  end

  end_date = DateTime.now.to_s
  time = Time.now - start
  status = (res)? :success : :failure
  txrec.add_record :status => status,
                   :end_date => end_date,
                   :time => time,
                   :count => count,
                   :wanted => list,
                   :done => done
  count_failed = list.count - done.count
  txmonitor = TxNotifier::new({name: record[:name], nbfiles: count,nbfiles_failed: count_failed, time: time})
  if txmonitor.notify then
    log.ok "Sending metrics to Prometheus Pushgateway"
  else
    log.ko "Failed to send metrics to Prometheus Pushgateway"
  end
  return res
end

#run_txs(options = {}) ⇒ Object



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
# File 'lib/splash/transfers.rb', line 98

def run_txs(options = {})
  log = get_logger
  log.info 'Running Transfers'
  count=0
  get_config.transfers.each do |record|
    txrec =  TxRecords::new record[:name]
    txrec.purge(record[:retention])
    log.item "Execute : #{record[:name]},  #{record[:desc]}"
    case txrec.check_prepared
    when :prepared
      if record[:type] == :push then
        unless push record
          count += 1
        end
      elsif record[:type] == :pull then
        unless pull record
          count += 1
        end
      else
        log.ko "Transfer type unkown"
        count += 1
      end
    when :never_prepare
      log.ko "#{record[:name]} : Never prepared, ignored"
      txrec.add_record :status => :never_prepared
      count += 1
    when :never_run_prepare
      log.ko "#{record[:name]} : Never Executed and never prepared, ignored"
      txrec.add_record :status => :never_prepared
      count += 1
    end
  end
  return {:case => :error_exit, :more => "#{count} Transfer(s) failed"} if count > 0
  return {:case => :quiet_exit }
end

#save_dataObject



173
174
175
# File 'lib/splash/transfers.rb', line 173

def save_data

end