[PATCH/foreman 1/1] fixes -#1313 ensure all host names are lowercased - second edition

Signed-off-by: Florian Koch <florian.koch1981@googlemail.com>

··· --- app/controllers/hosts_controller.rb | 1 + app/models/host.rb | 9 +++++++++ ...227095806_ensure_all_hostnames_are_lowercase.rb | 10 ++++++++++ 3 files changed, 20 insertions(+), 0 deletions(-) create mode 100644 db/migrate/20111227095806_ensure_all_hostnames_are_lowercase.rb

diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb
index 9e9d310…8e1c1ab 100644
— a/app/controllers/hosts_controller.rb
+++ b/app/controllers/hosts_controller.rb
@@ -440,6 +440,7 @@ class HostsController < ApplicationController

def find_by_name
# find host first, if we fail, do nothing

  • params[:id].downcase! if params[:id].present?
    super
    return false unless @host
    deny_access and return unless User.current.admin? or Host.my_hosts.include?(@host)
    diff --git a/app/models/host.rb b/app/models/host.rb
    index 64b1a12…13b3076 100644
    — a/app/models/host.rb
    +++ b/app/models/host.rb
    @@ -115,6 +115,11 @@ class Host < Puppet::Rails::Host
    acts_as_audited :except => [:last_report, :puppet_status, :last_compile]
    has_associated_audits

  • def is_name_downcased

  •     return unless name.present?
    
  •     errors.add(:name, "must be downcase") unless name == name.downcase
    
  • end

  • some shortcuts

    alias_attribute :os, :operatingsystem
    alias_attribute :arch, :architecture
    @@ -123,6 +128,8 @@ class Host < Puppet::Rails::Host

    validates_uniqueness_of :name
    validates_presence_of :name, :environment_id

  • validate :is_name_downcased

  • if SETTINGS[:unattended]

    handles all orchestration of smart proxies.

    include Foreman::Renderer
    @@ -150,6 +157,8 @@ class Host < Puppet::Rails::Host
    before_validation :set_hostgroup_defaults, :set_ip_address, :set_default_user, :normalize_addresses, :normalize_hostname
    after_validation :ensure_assoications

  • def set_default_user
    self.owner ||= User.current
    end
    diff --git a/db/migrate/20111227095806_ensure_all_hostnames_are_lowercase.rb b/db/migrate/20111227095806_ensure_all_hostnames_are_lowercase.rb
    new file mode 100644
    index 0000000…944e272
    — /dev/null
    +++ b/db/migrate/20111227095806_ensure_all_hostnames_are_lowercase.rb
    @@ -0,0 +1,10 @@
    +class EnsureAllHostnamesAreLowercase < ActiveRecord::Migration

  • def self.up

  • execute “UPDATE hosts SET name=LOWER(name)”

  • end

  • def self.down

  • raise ActiveRecord::IrreversibleMigration

  • end
    +end

    1.7.6.4

So, I've lost track, is this safe to apply or not? you mentioned this might
not work on all DB types?

Thanks,
Ohad

··· On Thu, Dec 29, 2011 at 2:33 PM, Florian Koch < florian.koch1981@googlemail.com> wrote:

Signed-off-by: Florian Koch florian.koch1981@googlemail.com

app/controllers/hosts_controller.rb | 1 +
app/models/host.rb | 9 +++++++++
…227095806_ensure_all_hostnames_are_lowercase.rb | 10 ++++++++++
3 files changed, 20 insertions(+), 0 deletions(-)
create mode 100644
db/migrate/20111227095806_ensure_all_hostnames_are_lowercase.rb

diff --git a/app/controllers/hosts_controller.rb
b/app/controllers/hosts_controller.rb
index 9e9d310…8e1c1ab 100644
— a/app/controllers/hosts_controller.rb
+++ b/app/controllers/hosts_controller.rb
@@ -440,6 +440,7 @@ class HostsController < ApplicationController

def find_by_name
# find host first, if we fail, do nothing

  • params[:id].downcase! if params[:id].present?
    super
    return false unless @host
    deny_access and return unless User.current.admin? or
    Host.my_hosts.include?(@host)
    diff --git a/app/models/host.rb b/app/models/host.rb
    index 64b1a12…13b3076 100644
    — a/app/models/host.rb
    +++ b/app/models/host.rb
    @@ -115,6 +115,11 @@ class Host < Puppet::Rails::Host
    acts_as_audited :except => [:last_report, :puppet_status, :last_compile]
    has_associated_audits

  • def is_name_downcased

  •     return unless name.present?
    
  •     errors.add(:name, "must be downcase") unless name ==
    

name.downcase

  • end

  • some shortcuts

    alias_attribute :os, :operatingsystem
    alias_attribute :arch, :architecture
    @@ -123,6 +128,8 @@ class Host < Puppet::Rails::Host

    validates_uniqueness_of :name
    validates_presence_of :name, :environment_id

  • validate :is_name_downcased

  • if SETTINGS[:unattended]

    handles all orchestration of smart proxies.

    include Foreman::Renderer
    @@ -150,6 +157,8 @@ class Host < Puppet::Rails::Host
    before_validation :set_hostgroup_defaults, :set_ip_address,
    :set_default_user, :normalize_addresses, :normalize_hostname
    after_validation :ensure_assoications

  • def set_default_user
    self.owner ||= User.current
    end
    diff --git
    a/db/migrate/20111227095806_ensure_all_hostnames_are_lowercase.rb
    b/db/migrate/20111227095806_ensure_all_hostnames_are_lowercase.rb
    new file mode 100644
    index 0000000…944e272
    — /dev/null
    +++ b/db/migrate/20111227095806_ensure_all_hostnames_are_lowercase.rb
    @@ -0,0 +1,10 @@
    +class EnsureAllHostnamesAreLowercase < ActiveRecord::Migration

  • def self.up

  •    execute  "UPDATE hosts SET name=LOWER(name)"
    
  • end

  • def self.down

  •   raise ActiveRecord::IrreversibleMigration
    
  • end
    +end

    1.7.6.4

yes this is safe to apply, the migration works on all sql compatible
databases

··· On Jan 8, 4:09 pm, Ohad Levy wrote: > On Thu, Dec 29, 2011 at 2:33 PM, Florian Koch < > > > > > > > > > > florian.koch1...@googlemail.com> wrote: > > > Signed-off-by: Florian Koch > > --- > > app/controllers/hosts_controller.rb | 1 + > > app/models/host.rb | 9 +++++++++ > > ...227095806_ensure_all_hostnames_are_lowercase.rb | 10 ++++++++++ > > 3 files changed, 20 insertions(+), 0 deletions(-) > > create mode 100644 > > db/migrate/20111227095806_ensure_all_hostnames_are_lowercase.rb > > > diff --git a/app/controllers/hosts_controller.rb > > b/app/controllers/hosts_controller.rb > > index 9e9d310..8e1c1ab 100644 > > --- a/app/controllers/hosts_controller.rb > > +++ b/app/controllers/hosts_controller.rb > > @@ -440,6 +440,7 @@ class HostsController < ApplicationController > > > def find_by_name > > # find host first, if we fail, do nothing > > + params[:id].downcase! if params[:id].present? > > super > > return false unless @host > > deny_access and return unless User.current.admin? or > > Host.my_hosts.include?(@host) > > diff --git a/app/models/host.rb b/app/models/host.rb > > index 64b1a12..13b3076 100644 > > --- a/app/models/host.rb > > +++ b/app/models/host.rb > > @@ -115,6 +115,11 @@ class Host < Puppet::Rails::Host > > acts_as_audited :except => [:last_report, :puppet_status, :last_compile] > > has_associated_audits > > > + def is_name_downcased > > + return unless name.present? > > + errors.add(:name, "must be downcase") unless name == > > name.downcase > > + end > > + > > # some shortcuts > > alias_attribute :os, :operatingsystem > > alias_attribute :arch, :architecture > > @@ -123,6 +128,8 @@ class Host < Puppet::Rails::Host > > > validates_uniqueness_of :name > > validates_presence_of :name, :environment_id > > + validate :is_name_downcased > > + > > if SETTINGS[:unattended] > > # handles all orchestration of smart proxies. > > include Foreman::Renderer > > @@ -150,6 +157,8 @@ class Host < Puppet::Rails::Host > > before_validation :set_hostgroup_defaults, :set_ip_address, > > :set_default_user, :normalize_addresses, :normalize_hostname > > after_validation :ensure_assoications > > > + > > + > > def set_default_user > > self.owner ||= User.current > > end > > diff --git > > a/db/migrate/20111227095806_ensure_all_hostnames_are_lowercase.rb > > b/db/migrate/20111227095806_ensure_all_hostnames_are_lowercase.rb > > new file mode 100644 > > index 0000000..944e272 > > --- /dev/null > > +++ b/db/migrate/20111227095806_ensure_all_hostnames_are_lowercase.rb > > @@ -0,0 +1,10 @@ > > +class EnsureAllHostnamesAreLowercase < ActiveRecord::Migration > > + def self.up > > + execute "UPDATE hosts SET name=LOWER(name)" > > + > > + end > > + > > + def self.down > > + raise ActiveRecord::IrreversibleMigration > > + end > > +end > > -- > > 1.7.6.4 > > So, I've lost track, is this safe to apply or not? you mentioned this might > not work on all DB types? > > Thanks, > Ohad