# Copyright (C) 2011-2012 RightScale, Inc, All Rights Reserved Worldwide. # # THIS PROGRAM IS CONFIDENTIAL AND PROPRIETARY TO RIGHTSCALE # AND CONSTITUTES A VALUABLE TRADE SECRET. Any unauthorized use, # reproduction, modification, or disclosure of this program is # strictly prohibited. Any use of this program by an authorized # licensee is strictly subject to the terms and conditions, # including confidentiality obligations, set forth in the applicable # License Agreement between RightScale.com, Inc. and # the licensee module RightConf # Common base to all progress reporters # # Reporters expose the following report methods whose default implementations # can be overridden in descendants: # # - report_message:: Standard progress report message # - report_section:: Title # - report_error:: Non fatal error # - report_fatal:: Fatal error, will raise after reporting message # - report_check:: Report a check being done on system, should be followed by # - report_success:: Report check success # - report_failure:: Report check failure # - report_result:: Report check success or failure depending on argument # # Alternatively reporters can override any associated formatting method # (:format_message, :format_section, etc.) and/or the :write method used to # write the progress report. class BaseReporter # Report methods are associated with formatting methods # Call the formatting method then call the reporter 'write' method # Reporters can override any formatting method and/or the write method # # === Parameters # meth(Symbol):: Reporting method # args(Array):: Arguments # # === Return # true:: Always return true # # === Raise # (Exception):: If there is no formatting method def method_missing(meth, *args) # Shortcut for standard progress report message meth = :report_message if meth == :report if meth.to_s =~ /^report_([a-zA-Z]+)$/ format_method = "format_#{Regexp.last_match[1]}".to_s if self.respond_to?(format_method) text = self.__send__(format_method, *args) write(text) exit 1 if Regexp.last_match[1] == 'fatal' else super(meth, *args) end else super(meth, *args) end end # Report success or failure # # === Parameters # res(TrueClass|FalseClass):: true for success, false for failure # # === Return # true:: Always return true def report_result(res) res ? report_success : report_failure end protected # Actually write text, implement in descendant # # === Parameters # text(String):: Text to be written # # === Return # true:: Always return true def write(text) raise "Implement!" end # Generic progress report formatting # # === Parameters # message(String):: Progress report content # # === Return # text(String):: Resulting text def format_message(message) text = message + "\n" end # Format new progress report section # # === Parameters # section(String):: Section title # # === Return # text(String):: Resulting text def format_section(section) text = ['', '-' * 80, section.upcase, '-' * 80, ''].join("\n") end # Format non-fatal error # # === Parameters # error(String):: Error to report # # === Return # text(String):: Resulting text def format_error(error) text = '**WARN: ' + error + "\n" end # Format check # # === Parameters # check(String):: Check title # # === Return # text(String):: Resulting text def format_check(check) text = check + '...' end # Format check success # # === Return # text(String):: Resulting text def format_success text ="OK\n" end # Format check failure # # === Return # text(String):: Resulting text def format_failure text ="FAILED\n" end # Format fatal error # # === Return # text(String):: Resulting text def format_fatal(error) text = "\n***FATAL: " + error + "\n" end end end