Project

General

Profile

Feature #263 » 0001-Fixes-263-correct-pluralization-of-medium-media.patch

Proposed patch - Jochen Schalanda, 05/23/2010 09:22 AM

View differences:

app/controllers/media_controller.rb
class MediaController < ApplicationController
def index
@media = Medium.all(:include => [:operatingsystem])
end
def new
@medium = Medium.new
end
def create
@medium = Medium.new(params[:medium])
if @medium.save
flash[:notice] = "Successfully created medium."
redirect_to media_url
else
render :action => 'new'
end
end
def edit
@medium = Medium.find(params[:id])
end
def update
@medium = Medium.find(params[:id])
if @medium.update_attributes(params[:medium])
flash[:notice] = "Successfully updated medium."
redirect_to media_url
else
render :action => 'edit'
end
end
def destroy
@medium = Medium.find(params[:id])
@medium.destroy
flash[:notice] = "Successfully destroyed medium."
redirect_to media_url
end
end
app/controllers/medias_controller.rb
class MediasController < ApplicationController
def index
@medias = Media.all(:include => [:operatingsystem])
end
def new
@media = Media.new
end
def create
@media = Media.new(params[:media])
if @media.save
flash[:notice] = "Successfully created media."
redirect_to medias_url
else
render :action => 'new'
end
end
def edit
@media = Media.find(params[:id])
end
def update
@media = Media.find(params[:id])
if @media.update_attributes(params[:media])
flash[:notice] = "Successfully updated media."
redirect_to medias_url
else
render :action => 'edit'
end
end
def destroy
@media = Media.find(params[:id])
@media.destroy
flash[:notice] = "Successfully destroyed media."
redirect_to medias_url
end
end
app/controllers/unattended_controller.rb
def kickstart
logger.info "#{controller_name}: Kickstart host #{@host.name}"
@dynamic = @host.diskLayout=~/^#Dynamic/
@arch = @host.architecture.name
os = @host.operatingsystem
@osver = os.major.to_i
@mediapath = os.mediapath @host
@epel = os.epel @host
@yumrepo = os.yumrepo @host
@arch = @host.architecture.name
os = @host.operatingsystem
@osver = os.major.to_i
@mediumpath = os.mediumpath @host
@epel = os.epel @host
@yumrepo = os.yumrepo @host
unattended_local "kickstart"
end
......
end
conditions = (mac and ip) ? ["mac = ? and ip = ?",mac, ip] : ["ip = ?",ip];
@host = Host.find(:first, :include => [:architecture, :media, :operatingsystem, :domain], :conditions => conditions)
@host = Host.find(:first, :include => [:architecture, :medium, :operatingsystem, :domain], :conditions => conditions)
if @host.nil?
logger.info "#{controller_name}: unable to find #{ip}#{"/"+mac unless mac.nil?}"
head(:not_found) and return
app/helpers/medias_helper.rb
module MediasHelper
end
app/helpers/medium_helper.rb
module MediumHelper
end
app/models/host.rb
class Host < Puppet::Rails::Host
belongs_to :architecture
belongs_to :media
belongs_to :medium
belongs_to :model
belongs_to :domain
belongs_to :operatingsystem
......
validates_format_of :sp_mac, :with => /([a-f0-9]{1,2}:){5}[a-f0-9]{1,2}/, :allow_nil => true, :allow_blank => true
validates_format_of :sp_ip, :with => /(\d{1,3}\.){3}\d{1,3}/, :allow_nil => true, :allow_blank => true
validates_format_of :serial, :with => /[01],\d{3,}n\d/, :message => "should follow this format: 0,9600n8", :allow_blank => true, :allow_nil => true
validates_associated :domain, :operatingsystem, :architecture, :subnet,:media#, :user, :deployment, :model
validates_associated :domain, :operatingsystem, :architecture, :subnet,:medium #, :user, :deployment, :model
end
before_validation :normalize_addresses, :normalize_hostname
app/models/media.rb
class Media < ActiveRecord::Base
belongs_to :operatingsystem
has_many :hosts
validates_uniqueness_of :name, :scope => :operatingsystem_id
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|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)
def to_s
name
end
end
app/models/medium.rb
class Medium < ActiveRecord::Base
belongs_to :operatingsystem
has_many :hosts
validates_uniqueness_of :name, :scope => :operatingsystem_id
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|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)
def to_s
name
end
end
app/models/operatingsystem.rb
class Operatingsystem < ActiveRecord::Base
has_many :hosts
has_many :medias
has_many :media
has_and_belongs_to_many :ptables
has_and_belongs_to_many :architectures
has_and_belongs_to_many :puppetclasses
app/views/home/settings.erb
<li><%= link_to 'Host Groups', hostgroups_path %> </li>
<li><%= link_to 'Domains', domains_path %> </li>
<li><%= link_to 'Operating Systems', operatingsystems_path %> </li>
<li><%= link_to 'Installation Medias',medias_path %> </li>
<li><%= link_to 'Installation Media', media_path %> </li>
<li><%= link_to 'Hardware Models', models_path %> </li>
<li><%= link_to 'Environments', environments_path %> </li>
<li><%= link_to 'Architectures', architectures_path %> </li>
app/views/hosts/_operatingsystem.html.erb
Media
<%= collection_select :host, :media_id, @operatingsystem.medias, :id, :to_label %>
<%= collection_select :host, :medium_id, @operatingsystem.media, :id, :to_label %>
Partition Table
<%= collection_select :host, :ptable_id, @operatingsystem.ptables, :id, :to_label %>
app/views/media/_form.html.erb
<% form_for @medium do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :path %><br />
<%= f.text_field :path %>
</p >
<small>
The path to the medium, can be a URL or a valid NFS server (exclusive of the architecture).<br />
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 />
and <strong>$version</strong>, <strong>$major</strong> and <strong>$minor</strong> will be substituted for the version of the operating system.
</small>
<p>
<%= f.label :operatingsystem_id %><br />
<%= f.collection_select :operatingsystem_id, Operatingsystem.all, :id, :to_label %>
</p>
<% unless @medium.new_record? -%>
<p>Currently used by <%= @medium.hosts.count %> hosts</p>
<% end -%>
<p><%= f.submit "Submit" %></p>
<% end %>
app/views/media/edit.html.erb
<% title "Edit Medium" %>
<%= render :partial => 'form' %>
<p>
<%= link_to "View All", media_path %>
</p>
app/views/media/index.html.erb
<% title "Installation Media" %>
<table class="list">
<tr>
<th>Name</th>
<th>Path</th>
<th>Operating system</th>
</tr>
<% for medium in @media %>
<tr>
<td><%= link_to h(medium), edit_medium_path(medium) %></td>
<td><%=h medium.path %></td>
<td><%=h medium.operatingsystem %></td>
<td><%= link_to "Destroy", medium, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<p><%= link_to "New Medium", new_medium_path %></p>
app/views/media/new.html.erb
<% title "New Medium" %>
<%= render :partial => 'form' %>
<p><%= link_to "Back to List", media_path %></p>
app/views/medias/_form.html.erb
<% form_for @media do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :path %><br />
<%= f.text_field :path %>
</p >
<small>
The path to the media, can be a URL or a valid NFS server (exclusive of the architecture).<br />
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 />
and <strong>$version</strong>, <strong>$major</strong> and <strong>$minor</strong> will be substituted for the version of the operating system.
</small>
<p>
<%= f.label :operatingsystem_id %><br />
<%= f.collection_select :operatingsystem_id, Operatingsystem.all, :id, :to_label %>
</p>
<% unless @media.new_record? -%>
<p>Currently used by <%= @media.hosts.count %> hosts</p>
<% end -%>
<p><%= f.submit "Submit" %></p>
<% end %>
app/views/medias/edit.html.erb
<% title "Edit Media" %>
<%= render :partial => 'form' %>
<p>
<%= link_to "View All", medias_path %>
</p>
app/views/medias/index.html.erb
<% title "Medias" %>
<table class="list">
<tr>
<th>Name</th>
<th>Path</th>
<th>Operatingsystem</th>
</tr>
<% for media in @medias %>
<tr>
<td><%= link_to h(media), edit_media_path(media) %></td>
<td><%=h media.path %></td>
<td><%=h media.operatingsystem %></td>
<td><%= link_to "Destroy", media, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<p><%= link_to "New Media", new_media_path %></p>
app/views/medias/new.html.erb
<% title "New Media" %>
<%= render :partial => 'form' %>
<p><%= link_to "Back to List", medias_path %></p>
app/views/operatingsystems/_form.html.erb
<% end -%>
</p>
<p>
<% field_set_tag("Installation Medias") do %>
<%= edit_habtm @operatingsystem, Media %>
<% field_set_tag("Installation Media") do %>
<%= edit_habtm @operatingsystem, Medium %>
<% end -%>
</p>
app/views/unattended/kickstart.rhtml
install
<%= @mediapath %>
<%= @mediumpath %>
lang en_US.UTF-8
<%= "langsupport --default en_US.UTF-8 en_GB.UTF-8 en_US.UTF-8\n" if @osver < 5 -%>
<%= "mouse generic3usb --device input/mice\n" if @osver == 3 -%>
config/routes.rb
map.connect "/lookup", :controller => "lookup_keys", :action => "q"
map.resources :domains
map.resources :operatingsystems
map.resources :medias
map.resources :media
map.resources :models
map.resources :architectures
map.resources :puppetclasses, :member => { :assign => :post }, :collection => {:import_environments => :get}
db/migrate/20100523075526_drop_medias.rb
class DropMedias < ActiveRecord::Migration
def self.up
drop_table :medias
end
def self.down
create_table :medias do |t|
t.string :name, :limit => 50, :default => "", :null => false
t.string :path, :limit => 100, :default => "", :null => false
t.references :operatingsystem
t.timestamps
end
Media.create :name => "CentOS 5 mirror", :path => "http://mirror.averse.net/centos/5.3/os/$arch"
Media.create :name => "Fedora 11 Mirror", :path => "http://mirror.nus.edu.sg/fedora/releases/11/Fedora/$arch/os/"
end
end
db/migrate/20100523075701_create_media.rb
class CreateMedia < ActiveRecord::Migration
def self.up
create_table :media do |t|
t.string :name, :limit => 50, :default => "", :null => false
t.string :path, :limit => 100, :default => "", :null => false
t.references :operatingsystem
t.timestamps
end
Medium.create :name => "CentOS 5 mirror", :path => "http://mirror.averse.net/centos/5.3/os/$arch"
Medium.create :name => "Fedora 11 Mirror", :path => "http://mirror.nus.edu.sg/fedora/releases/11/Fedora/$arch/os/"
end
def self.down
drop_table :media
end
end
db/migrate/20100523080255_remove_index_from_host.rb
class RemoveIndexFromHost < ActiveRecord::Migration
def self.up
remove_index "hosts", :name => 'host_media_id_ix'
end
def self.down
add_index "hosts", :media_id, :name => 'host_media_id_ix'
end
end
db/migrate/20100523080316_change_host_media_id.rb
class ChangeHostMediaId < ActiveRecord::Migration
def self.up
change_table :hosts do |t|
t.rename :media_id, :medium_id
end
end
def self.down
change_table :hosts do |t|
t.rename :medium_id, :media_id
end
end
end
db/migrate/20100523080344_add_index_media_id_to_host.rb
class AddIndexMediaIdToHost < ActiveRecord::Migration
def self.up
add_index "hosts", :medium_id, :name => 'host_medium_id_ix'
end
def self.down
remove_index "hosts", :name => 'host_medium_id_ix'
end
end
lib/family.rb
FAMILIES.map{|e| OpenStruct.new(:name => e, :value => FAMILIES.index(e)) }
end
def media_uri host, url = nil
url ||= host.media.path
def medium_uri host, url = nil
url ||= host.medium.path
URI.parse(url.gsub('$arch',host.architecture.name).
gsub('$major', host.os.major).
gsub('$minor', host.os.minor).
......
include Family
def preseed_server host
media_uri(host).select(:host, :port).compact.join(':')
medium_uri(host).select(:host, :port).compact.join(':')
end
def preseed_path host
media_uri(host).select(:path, :query).compact.join('?')
medium_uri(host).select(:path, :query).compact.join('?')
end
end
module RedHat
include Family
# outputs kickstart installation media based on the media type (NFS or URL)
# outputs kickstart installation medium based on the medium type (NFS or URL)
# it also convert the $arch string to the current host architecture
def mediapath host
uri = media_uri(host)
def mediumpath host
uri = medium_uri(host)
server = uri.select(:host, :port).compact.join(':')
dir = uri.select(:path, :query).compact.join('?') unless uri.scheme == 'ftp'
......
else
return ""
end
return "su -c 'rpm -Uvh #{media_uri(host, epel_url)}'"
return "su -c 'rpm -Uvh #{medium_uri(host, epel_url)}'"
end
def yumrepo host
test/fixtures/media.yml
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
one:
name: CentOS 5.4
path: http://mirror.averse.net/centos/6.0/os/$arch
operatingsystem_id: 1
test/fixtures/medias.yml
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
one:
name: CentOS 5.4
path: http://mirror.averse.net/centos/6.0/os/$arch
operatingsystem_id: 1
test/functional/media_controller_test.rb
require 'test_helper'
class MediaControllerTest < ActionController::TestCase
def test_index
get :index, {}, set_session_user
assert_template 'index'
end
def test_new
get :new, {}, set_session_user
assert_template 'new'
end
def test_create_invalid
Medium.any_instance.stubs(:valid?).returns(false)
post :create, {}, set_session_user
assert_template 'new'
end
def test_create_valid
Medium.any_instance.stubs(:valid?).returns(true)
post :create, {}, set_session_user
assert_redirected_to media_url
end
def test_edit
get :edit, {:id => Medium.first}, set_session_user
assert_template 'edit'
end
def test_update_invalid
Medium.any_instance.stubs(:valid?).returns(false)
put :update, {:id => Medium.first}, set_session_user
assert_template 'edit'
end
def test_update_valid
Medium.any_instance.stubs(:valid?).returns(true)
put :update, {:id => Medium.first}, set_session_user
assert_redirected_to media_url
end
def test_destroy
medium = Medium.first
delete :destroy, {:id => medium}, set_session_user
assert_redirected_to media_url
assert !Medium.exists?(medium.id)
end
end
test/functional/medias_controller_test.rb
require 'test_helper'
class MediasControllerTest < ActionController::TestCase
def test_index
get :index, {}, set_session_user
assert_template 'index'
end
def test_new
get :new, {}, set_session_user
assert_template 'new'
end
def test_create_invalid
Media.any_instance.stubs(:valid?).returns(false)
post :create, {}, set_session_user
assert_template 'new'
end
def test_create_valid
Media.any_instance.stubs(:valid?).returns(true)
post :create, {}, set_session_user
assert_redirected_to medias_url
end
def test_edit
get :edit, {:id => Media.first}, set_session_user
assert_template 'edit'
end
def test_update_invalid
Media.any_instance.stubs(:valid?).returns(false)
put :update, {:id => Media.first}, set_session_user
assert_template 'edit'
end
def test_update_valid
Media.any_instance.stubs(:valid?).returns(true)
put :update, {:id => Media.first}, set_session_user
assert_redirected_to medias_url
end
def test_destroy
media = Media.first
delete :destroy, {:id => media}, set_session_user
assert_redirected_to medias_url
assert !Media.exists?(media.id)
end
end
test/unit/helpers/media_helper_test.rb
require 'test_helper'
class MediaHelperTest < ActionView::TestCase
end
test/unit/helpers/medium_helper_test.rb
require 'test_helper'
class MediumHelperTest < ActionView::TestCase
end
test/unit/media_test.rb
require 'test_helper'
class MediaTest < ActiveSupport::TestCase
test "name can't be blank" do
media = Media.new :name => " ", :path => "http://www.google.com"
assert media.name.strip.empty?
assert !media.save
end
test "name can't contain white spaces" do
media = Media.new :name => " Archlinux mirror thing ", :path => "http://www.google.com"
assert !media.name.strip.squeeze(" ").empty?
assert !media.save
media.name.strip!.squeeze!(" ")
assert media.save!
end
test "name must be unique" do
media = Media.new :name => "Archlinux mirror", :path => "http://www.google.com"
assert media.save!
other_media = Media.new :name => "Archlinux mirror", :path => "http://www.youtube.com"
assert !other_media.save
end
test "path can't be blank" do
media = Media.new :name => "Archlinux mirror", :path => " "
assert media.path.strip.empty?
assert !media.save
end
test "path must be unique" do
media = Media.new :name => "Archlinux mirror", :path => "http://www.google.com"
assert media.save!
other_media = Media.new :name => "Ubuntu mirror", :path => "http://www.google.com"
assert !other_media.save
end
test "should not destroy while using" do
media = Media.new :name => "Archlinux mirror", :path => "http://www.google.com"
assert media.save!
host = Host.new :name => "myfullhost", :mac => "aabbecddeeff", :ip => "123.05.02.03",
:domain => Domain.find_or_create_by_name("company.com"), :operatingsystem => Operatingsystem.first,
:architecture => Architecture.first, :environment => Environment.first, :disk => "empty partition",
:ptable => Ptable.first
assert host.save!
media.hosts << host
assert !media.destroy
end
end
test/unit/medium_test.rb
require 'test_helper'
class MediumTest < ActiveSupport::TestCase
test "name can't be blank" do
medium = Medium.new :name => " ", :path => "http://www.google.com"
assert medium.name.strip.empty?
assert !medium.save
end
test "name can't contain white spaces" do
medium = Medium.new :name => " Archlinux mirror thing ", :path => "http://www.google.com"
assert !medium.name.strip.squeeze(" ").empty?
assert !medium.save
medium.name.strip!.squeeze!(" ")
assert medium.save!
end
test "name must be unique" do
medium = Medium.new :name => "Archlinux mirror", :path => "http://www.google.com"
assert medium.save!
other_medium = Medium.new :name => "Archlinux mirror", :path => "http://www.youtube.com"
assert !other_medium.save
end
test "path can't be blank" do
medium = Medium.new :name => "Archlinux mirror", :path => " "
assert medium.path.strip.empty?
assert !medium.save
end
test "path must be unique" do
medium = Medium.new :name => "Archlinux mirror", :path => "http://www.google.com"
assert medium.save!
other_medium = Medium.new :name => "Ubuntu mirror", :path => "http://www.google.com"
assert !other_medium.save
end
test "should not destroy while using" do
medium = Medium.new :name => "Archlinux mirror", :path => "http://www.google.com"
assert medium.save!
host = Host.new :name => "myfullhost", :mac => "aabbecddeeff", :ip => "123.05.02.03",
:domain => Domain.find_or_create_by_name("company.com"), :operatingsystem => Operatingsystem.first,
:architecture => Architecture.first, :environment => Environment.first, :disk => "empty partition",
:ptable => Ptable.first
assert host.save!
medium.hosts << host
assert !medium.destroy
end
end
(1-1/2)