Class: PreviewAdd::Vhost
- Inherits:
-
Object
- Object
- PreviewAdd::Vhost
- Includes:
- FileUtils
- Defined in:
- lib/preview_add/vhost.rb
Instance Attribute Summary collapse
-
#vhost ⇒ Object
Returns the value of attribute vhost.
Instance Method Summary collapse
- #auth_user_file ⇒ Object
- #create! ⇒ Object
- #generate_vhost ⇒ Object
- #htpasswd(username, password) ⇒ Object
-
#initialize(config, names) ⇒ Vhost
constructor
A new instance of Vhost.
Constructor Details
#initialize(config, names) ⇒ Vhost
Returns a new instance of Vhost.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/preview_add/vhost.rb', line 11 def initialize(config, names) @host = config[:host] @config = config @names = names @auth_name = 'TODO' @auth_user_file = auth_user_file @document_root = File.join(@config[:locations][:vhosts], @names.document_root) @server_name = "#{@names.server_name}.#{@config[:preview_domain]}" end |
Instance Attribute Details
#vhost ⇒ Object
Returns the value of attribute vhost.
9 10 11 |
# File 'lib/preview_add/vhost.rb', line 9 def vhost @vhost end |
Instance Method Details
#auth_user_file ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/preview_add/vhost.rb', line 23 def auth_user_file htpasswd_dir = @config[:locations][:htpasswd] extension = '.htpasswd' auth_user_file = File.join(htpasswd_dir, "#{@names.domain + extension}") unless @config[:htpasswd].nil? if @config[:htpasswd].end_with?(extension) auth_user_file = File.join(htpasswd_dir, "#{@config[:htpasswd]}") else auth_user_file = File.join(htpasswd_dir, "#{@config[:htpasswd] + extension}") end end auth_user_file end |
#create! ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/preview_add/vhost.rb', line 49 def create! # Create the document root unless File.exist? @document_root mkdir(@document_root) end # Create the vhost File.open(File.join(@config[:locations][:sites_available], @names.document_root), 'w') { |f| f.write(generate_vhost) } # Create the htpasswd file if it doesn't exist unless File.exists? @auth_user_file username = @config[:username] || @names.base_name password = @config[:password] || nil if password.nil? puts "No htpasswd file exists." puts "Username: #{username}" print "Password: " password = gets.to_s.strip end File.open(@auth_user_file, 'w') { |f| f.write(htpasswd(username, password)) } end unless @config[:mode] == 'test' # Enable the site `a2ensite #{@names.document_root}` # reload apache `service apache2 reload` end end |
#generate_vhost ⇒ Object
39 40 41 42 43 |
# File 'lib/preview_add/vhost.rb', line 39 def generate_vhost template = File.join(File.dirname(__FILE__), 'vhost.erb') vhost = ERB.new(File.read(template)) vhost.result(binding) end |
#htpasswd(username, password) ⇒ Object
45 46 47 |
# File 'lib/preview_add/vhost.rb', line 45 def htpasswd(username, password) `htpasswd -bn #{username} #{password}` end |