Class: Reparto

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

Overview

Clase Reparto Lee archivo .ini parsea, y crea hilos de ejecucion segun instrucciones indicadas

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Reparto

::filename

archivo .ini



205
206
207
208
209
210
211
212
213
# File 'lib/reparto.rb', line 205

def initialize(filename)
  @fn = filename
  @ini = IniFile.new(@fn)
  
  @ssh_clients = {}
  
  @types_supported = ['ssh']
  parse
end

Instance Method Details

#parseObject

Parse el archivo indicado y crea hilos de ejecucion



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/reparto.rb', line 216

def parse

  #Por cada seccion o equipo, es un hilo
  @ini.each_section do |section|
    
    #@todo validate ip
    ip = section
    
    #obtiene tipo
    if @ini[section].has_key? $t.ini['type']
      type = @ini[section][$t.ini['type']]
      unless @types_supported.member? type
        raise RuntimeError, $t.reparto.type_not_supported(type)
      end
    else
      type = 'ssh'
    end
    
    #valida campos requeridos
    raise RuntimeError, $t.reparto.require_username unless @ini[section].has_key? $t.ini.username
    raise RuntimeError, $t.reparto.require_password unless @ini[section].has_key? $t.ini.password
    
    username = @ini[section][$t.ini.username]
    password = @ini[section][$t.ini.password]
    
    #se obtiene puerto o por defecto 22
    if @ini[section].has_key? $t.ini.port
      port = @ini[section][$t.ini.port]
      unless port.is_number?
        raise RuntimeError, $t.reparto.port_numeric
      end
      port = port.to_i
    else
      case type
      when 'ssh'
        port = 22
      end
    end

    #comandos a ejecutar
    #en orden , segun paramentro terminado en [0-9]+
    cmds = []
    cmd_index = nil

    #se lee parametros y se asignan segun nombre
    @ini[section].keys.each do |param|
      cpdirs = nil
      cpfiles = nil
      updatedirs = nil

      cmd = param.match(/cmd_([0-9]+)/)
      if param =~ /cmd/
        cmd_index = cmd[1].to_i
        cmds[cmd_index] = ["cmd",  @ini[section][param]]
      end

      #cp directorios
      cpdir = param.match(/cpdir_local_([0-9]+)/)
      if cpdir
        cpdir_num = cpdir[1].to_i
        cpdir_index = "cpdir_%d" % cpdir_num
        cmd_index = cpdir_num
        cpdirs = cmds.assoc(cpdir_index)

        if cpdirs.nil?
          cpdirs = [cpdir_index, {}] 
          cpdirs[1][:local] = @ini[section][param]            
          cmds[cmd_index] = cpdirs
        else
          cmds[cmds.index{|x| x[0] == cpdir_index unless x.nil?}][1][:local] = @ini[section][param]
        end
      end

      cpdir = param.match(/cpdir_remote_([0-9]+)/)
      if cpdir
        cpdir_num = cpdir[1].to_i
        cpdir_index = "cpdir_%d" % cpdir_num
        cmd_index = cpdir_num
        cpdirs = cmds.assoc(cpdir_index) 
        if cpdirs.nil?
          cpdirs = [cpdir_index, {}]
          cpdirs[1][:remote] = @ini[section][param]
          cmds[cmd_index] = cpdirs
        else
          cmds[cmds.index{|x| x[0] == cpdir_index unless x.nil?}][1][:remote] = @ini[section][param]
        end
      end
      

      #cp archivos
      cpfile = param.match(/cp_local_([0-9]+)/)
      if cpfile
        cp_num = cpfile[1].to_i
        cp_index = "cp_%d" % cp_num
        cmd_index = cp_num
        cpfiles = cmds.assoc(cp_index)
        if cpfiles.nil?
          cpfiles = [cp_index, {}]
          cpfiles[1][:local] = @ini[section][param]
          cmds[cmd_index] = cpfiles
        else
          cmds[cmds.index{|x| x[0] == cp_index unless x.nil?}][1][:remote] = @ini[section][param]
        end
      end
      
      cpfile = param.match(/cp_remote_([0-9]+)/)
      if cpfile
        cp_num = cpfile[1].to_i
        cp_index = "cp_%d" % cp_num
        cmd_index = cp_num
        cpfiles = cmds.assoc(cp_index)
        if cpfiles.nil?
          cpfiles = [cp_index, {}]
          cpfiles[1][:remote] = @ini[section][param]
          cmds[cmd_index] = cpfiles
        else
          cmds[cmds.index{|x| x[0] == cp_index unless x.nil?}][1][:remote] = @ini[section][param]
        end
      end


      #actualiza archivos/directorios en remoto
      updatedir = param.match(/updatedir_([a-z]+_)?([0-9]+)/)

      if param =~ /^updatedir_([0-9]+)$/
        i_num = updatedir[2].to_i
        cmd_index = i_num
        up_index = "updatedir_%d" % i_num
        updatedirs = cmds.assoc(up_index)
        if updatedirs.nil?
          updatedirs = [up_index, {}]
          updatedirs[1][:remote] = @ini[section][param]
          updatedirs[1][:local] = @ini[section][param]
          cmds[cmd_index] = updatedirs
        else
          cmds[cmds.index{|x| x[0] == up_index unless x.nil?}][1][:local] = @ini[section][param]
          cmds[cmds.index{|x| x[0] == up_index unless x.nil?}][1][:remote] = @ini[section][param]
        end
      end

      if param =~ /^updatedir_local/
        i_num = updatedir[2].to_i
        cmd_index = i_num
        up_index = "updatedir_%d" % i_num
        updatedirs = cmds.assoc(up_index)
        if updatedirs.nil?
          updatedirs = [up_index, {}] 
          updatedirs[1][:local] = @ini[section][param]
          cmds[cmd_index] = updatedirs
        else
          cmds[cmds.index{|x| x[0] == up_index unless x.nil?}][1][:local] = @ini[section][param]
        end
      end

      if param =~ /^updatedir_remote/
        i_num = updatedir[2].to_i
        cmd_index = i_num
        up_index = "updatedir_%d" % i_num
        updatedirs = cmds.assoc(up_index)
        if updatedirs.nil?
          updatedirs = [up_index, {}] 
          updatedirs[1][:remote] = @ini[section][param]
          cmds[cmd_index] = updatedirs
        else
          cmds[cmds.index{|x| x[0] == up_index unless x.nil?}][1][:remote] = @ini[section][param]
        end
      end


    end

    case type
    when 'ssh'
      cssh = SSHClient.new(ip, port, username, password)
      cmds.each do |cmd|
        next if cmd.nil?
        type = cmd[0]
        args = cmd[1]

        if type =~ /cmd/
          cssh.cmd(args)
        elsif type =~ /cpdir/
          cssh.cp_dir(args[:local], args[:remote])
        elsif type =~ /cp/
          cssh.cp(args[:local], args[:remote])
        elsif type =~ /updatedir/
          cssh.update_dir(args[:local], args[:remote])
        else
          $logs.error("Unknown action %s\n" % type)
        end
      end

      #Hilo por cliente
      $threads << Thread.new(cssh) do |tssh|
        begin
          cssh.do
        rescue Errno::ENETUNREACH
          $logs.error("Can't connect to %s" % ip)
        rescue Errno::EHOSTUNREACH
          $logs.error("Can't route to host %s" % ip)
        end
      end
      
    end
    
  end

end