PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/network_setting.rb

https://github.com/amooma/GS4
Ruby | 141 lines | 102 code | 28 blank | 11 comment | 15 complexity | ee0d47c918763e957a0d374b79254e14 MD5 | raw file
  1. class NetworkSetting < ActiveRecord::Base
  2. #OPTIMIZE Make netmask an integer instead of a string and use the prefix length instead of a dotted quad. Easier to handle (validate/store/convert) and works for both IPv4 and IPv6.
  3. before_validation( :on => :create ) {
  4. if NetworkSetting.count > 0
  5. errors.add( :base, I18n.t(:network_settings_already_configured))
  6. end
  7. }
  8. before_validation( :on => :update ) {
  9. if ip_address != ip_address_was
  10. errors.add( :ip_address, I18n.t(:can_not_be_changed))
  11. end
  12. if dhcp_client != dhcp_client_was
  13. errors.add( :dhcp_client, I18n.t(:can_not_be_changed))
  14. end
  15. }
  16. validates_inclusion_of [
  17. :ip_address,
  18. :netmask,
  19. :gateway,
  20. :dhcp_range_start,
  21. :dhcp_range_end,
  22. :name_server,
  23. ], :in => [ nil, '' ] , :if => Proc.new { |me| me.dhcp_client },
  24. :message => "#{ I18n.t(:must_not_be_set) } (DHCP client)"
  25. validates_inclusion_of [
  26. :start_dhcp_server,
  27. ], :in => [ false ] , :if => Proc.new { |me| me.dhcp_client },
  28. :message => "#{ I18n.t(:must_not_be_set) } (DHCP client)"
  29. validate_hostname_or_ip [
  30. :ip_address,
  31. ], :allow_blank => false , :if => Proc.new { |me| ! me.dhcp_client }
  32. validate_hostname_or_ip [
  33. :gateway,
  34. :name_server,
  35. ], :allow_blank => true , :if => Proc.new { |me| ! me.dhcp_client }
  36. validate_netmask [
  37. :netmask,
  38. ], :allow_blank => false , :if => Proc.new { |me| ! me.dhcp_client }
  39. validate_hostname_or_ip [
  40. :dhcp_range_start,
  41. :dhcp_range_end,
  42. ], :allow_blank => false , :if => Proc.new { |me| me.start_dhcp_server }
  43. validates_inclusion_of :interface, :in => ['eth0'] #OPTIMIZE Interface name is system-specific.
  44. network_interfaces = "
  45. auto lo
  46. iface lo inet loopback
  47. "
  48. after_save {
  49. if Configuration.get( :is_appliance, false, Configuration::Boolean ); (
  50. # is_appliance => This is a Knoppix system.
  51. if self.dhcp_client == false
  52. network_interfaces_write = "
  53. #{network_interfaces}
  54. auto #{interface}
  55. iface #{interface} inet static
  56. address #{ip_address}
  57. netmask #{netmask}
  58. #{"gateway #{gateway}" if ! gateway.blank?}
  59. #{"gateway #{ip_address}" if gateway.blank?}
  60. "
  61. else
  62. network_interfaces_write = "
  63. #{network_interfaces}
  64. auto #{interface}
  65. iface #{interface} inet dhcp
  66. "
  67. end
  68. if ! name_server.empty? && ! dhcp_client
  69. resolv_conf= "nameserver #{name_server}"
  70. write_files('/tmp/resolv', resolv_conf)
  71. else
  72. resolv_conf = ""
  73. end
  74. if start_dhcp_server
  75. dnsmasq_conf ="
  76. dhcp-range=#{dhcp_range_start},#{dhcp_range_end},12h
  77. dhcp-option=66,https://#{ip_address}:443
  78. dhcp-option=67,settings-{mac}
  79. "
  80. else
  81. dnsmasq_conf = ""
  82. end
  83. file_path_etc = "/tmp/"
  84. file_path_network = "/tmp/"
  85. if ::Rails.env.to_s == "production"
  86. file_path_etc = "/etc/"
  87. file_path_network = "/etc/network/"
  88. end
  89. write_files("#{file_path_etc}dnsmasq.conf", dnsmasq_conf)
  90. write_files("#{file_path_etc}resolv.conf", resolv_conf)
  91. write_files("#{file_path_network}interfaces", network_interfaces_write)
  92. )end
  93. }
  94. after_save( :on => :create ) {
  95. if ! dhcp_client
  96. servers = [ 'SipServer', 'SipProxy', 'VoicemailServer' ]
  97. servers.each do |server|
  98. if server.constantize.where(:is_local => true).empty?
  99. server.constantize.create(:host => ip_address, :is_local => true)
  100. end
  101. end
  102. end
  103. }
  104. private
  105. def write_files( filename, output )
  106. #begin
  107. File.open(filename, 'w') {|f| f.write(output) }
  108. #rescue => e
  109. # #OPTIMIZE We are called from after_save, so we can't add errors.
  110. # errors.add( :base, "Failed to write #{filename.inspect}." )
  111. #end
  112. end
  113. end