Project

General

Profile

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
1
class MediaController < ApplicationController
2
  before_filter :find_medium, :only => %w{show edit update destroy}
3

  
4
  def index
5
    respond_to do |format|
6
      format.html do
7
        @search = Medium.search params[:search]
8
        @media = @search.paginate(:page => params[:page], :include => [:operatingsystems])
9
      end
10
      format.json { render :json => Medium.all.as_json }
11
    end
12
  end
13

  
14
  def show
15
    respond_to do |format|
16
      format.json { render :json => @medium.as_json({:only => [:name, :id, :path]}) }
17
    end
18
  end
19

  
20
  def new
21
    @medium = Medium.new
22
  end
23

  
24
  def create
25
    @medium = Medium.new(params[:medium])
26
    if @medium.save
27
      notice "Successfully created medium."
28
      redirect_to media_url
29
    else
30
      render :action => 'new'
31
    end
32
  end
33

  
34
  def edit
35
  end
36

  
37
  def update
38
    if @medium.update_attributes(params[:medium])
39
      notice "Successfully updated medium."
40
      redirect_to media_url
41
    else
42
      render :action => 'edit'
43
    end
44
  end
45

  
46
  def destroy
47
    @medium.destroy
48
    notice "Successfully destroyed medium."
49
    redirect_to media_url
50
  end
51

  
52
  private
53
  def find_medium
54
    @medium = Medium.find(params[:id])
55
  end
56

  
57
end
app/controllers/medias_controller.rb
1
class MediasController < ApplicationController
2
  before_filter :find_media, :only => %w{show edit update destroy}
3

  
4
  def index
5
    respond_to do |format|
6
      format.html do
7
        @search = Media.search params[:search]
8
        @medias = @search.paginate(:page => params[:page], :include => [:operatingsystems])
9
      end
10
      format.json { render :json => Media.all.as_json }
11
    end
12
  end
13

  
14
  def show
15
    respond_to do |format|
16
      format.json { render :json => @media.as_json({:only => [:name, :id, :path]}) }
17
    end
18
  end
19

  
20
  def new
21
    @media = Media.new
22
  end
23

  
24
  def create
25
    @media = Media.new(params[:media])
26
    if @media.save
27
      notice "Successfully created media."
28
      redirect_to medias_url
29
    else
30
      render :action => 'new'
31
    end
32
  end
33

  
34
  def edit
35
  end
36

  
37
  def update
38
    if @media.update_attributes(params[:media])
39
      notice "Successfully updated media."
40
      redirect_to medias_url
41
    else
42
      render :action => 'edit'
43
    end
44
  end
45

  
46
  def destroy
47
    @media.destroy
48
    notice "Successfully destroyed media."
49
    redirect_to medias_url
50
  end
51

  
52
  private
53
  def find_media
54
    @media = Media.find(params[:id])
55
  end
56

  
57
end
app/controllers/operatingsystems_controller.rb
7 7
        @search           = Operatingsystem.search(params[:search])
8 8
        @operatingsystems = @search.all.paginate(:page => params[:page], :include => [:architectures], :order => :name)
9 9
      end
10
      format.json { render :json => Operatingsystem.all(:include => [:medias, :architectures, :ptables]) }
10
      format.json { render :json => Operatingsystem.all(:include => [:media, :architectures, :ptables]) }
11 11
    end
12 12

  
13 13
  end
......
55 55
  end
56 56

  
57 57
  def bootfiles
58
    media = Media.find_by_name(params[:media])
58
    medium = Medium.find_by_name(params[:medium])
59 59
    arch =  Architecture.find_by_name(params[:architecture])
60 60
    respond_to do |format|
61
      format.json { render :json => @operatingsystem.pxe_files(media, arch)}
61
      format.json { render :json => @operatingsystem.pxe_files(medium, arch)}
62 62
    end
63 63
  rescue => e
64 64
    respond_to do |format|
app/controllers/unattended_controller.rb
25 25
    @arch      = @host.architecture.name
26 26
    os         = @host.operatingsystem
27 27
    @osver     = os.major.to_i
28
    @mediapath = os.mediapath @host
28
    @mediumpath = os.mediumpath @host
29 29
    @epel      = os.epel      @host
30 30
    @yumrepo   = os.yumrepo   @host
31 31

  
......
105 105

  
106 106
    # we try to match first based on the MAC, falling back to the IP
107 107
    conditions = (!maclist.empty? ? {:mac => maclist} : {:ip => ip})
108
    @host = Host.find(:first, :include => [:architecture, :media, :operatingsystem, :domain], :conditions => conditions)
108
    @host = Host.find(:first, :include => [:architecture, :medium, :operatingsystem, :domain], :conditions => conditions)
109 109
    unless @host
110 110
      logger.info "#{controller_name}: unable to find ip/mac match for #{ip}"
111 111
      head(:not_found) and return
app/helpers/medias_helper.rb
1
module MediasHelper
2
end
app/helpers/medium_helper.rb
1
module MediumHelper
2
end
app/models/debian.rb
3 3
  PXEFILES = {:kernel => "linux", :initrd => "initrd.gz"}
4 4

  
5 5
  def preseed_server host
6
    media_uri(host).select(:host, :port).compact.join(':')
6
    medium_uri(host).select(:host, :port).compact.join(':')
7 7
  end
8 8

  
9 9
  def preseed_path host
10
    media_uri(host).select(:path, :query).compact.join('?')
10
    medium_uri(host).select(:path, :query).compact.join('?')
11 11
  end
12 12

  
13 13
  # Override the class representation, as this breaks many rails helpers
......
15 15
    Operatingsystem
16 16
  end
17 17

  
18
  def boot_files_uri(media, architecture)
19
    raise "invalid media for #{to_s}" unless medias.include?(media)
18
  def boot_files_uri(medium, architecture)
19
    raise "invalid medium for #{to_s}" unless media.include?(medium)
20 20
    raise "invalid architecture for #{to_s}" unless architectures.include?(architecture)
21 21

  
22 22
    # Debian stores x86_64 arch is amd64
......
24 24
    pxe_dir = "dists/#{release_name}/main/installer-#{arch}/current/images/netboot/#{guess_os}-installer/#{arch}"
25 25

  
26 26
    PXEFILES.values.collect do |img|
27
      URI.parse("#{media_vars_to_uri(media.path, architecture.name, self)}/#{pxe_dir}/#{img}").normalize
27
      URI.parse("#{medium_vars_to_uri(medium.path, architecture.name, self)}/#{pxe_dir}/#{img}").normalize
28 28
    end
29 29
  end
30 30

  
app/models/host.rb
1 1
class Host < Puppet::Rails::Host
2 2
  include Authorization
3 3
  belongs_to :architecture
4
  belongs_to :media
4
  belongs_to :medium
5 5
  belongs_to :model
6 6
  belongs_to :domain
7 7
  belongs_to :operatingsystem
......
109 109
    validates_format_of      :sp_mac,    :with => /([a-f0-9]{1,2}:){5}[a-f0-9]{1,2}/, :allow_nil => true, :allow_blank => true
110 110
    validates_format_of      :sp_ip,     :with => /(\d{1,3}\.){3}\d{1,3}/, :allow_nil => true, :allow_blank => true
111 111
    validates_format_of      :serial,    :with => /[01],\d{3,}n\d/, :message => "should follow this format: 0,9600n8", :allow_blank => true, :allow_nil => true
112
    validates_associated     :domain, :operatingsystem,  :architecture, :subnet,:media#, :user, :deployment, :model
112
    validates_associated     :domain, :operatingsystem,  :architecture, :subnet,:medium#, :user, :deployment, :model
113 113
  end
114 114

  
115 115
  before_validation :normalize_addresses, :normalize_hostname
......
478 478

  
479 479
  # Returns a url pointing to boot file
480 480
  def url_for_boot file
481
    "#{os.media_uri(self)}/#{os.url_for_boot(file)}"
481
    "#{os.medium_uri(self)}/#{os.url_for_boot(file)}"
482 482
  end
483 483

  
484 484
  private
app/models/media.rb
1
class Media < ActiveRecord::Base
2
  include Authorization
3
  has_and_belongs_to_many :operatingsystems
4
  has_many :hosts
5
  validates_uniqueness_of :name
6
  validates_uniqueness_of :path
7
  validates_presence_of :name, :path
8
  validates_format_of :name, :with => /\A(\S+\s?)+\Z/, :message => "can't be blank or contain trailing white spaces."
9
  validates_format_of :path, :with => /^(http|https|ftp|nfs):\/\//,
10
    :message => "Only URLs with schema http://, https://, ftp:// or nfs:// are allowed (e.g. nfs://server/vol/dir)"
11

  
12
  alias_attribute :os, :operatingsystem
13
  before_destroy Ensure_not_used_by.new(:hosts)
14

  
15
  def as_json(options={})
16
    super({:only => [:name, :id]}.merge(options))
17
  end
18

  
19
end
app/models/medium.rb
1
class Medium < ActiveRecord::Base
2
  include Authorization
3
  has_and_belongs_to_many :operatingsystems
4
  has_many :hosts
5
  validates_uniqueness_of :name
6
  validates_uniqueness_of :path
7
  validates_presence_of :name, :path
8
  validates_format_of :name, :with => /\A(\S+\s?)+\Z/, :message => "can't be blank or contain trailing white spaces."
9
  validates_format_of :path, :with => /^(http|https|ftp|nfs):\/\//,
10
    :message => "Only URLs with schema http://, https://, ftp:// or nfs:// are allowed (e.g. nfs://server/vol/dir)"
11

  
12
  alias_attribute :os, :operatingsystem
13
  before_destroy Ensure_not_used_by.new(:hosts)
14

  
15
  def as_json(options={})
16
    super({:only => [:name, :id]}.merge(options))
17
  end
18

  
19
end
app/models/operatingsystem.rb
4 4
class Operatingsystem < ActiveRecord::Base
5 5
  include Authorization
6 6
  has_many :hosts
7
  has_and_belongs_to_many :medias
7
  has_and_belongs_to_many :media
8 8
  has_and_belongs_to_many :ptables
9 9
  has_and_belongs_to_many :architectures
10 10
  has_and_belongs_to_many :puppetclasses
......
42 42
    families.map{|e| OpenStruct.new(:name => e, :value => e) }
43 43
  end
44 44

  
45
  def media_uri host, url = nil
46
    url ||= host.media.path
47
    media_vars_to_uri(url, host.architecture.name, host.os)
45
  def medium_uri host, url = nil
46
    url ||= host.medium.path
47
    medium_vars_to_uri(url, host.architecture.name, host.os)
48 48
  end
49 49

  
50
  def media_vars_to_uri (url, arch, os)
50
  def medium_vars_to_uri (url, arch, os)
51 51
    URI.parse(url.gsub('$arch',  arch).
52 52
              gsub('$major',  os.major).
53 53
              gsub('$minor',  os.minor).
......
73 73
    "boot/#{to_s}-#{arch}".gsub(" ","-")
74 74
  end
75 75

  
76
  def pxe_files(media, arch)
77
    boot_files_uri(media, arch).collect do |img|
76
  def pxe_files(medium, arch)
77
    boot_files_uri(medium, arch).collect do |img|
78 78
      { pxe_prefix(arch).to_sym => img.to_s}
79 79
    end
80 80
  end
81 81

  
82 82
  def as_json(options={})
83
    {:operatingsystem => {:name => to_s, :id => id, :medias => medias, :architectures => architectures, :ptables => ptables}}
83
    {:operatingsystem => {:name => to_s, :id => id, :media => media, :architectures => architectures, :ptables => ptables}}
84 84
  end
85 85

  
86 86
  private
......
100 100
    self.release_name.downcase! unless defined?(Rake) or release_name.nil? or release_name.empty?
101 101
  end
102 102

  
103
  def boot_files_uri(media = nil , architecture = nil)
103
  def boot_files_uri(medium = nil , architecture = nil)
104 104
    "Abstract"
105 105
  end
106 106

  
app/models/redhat.rb
3 3
  PXEDIR   = "images/pxeboot"
4 4
  PXEFILES = {:kernel => "vmlinuz", :initrd => "initrd.img"}
5 5

  
6
  # outputs kickstart installation media based on the media type (NFS or URL)
6
  # outputs kickstart installation medium based on the medium type (NFS or URL)
7 7
  # it also convert the $arch string to the current host architecture
8
  def mediapath host
9
    uri = media_uri(host)
8
  def mediumpath host
9
    uri = medium_uri(host)
10 10
    server = uri.select(:host, :port).compact.join(':')
11 11
    dir = uri.select(:path, :query).compact.join('?') unless uri.scheme == 'ftp'
12 12

  
......
33 33
    else
34 34
      return ""
35 35
    end
36
    return "su -c 'rpm -Uvh #{media_uri(host, epel_url)}'"
36
    return "su -c 'rpm -Uvh #{medium_uri(host, epel_url)}'"
37 37
  end
38 38

  
39 39
  def yumrepo host
......
47 47
    Operatingsystem
48 48
  end
49 49

  
50
  def boot_files_uri(media, architecture)
51
    raise "invalid media for #{to_s}" unless medias.include?(media)
50
  def boot_files_uri(medium, architecture)
51
    raise "invalid medium for #{to_s}" unless media.include?(medium)
52 52
    raise "invalid architecture for #{to_s}" unless architectures.include?(architecture)
53 53
    PXEFILES.values.collect do |img|
54
      URI.parse("#{media_vars_to_uri(media.path, architecture.name, self)}/#{PXEDIR}/#{img}").normalize
54
      URI.parse("#{medium_vars_to_uri(medium.path, architecture.name, self)}/#{PXEDIR}/#{img}").normalize
55 55
    end
56 56
  end
57 57

  
app/views/home/_settings.html.erb
8 8
    ['Global Parameters',   common_parameters_url],
9 9
    ['Hardware Models',     models_url],
10 10
    ['Host Groups',         hostgroups_url],
11
    ['Installation Medias', medias_url],
11
    ['Installation Media', media_url],
12 12
    ['LDAP Authentication', auth_source_ldaps_url],
13 13
    ['Operating Systems',   operatingsystems_url],
14 14
    ['Partition Tables',    ptables_url],
app/views/hosts/_operatingsystem.html.erb
1 1
Media
2
<%= collection_select :host, :media_id, @operatingsystem.medias, :id, :to_label %>
2
<%= collection_select :host, :medium_id, @operatingsystem.media, :id, :to_label %>
3 3
Partition Table
4 4
<%= collection_select :host, :ptable_id, @operatingsystem.ptables, :id, :to_label %>
app/views/media/_form.html.erb
1
<% form_for @medium do |f| %>
2
  <%= f.error_messages %>
3
  <p>
4
  <%= f.label :name %><br />
5
  <%= f.text_field :name %>
6
  </p>
7
  <p>
8
  <%= f.label :path %><br />
9
  <%= f.text_field :path, :size => 70 %>
10
  </p >
11
  <small>
12
    The path to the medium, can be a URL or a valid NFS server (exclusive of the architecture).<br />
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.
15
  </small>
16
  <p>
17
  <%= f.label :operatingsystems %><br />
18
  <%= authorized_edit_habtm @medium, Operatingsystem %>
19
  </p>
20
   <br/>
21
  <% unless @medium.new_record? -%>
22
    <p>Currently used by <%= @medium.hosts.count %> hosts</p>
23
  <% end -%>
24

  
25
  <p><%= f.submit "Submit" %></p>
26
<% end %>
app/views/media/edit.html.erb
1
<% title "Edit Medium" %>
2

  
3
<%= render :partial => 'form' %>
4

  
5
<p>
6
  <%= link_to "View All", media_path %>
7
</p>
app/views/media/index.html.erb
1
<% title "Media" %>
2

  
3
<table class="list">
4
  <tr>
5
    <th>Name</th>
6
    <th>Path</th>
7
    <th>Operatingsystem</th>
8
    <th></th>
9
  </tr>
10
  <% for medium in @media %>
11
    <tr class="<%= cycle("even", "odd") -%>">
12
      <td><%= link_to_if_authorized h(medium), hash_for_edit_medium_path(:id => medium.id) %></td>
13
      <td><%=h medium.path %></td>
14
      <td><%=h medium.operatingsystems.to_sentence %></td>
15
      <td align="right">
16
        <%= display_link_if_authorized "Destroy", hash_for_medium_path(:id => medium, :auth_action => :destroy), :confirm => "Delete #{medium.name}?", :method => :delete %>
17
      </td>
18
    </tr>
19
  <% end %>
20
</table>
21
<%= page_entries_info @media %>
22
<%= will_paginate     @media %>
23
<p><%= display_link_if_authorized "New Medium", hash_for_new_medium_path %></p>
app/views/media/new.html.erb
1
<% title "New Medium" %>
2

  
3
<%= render :partial => 'form' %>
4

  
5
<p><%= link_to "Back to List", media_path %></p>
app/views/media/welcome.html.erb
1
<div id="welcome">
2
  <h2>Installation medium configuration</h2>
3
  <P>
4
  As your configuration does not appear to have any installation media declared then you might want to configure some at this point.
5
  </p>
6
  <p>
7
  This information represents the source of one or more operating system's installation files, accessible via the network.
8
  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.
9
  </p>
10
  <p>
11
  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
12
  could create a single medium entry describing them all.
13
  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".
14

  
15
  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.
16
  </p>
17
  <p>
18
  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.
19
  </p>
20
  </p>
21
  <%= link_to "New installation medium", new_medium_path -%>.
22
  </p>
23
</div>
app/views/medias/_form.html.erb
1
<% form_for @media do |f| %>
2
  <%= f.error_messages %>
3
  <p>
4
  <%= f.label :name %><br />
5
  <%= f.text_field :name %>
6
  </p>
7
  <p>
8
  <%= f.label :path %><br />
9
  <%= f.text_field :path, :size => 70 %>
10
  </p >
11
  <small>
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/$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.
15
  </small>
16
  <p>
17
  <%= f.label :operatingsystems %><br />
18
  <%= authorized_edit_habtm @media, Operatingsystem %>
19
  </p>
20
   <br/>
21
  <% unless @media.new_record? -%>
22
    <p>Currently used by <%= @media.hosts.count %> hosts</p>
23
  <% end -%>
24

  
25
  <p><%= f.submit "Submit" %></p>
26
<% end %>
app/views/medias/edit.html.erb
1
<% title "Edit Media" %>
2

  
3
<%= render :partial => 'form' %>
4

  
5
<p>
6
  <%= link_to "View All", medias_path %>
7
</p>
app/views/medias/index.html.erb
1
<% title "Medias" %>
2

  
3
<table class="list">
4
  <tr>
5
    <th>Name</th>
6
    <th>Path</th>
7
    <th>Operatingsystem</th>
8
    <th></th>
9
  </tr>
10
  <% for media in @medias %>
11
    <tr class="<%= cycle("even", "odd") -%>">
12
      <td><%= link_to_if_authorized h(media), hash_for_edit_media_path(:id => media.id) %></td>
13
      <td><%=h media.path %></td>
14
      <td><%=h media.operatingsystems.to_sentence %></td>
15
      <td align="right">
16
        <%= display_link_if_authorized "Destroy", hash_for_media_path(:id => media, :auth_action => :destroy), :confirm => "Delete #{media.name}?", :method => :delete %>
17
      </td>
18
    </tr>
19
  <% end %>
20
</table>
21
<%= page_entries_info @medias %>
22
<%= will_paginate     @medias %>
23
<p><%= display_link_if_authorized "New Media", hash_for_new_media_path %></p>
app/views/medias/new.html.erb
1
<% title "New Media" %>
2

  
3
<%= render :partial => 'form' %>
4

  
5
<p><%= link_to "Back to List", medias_path %></p>
app/views/medias/welcome.html.erb
1
<div id="welcome">
2
  <h2>Installation media configuration</h2>
3
  <P>
4
  As your configuration does not appear to have any installation medias declared then you might want to configure some at this point.
5
  </p>
6
  <p>
7
  This information represents the source of one or more operating system's installation files, accessible via the network.
8
  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.
9
  </p>
10
  <p>
11
  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
12
  could create a single media entry describing them all.
13
  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".
14

  
15
  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.
16
  </p>
17
  <p>
18
  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.
19
  </p>
20
  </p>
21
  <%= link_to "New installation media", new_media_path -%>.
22
  </p>
23
</div>
app/views/operatingsystems/_form.html.erb
29 29
  <% end -%>
30 30
  </p>
31 31
  <p>
32
  <% field_set_tag("Installation Medias") do %>
33
    <%= authorized_edit_habtm @operatingsystem, Media %>
32
  <% field_set_tag("Installation Media") do %>
33
    <%= authorized_edit_habtm @operatingsystem, Medium %>
34 34
  <% end -%>
35 35
  </p>
36 36

  
app/views/operatingsystems/welcome.html.erb
2 2
  <h2>Operatingsystem configuration</h2>
3 3
  <P>
4 4
  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
5
  <%= link_to "installation media", medias_path -%> pages then it would be sensible to initialise these first.)
5
  <%= link_to "installation medium", media_path -%> pages then it would be sensible to initialise these first.)
6 6
  </p>
7 7
  <p>
8 8
  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
9
  layout of the operatingsystem's installation media.
9
  layout of the operatingsystem's installation medium.
10 10
  </p>
11 11
  <p>
12 12
  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
13 13
  machines of this type and from where you would be expecting the machine to retrieve its installation files.
14 14
  </p>
15 15
  <p>
16
  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 -%>.
16
  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 -%>.
17 17
  </p>
18 18

  
19 19
  <%= link_to "New operatingsystem", new_operatingsystem_path -%>.
app/views/unattended/kickstart.rhtml
1 1
install
2
<%= @mediapath %>
2
<%= @mediumpath %>
3 3
lang en_US.UTF-8
4 4
<%= "langsupport --default en_US.UTF-8 en_GB.UTF-8 en_US.UTF-8\n" if @osver < 5 -%>
5 5
<%= "mouse generic3usb --device input/mice\n" if @osver == 3 -%>
config/routes.rb
26 26
  map.connect   "/lookup", :controller => "lookup_keys", :action => "q"
27 27
  map.resources :domains, :requirements => {:id => /[^\/]+/}
28 28
  map.resources :operatingsystems, :member => {:bootfiles => :get}
29
  map.resources :medias
29
  map.resources :media
30 30
  map.resources :models
31 31
  map.resources :architectures
32 32
  map.resources :puppetclasses, :member => { :assign => :post }, :collection => {:import_environments => :get}
db/migrate/20090714132448_create_hosts.rb
25 25
    add_column :hosts, :subnet_id, :integer
26 26
    add_column :hosts, :sp_subnet_id, :integer
27 27
    add_column :hosts, :ptable_id, :integer
28
    add_column :hosts, :media_id, :integer
28
    add_column :hosts, :medium_id, :integer
29 29
    add_column :hosts, :build, :boolean, :default => true
30 30
    add_column :hosts, :comment, :text
31 31
    add_column :hosts, :disk, :text
......
39 39
      remove_columns :hosts, :mac, :sp_mac, :sp_ip, :sp_name, :root_pass, :serial,
40 40
        :puppetmaster, :puppet_status, :domain_id, :architecture_id, :operatingsystem_id,
41 41
        :environment_id, :subnet_id, :sp_subnet_id, :ptable_id, :hosttype_id,
42
        :media_id, :build, :comment, :disk, :installed_at
42
        :medium_id, :build, :comment, :disk, :installed_at
43 43
    else
44 44
      drop_table :hosts
45 45
    end
db/migrate/20090717025820_create_media.rb
1
class CreateMedia < ActiveRecord::Migration
2
  def self.up
3
    create_table :media do |t|
4
      t.string :name, :limit => 50, :default => "", :null => false
5
      t.string :path, :limit => 100, :default => "", :null => false
6
      t.references :operatingsystem
7
      t.timestamps
8
    end
9
    Medium.create :name => "CentOS mirror", :path => "http://mirror.averse.net/centos/$major.$minor/os/$arch"
10
    Medium.create :name => "Fedora Mirror", :path => "http://mirror.nus.edu.sg/fedora/releases/$major/Fedora/$arch/os/"
11
    Medium.create :name => "RedHat Beta", :path => "http://ftp.redhat.com/pub/redhat/rhel/beta/$major/$arch/os"
12
    Medium.create :name => "Ubuntu Mirror", :path => "http://sg.archive.ubuntu.com"
13

  
14
  end
15

  
16
  def self.down
17
    drop_table :media
18
  end
19
end
db/migrate/20090717025820_create_medias.rb
1
class CreateMedias < ActiveRecord::Migration
2
  def self.up
3
    create_table :medias do |t|
4
      t.string :name, :limit => 50, :default => "", :null => false
5
      t.string :path, :limit => 100, :default => "", :null => false
6
      t.references :operatingsystem
7
      t.timestamps
8
    end
9
    Media.create :name => "CentOS mirror", :path => "http://mirror.averse.net/centos/$major.$minor/os/$arch"
10
    Media.create :name => "Fedora Mirror", :path => "http://mirror.nus.edu.sg/fedora/releases/$major/Fedora/$arch/os/"
11
    Media.create :name => "RedHat Beta", :path => "http://ftp.redhat.com/pub/redhat/rhel/beta/$major/$arch/os"
12
    Media.create :name => "Ubuntu Mirror", :path => "http://sg.archive.ubuntu.com"
13

  
14
  end
15

  
16
  def self.down
17
    drop_table :medias
18
  end
19
end
db/migrate/20090920043521_add_index_to_host.rb
7 7
    add_index "hosts", :architecture_id, :name => 'host_arch_id_ix'
8 8
    add_index "hosts", :operatingsystem_id, :name => 'host_os_id_ix'
9 9
    add_index "hosts", :environment_id, :name => 'host_env_id_ix'
10
    add_index "hosts", :media_id, :name => 'host_media_id_ix'
10
    add_index "hosts", :medium_id, :name => 'host_medium_id_ix'
11 11
    add_index "hosts", :hostgroup_id, :name => 'host_group_id_ix'
12 12
  end
13 13

  
......
19 19
    remove_index "hosts", :name => 'host_arch_id_ix'
20 20
    remove_index "hosts", :name => 'host_os_id_ix'
21 21
    remove_index "hosts", :name => 'host_env_id_ix'
22
    remove_index "hosts", :name => 'host_media_id_ix'
22
    remove_index "hosts", :name => 'host_medium_id_ix'
23 23
    remove_index "hosts", :name => 'host_group_id_ix'
24 24
  end
25 25
end
db/migrate/20100523141204_create_media_operatingsystems_and_migrate_data.rb
1
class CreateMediaOperatingsystemsAndMigrateData < ActiveRecord::Migration
2
  def self.up
3

  
4
    medium_hash = Hash.new
5
    Medium.all.each do |medium|
6
      unless medium.operatingsystem_id.nil?
7
        if Operatingsystem.exists?(medium.operatingsystem_id)
8
          os = Operatingsystem.find(medium.operatingsystem_id)
9
          medium_hash[os] = medium
10
        else
11
          say "skipped #{medium}"
12
        end
13
      end
14
    end
15

  
16
    create_table :media_operatingsystems , :id => false do |t|
17
      t.references :medium, :null => false
18
      t.references :operatingsystem, :null => false
19
    end
20

  
21
    medium_hash.keys.each { |os| os.media << medium_hash[os] }
22

  
23
    remove_column :media, :operatingsystem_id
24
  end
25

  
26
  def self.down
27
    add_column :media, :operatingsystem_id, :integer
28
    drop_table :media_operatingsystems
29
  end
30
end
db/migrate/20100523141204_create_medias_operatingsystems_and_migrate_data.rb
1
class CreateMediasOperatingsystemsAndMigrateData < ActiveRecord::Migration
2
  def self.up
3

  
4
    media_hash = Hash.new
5
    Media.all.each do |medium|
6
      unless medium.operatingsystem_id.nil?
7
        if Operatingsystem.exists?(medium.operatingsystem_id)
8
          os = Operatingsystem.find(medium.operatingsystem_id)
9
          media_hash[os] = medium
10
        else
11
          say "skipped #{medium}"
12
        end
13
      end
14
    end
15

  
16
    create_table :medias_operatingsystems , :id => false do |t|
17
      t.references :media, :null => false
18
      t.references :operatingsystem, :null => false
19
    end
20

  
21
    media_hash.keys.each { |os| os.medias << media_hash[os] }
22

  
23
    remove_column :medias, :operatingsystem_id
24
  end
25

  
26
  def self.down
27
    add_column :medias, :operatingsystem_id, :integer
28
    drop_table :medias_operatingsystems
29
  end
30
end
db/migrate/20100524080302_migrate_installation_media_uri.rb
1
class MigrateInstallationMediaUri < ActiveRecord::Migration
2
  def self.up
3
    Media.all.each { |medium|
4
      matches = /^([^:]+):(\/.+)/.match(medium.path)
5

  
6
      if matches.size == 3 and ![ 'http', 'https', 'ftp', 'ftps', 'nfs' ].include?(matches[1])
7
        medium.path = 'nfs://' + matches[1] + matches[2]
8
        medium.save
9
      end
10
    }
11
  end
12

  
13
  def self.down
14
  end
15
end
db/migrate/20100524080302_migrate_installation_medium_uri.rb
1
class MigrateInstallationMediumUri < ActiveRecord::Migration
2
  def self.up
3
    Medium.all.each { |medium|
4
      matches = /^([^:]+):(\/.+)/.match(medium.path)
5

  
6
      if matches.size == 3 and ![ 'http', 'https', 'ftp', 'ftps', 'nfs' ].include?(matches[1])
7
        medium.path = 'nfs://' + matches[1] + matches[2]
8
        medium.save
9
      end
10
    }
11
  end
12

  
13
  def self.down
14
  end
15
end
db/migrate/20101118130026_correct_media.rb
1
class CorrectMedia < ActiveRecord::Migration
2
  def self.up
3
    if table_exists? :medias
4
      if table_exists? :medias_operatingsystems
5
        rename_column :medias_operatingsystems, :media_id, :medium_id
6
        rename_table :medias_operatingsystems, :media_operatingsystems
7
      end
8

  
9
      change_table :hosts do |t|
10
        t.remove_index :name => :host_media_id_ix
11
        t.rename :media_id, :medium_id
12
        t.index :medium_id, :name => :host_medium_id_ix
13
      end
14

  
15
      rename_table :medias, :media
16
    end
17
  end
18

  
19
  def self.down
20
    if table_exists? :media
21
      if table_exists? :media_operatingsystems
22
        rename_column :medias_operatingsystems, :medium_id, :media_id
23
        rename_table :media_operatingsystems, :medias_operatingsystems
24
      end
25

  
26
      change_table :hosts do |t|
27
        t.remove_index :name => :host_medium_id_ix
28
        t.rename :medium_id, :media_id
29
        t.index :media_id, :name => :host_media_id_ix
30
      end
31

  
32
      rename_table :media, :medias
33
    end
34
  end
35
end
lib/access_permissions.rb
62 62
    map.permission :destroy_hosts, {:hosts => [:destroy, :multiple_actions, :reset_multiple, :multiple_destroy, :submit_multiple_destroy]}
63 63
  end
64 64

  
65
  map.security_block :medias do |map|
66
    map.permission :view_medias,    {:medias => [:index, :show]}
67
    map.permission :create_medias,  {:medias => [:new, :create]}
68
    map.permission :edit_medias,    {:medias => [:edit, :update]}
69
    map.permission :destroy_medias, {:medias => [:destroy]}
65
  map.security_block :media do |map|
66
    map.permission :view_media,    {:media => [:index, :show]}
67
    map.permission :create_media,  {:media => [:new, :create]}
68
    map.permission :edit_media,    {:media => [:edit, :update]}
69
    map.permission :destroy_media, {:media => [:destroy]}
70 70
  end
71 71

  
72 72
  map.security_block :models do |map|
lib/foreman/default_data/loader.rb
62 62
                :view_hostgroups,
63 63
                :view_domains,
64 64
                :view_operatingsystems,
65
                :view_medias,
65
                :view_media,
66 66
                :view_models,
67 67
                :view_environments,
68 68
                :view_architectures,
......
101 101
                :create_hosts,
102 102
                :edit_hosts,
103 103
                :destroy_hosts,
104
                :view_medias,
105
                :create_medias,
106
                :edit_medias,
107
                :destroy_medias,
104
                :view_media,
105
                :create_media,
106
                :edit_media,
107
                :destroy_media,
108 108
                :view_models,
109 109
                :view_operatingsystems,
110 110
                :view_ptables,
......
127 127
                :view_hostgroups,
128 128
                :view_domains,
129 129
                :view_operatingsystems,
130
                :view_medias,
130
                :view_media,
131 131
                :view_models,
132 132
                :view_environments,
133 133
                :view_architectures,
test/fixtures/hosts.yml
19 19
  operatingsystem: one
20 20
  ptable: one
21 21
  domain: yourdomain
22
  media: one
22
  medium: one
23 23

  
24 24
otherfullhost:
25 25
  name: otherfullhost
......
58 58
  build: true
59 59
  ptable: one
60 60
  domain: yourdomain
61
  media: one
61
  medium: one
62 62
  operatingsystem: redhat
63 63

  
64 64
ubuntu:
......
71 71
  build: true
72 72
  ptable: ubuntu
73 73
  domain: yourdomain
74
  media: ubuntu
74
  medium: ubuntu
test/fixtures/media.yml
1
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2

  
3
one:
4
  name: CentOS 5.4
5
  path: http://mirror.averse.net/centos/6.0/os/$arch
6

  
7
ubuntu:
8
  name: Ubuntu Mirror
9
  path: http://sg.archive.ubuntu.com
10

  
11
unused:
12
  name: unused
13
  path: http://nothing.intersting.com
test/fixtures/medias.yml
1
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2

  
3
one:
4
  name: CentOS 5.4
5
  path: http://mirror.averse.net/centos/6.0/os/$arch
6

  
7
ubuntu:
8
  name: Ubuntu Mirror
9
  path: http://sg.archive.ubuntu.com
10

  
11
unused:
12
  name: unused
13
  path: http://nothing.intersting.com
test/fixtures/roles.yml
38 38
    - :create_hosts
39 39
    - :edit_hosts
40 40
    - :destroy_hosts
41
    - :view_medias
42
    - :create_medias
43
    - :edit_medias
44
    - :destroy_medias
41
    - :view_media
42
    - :create_media
43
    - :edit_media
44
    - :destroy_media
45 45
    - :view_models
46 46
    - :create_models
47 47
    - :edit_models
......
119 119
    - :view_globals
120 120
    - :view_hostgroups
121 121
    - :view_hosts
122
    - :view_medias
122
    - :view_media
123 123
    - :view_models
124 124
    - :view_operatingsystems
125 125
    - :view_ptables
......
141 141
    - :view_hostgroups
142 142
    - :view_domains
143 143
    - :view_oses
144
    - :view_medias
144
    - :view_media
145 145
    - :view_models
146 146
    - :view_environments
147 147
    - :view_architectures
test/functional/media_controller_test.rb
1
require 'test_helper'
2

  
3
class MediaControllerTest < ActionController::TestCase
4
  def test_index
5
    get :index, {}, set_session_user
6
    assert_template 'index'
7
  end
8

  
9
  def test_new
10
    get :new, {}, set_session_user
11
    assert_template 'new'
12
  end
13

  
14
  def test_create_invalid
15
    Medium.any_instance.stubs(:valid?).returns(false)
16
    post :create, {}, set_session_user
17
    assert_template 'new'
18
  end
19

  
20
  def test_create_valid
21
    Medium.any_instance.stubs(:valid?).returns(true)
22
    post :create, {}, set_session_user
23
    assert_redirected_to media_url
24
  end
25

  
26
  def test_edit
27
    get :edit, {:id => Medium.first}, set_session_user
28
    assert_template 'edit'
29
  end
30

  
31
  def test_update_invalid
32
    Medium.any_instance.stubs(:valid?).returns(false)
33
    put :update, {:id => Medium.first}, set_session_user
34
    assert_template 'edit'
35
  end
36

  
37
  def test_update_valid
38
    Medium.any_instance.stubs(:valid?).returns(true)
39
    put :update, {:id => Medium.first}, set_session_user
40
    assert_redirected_to media_url
41
  end
42

  
43
  def test_destroy
44
    medium = media(:unused)
45
    delete :destroy, {:id => medium}, set_session_user
46
    assert_redirected_to media_url
47
    assert !Medium.exists?(medium.id)
48
  end
49

  
50
  def setup_user
51
    @request.session[:user] = users(:one).id
52
    users(:one).roles       = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
53
  end
54

  
55
  test 'user with viewer rights should fail to edit a medium' do
56
    setup_user
57
    get :edit, {:id => Medium.first.id}
58
    assert @response.status == '403 Forbidden'
59
  end
60

  
61
  test 'user with viewer rights should succeed in viewing media' do
62
    setup_user
63
    get :index
64
    assert_response :success
65
  end
66
end
test/functional/medias_controller_test.rb
1
require 'test_helper'
2

  
3
class MediasControllerTest < ActionController::TestCase
4
  def test_index
5
    get :index, {}, set_session_user
6
    assert_template 'index'
7
  end
8

  
9
  def test_new
10
    get :new, {}, set_session_user
11
    assert_template 'new'
12
  end
13

  
14
  def test_create_invalid
15
    Media.any_instance.stubs(:valid?).returns(false)
16
    post :create, {}, set_session_user
17
    assert_template 'new'
18
  end
19

  
20
  def test_create_valid
21
    Media.any_instance.stubs(:valid?).returns(true)
22
    post :create, {}, set_session_user
23
    assert_redirected_to medias_url
24
  end
25

  
26
  def test_edit
27
    get :edit, {:id => Media.first}, set_session_user
28
    assert_template 'edit'
29
  end
30

  
31
  def test_update_invalid
32
    Media.any_instance.stubs(:valid?).returns(false)
33
    put :update, {:id => Media.first}, set_session_user
34
    assert_template 'edit'
35
  end
36

  
37
  def test_update_valid
38
    Media.any_instance.stubs(:valid?).returns(true)
39
    put :update, {:id => Media.first}, set_session_user
40
    assert_redirected_to medias_url
41
  end
42

  
43
  def test_destroy
44
    media = medias(:unused)
45
    delete :destroy, {:id => media}, set_session_user
46
    assert_redirected_to medias_url
47
    assert !Media.exists?(media.id)
48
  end
49

  
50
  def setup_user
51
    @request.session[:user] = users(:one).id
52
    users(:one).roles       = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
53
  end
54

  
55
  test 'user with viewer rights should fail to edit a media' do
56
    setup_user
57
    get :edit, {:id => Media.first.id}
58
    assert @response.status == '403 Forbidden'
59
  end
60

  
61
  test 'user with viewer rights should succeed in viewing medias' do
62
    setup_user
63
    get :index
64
    assert_response :success
65
  end
66
end
test/functional/roles_controller_test.rb
120 120
    assert Role.find(1).permissions.empty?
121 121
  end
122 122

  
123
  viewable = %w{Architecture Audit AuthSourceLdap Dashboard Domain Environment LookupKey FactValue CommonParameter Hostgroup Host Media Model Operatingsystem Ptable Puppetclass Report Setting Statistic Usergroup User}
124
  editable = %w{Architecture Audit AuthSourceLdap           Domain Environment LookupKey           CommonParameter Hostgroup Host Media Model Operatingsystem Ptable Puppetclass Report                   Usergroup User}
123
  viewable = %w{Architecture Audit AuthSourceLdap Dashboard Domain Environment LookupKey FactValue CommonParameter Hostgroup Host Medium Model Operatingsystem Ptable Puppetclass Report Setting Statistic Usergroup User}
124
  editable = %w{Architecture Audit AuthSourceLdap           Domain Environment LookupKey           CommonParameter Hostgroup Host Medium Model Operatingsystem Ptable Puppetclass Report                   Usergroup User}
125 125
end
test/unit/helpers/media_helper_test.rb
1
require 'test_helper'
2

  
3
class MediaHelperTest < ActionView::TestCase
4
end
test/unit/helpers/medium_helper_test.rb
1
require 'test_helper'
2

  
3
class MediumHelperTest < ActionView::TestCase
4
end
test/unit/media_test.rb
1
require 'test_helper'
2

  
3
class MediaTest < ActiveSupport::TestCase
4
  setup do
5
    User.current = User.find_by_login "admin"
6
  end
7

  
8
  test "name can't be blank" do
9
    media = Media.new :name => "   ", :path => "http://www.google.com"
10
    assert media.name.strip.empty?
11
    assert !media.save
12
  end
13

  
14
  test "name can't contain white spaces" do
15
    media = Media.new :name => "   Archlinux mirror   thing   ", :path => "http://www.google.com"
16
    assert !media.name.strip.squeeze(" ").empty?
17
    assert !media.save
18

  
19
    media.name.strip!.squeeze!(" ")
20
    assert media.save!
21
  end
22

  
23
  test "name must be unique" do
24
    media = Media.new :name => "Archlinux mirror", :path => "http://www.google.com"
25
    assert media.save!
26

  
27
    other_media = Media.new :name => "Archlinux mirror", :path => "http://www.youtube.com"
28
    assert !other_media.save
29
  end
30

  
31
  test "path can't be blank" do
32
    media = Media.new :name => "Archlinux mirror", :path => "  "
33
    assert media.path.strip.empty?
34
    assert !media.save
35
  end
36

  
37
  test "path must be unique" do
38
    media = Media.new :name => "Archlinux mirror", :path => "http://www.google.com"
39
    assert media.save!
40

  
41
    other_media = Media.new :name => "Ubuntu mirror", :path => "http://www.google.com"
42
    assert !other_media.save
43
  end
44

  
45
  test "should not destroy while using" do
46
    media = Media.new :name => "Archlinux mirror", :path => "http://www.google.com"
47
    assert media.save!
48

  
49
    host = Host.new :name => "myfullhost", :mac => "aabbecddeeff", :ip => "123.05.02.03",
50
      :domain => Domain.find_or_create_by_name("company.com"), :operatingsystem => Operatingsystem.first,
51
      :architecture => Architecture.first, :environment => Environment.first, :disk => "empty partition",
52
      :ptable => Ptable.first
53
    assert host.save!
54

  
55
    media.hosts << host
56

  
57
    assert !media.destroy
58
  end
59

  
60
  def setup_user operation
61
    @one = users(:one)
62
    as_admin do
63
      role = Role.find_or_create_by_name :name => "#{operation}_medias"
64
      role.permissions = ["#{operation}_medias".to_sym]
65
      @one.roles = [role]
66
      @one.save!
67
    end
68
    User.current = @one
69
  end
70

  
71
  test "user with create permissions should be able to create" do
72
    setup_user "create"
73
    record =  Media.create :name => "dummy", :path => "http://hello"
74
    assert record.valid?
75
    assert !record.new_record?
76
  end
77

  
78
  test "user with view permissions should not be able to create" do
79
    setup_user "view"
80
    record =  Media.create :name => "dummy", :path => "http://hello"
81
    assert record.valid?
82
    assert record.new_record?
83
  end
84

  
85
  test "user with destroy permissions should be able to destroy" do
86
    setup_user "destroy"
87
    record =  Media.first
88
    as_admin do
89
      record.hosts = []
90
    end
91
    assert record.destroy
92
    assert record.frozen?
93
  end
94

  
95
  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.