# Copyright (C) 2011 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 require 'shellwords' module RightConf class Command include Singleton include ProgressReporter # Execute given command with given arguments # # === Parameters # command(String):: Command to run # args(Array):: Command arguments # # === Return # result(CommandResult):: Result of execution (output and exit status) def execute(command, *args) opts = {} if !args.empty? && args[-1].is_a?(Hash) opts = args[-1] args = args[0..-2] end res = Platform.dispatch(command, *args) { :execute } if !res.success? && msg = opts[:abort_on_failure] report_fatal("#{msg}: '#{command} #{args.join(' ')}' returned\n#{res.output}") end res end # Execute given command on *nix systems # # === Parameters # command(String):: Command name # params(Array):: List of parameters to pass to command # # === Return # result(CommandResult):: Result of execution (output and exit status) def execute_linux(command, *args) out = `#{@prefix} #{Shellwords.join([command, *args])} 2>&1` result = CommandResult.new(out, $?.exitstatus) end alias :execute_darwin :execute_linux # Execute given command on Windows systems # # === Parameters # command(String):: Command name # params(Array):: List of parameters to pass to command # # === Return # result(CommandResult):: Result of execution (output and exit status) def execute_windows(command, *args) raise 'TBD!' end # Set prefix to be used for all commands # # === Parameters # prefix(String):: Commands prefix # # === Return # true:: Always return true def set_prefix(prefix) @prefix = prefix true end end # Command results class CommandResult # Process output attr_reader :output # Process exit status attr_reader :status # Initialize output and exit status # # === Parameters # output(String):: Process output # status(Fixnum):: Process exit status def initialize(output, status) @output = output @status = status end # Whether process exited successfully # # === Return # true:: If process exited with status code 0 # false:: Otherwise def success? @status == 0 end end end