namespace :webserver do task(:default) { list } import_pack(:connect, self, "webserver") desc <<-DESC List all webservers on either a particular VPS, or all VPS's. Environment variables: (optional) $ON: hostname of the VPS to list the webservers of. Default: the newest VPS. DESC task :list do puts_title "All webservers" puts_list particular_set_of_webservers || Webserver.all, :title => :vps do |t| t.col("Running on", :vps) t.col("Running as", :user) end end desc <<-DESC Make sure that at least one webserver is present. Failing that, one is set up automatically on the newest VPS. DESC task :ensure_any do if (count = Webserver.all.size).zero? setup else puts_title "Webserver present" puts "There exists #{count} webservers." end end desc <<-DESC Set up a new webserver on a given VPS. Automatically installs and configures the necessary software, running as its own UNIX user for security purposes. Environment variables: (optional) $ON: hostname of the VPS to set up the webserver onto. Default: the newest VPS. DESC task :setup do vps = particular_vps || VPS.last webserver = vps.webservers.build try_save webserver do puts_title "Request sent" puts "Webserver successfully scheduled for setup on VPS #{q vps}." end end desc <<-DESC Force the setup procedure of a webserver to be re-run. You might want to do that if you think you messed up the automatic configuration, UNIX account or software installations. Environment variables: (optional) $ID: the ID of the webserver (get the list with #{qcommand "webserver:list"}). Default: the webserver last set up. DESC task :reprepare do fetch_current.put(:reprepare) puts_title "Request sent" puts "Webserver has been scheduled for re-preparation." end desc <<-DESC Force the reconfiguration of a webserver. You might want to do that if you think you messed up the automatic configuration. Environment variables: (optional) $ID: the ID of the webserver (get the list with #{qcommand "webserver:list"}). Default: the webserver last set up. DESC task :reconfigure do fetch_current.put(:reconfigure) puts_title "Request sent" puts "Webserver has been scheduled for re-configuration." end desc <<-DESC List all websites hosted by a given webserver. That is, websites served via the webserver by having one of their backends running on the same VPS as the webserver. Environment variables: $ID: the ID of the webserver (get the list with #{qcommand "webserver:list"}). DESC task :hosted do webserver = fetch_current! puts_title "Hosted websites" top.website.backends.show_list(webserver.backends.all) end desc <<-DESC Remove a webserver. This is accomplished by stopping all services run as the UNIX user of the webserver, and then suppressing that user. No data is lost: as with any automated suppression of a UNIX user, the home folder is backed up at /home/_foo, where "foo" is the name newly suppressed user. The removal of a webserver is only allowed once all the backends of the sites it serves have been removed (or switched webservers by way of removal followed by re-creation). Get the list of all these remaining backends with #{qcommand "webserver:hosted"}. Environment variables: $ID: the ID of the webserver (get the list with #{qcommand "webserver:list"}). DESC task :remove do webserver = fetch_current! begin webserver.destroy rescue WebService::ResourceConflict error! $!.response.data['errors'] else puts_title "Request sent" puts "Webserver on VPS #{q webserver.vps} has been scheduled for removal." end end def require_id! ENV["ID"] or error! <<-MSG The ID of the webserver to deal with must be specified by setting the $ID environment variable. To list all webservers, run #{qcommand "webserver:list"}. MSG end def fetch_current particular_webserver || Webserver.last end def fetch_current! id = require_id! Webserver.find(id) end def particular_vps VPS.find(ENV["ON"]) if ENV["ON"] end def particular_webserver Webserver.find(ENV["ID"]) if ENV["ID"] end def particular_set_of_webservers vps = particular_vps and vps.webservers end end