2014-04-17 107 views
0

我对ruby非常陌生,我正在尝试排查puppet给我的错误。到目前为止谷歌并没有帮助我更接近理解这个问题。其他information here这个ruby函数做了什么?

当傀儡启动Redis的初始化脚本,它返回以下错误:

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命令:”木偶的源代码内。

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 

回答

0

我打算假设这两个令人困惑的部分是||=*command

||=有条件分配操作者其中仅执行时在其上的变量的左侧的分配的计算结果为false,典型地当它是nilfalse。这通常用于实例变量或散列成员,两者在未设置时评估为零。

*图示操作者和用于将阵列转换成一系列的通入方法的参数。下面的实施例是相同的:

# 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) 

同样地,可以使用图示操作者在方法声明接收剩余传递的参数为Array:

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

options[:custom_environment] ||= {}意思是 “分配 '{}' 如果选择[:custom_environment]是nilfalse”。

并执行系统命令command

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

我希望它有帮助。