Project

General

Profile

0001-Small-refactoring-of-the-media-handling-for-operatin.patch

Jochen Schalanda, 05/03/2010 02:02 PM

View differences:

app/controllers/unattended_controller.rb
26 26
  end
27 27

  
28 28
  def preseed
29
    @preseed_path   = @host.os.preseed_path   @host.media
30
    @preseed_server = @host.os.preseed_server @host.media
29
    @preseed_path   = @host.os.preseed_path   @host
30
    @preseed_server = @host.os.preseed_server @host
31 31
    unattended_local "preseed"
32 32
  end
33 33

  
app/models/media.rb
5 5
  validates_uniqueness_of :path, :scope => :operatingsystem_id
6 6
  validates_presence_of :name, :path
7 7
  validates_format_of :name, :with => /\A(\S+\s?)+\Z/, :message => "can't be blank or contain trailing white spaces."
8
  validates_format_of :path, :with => /^((http|ftp):\/\/)|\w+:\/\w+/,
9
    :message => "Url (http:// or ftp://) or a NFS share area allowed (e.g. server:/vol/dir)"
8
  validates_format_of :path, :with => /^(http|https|ftp|nfs):\/\//,
9
    :message => "Only URLs with schema http://, https://, ftp:// or nfs:// are allowed (e.g. nfs://server/vol/dir)"
10 10

  
11 11
  alias_attribute :os, :operatingsystem
12 12
  before_destroy Ensure_not_used_by.new(:hosts)
app/views/medias/_form.html.erb
10 10
  </p >
11 11
  <small>
12 12
    The path to the media, can be a URL or a valid NFS server (exclusive of the architecture).<br />
13
    for example http://mirror.averse.net/centos/6.0/os/$arch where <b>$arch</b> will be substituted for the host actual OS architecture.
13
    for example http://mirror.averse.net/centos/$version/os/$arch where <strong>$arch</strong> will be substituted for the host's actual OS architecture<br />
14
    and <strong>$version</strong>, <strong>$major</strong> and <strong>$minor</strong> will be substituted for the version of the operating system.
14 15
  </small>
15 16
  <p>
16 17
  <%= f.label :operatingsystem_id %><br />
lib/family.rb
1 1
# Adds operatingsystem family behaviour to the Operatingsystem class
2 2
# The variant is calculated at run-time
3 3
require 'ostruct'
4
require 'uri'
5

  
4 6
module Family
5 7
  # NEVER, EVER reorder this list. Additions are allowed but offsets are encoded in the database
6 8
  FAMILIES  = [:Debian, :RedHat, :Solaris]
......
12 14
    FAMILIES.map{|e| OpenStruct.new(:name => e, :value => FAMILIES.index(e)) }
13 15
  end
14 16

  
17
  def media_uri host
18
    URI.parse(host.media.path.gsub('$arch',host.architecture.name).
19
                              gsub('$major', host.os.major).
20
                              gsub('$minor', host.os.minor).
21
                              gsub('$version', [ host.os.major, host.os.minor ].compact.join('.'))
22
    ).normalize
23
  end
24

  
15 25
  module Debian
16 26
    include Family
17 27

  
18
    def preseed_server media
19
      media.path.match('^(\w+):\/\/((\w|\.)+)((\w|\/)+)$')[2]
28
    def preseed_server host
29
      media_uri(host).select(:host, :port).compact.join(':')
20 30
    end
21 31

  
22
    #TODO: rethink of a more generic way
23
    def preseed_path media
24
      media.path.match('^(\w+):\/\/((\w|\.)+)((\w|\/)+)$')[4]
32
    def preseed_path host
33
      media_uri(host).select(:path, :query).compact.join('?')
25 34
    end
26 35
  end
27 36

  
......
30 39
    # outputs kickstart installation media based on the media type (NFS or URL)
31 40
    # it also convert the $arch string to the current host architecture
32 41
    def mediapath host
33
      server, dir  = host.media.path.split(":")
34
      dir.gsub!('$arch',host.architecture.name)
42
      uri = media_uri(host)
43
      server = uri.select(:host, :port).compact.join(':')
44
      dir = uri.select(:path, :query).compact.join('?')
35 45

  
36
      return server =~ /^(h|f)t*p$/ ? "url --url #{server+":"+dir}" : "nfs --server #{server} --dir #{dir}"
46
      case uri.scheme
47
        when 'http', 'https', 'ftp'
48
           "url --url #{uri.to_s}"
49
        else
50
          "nfs --server #{server} --dir #{dir}"
51
      end
37 52
    end
53

  
38 54
    def epel arch
39 55
      ["4","5"].include?(major) ? "su -c 'rpm -Uvh http://download.fedora.redhat.com/pub/epel/#{major}/#{arch}/epel-release-#{to_version}.noarch.rpm'" : ""
40 56
    end
41
-