crnixon / thor_tasks
- Source
- Commits
- Network (4)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
1c552b3
commit 1c552b331ef1a694d23edce83ef82b0943ecd467
tree 2006d63a09a615dae483e1c918efde1dd07848f3
parent 98485c0651211a1351e634695e768411a846a9c3
tree 2006d63a09a615dae483e1c918efde1dd07848f3
parent 98485c0651211a1351e634695e768411a846a9c3
thor_tasks / provision_apache.thor
| 9916f4b0 » | crnixon | 2008-09-03 | 1 | # module: provision_apache | |
| 2 | |||||
| 3 | class Provision < Thor | ||||
| 4 | def initialize(*args) | ||||
| 6780faeb » | crnixon | 2008-09-03 | 5 | raise "You must have provision_base installed." unless defined?(BASE_LOADED) | |
| 9916f4b0 » | crnixon | 2008-09-03 | 6 | super(*args) | |
| 7 | end | ||||
| 8 | |||||
| 9 | desc "apache SERVER DOMAIN", "Adds a virtual host config for DOMAIN " + | ||||
| 10 | "on the remote server SERVER." | ||||
| 11 | method_options(:user => :optional, :ip => :optional, :ssl => :boolean) | ||||
| cd26ca0d » | crnixon | 2008-09-08 | 12 | def apache(server, domain) | |
| 9916f4b0 » | crnixon | 2008-09-03 | 13 | ip_regex = /\b(?:\d{1,3}\.){3}\d{1,3}$/ # not perfect, but works | |
| cd26ca0d » | crnixon | 2008-09-08 | 14 | get_user_and_password(options) | |
| 9916f4b0 » | crnixon | 2008-09-03 | 15 | ||
| 2d358df1 » | crnixon | 2009-01-23 | 16 | @ip = options['ip'] || \ | |
| 9916f4b0 » | crnixon | 2008-09-03 | 17 | ask("Enter the IP address for the virtual host: ") { | |
| 18 | |q| q.default = `host -t A #{server}`.scan(ip_regex)[0] || server | ||||
| 19 | } | ||||
| 20 | |||||
| 2d358df1 » | crnixon | 2009-01-23 | 21 | @docroot = options['docroot'] || \ | |
| 9916f4b0 » | crnixon | 2008-09-03 | 22 | ask("Enter the docroot for the virtual host: ") { | |
| 23 | |q| q.default = "/var/www/#{domain}" | ||||
| 24 | } | ||||
| 25 | |||||
| 98485c06 » | crnixon | 2009-01-23 | 26 | get_apache_cap(server, domain, options).provision | |
| 9916f4b0 » | crnixon | 2008-09-03 | 27 | end | |
| 28 | |||||
| 29 | private | ||||
| 30 | |||||
| 98485c06 » | crnixon | 2009-01-23 | 31 | def get_apache_cap(server, domain, options) | |
| 9916f4b0 » | crnixon | 2008-09-03 | 32 | cap = Capistrano::Configuration.new | |
| 33 | cap.logger.level = Capistrano::Logger::TRACE | ||||
| 98485c06 » | crnixon | 2009-01-23 | 34 | ||
| d5d5429f » | crnixon | 2009-01-23 | 35 | cap.set :user, @user | |
| 36 | cap.set :password, @password | ||||
| 9916f4b0 » | crnixon | 2008-09-03 | 37 | ||
| 38 | cap.role :app, server | ||||
| 39 | |||||
| 98485c06 » | crnixon | 2009-01-23 | 40 | # used to make closure below work | |
| 41 | ip = @ip | ||||
| 42 | docroot = @docroot | ||||
| 43 | |||||
| 9916f4b0 » | crnixon | 2008-09-03 | 44 | cap.task :provision do | |
| 45 | conf_text = APACHE_VHOST_CONF | ||||
| cd26ca0d » | crnixon | 2008-09-08 | 46 | conf_text += "\n\n#{APACHE_SSL_VHOST_CONF}" if options[:ssl] | |
| 98485c06 » | crnixon | 2009-01-23 | 47 | conf_text.gsub!('_IP_', ip) | |
| 48 | conf_text.gsub!('_DOCROOT_', docroot) | ||||
| 9916f4b0 » | crnixon | 2008-09-03 | 49 | ||
| 50 | apache_conf_dir = capture('apxs2 -q SYSCONFDIR') | ||||
| 51 | put conf_text, "/tmp/#{domain}" | ||||
| 52 | sudo "mv /tmp/#{domain} #{apache_conf_dir}/sites-available/" | ||||
| 53 | sudo "a2dissite #{domain}" | ||||
| 54 | sudo "a2ensite #{domain}" | ||||
| 55 | sudo "apache2ctl graceful" | ||||
| 56 | end | ||||
| 57 | |||||
| 58 | cap | ||||
| 59 | end | ||||
| 60 | |||||
| 61 | APACHE_VHOST_CONF = <<EOF | ||||
| 62 | NameVirtualHost _IP_:80 | ||||
| 63 | |||||
| 64 | <VirtualHost _IP_:80> | ||||
| 65 | ServerName _VHOST_ | ||||
| 66 | DocumentRoot _DOCROOT_/current/public | ||||
| 67 | CustomLog _DOCROOT_/shared/log/access.log combined | ||||
| 68 | ErrorLog _DOCROOT_/shared/log/error.log | ||||
| 69 | |||||
| 70 | RewriteEngine On | ||||
| 71 | RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f | ||||
| 72 | RewriteCond %{SCRIPT_FILENAME} !maintenance.html | ||||
| 73 | RewriteRule ^.*$ /system/maintenance.html [L] | ||||
| 74 | </VirtualHost> | ||||
| 75 | EOF | ||||
| 76 | |||||
| 77 | APACHE_SSL_VHOST_CONF = <<EOF | ||||
| 78 | NameVirtualHost _IP_:80 | ||||
| 79 | |||||
| 80 | <VirtualHost _IP_:443> | ||||
| 81 | ServerName _VHOST_ | ||||
| 82 | DocumentRoot _DOCROOT_/current/public | ||||
| 83 | CustomLog _DOCROOT_/shared/log/access.log combined | ||||
| 84 | ErrorLog _DOCROOT_/shared/log/error.log | ||||
| 85 | |||||
| 86 | RewriteEngine On | ||||
| 87 | RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f | ||||
| 88 | RewriteCond %{SCRIPT_FILENAME} !maintenance.html | ||||
| 89 | RewriteRule ^.*$ /system/maintenance.html [L] | ||||
| 90 | |||||
| 91 | SSLEngine on | ||||
| 92 | SSLCertificateFile /etc/apache2/certs/_VHOST_.crt | ||||
| 93 | SSLCertificateKeyFile /etc/apache2/certs/_VHOST_.key | ||||
| 94 | </VirtualHost> | ||||
| 95 | EOF | ||||
| 96 | |||||
| 2d358df1 » | crnixon | 2009-01-23 | 97 | end | |
