标签归档:underscore

Host save fails if name contains underscore with Failed to save: Name is invalid

当使用Foreman时,在WEB上对主机名包含下划线的主机进行保存操作时,会提示“Name is invalid

或者在/var/log/foreman/production.log日志中会出现类似错误:

| Started POST “/api/hosts/facts” for 124.202.155.164 at 2016-05-12 16:23:06 +0800
2016-05-12T16:23:06 [app] [I] Processing by Api::V2::HostsController#facts as JSON
2016-05-12T16:23:06 [app] [I]   Parameters: {“facts”=>”[FILTERED]”, “certname”=>”mx136v_bak”, “name”=>”mx136v_bak”, “apiv”=>”v2”, :host=>{“name”=>”mx136v_bak”, “certname”=>”mx136v_bak”}}
2016-05-12T16:23:06 [app] [I] Import facts for ‘mx136v_bak’ completed. Added: 0, Updated: 5, Deleted 0 facts
2016-05-12T16:23:06 [app] [W] Action failed
| ActiveRecord::RecordInvalid: Validation failed: Name is invalid
| /opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/validations.rb:56:in `save!’
| /opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/attribute_methods/dirty.rb:33:in `save!’
| /opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/transactions.rb:246:in `block in save!’
| /opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status’

| Started POST “/api/hosts/facts” for 124.202.155.164 at 2016-05-12 03:47:06 +0800
2016-05-12T03:47:06 [app] [I] Processing by Api::V2::HostsController#facts as JSON
2016-05-12T03:47:06 [app] [I]   Parameters: {“facts”=>”[FILTERED]”, “certname”=>”s108k_bak.domain.com”, “name”=>”s108k_bak.domain.com”, “apiv”=>”v2”, :host=>{“name”=>”s108k_bak.domain.com”, “certname”=>”s108k_bak.domain.com”}}
2016-05-12T03:47:06 [app] [W] Action failed
| ActiveRecord::RecordInvalid: Validation failed: Name is invalid
| /opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/validations.rb:56:in `save!’
| /opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/attribute_methods/dirty.rb:33:in `save!’
| /opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/transactions.rb:246:in `block in save!’
| /opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/transactions.rb:295:in `block in with_transaction_r
eturning_status’
| /opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb
:192:in `transaction’
| /opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/transactions.rb:208:in `transaction’
| /opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/transactions.rb:293:in `with_transaction_returning_
status’
| /opt/rh/ruby193/root/usr/share/gems/gems/activerecord-3.2.8/lib/active_record/transactions.rb:246:in `save!’

临时解决办法:

修改 /usr/share/foreman/lib/net/validations.rb文件中的 “HOST_REGEXP”
HOST_REGEXP = /\A(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\z/更改为 HOST_REGEXP = /\A(([a-z0-9]|[a-z0-9][a-z0-9\-_]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\z/