Class: ProvisionVhost

Inherits:
Tap::Task
  • Object
show all
Defined in:
lib/provision_vhost.rb

Overview

ProvisionVhost Documentation

Constant Summary collapse

IP_REGEX =

not perfect, but works

/\b(?:\d{1,3}\.){3}\d{1,3}$/
APACHE_VHOST_CONF =
<<EOF
NameVirtualHost _IP_:80

<VirtualHost _IP_:80>
  ServerName _VHOST_
  DocumentRoot _DOCROOT_/current/public
  CustomLog _DOCROOT_/shared/log/access.log combined
  ErrorLog _DOCROOT_/shared/log/error.log
  
  RewriteEngine On
  RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /system/maintenance.html [L]
</VirtualHost>
EOF
APACHE_SSL_VHOST_CONF =
<<EOF
NameVirtualHost _IP_:80

<VirtualHost _IP_:443>
  ServerName _VHOST_
  DocumentRoot _DOCROOT_/current/public
  CustomLog _DOCROOT_/shared/log/access.log combined
  ErrorLog _DOCROOT_/shared/log/error.log
  
  RewriteEngine On
  RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /system/maintenance.html [L]
  
  SSLEngine on
  SSLCertificateFile    /etc/apache2/certs/_VHOST_.crt
  SSLCertificateKeyFile /etc/apache2/certs/_VHOST_.key
</VirtualHost>
EOF

Instance Method Summary collapse

Instance Method Details

#get_apache_cap(server, domain) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/provision_vhost.rb', line 38

def get_apache_cap(server, domain)
  cap = Capistrano::Configuration.new
  cap.logger.level = Capistrano::Logger::TRACE

  cap.set :user, user.to_s
  cap.set :password, password.to_s
  
  cap.role :app, server
  
  # fix for closure below
  provision = self
      
  cap.task :provision do
    conf_text = APACHE_VHOST_CONF
    conf_text += "\n\n#{APACHE_SSL_VHOST_CONF}" if provision.ssl
    conf_text.gsub!('_IP_', provision.ip)
    conf_text.gsub!('_DOCROOT_', provision.docroot)
    
    apache_conf_dir = capture('apxs2 -q SYSCONFDIR')
    put conf_text, "/tmp/#{domain}"
    sudo "mv /tmp/#{domain} #{apache_conf_dir}/sites-available/"
    sudo "a2dissite #{domain}"
    sudo "a2ensite #{domain}"
    sudo "apache2ctl graceful"
  end
  
  cap
end

#get_docroot(domain) ⇒ Object



24
25
26
27
28
# File 'lib/provision_vhost.rb', line 24

def get_docroot(domain)
  ask("Enter the docroot for the virtual host: ") {
    |q| q.default = "/var/www/#{domain}"
  }
end

#get_ip(server) ⇒ Object



18
19
20
21
22
# File 'lib/provision_vhost.rb', line 18

def get_ip(server)
  ask("Enter the IP address for the virtual host: ") { 
    |q| q.default = `host -t A #{server}`.scan(IP_REGEX)[0] || server
  }
end

#process(server, domain) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/provision_vhost.rb', line 30

def process(server, domain)
  self.user ||= ProvisionBase.get_user.call
  self.password ||= ProvisionBase.get_password.call
  self.ip ||= get_ip(server)
  self.docroot ||= get_docroot(domain)
  get_apache_cap(server, domain).provision    
end