Module: SNAP

Included in:
PRM::Repo
Defined in:
lib/prm/repo.rb

Instance Method Summary collapse

Instance Method Details

#snapshot_to(path, component, release, snapname, type, recent) ⇒ Object



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
# File 'lib/prm/repo.rb', line 330

def snapshot_to(path,component,release,snapname,type,recent)
  if type != "deb"
    puts "Only deb supported"
    return
  end

  release.each do |r| 
    time = Time.new
    now = time.strftime("%Y-%m-%d-%H-%M")
    new_snap = "#{snapname}-#{now}"


    component.each do |c|
        if !File.exists?("#{path}/dists/#{r}/#{c}")
            puts "Component doesn't exist! To snapshot you need to have an existing component\n"
            return
        end
    end

    if File.exists?("#{path}/dists/#{r}/#{snapname}") && !File.symlink?("#{path}/dists/#{r}/#{snapname}") 
      puts "Snapshot target is a filesystem, remove it or rename your snap target"
      return
    end

    unless File.exists?("#{path}/dists/#{r}/#{new_snap}/")
      Dir.mkdir("#{path}/dists/#{r}/#{new_snap}")
    end

    if File.exists?("#{path}/dists/#{r}/#{snapname}")
        FileUtils.rm("#{path}/dists/#{r}/#{snapname}")
    end

    if recent
      component.each do |c|
          arch.each do |a|
              source_dir = "#{path}/dists/#{r}/#{c}/binary-#{a}"
              target_dir = "#{path}/dists/#{r}/#{new_snap}/binary-#{a}"
              pfiles = Dir.glob("#{source_dir}/*").sort_by { |f| File.mtime(f) }

              package_hash = Hash.new
              pfiles.each do |p|
                  file = p.split(/[_]/)
                  mtime = File.mtime(p)
                  date_in_mil = mtime.to_f
                  
                  if !package_hash.has_key?(file[0])
                      package_hash[file[0]] = { "name" => p, "time" => date_in_mil }
                  else
                      if date_in_mil > package_hash[file[0]]["time"]
                          package_hash[file[0]] = { "name" => p, "time" => date_in_mil }
                      end
                  end
              end

              if !File.exists?(target_dir)
                  FileUtils.mkdir_p(target_dir)
              end

              package_hash.each do |key,value|
                  value["name"].each do |k|
                      target_file = k.split("/").last
                      FileUtils.cp(k, "#{target_dir}/#{target_file}")
                  end
              end

          end

          FileUtils.ln_s "#{new_snap}", "#{path}/dists/#{r}/#{snapname}", :force => true
          puts "Created #{snapname} snapshot of #{component} with --recent flag\n"
      end
    else
      FileUtils.cp_r(Dir["#{path}/dists/#{r}/#{component}/*"], "#{path}/dists/#{r}/#{new_snap}")
      if File.exists?("#{path}/dists/#{r}/#{snapname}")
          FileUtils.rm("#{path}/dists/#{r}/#{snapname}")
      end
      FileUtils.ln_s "#{new_snap}", "#{path}/dists/#{r}/#{snapname}", :force => true
      puts "Created #{snapname} snapshot of #{component}\n"
    end
  end
end