PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/generate-vhosts.rb

https://github.com/rafaelfranca/generate-vhosts
Ruby | 139 lines | 92 code | 32 blank | 15 comment | 1 complexity | 0f39b07f9dd21e54ab7aeace75aaf0ed MD5 | raw file
  1. #!/usr/bin/env ruby1.8
  2. begin
  3. require 'rubygems'
  4. rescue LoadError
  5. end
  6. require 'yaml'
  7. CONFIG_PATH = '/etc/apache2/sites-available'
  8. # No syntax checking whatsoever on the yaml files. You're on your own.
  9. class Vhost
  10. def initialize(args)
  11. # This might be dangerous? I'm pretty sure we can trust whoever's
  12. # writing our vhost definitions :)
  13. args.each do |k,v|
  14. instance_variable_set "@#{k}", v
  15. end
  16. @type ||= 'standard'
  17. # We need to wrap @location in slashes, but if it's root, that gives us
  18. # // or ///, so we collapse multiple sequential slashes to a single /.
  19. @location = "/#{@location}/"
  20. @location.gsub!(/\/+/,'/')
  21. @types = YAML.load(open("types.yml"))
  22. end
  23. def to_s
  24. if @types.has_key? @type
  25. eval("return \"#{@types[@type]}\"")
  26. end
  27. end
  28. end
  29. output = ""
  30. # Default/catchall vhost.
  31. output << <<END
  32. <VirtualHost *:80>
  33. ServerAdmin webmaster@localhost
  34. DocumentRoot /var/www
  35. <Directory />
  36. Options FollowSymLinks
  37. AllowOverride None
  38. </Directory>
  39. <Directory /var/www/>
  40. Options Indexes FollowSymLinks MultiViews
  41. AllowOverride None
  42. Order allow,deny
  43. allow from all
  44. </Directory>
  45. ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
  46. <Directory "/usr/lib/cgi-bin">
  47. AllowOverride None
  48. Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
  49. Order allow,deny
  50. Allow from all
  51. </Directory>
  52. ErrorLog /var/log/apache2/error.log
  53. # Possible values include: debug, info, notice, warn, error, crit,
  54. # alert, emerg.
  55. LogLevel warn
  56. CustomLog /var/log/apache2/access.log combined
  57. Alias /doc/ "/usr/share/doc/"
  58. <Directory "/usr/share/doc/">
  59. Options Indexes MultiViews FollowSymLinks
  60. AllowOverride None
  61. Order deny,allow
  62. Deny from all
  63. Allow from 127.0.0.0/255.0.0.0 ::1/128
  64. </Directory>
  65. </VirtualHost>
  66. END
  67. hosts = YAML.load(open("vhosts.yml"))
  68. begin
  69. hosts.each do |k,v|
  70. vhost = Vhost.new(v.merge({'domain' => k}))
  71. output << vhost.to_s
  72. end
  73. rescue
  74. exit 1 # Just return an error. We don't care what it is.
  75. # The initscript will smack somebody on the head and fail.
  76. end
  77. # Write the output file
  78. File.open("#{CONFIG_PATH}/default",'w') do |f|
  79. f.puts output
  80. end
  81. #host
  82. CONFIG_HOSTS_PATH = '/etc'
  83. output = ""
  84. # Default/catchall vhost.
  85. output << <<END
  86. 192.168.1.106 firestorm # Added by NetworkManager
  87. 127.0.0.1 localhost.localdomain localhost
  88. ::1 firestorm localhost6.localdomain6 localhost6
  89. END
  90. hosts.each do |k,v|
  91. output << '127.0.0.1 '+k+'
  92. '
  93. end
  94. output << <<END
  95. # The following lines are desirable for IPv6 capable hosts
  96. ::1 localhost ip6-localhost ip6-loopback
  97. fe00::0 ip6-localnet
  98. ff00::0 ip6-mcastprefix
  99. ff02::1 ip6-allnodes
  100. ff02::2 ip6-allrouters
  101. ff02::3 ip6-allhosts
  102. END
  103. # Write the output file
  104. File.open("#{CONFIG_HOSTS_PATH}/hosts",'w') do |f|
  105. f.puts output
  106. end
  107. exec '/etc/init.d/apache2 restart'