这个ruby函数有什么作用?

时间:2014-04-17 17:57:42

标签: ruby puppet

我对红宝石很新,而且我正试图解决木偶给我的错误。到目前为止谷歌还没有帮助我更接近理解这个问题。额外的information here

当puppet启动redis init脚本时,它会返回以下错误:

Debug: Executing '/etc/init.d/redis_6390 start'
Error: Could not start Service[redis_6390]: Execution of '/etc/init.d/redis_6390 start' returned 1: Error: Could not execute posix command: Exec format error - /etc/init.d/redis_6390
Wrapped exception:
Execution of '/etc/init.d/redis_6390 start' returned 1: Error: Could not execute posix command: Exec format error - /etc/init.d/redis_6390
Error: /Stage[main]/Ac_redis_6390/Service[redis_6390]/ensure: change from stopped to running failed: Could not start Service[redis_6390]: Execution of '/etc/init.d/redis_6390 start' returned 1: Error: Could not execute posix command: Exec format error - /etc/init.d/redis_6390
Debug: Class[Ac_redis_6390]: The container Stage[main] will propagate my refresh event

我找到了确切的行"无法执行posix命令:"在puppet源代码中。

https://github.com/puppetlabs/puppet/blob/master/lib/puppet/util/execution.rb

  # This is private method.
  # @comment see call to private_class_method after method definition
  # @api private
  #
  def self.execute_posix(command, options, stdin, stdout, stderr)
    child_pid = Puppet::Util.safe_posix_fork(stdin, stdout, stderr) do

      # We can't just call Array(command), and rely on it returning
      # things like ['foo'], when passed ['foo'], because
      # Array(command) will call command.to_a internally, which when
      # given a string can end up doing Very Bad Things(TM), such as
      # turning "/tmp/foo;\r\n /bin/echo" into ["/tmp/foo;\r\n", " /bin/echo"]
      command = [command].flatten
      Process.setsid
      begin
        Puppet::Util::SUIDManager.change_privileges(options[:uid], options[:gid], true)

        # if the caller has requested that we override locale environment variables,
        if (options[:override_locale]) then
          # loop over them and clear them
          Puppet::Util::POSIX::LOCALE_ENV_VARS.each { |name| ENV.delete(name) }
          # set LANG and LC_ALL to 'C' so that the command will have consistent, predictable output
          # it's OK to manipulate these directly rather than, e.g., via "withenv", because we are in
          # a forked process.
          ENV['LANG'] = 'C'
          ENV['LC_ALL'] = 'C'
        end

        # unset all of the user-related environment variables so that different methods of starting puppet
        # (automatic start during boot, via 'service', via /etc/init.d, etc.) won't have unexpected side
        # effects relating to user / home dir environment vars.
        # it's OK to manipulate these directly rather than, e.g., via "withenv", because we are in
        # a forked process.
        Puppet::Util::POSIX::USER_ENV_VARS.each { |name| ENV.delete(name) }

        options[:custom_environment] ||= {}
        Puppet::Util.withenv(options[:custom_environment]) do
          Kernel.exec(*command)
        end
      rescue => detail
        Puppet.log_exception(detail, "Could not execute posix command: #{detail}")
        exit!(1)
      end
    end
    child_pid
  end
  private_class_method :execute_posix

这些线做什么?我试图了解抛出此异常的原因。

        options[:custom_environment] ||= {}
        Puppet::Util.withenv(options[:custom_environment]) do
          Kernel.exec(*command)
        end

2 个答案:

答案 0 :(得分:1)

options[:custom_environment] ||= {}表示"分配' {}'如果选项[:custom_environment]为nilfalse"。

执行系统命令command

commandArray,它的第一个值是命令本身,其他数组值是系统命令的参数。

我希望它有所帮助。

答案 1 :(得分:0)

我将假设两件令人困惑的事件是||=*command

||=条件赋值运算符,仅当左侧的变量求值为false时才执行赋值,通常为nil时{} 1}}或false。这通常与实例变量或散列成员一起使用,两者都在未设置时计算为nil。

* splat运算符,用于将数组转换为传递给方法的一系列参数。以下示例完全相同:

# Pass colors directly to use_colors.
use_colors('red', 'green', 'blue')

# Unpack an Array into an argument list for use_colors.
colors = ['red', 'green', 'blue']
use_colors(*colors)

同样,您可以在方法声明中使用 splat运算符来接收剩余的传递参数作为数组:

# This method can accept any number of colors, including zero.
def use_colors(*colors)
  colors.each do |color|
    puts "Using color: #{color}!"
  end
end