2014-06-25 43 views
-4

我有一個連接到Red Hat Satellite API的腳本。它列出了自提供日期以來發布的勘誤表。一旦生成列表,它會對接收到的每個勘誤再次調用API,以創建需要它的服務器列表。無法使用.emtpy?在Ruby腳本的數組中

#!/usr/bin/ruby 

require "xmlrpc/client" 
require "openssl" 
require "ap" 

# These are the parameters we use to create our XMLRPC client 
params = { 
    # Dev 
    #host: "satellite-dev.example.com", 
    # Prod 
    host: "satellite.example.com", 
    path: "/rpc/api", 
    use_ssl: "true", 
    user: "user", 
    pass: "pass" 
} 

# Create our client using the parameters above 
client = XMLRPC::Client.new(params[:host],params[:path]) 
# We don't want to bother with validating the Satellite SSL cert 
# since it is self-signed; Disable this option in the client 
client.instance_variable_get(:@http).instance_variable_set(:@verify_mode, OpenSSL::SSL::VERIFY_NONE) 

# Create our Satellite API session 
session = client.call("auth.login",params[:user],params[:pass]) 

channels_5 = ["rhel-x86_64-server-5","rhn-tools-rhel-x86_64-server-5"] 
channels_6 = ["rhel-x86_64-server-6","rhel-x86_64-server-ha-6","rhel-x86_64-server-optional-6","rhn-tools-rhel-x86_64-server-6"] 
channels_eus = ["rhel-x86_64-server-6.4.z","rhel-x86_64-server-6-rhs-2.1"] 
all_channels = [channels_5, channels_6, channels_eus] 

date = ARGV[0] 
errata = Array.new 

if date =~ /^\d\d\d\d-\d\d-\d\d$/ 
    security = 0 

    puts "\n" 
    all_channels.each do |channels| 
    puts "#{channels}" 
    advisories = Hash.new 

    channels.each do |channel| 
     errata = client.call("channel.software.listErrata", session, channel, date) 

     errata.each do |erratum| 
     if erratum["advisory_type"] == "Security Advisory" 
      if advisories.key?(erratum["advisory"]) 
      next 
      else 
      advisories[erratum["advisory"]] = { 
           advisory: erratum["advisory"], 
           synopsis: erratum["synopsis"], 
           issue_date: erratum["issue_date"][0..9] 
           } 
      end 
     else 
      next 
     end 
     end 
    end 
    advisories.keys.each do |advisory| 
     puts advisory 
     servers = client.call("errata.listAffectedSystems", session, advisory) 
     puts servers.class 

#  servers.each {|server| ap server} unless servers.emtpy? 
    end 
    puts "\n\n" 
    end 

else 
    puts "Usage: #{$0} YYYY-MM-DD\n" 
    puts "YYYY-MM-DD is the start date for sought errata.\n\n" 
    puts " Ex. #{$0} 2014-04-08" 
    puts " Returns all errata released since 08 April 2014\n\n" 
end 

# Close the API session 
client.call("auth.logout", session) 

每個API調用都會返回一個結構數組,其中包含服務器ID號,名稱以及上次檢入Satellite的時間。

在上面的代碼中,我有一個塊,它顯示每個結構的內容,除非它是空的。不幸的是,這失敗了。我所得到的回報是

["rhel-x86_64-server-5", "rhn-tools-rhel-x86_64-server-5"] 
RHSA-2014:0626 
Array 
./sat_qa_patching.rb:88:in `block (2 levels) in <main>': undefined method `emtpy?' for []:Array (NoMethodError) 
    from ./sat_qa_patching.rb:83:in `each' 
    from ./sat_qa_patching.rb:83:in `block in <main>' 
    from ./sat_qa_patching.rb:60:in `each' 
    from ./sat_qa_patching.rb:60:in `<main>' 

如果我打印出每個servers陣列我得到emtpy迴應:

["rhel-x86_64-server-5", "rhn-tools-rhel-x86_64-server-5"] 
RHSA-2014:0626 
[] 
RHSA-2014:0624 
[] 
RHSA-2014:0594 
[] 
RHSA-2014:0536 
[] 
RHSA-2014:0474 
[] 
RHSA-2014:0448 
[] 
RHSA-2014:0433 
[] 
RHSA-2014:0408 
[] 
RHSA-2014:0407 
[] 


["rhel-x86_64-server-6", "rhel-x86_64-server-ha-6", "rhel-x86_64-server-optional-6", "rhn-tools-rhel-x86_64-server-6"] 
RHSA-2014:0743 
[] 
RHSA-2014:0626 
[] 
RHSA-2014:0625 
[] 
RHSA-2014:0597 
[] 
RHSA-2014:0595 
[] 
RHSA-2014:0596 
[] 
RHSA-2014:0560 
[] 
RHSA-2014:0561 
[] 
RHSA-2014:0513 
[] 
RHSA-2014:0475 
[] 
RHSA-2014:0448 
[] 
RHSA-2014:0429 
[] 
RHSA-2014:0420 
[] 
RHSA-2014:0408 
[] 
RHSA-2014:0406 
[] 
RHSA-2014:0383 
[] 
RHSA-2014:0449 
[] 


["rhel-x86_64-server-6.4.z", "rhel-x86_64-server-6-rhs-2.1"] 
RHSA-2014:0627 
[] 
RHSA-2014:0634 
[] 
RHSA-2014:0432 
[] 
RHSA-2014:0628 
[] 
RHSA-2014:0409 
[] 
RHSA-2014:0377 
[] 

我敢肯定,他們是爲我所用servers.class並得到Array正在被返回的數組在每一個。

爲什麼它在.empty?方法錯誤,如果我回來應該支持它?

+2

空拼寫爲「空」,而不是「emtpy」 – meagar

回答

3

你有一個錯字

servers.each {|server| ap server} unless servers.emtpy? # <~ see it should be empty? 
0

當然的代碼不能使用emtpy?作爲沒有這樣的方法存在;見empty?

由於錯誤說 -

未定義的方法`emtpy「?爲[]:陣列(NoMethodError)

信託錯誤消息並利用API documentation推理如果所述方法真的被提供。在這種情況下,答案是:否,因爲只有正確的拼寫才被接受。