From c9a97b57a5a94de9b21ce5a22f4c4967263d1811 Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Mon, 3 May 2010 08:46:02 +0000 Subject: [PATCH] Small refactoring of the media handling for operating system families: * Use URI class for any URI related operations * Added substitution for $version, $major and $minor in media path * Force use of http(s)|ftp|nfs URI schema: NFS shares have now to be entered in URI form: nfs://server/path/.../ instead of server:/path/.../ --- app/controllers/unattended_controller.rb | 4 +- app/models/media.rb | 4 +- app/views/medias/_form.html.erb | 3 +- lib/family.rb | 32 ++++++++++++++++++++++------- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/app/controllers/unattended_controller.rb b/app/controllers/unattended_controller.rb index 1dfe073..106bfec 100644 --- a/app/controllers/unattended_controller.rb +++ b/app/controllers/unattended_controller.rb @@ -26,8 +26,8 @@ class UnattendedController < ApplicationController end def preseed - @preseed_path = @host.os.preseed_path @host.media - @preseed_server = @host.os.preseed_server @host.media + @preseed_path = @host.os.preseed_path @host + @preseed_server = @host.os.preseed_server @host unattended_local "preseed" end diff --git a/app/models/media.rb b/app/models/media.rb index 82c5db0..6026e3a 100644 --- a/app/models/media.rb +++ b/app/models/media.rb @@ -5,8 +5,8 @@ class Media < ActiveRecord::Base validates_uniqueness_of :path, :scope => :operatingsystem_id validates_presence_of :name, :path validates_format_of :name, :with => /\A(\S+\s?)+\Z/, :message => "can't be blank or contain trailing white spaces." - validates_format_of :path, :with => /^((http|ftp):\/\/)|\w+:\/\w+/, - :message => "Url (http:// or ftp://) or a NFS share area allowed (e.g. server:/vol/dir)" + validates_format_of :path, :with => /^(http|https|ftp|nfs):\/\//, + :message => "Only URLs with schema http://, https://, ftp:// or nfs:// are allowed (e.g. nfs://server/vol/dir)" alias_attribute :os, :operatingsystem before_destroy Ensure_not_used_by.new(:hosts) diff --git a/app/views/medias/_form.html.erb b/app/views/medias/_form.html.erb index 70f96d8..ed730dd 100644 --- a/app/views/medias/_form.html.erb +++ b/app/views/medias/_form.html.erb @@ -10,7 +10,8 @@

The path to the media, can be a URL or a valid NFS server (exclusive of the architecture).
- for example http://mirror.averse.net/centos/6.0/os/$arch where $arch will be substituted for the host actual OS architecture. + for example http://mirror.averse.net/centos/$version/os/$arch where $arch will be substituted for the host's actual OS architecture
+ and $version, $major and $minor will be substituted for the version of the operating system.

<%= f.label :operatingsystem_id %>
diff --git a/lib/family.rb b/lib/family.rb index f5f25cb..d10a8b2 100644 --- a/lib/family.rb +++ b/lib/family.rb @@ -1,6 +1,8 @@ # Adds operatingsystem family behaviour to the Operatingsystem class # The variant is calculated at run-time require 'ostruct' +require 'uri' + module Family # NEVER, EVER reorder this list. Additions are allowed but offsets are encoded in the database FAMILIES = [:Debian, :RedHat, :Solaris] @@ -12,16 +14,23 @@ module Family FAMILIES.map{|e| OpenStruct.new(:name => e, :value => FAMILIES.index(e)) } end + def media_uri host + URI.parse(host.media.path.gsub('$arch',host.architecture.name). + gsub('$major', host.os.major). + gsub('$minor', host.os.minor). + gsub('$version', [ host.os.major, host.os.minor ].compact.join('.')) + ).normalize + end + module Debian include Family - def preseed_server media - media.path.match('^(\w+):\/\/((\w|\.)+)((\w|\/)+)$')[2] + def preseed_server host + media_uri(host).select(:host, :port).compact.join(':') end - #TODO: rethink of a more generic way - def preseed_path media - media.path.match('^(\w+):\/\/((\w|\.)+)((\w|\/)+)$')[4] + def preseed_path host + media_uri(host).select(:path, :query).compact.join('?') end end @@ -30,11 +39,18 @@ module Family # outputs kickstart installation media based on the media type (NFS or URL) # it also convert the $arch string to the current host architecture def mediapath host - server, dir = host.media.path.split(":") - dir.gsub!('$arch',host.architecture.name) + uri = media_uri(host) + server = uri.select(:host, :port).compact.join(':') + dir = uri.select(:path, :query).compact.join('?') - return server =~ /^(h|f)t*p$/ ? "url --url #{server+":"+dir}" : "nfs --server #{server} --dir #{dir}" + case uri.scheme + when 'http', 'https', 'ftp' + "url --url #{uri.to_s}" + else + "nfs --server #{server} --dir #{dir}" + end end + def epel arch ["4","5"].include?(major) ? "su -c 'rpm -Uvh http://download.fedora.redhat.com/pub/epel/#{major}/#{arch}/epel-release-#{to_version}.noarch.rpm'" : "" end -- 1.6.3.3