Project

General

Profile

Feature #263 » 0001-Fixes-263-Refactored-Foreman-to-use-correct-singular.patch

Jochen Schalanda, 11/18/2010 01:30 PM

View differences:

app/controllers/media_controller.rb
class MediaController < ApplicationController
before_filter :find_medium, :only => %w{show edit update destroy}
def index
respond_to do |format|
format.html do
@search = Medium.search params[:search]
@media = @search.paginate(:page => params[:page], :include => [:operatingsystems])
end
format.json { render :json => Medium.all.as_json }
end
end
def show
respond_to do |format|
format.json { render :json => @medium.as_json({:only => [:name, :id, :path]}) }
end
end
def new
@medium = Medium.new
end
def create
@medium = Medium.new(params[:medium])
if @medium.save
notice "Successfully created medium."
redirect_to media_url
else
render :action => 'new'
end
end
def edit
end
def update
if @medium.update_attributes(params[:medium])
notice "Successfully updated medium."
redirect_to media_url
else
render :action => 'edit'
end
end
def destroy
@medium.destroy
notice "Successfully destroyed medium."
redirect_to media_url
end
private
def find_medium
@medium = Medium.find(params[:id])
end
end
app/controllers/medias_controller.rb
class MediasController < ApplicationController
before_filter :find_media, :only => %w{show edit update destroy}
def index
respond_to do |format|
format.html do
@search = Media.search params[:search]
@medias = @search.paginate(:page => params[:page], :include => [:operatingsystems])
end
format.json { render :json => Media.all.as_json }
end
end
def show
respond_to do |format|
format.json { render :json => @media.as_json({:only => [:name, :id, :path]}) }
end
end
def new
@media = Media.new
end
def create
@media = Media.new(params[:media])
if @media.save
notice "Successfully created media."
redirect_to medias_url
else
render :action => 'new'
end
end
def edit
end
def update
if @media.update_attributes(params[:media])
notice "Successfully updated media."
redirect_to medias_url
else
render :action => 'edit'
end
end
def destroy
@media.destroy
notice "Successfully destroyed media."
redirect_to medias_url
end
private
def find_media
@media = Media.find(params[:id])
end
end
app/controllers/operatingsystems_controller.rb
@search = Operatingsystem.search(params[:search])
@operatingsystems = @search.all.paginate(:page => params[:page], :include => [:architectures], :order => :name)
end
format.json { render :json => Operatingsystem.all(:include => [:medias, :architectures, :ptables]) }
format.json { render :json => Operatingsystem.all(:include => [:media, :architectures, :ptables]) }
end
end
......
end
def bootfiles
media = Media.find_by_name(params[:media])
medium = Medium.find_by_name(params[:medium])
arch = Architecture.find_by_name(params[:architecture])
respond_to do |format|
format.json { render :json => @operatingsystem.pxe_files(media, arch)}
format.json { render :json => @operatingsystem.pxe_files(medium, arch)}
end
rescue => e
respond_to do |format|
app/controllers/unattended_controller.rb
@arch = @host.architecture.name
os = @host.operatingsystem
@osver = os.major.to_i
@mediapath = os.mediapath @host
@mediumpath = os.mediumpath @host
@epel = os.epel @host
@yumrepo = os.yumrepo @host
......
# we try to match first based on the MAC, falling back to the IP
conditions = (!maclist.empty? ? {:mac => maclist} : {:ip => ip})
@host = Host.find(:first, :include => [:architecture, :media, :operatingsystem, :domain], :conditions => conditions)
@host = Host.find(:first, :include => [:architecture, :medium, :operatingsystem, :domain], :conditions => conditions)
unless @host
logger.info "#{controller_name}: unable to find ip/mac match for #{ip}"
head(:not_found) and return
app/helpers/medias_helper.rb
module MediasHelper
end
app/helpers/medium_helper.rb
module MediumHelper
end
app/models/debian.rb
PXEFILES = {:kernel => "linux", :initrd => "initrd.gz"}
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
# Override the class representation, as this breaks many rails helpers
......
Operatingsystem
end
def boot_files_uri(media, architecture)
raise "invalid media for #{to_s}" unless medias.include?(media)
def boot_files_uri(medium, architecture)
raise "invalid medium for #{to_s}" unless media.include?(medium)
raise "invalid architecture for #{to_s}" unless architectures.include?(architecture)
# Debian stores x86_64 arch is amd64
......
pxe_dir = "dists/#{release_name}/main/installer-#{arch}/current/images/netboot/#{guess_os}-installer/#{arch}"
PXEFILES.values.collect do |img|
URI.parse("#{media_vars_to_uri(media.path, architecture.name, self)}/#{pxe_dir}/#{img}").normalize
URI.parse("#{medium_vars_to_uri(medium.path, architecture.name, self)}/#{pxe_dir}/#{img}").normalize
end
end
app/models/host.rb
class Host < Puppet::Rails::Host
include Authorization
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
......
# Returns a url pointing to boot file
def url_for_boot file
"#{os.media_uri(self)}/#{os.url_for_boot(file)}"
"#{os.medium_uri(self)}/#{os.url_for_boot(file)}"
end
private
app/models/media.rb
class Media < ActiveRecord::Base
include Authorization
has_and_belongs_to_many :operatingsystems
has_many :hosts
validates_uniqueness_of :name
validates_uniqueness_of :path
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 as_json(options={})
super({:only => [:name, :id]}.merge(options))
end
end
app/models/medium.rb
class Medium < ActiveRecord::Base
include Authorization
has_and_belongs_to_many :operatingsystems
has_many :hosts
validates_uniqueness_of :name
validates_uniqueness_of :path
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 as_json(options={})
super({:only => [:name, :id]}.merge(options))
end
end
app/models/operatingsystem.rb
class Operatingsystem < ActiveRecord::Base
include Authorization
has_many :hosts
has_and_belongs_to_many :medias
has_and_belongs_to_many :media
has_and_belongs_to_many :ptables
has_and_belongs_to_many :architectures
has_and_belongs_to_many :puppetclasses
......
families.map{|e| OpenStruct.new(:name => e, :value => e) }
end
def media_uri host, url = nil
url ||= host.media.path
media_vars_to_uri(url, host.architecture.name, host.os)
def medium_uri host, url = nil
url ||= host.medium.path
medium_vars_to_uri(url, host.architecture.name, host.os)
end
def media_vars_to_uri (url, arch, os)
def medium_vars_to_uri (url, arch, os)
URI.parse(url.gsub('$arch', arch).
gsub('$major', os.major).
gsub('$minor', os.minor).
......
"boot/#{to_s}-#{arch}".gsub(" ","-")
end
def pxe_files(media, arch)
boot_files_uri(media, arch).collect do |img|
def pxe_files(medium, arch)
boot_files_uri(medium, arch).collect do |img|
{ pxe_prefix(arch).to_sym => img.to_s}
end
end
def as_json(options={})
{:operatingsystem => {:name => to_s, :id => id, :medias => medias, :architectures => architectures, :ptables => ptables}}
{:operatingsystem => {:name => to_s, :id => id, :media => media, :architectures => architectures, :ptables => ptables}}
end
private
......
self.release_name.downcase! unless defined?(Rake) or release_name.nil? or release_name.empty?
end
def boot_files_uri(media = nil , architecture = nil)
def boot_files_uri(medium = nil , architecture = nil)
"Abstract"
end
app/models/redhat.rb
PXEDIR = "images/pxeboot"
PXEFILES = {:kernel => "vmlinuz", :initrd => "initrd.img"}
# 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
......
Operatingsystem
end
def boot_files_uri(media, architecture)
raise "invalid media for #{to_s}" unless medias.include?(media)
def boot_files_uri(medium, architecture)
raise "invalid medium for #{to_s}" unless media.include?(medium)
raise "invalid architecture for #{to_s}" unless architectures.include?(architecture)
PXEFILES.values.collect do |img|
URI.parse("#{media_vars_to_uri(media.path, architecture.name, self)}/#{PXEDIR}/#{img}").normalize
URI.parse("#{medium_vars_to_uri(medium.path, architecture.name, self)}/#{PXEDIR}/#{img}").normalize
end
end
app/views/home/_settings.html.erb
['Global Parameters', common_parameters_url],
['Hardware Models', models_url],
['Host Groups', hostgroups_url],
['Installation Medias', medias_url],
['Installation Media', media_url],
['LDAP Authentication', auth_source_ldaps_url],
['Operating Systems', operatingsystems_url],
['Partition Tables', ptables_url],
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, :size => 70 %>
</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 :operatingsystems %><br />
<%= authorized_edit_habtm @medium, Operatingsystem %>
</p>
<br/>
<% 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 "Media" %>
<table class="list">
<tr>
<th>Name</th>
<th>Path</th>
<th>Operatingsystem</th>
<th></th>
</tr>
<% for medium in @media %>
<tr class="<%= cycle("even", "odd") -%>">
<td><%= link_to_if_authorized h(medium), hash_for_edit_medium_path(:id => medium.id) %></td>
<td><%=h medium.path %></td>
<td><%=h medium.operatingsystems.to_sentence %></td>
<td align="right">
<%= display_link_if_authorized "Destroy", hash_for_medium_path(:id => medium, :auth_action => :destroy), :confirm => "Delete #{medium.name}?", :method => :delete %>
</td>
</tr>
<% end %>
</table>
<%= page_entries_info @media %>
<%= will_paginate @media %>
<p><%= display_link_if_authorized "New Medium", hash_for_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/media/welcome.html.erb
<div id="welcome">
<h2>Installation medium configuration</h2>
<P>
As your configuration does not appear to have any installation media declared then you might want to configure some at this point.
</p>
<p>
This information represents the source of one or more operating system's installation files, accessible via the network.
It will probably be a copy of one or more CD or DVDs, though it can be any valid URL, including the "nfs://" schema or even refer to the Distro's own release area.
</p>
<p>
For example, if you have copied several Redhat release disks into a directory structure where the disk images are renamed 4.8 or 5.5, and each contained both x86_64 and i386 binaries, then you
could create a single medium entry describing them all.
The entry, which could be just named "Redhat" could contain a path like this "nfs://server/export/redhat/$version/$arch" or "http://server/redhat/$version/$arch".
The keywords <b>$version</b>, <b>$major</b> and <b>$minor</b> will be interpolated back into the path specification to calculate the true URL address.
</p>
<p>
You may also associate one or more operating systems with this medium or alternatively set this up later on the <%= link_to "Operating systems", operatingsystems_path -%> page.
</p>
</p>
<%= link_to "New installation medium", new_medium_path -%>.
</p>
</div>
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, :size => 70 %>
</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 :operatingsystems %><br />
<%= authorized_edit_habtm @media, Operatingsystem %>
</p>
<br/>
<% 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>
<th></th>
</tr>
<% for media in @medias %>
<tr class="<%= cycle("even", "odd") -%>">
<td><%= link_to_if_authorized h(media), hash_for_edit_media_path(:id => media.id) %></td>
<td><%=h media.path %></td>
<td><%=h media.operatingsystems.to_sentence %></td>
<td align="right">
<%= display_link_if_authorized "Destroy", hash_for_media_path(:id => media, :auth_action => :destroy), :confirm => "Delete #{media.name}?", :method => :delete %>
</td>
</tr>
<% end %>
</table>
<%= page_entries_info @medias %>
<%= will_paginate @medias %>
<p><%= display_link_if_authorized "New Media", hash_for_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/medias/welcome.html.erb
<div id="welcome">
<h2>Installation media configuration</h2>
<P>
As your configuration does not appear to have any installation medias declared then you might want to configure some at this point.
</p>
<p>
This information represents the source of one or more operating system's installation files, accessible via the network.
It will probably be a copy of one or more CD or DVDs, though it can be any valid URL, including the "nfs://" schema or even refer to the Distro's own release area.
</p>
<p>
For example, if you have copied several Redhat release disks into a directory structure where the disk images are renamed 4.8 or 5.5, and each contained both x86_64 and i386 binaries, then you
could create a single media entry describing them all.
The entry, which could be just named "Redhat" could contain a path like this "nfs://server/export/redhat/$version/$arch" or "http://server/redhat/$version/$arch".
The keywords <b>$version</b>, <b>$major</b> and <b>$minor</b> will be interpolated back into the path specification to calculate the true URL address.
</p>
<p>
You may also associate one or more operating systems with this media or alternatively set this up later on the <%= link_to "Operating systems", operatingsystems_path -%> page.
</p>
</p>
<%= link_to "New installation media", new_media_path -%>.
</p>
</div>
app/views/operatingsystems/_form.html.erb
<% end -%>
</p>
<p>
<% field_set_tag("Installation Medias") do %>
<%= authorized_edit_habtm @operatingsystem, Media %>
<% field_set_tag("Installation Media") do %>
<%= authorized_edit_habtm @operatingsystem, Medium %>
<% end -%>
</p>
app/views/operatingsystems/welcome.html.erb
<h2>Operatingsystem configuration</h2>
<P>
As your installation does not appear to have any operating systems declared then you might want to configure some at this point. (If you have not already visited the <%= link_to "partition table", ptables_path -%> or
<%= link_to "installation media", medias_path -%> pages then it would be sensible to initialise these first.)
<%= link_to "installation medium", media_path -%> pages then it would be sensible to initialise these first.)
</p>
<p>
For example, if you intend to support "lucid" Ubuntu then enter "Ubuntu", "10", "04", "lucid" and select the "Debian" family of operatingsystems. The <b>Family</b> selection allows Foreman to properly understand the
layout of the operatingsystem's installation media.
layout of the operatingsystem's installation medium.
</p>
<p>
This page also provides a mechanism for selecting the target architectures that your installation will be building. You will also be prompted to provide a default disk layout for all
machines of this type and from where you would be expecting the machine to retrieve its installation files.
</p>
<p>
It is possible that some of these selections are empty, and if so, you should go to the correct settings page to setup your <%= link_to "installation medias", medias_path -%> and <%= link_to "disk layouts", ptables_path -%>.
It is possible that some of these selections are empty, and if so, you should go to the correct settings page to setup your <%= link_to "installation media", media_path -%> and <%= link_to "disk layouts", ptables_path -%>.
</p>
<%= link_to "New operatingsystem", new_operatingsystem_path -%>.
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, :requirements => {:id => /[^\/]+/}
map.resources :operatingsystems, :member => {:bootfiles => :get}
map.resources :medias
map.resources :media
map.resources :models
map.resources :architectures
map.resources :puppetclasses, :member => { :assign => :post }, :collection => {:import_environments => :get}
db/migrate/20090714132448_create_hosts.rb
add_column :hosts, :subnet_id, :integer
add_column :hosts, :sp_subnet_id, :integer
add_column :hosts, :ptable_id, :integer
add_column :hosts, :media_id, :integer
add_column :hosts, :medium_id, :integer
add_column :hosts, :build, :boolean, :default => true
add_column :hosts, :comment, :text
add_column :hosts, :disk, :text
......
remove_columns :hosts, :mac, :sp_mac, :sp_ip, :sp_name, :root_pass, :serial,
:puppetmaster, :puppet_status, :domain_id, :architecture_id, :operatingsystem_id,
:environment_id, :subnet_id, :sp_subnet_id, :ptable_id, :hosttype_id,
:media_id, :build, :comment, :disk, :installed_at
:medium_id, :build, :comment, :disk, :installed_at
else
drop_table :hosts
end
db/migrate/20090717025820_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 mirror", :path => "http://mirror.averse.net/centos/$major.$minor/os/$arch"
Medium.create :name => "Fedora Mirror", :path => "http://mirror.nus.edu.sg/fedora/releases/$major/Fedora/$arch/os/"
Medium.create :name => "RedHat Beta", :path => "http://ftp.redhat.com/pub/redhat/rhel/beta/$major/$arch/os"
Medium.create :name => "Ubuntu Mirror", :path => "http://sg.archive.ubuntu.com"
end
def self.down
drop_table :media
end
end
db/migrate/20090717025820_create_medias.rb
class CreateMedias < ActiveRecord::Migration
def self.up
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 mirror", :path => "http://mirror.averse.net/centos/$major.$minor/os/$arch"
Media.create :name => "Fedora Mirror", :path => "http://mirror.nus.edu.sg/fedora/releases/$major/Fedora/$arch/os/"
Media.create :name => "RedHat Beta", :path => "http://ftp.redhat.com/pub/redhat/rhel/beta/$major/$arch/os"
Media.create :name => "Ubuntu Mirror", :path => "http://sg.archive.ubuntu.com"
end
def self.down
drop_table :medias
end
end
db/migrate/20090920043521_add_index_to_host.rb
add_index "hosts", :architecture_id, :name => 'host_arch_id_ix'
add_index "hosts", :operatingsystem_id, :name => 'host_os_id_ix'
add_index "hosts", :environment_id, :name => 'host_env_id_ix'
add_index "hosts", :media_id, :name => 'host_media_id_ix'
add_index "hosts", :medium_id, :name => 'host_medium_id_ix'
add_index "hosts", :hostgroup_id, :name => 'host_group_id_ix'
end
......
remove_index "hosts", :name => 'host_arch_id_ix'
remove_index "hosts", :name => 'host_os_id_ix'
remove_index "hosts", :name => 'host_env_id_ix'
remove_index "hosts", :name => 'host_media_id_ix'
remove_index "hosts", :name => 'host_medium_id_ix'
remove_index "hosts", :name => 'host_group_id_ix'
end
end
db/migrate/20100523141204_create_media_operatingsystems_and_migrate_data.rb
class CreateMediaOperatingsystemsAndMigrateData < ActiveRecord::Migration
def self.up
medium_hash = Hash.new
Medium.all.each do |medium|
unless medium.operatingsystem_id.nil?
if Operatingsystem.exists?(medium.operatingsystem_id)
os = Operatingsystem.find(medium.operatingsystem_id)
medium_hash[os] = medium
else
say "skipped #{medium}"
end
end
end
create_table :media_operatingsystems , :id => false do |t|
t.references :medium, :null => false
t.references :operatingsystem, :null => false
end
medium_hash.keys.each { |os| os.media << medium_hash[os] }
remove_column :media, :operatingsystem_id
end
def self.down
add_column :media, :operatingsystem_id, :integer
drop_table :media_operatingsystems
end
end
db/migrate/20100523141204_create_medias_operatingsystems_and_migrate_data.rb
class CreateMediasOperatingsystemsAndMigrateData < ActiveRecord::Migration
def self.up
media_hash = Hash.new
Media.all.each do |medium|
unless medium.operatingsystem_id.nil?
if Operatingsystem.exists?(medium.operatingsystem_id)
os = Operatingsystem.find(medium.operatingsystem_id)
media_hash[os] = medium
else
say "skipped #{medium}"
end
end
end
create_table :medias_operatingsystems , :id => false do |t|
t.references :media, :null => false
t.references :operatingsystem, :null => false
end
media_hash.keys.each { |os| os.medias << media_hash[os] }
remove_column :medias, :operatingsystem_id
end
def self.down
add_column :medias, :operatingsystem_id, :integer
drop_table :medias_operatingsystems
end
end
db/migrate/20100524080302_migrate_installation_media_uri.rb
class MigrateInstallationMediaUri < ActiveRecord::Migration
def self.up
Media.all.each { |medium|
matches = /^([^:]+):(\/.+)/.match(medium.path)
if matches.size == 3 and ![ 'http', 'https', 'ftp', 'ftps', 'nfs' ].include?(matches[1])
medium.path = 'nfs://' + matches[1] + matches[2]
medium.save
end
}
end
def self.down
end
end
db/migrate/20100524080302_migrate_installation_medium_uri.rb
class MigrateInstallationMediumUri < ActiveRecord::Migration
def self.up
Medium.all.each { |medium|
matches = /^([^:]+):(\/.+)/.match(medium.path)
if matches.size == 3 and ![ 'http', 'https', 'ftp', 'ftps', 'nfs' ].include?(matches[1])
medium.path = 'nfs://' + matches[1] + matches[2]
medium.save
end
}
end
def self.down
end
end
db/migrate/20101118130026_correct_media.rb
class CorrectMedia < ActiveRecord::Migration
def self.up
if table_exists? :medias
if table_exists? :medias_operatingsystems
rename_column :medias_operatingsystems, :media_id, :medium_id
rename_table :medias_operatingsystems, :media_operatingsystems
end
change_table :hosts do |t|
t.remove_index :name => :host_media_id_ix
t.rename :media_id, :medium_id
t.index :medium_id, :name => :host_medium_id_ix
end
rename_table :medias, :media
end
end
def self.down
if table_exists? :media
if table_exists? :media_operatingsystems
rename_column :medias_operatingsystems, :medium_id, :media_id
rename_table :media_operatingsystems, :medias_operatingsystems
end
change_table :hosts do |t|
t.remove_index :name => :host_medium_id_ix
t.rename :medium_id, :media_id
t.index :media_id, :name => :host_media_id_ix
end
rename_table :media, :medias
end
end
end
lib/access_permissions.rb
map.permission :destroy_hosts, {:hosts => [:destroy, :multiple_actions, :reset_multiple, :multiple_destroy, :submit_multiple_destroy]}
end
map.security_block :medias do |map|
map.permission :view_medias, {:medias => [:index, :show]}
map.permission :create_medias, {:medias => [:new, :create]}
map.permission :edit_medias, {:medias => [:edit, :update]}
map.permission :destroy_medias, {:medias => [:destroy]}
map.security_block :media do |map|
map.permission :view_media, {:media => [:index, :show]}
map.permission :create_media, {:media => [:new, :create]}
map.permission :edit_media, {:media => [:edit, :update]}
map.permission :destroy_media, {:media => [:destroy]}
end
map.security_block :models do |map|
lib/foreman/default_data/loader.rb
:view_hostgroups,
:view_domains,
:view_operatingsystems,
:view_medias,
:view_media,
:view_models,
:view_environments,
:view_architectures,
......
:create_hosts,
:edit_hosts,
:destroy_hosts,
:view_medias,
:create_medias,
:edit_medias,
:destroy_medias,
:view_media,
:create_media,
:edit_media,
:destroy_media,
:view_models,
:view_operatingsystems,
:view_ptables,
......
:view_hostgroups,
:view_domains,
:view_operatingsystems,
:view_medias,
:view_media,
:view_models,
:view_environments,
:view_architectures,
test/fixtures/hosts.yml
operatingsystem: one
ptable: one
domain: yourdomain
media: one
medium: one
otherfullhost:
name: otherfullhost
......
build: true
ptable: one
domain: yourdomain
media: one
medium: one
operatingsystem: redhat
ubuntu:
......
build: true
ptable: ubuntu
domain: yourdomain
media: ubuntu
medium: ubuntu
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
ubuntu:
name: Ubuntu Mirror
path: http://sg.archive.ubuntu.com
unused:
name: unused
path: http://nothing.intersting.com
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
ubuntu:
name: Ubuntu Mirror
path: http://sg.archive.ubuntu.com
unused:
name: unused
path: http://nothing.intersting.com
test/fixtures/roles.yml
- :create_hosts
- :edit_hosts
- :destroy_hosts
- :view_medias
- :create_medias
- :edit_medias
- :destroy_medias
- :view_media
- :create_media
- :edit_media
- :destroy_media
- :view_models
- :create_models
- :edit_models
......
- :view_globals
- :view_hostgroups
- :view_hosts
- :view_medias
- :view_media
- :view_models
- :view_operatingsystems
- :view_ptables
......
- :view_hostgroups
- :view_domains
- :view_oses
- :view_medias
- :view_media
- :view_models
- :view_environments
- :view_architectures
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 = media(:unused)
delete :destroy, {:id => medium}, set_session_user
assert_redirected_to media_url
assert !Medium.exists?(medium.id)
end
def setup_user
@request.session[:user] = users(:one).id
users(:one).roles = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
end
test 'user with viewer rights should fail to edit a medium' do
setup_user
get :edit, {:id => Medium.first.id}
assert @response.status == '403 Forbidden'
end
test 'user with viewer rights should succeed in viewing media' do
setup_user
get :index
assert_response :success
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 = medias(:unused)
delete :destroy, {:id => media}, set_session_user
assert_redirected_to medias_url
assert !Media.exists?(media.id)
end
def setup_user
@request.session[:user] = users(:one).id
users(:one).roles = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
end
test 'user with viewer rights should fail to edit a media' do
setup_user
get :edit, {:id => Media.first.id}
assert @response.status == '403 Forbidden'
end
test 'user with viewer rights should succeed in viewing medias' do
setup_user
get :index
assert_response :success
end
end
test/functional/roles_controller_test.rb
assert Role.find(1).permissions.empty?
end
viewable = %w{Architecture Audit AuthSourceLdap Dashboard Domain Environment LookupKey FactValue CommonParameter Hostgroup Host Media Model Operatingsystem Ptable Puppetclass Report Setting Statistic Usergroup User}
editable = %w{Architecture Audit AuthSourceLdap Domain Environment LookupKey CommonParameter Hostgroup Host Media Model Operatingsystem Ptable Puppetclass Report Usergroup User}
viewable = %w{Architecture Audit AuthSourceLdap Dashboard Domain Environment LookupKey FactValue CommonParameter Hostgroup Host Medium Model Operatingsystem Ptable Puppetclass Report Setting Statistic Usergroup User}
editable = %w{Architecture Audit AuthSourceLdap Domain Environment LookupKey CommonParameter Hostgroup Host Medium Model Operatingsystem Ptable Puppetclass Report Usergroup User}
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
setup do
User.current = User.find_by_login "admin"
end
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
def setup_user operation
@one = users(:one)
as_admin do
role = Role.find_or_create_by_name :name => "#{operation}_medias"
role.permissions = ["#{operation}_medias".to_sym]
@one.roles = [role]
@one.save!
end
User.current = @one
end
test "user with create permissions should be able to create" do
setup_user "create"
record = Media.create :name => "dummy", :path => "http://hello"
assert record.valid?
assert !record.new_record?
end
test "user with view permissions should not be able to create" do
setup_user "view"
record = Media.create :name => "dummy", :path => "http://hello"
assert record.valid?
assert record.new_record?
end
test "user with destroy permissions should be able to destroy" do
setup_user "destroy"
record = Media.first
as_admin do
record.hosts = []
end
assert record.destroy
assert record.frozen?
end
test "user with edit permissions should not be able to destroy" do
... This diff was truncated because it exceeds the maximum size that can be displayed.
(2-2/2)