module Deployment module Methods module Helper # Returns an time-dependant string that can be used in pseudo-unique # release versions and the such. # # The object will be cached for later reference, so multiple uses of this # method in a recipe will result in the same string. # # Parameters: # * +format+ - the strftime format that is to be used to generate the unique string. Defaults to "%Y-%m-%d_%s". def date_unique(format="%Y-%m-%d_%s") @date_unique ||= Time.now.strftime(format) end # Sends an email. # # Parameters: # * +options+ - a hash with needed configuration options for the email. # * +:body+ - the actual text to send # * +:subject+ - the subject line of the email # * +:from+ - the senders address # * +:to+ - the recipiants address def sendmail(options) Helper.validates_presence_of options[:from], "Mail-FROM not set" Helper.validates_presence_of options[:to], "Mail-TO not set" Helper.validates_presence_of options[:subject], "No mail subject set" Helper.validates_presence_of options[:body], "No mail body set" helper = ::Helper::Mail.new helper.smtp_host = @cdb['smtp_host'] if @cdb and @cdb['smtp_host'] helper.sendmail(options) end # Checks if a given url returns 200 and a body that matches a given regexp. # # Parameters: # * +options+ - a hash with needed configuration options # * +:check_protocol+ - "http" or "https" or maybe something else... # * +:check_host+ - the host to contact # * +:check_port+ - the port to connect to # * +:check_uri+ - the recipiants address # * +:check_response_string+ - the string or regexp to check def assert_url_response_of(options={}) asserter = ::Asserter::Url.new asserter.check_protocol = options[:check_protocol] || @cdb['check_protocol'] || "http" asserter.check_host = options[:check_host] || @cdb['check_host'] asserter.check_port = options[:check_port] || @cdb['check_port'] || '80' asserter.check_uri = options[:check_uri] || @cdb['check_uri'] || '/' asserter.check_response_string = options[:check_response_string] || @cdb['check_response_string'] || 'html' asserter.assert_url_response_of(options) end # Reports a deployment to its cdb. def report_to_cdb() reporter = ::Reporter::Cdb.new reporter.version = @version.to_json reporter.worker = self reporter.set_version() end # Sends an email to report a deployment to a given recepiant. # # Parameters: # * +options+ - a hash with needed configuration options for the email. All parameters are optional. # * +:application_name+ - which application has been deployed # * +:module_name+ - which module of the application has been deployed # * +:status+ - the resulting status of the deployment # * +:message+ - a additional message to send in the mail, commonly used to specify any non positive status. # * +:deploy_email_from+ - the senders address # * +:deploy_email_to+ - the recipiants address def report_by_mail(options={}) options = @cdb.merge($recipe_config).merge(options) options[:status] ||= 'success' options[:message] ||= '' mail_options = { :from => options[:deploy_email_from] || @cdb['deploy_email_from'], :to => options[:deploy_email_to] || @cdb['deploy_email_to'], :subject => "#{options[:env]} #{options[:job_name]} #{options[:module_name]} #{options[:application_name]} #{options[:version]} #{options[:status]}", :body => "Env: #{options[:env]}\nJob: #{options[:job_name]}\nModule: #{options[:module_name]}\nApplication: #{options[:application_name]}\nVersion: #{options[:version].to_json}\n#{options[:status]}.\n\n---\nx#{options[:message]}" } sendmail(mail_options) end # Deploys a tomcat based application to a set of app servers.
# The tomcats are restarted in this process. # # Parameters: # * +options+ - a hash with needed configuration options. # * +:servers+ - an enumerable list of the app servers # * +:runners+ - a subset of +:servers+ which are currently active. Is equal to +:servers+ if not set # * +:initd_script+ - the path to the init script of the appserver. Used to stop/start. # * +:application_name+ - the name of the current application # * +:catalina_home+ - the path in which the app servers webapps/ directory is # * +:tomcat_context+ - the tomcat context to deploy in # * +:logfilename+ - the name of the logfile. Will be automatically prefixed with #{catalina_home}/logs/ # * +:check_host+ - an options hash to pass to +#assert_url_response_of+ to check the status of the app # All parameters can also be spcified via CDB. def tomcat_deploy(options={}) publisher = ::Publisher::Tomcat.new publisher.servers = options[:servers] || @cdb['servers'] publisher.runners = options[:runners] || @cdb['runners'] || publisher.servers publisher.initd_script = options[:initd_script] || @cdb['initd_script'] publisher.application_name = options[:application_name] || $recipe_config[:application_name] || @cdb['application_name'] publisher.catalina_home = options[:catalina_home] || @cdb['catalina_home'] publisher.tomcat_context = options[:tomcat_context] || @cdb['tomcat_context'] publisher.logfilename = options[:logfilename] || @cdb['logfilename'] publisher.check_host = options[:check_host] || @cdb['check_host'] publisher.worker = self publisher.deploy(options) end # Mounts a SMB share. # # Parameters: # * +remote_path+ - the path to the remote share (e.g."smb://somehost/someshare") # * +:local_path+ - the local path in which the share will be mounted def samba_mount (remote_path, local_path) smb = ::Helper::Smb.new smb.remote_path = remote_path smb.local_path = local_path smb.samba_mount() end # Unmounts a previously mounted SMB share. # # Parameters: # * +:local_path+ - the local path in which the share is mounted def samba_umount (local_path) smb = ::Helper::Smb.new smb.local_path = local_path smb.samba_umount() end def validates_presence_of(attribute, message=nil) raise ArgumentError, message || "Missing parameter", caller if attribute.nil? end module_function :validates_presence_of def validates_not_empty(attribute, message=nil) raise ArgumentError, message || "Parameter is empty", caller if attribute.nil? end module_function :validates_not_empty end end end