Class: Simp::Rake::Build::Unpack

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Constants
Defined in:
lib/simp/rake/build/unpack.rb

Instance Method Summary collapse

Methods included from Constants

#init_member_vars, #os_build_metadata

Constructor Details

#initialize(base_dir) ⇒ Unpack

Returns a new instance of Unpack.



11
12
13
14
15
16
# File 'lib/simp/rake/build/unpack.rb', line 11

def initialize( base_dir )
  init_member_vars( base_dir )

  @mock = ENV['mock'] || '/usr/bin/mock'
  define_tasks
end

Instance Method Details

#define_tasksObject



18
19
20
21
22
23
24
25
26
27
28
29
30
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
66
67
68
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
# File 'lib/simp/rake/build/unpack.rb', line 18

def define_tasks
  #!/usr/bin/rake -T

  desc "Unpack an ISO. Unpacks either a RHEL or CentOS ISO into
  <targetdir>/<RHEL|CentOS><version>-<arch>.
   * :iso_path - Full path to the ISO image to unpack.
   * :merge - If true, then automatically merge any existing
     directories. Defaults to prompting.
   * :targetdir - The parent directory for the to-be-created directory
     containing the unpacked ISO. Defaults to the current directory.
   * :isoinfo - The isoinfo executable to use to extract stuff from the ISO.
     Defaults to 'isoinfo'.
   * :version - optional override for the <version> number (e.g., '7.0' instead of '7')

  "
  task :unpack,[:iso_path, :merge, :targetdir, :isoinfo, :version] do |t,args|
    args.with_defaults(
      :iso_path   => '',
      :isoinfo    => 'isoinfo',
      :targetdir  => Dir.pwd,
      :merge      => false,
      :version => false,
    )

    iso_path   = args.iso_path
    iso_info   = which(args.isoinfo)
    targetdir  = args.targetdir
    merge      = args.merge
    version = args.version

    # Checking for valid arguments
    File.exist?(args.iso_path) or
      fail "Error: You must provide the full path and filename of the ISO image."

    %x{file #{iso_path}}.split(":")[1..-1].to_s =~ /ISO/ or
      fail "Error: The file provided is not a valid ISO."

    pieces = File.basename(iso_path,'.iso').split('-')

    # Mappings of ISO name to target directory name.
    # This is a hash of hashes to provide room for growth.
    dvd_map = {
      # RHEL structure as provided from RHN:
      #   rhel-server-<version>-<arch>-<whatever>
      'rhel' => {
        'baseos'  => 'RHEL',
        'version' => version || pieces[2],
        'arch'    => pieces[3]
      },
      # CentOS structure as provided from the CentOS website:
      #   CentOS-<version>-<arch>-<whatever>
      'CentOS' => {
        'baseos'  => 'CentOS',
        'version' => version || pieces[1],
        'arch'    => pieces[2]
      }
    }

    # Determine the target directory
    map = dvd_map[pieces[0]]
    map.nil? and fail "Error: Could not find a mapping for '#{iso_path}'."
    out_dir = "#{File.expand_path(targetdir)}/#{map['baseos']}#{map['version']}-#{map['arch']}"

    # Attempt a merge
    if File.exist?(out_dir) and merge.to_s.strip == 'false'
      puts "Directory '#{out_dir}' already exists! Would you like to merge? [Yn]?"
      unless $stdin.gets.strip.match(/^(y.*|$)/i)
        puts "Skipping #{iso_path}"
        next
      end
    end

    puts "Target dir: #{out_dir}"
    mkdir_p(out_dir)

    # Unpack the ISO
    iso_toc = %x{#{iso_info} -Rf -i #{iso_path}}.split("\n")
    iso_toc.each do |iso_entry|
      iso_toc.delete(File.dirname(iso_entry))
    end

    progress = ProgressBar.create(:title => 'Unpacking', :total => iso_toc.size)

    iso_toc.each do |iso_entry|
      target = "#{out_dir}#{iso_entry}"
      unless File.exist?(target)
        FileUtils.mkdir_p(File.dirname(target))
        system("#{iso_info} -R -x #{iso_entry} -i #{iso_path} > #{target}")
      end
      if progress
        progress.increment
      else
        print "#"
      end
    end
  end
end