PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ohai/plugins/rackspace.rb

https://github.com/jmanero/ohai
Ruby | 109 lines | 55 code | 9 blank | 45 comment | 14 complexity | fd18146f6f3e4acd159231dc105b207b MD5 | raw file
Possible License(s): Apache-2.0
  1. #
  2. # Author:: Cary Penniman (<cary@rightscale.com>)
  3. # License:: Apache License, Version 2.0
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. provides "rackspace"
  17. require_plugin "kernel"
  18. require_plugin "network"
  19. # Checks for matching rackspace kernel name
  20. #
  21. # === Return
  22. # true:: If kernel name matches
  23. # false:: Otherwise
  24. def has_rackspace_kernel?
  25. kernel[:release].split('-').last.eql?("rscloud")
  26. end
  27. # Checks for matching rackspace arp mac
  28. #
  29. # === Return
  30. # true:: If mac address matches
  31. # false:: Otherwise
  32. def has_rackspace_mac?
  33. network[:interfaces].values.each do |iface|
  34. unless iface[:arp].nil?
  35. return true if iface[:arp].value?("00:00:0c:07:ac:01") or iface[:arp].value?("00:00:0c:9f:f0:01")
  36. end
  37. end
  38. false
  39. end
  40. # Identifies the rackspace cloud
  41. #
  42. # === Return
  43. # true:: If the rackspace cloud can be identified
  44. # false:: Otherwise
  45. def looks_like_rackspace?
  46. hint?('rackspace') || has_rackspace_mac? || has_rackspace_kernel?
  47. end
  48. # Names rackspace ip address
  49. #
  50. # === Parameters
  51. # name<Symbol>:: Use :public_ip or :private_ip
  52. # eth<Symbol>:: Interface name of public or private ip
  53. def get_ip_address(name, eth)
  54. network[:interfaces][eth][:addresses].each do |key, info|
  55. if info['family'] == 'inet'
  56. rackspace[name] = key
  57. break # break when we found an address
  58. end
  59. end
  60. end
  61. # Names rackspace ipv6 address for interface
  62. #
  63. # === Parameters
  64. # name<Symbol>:: Use :public_ip or :private_ip
  65. # eth<Symbol>:: Interface name of public or private ip
  66. def get_global_ipv6_address(name, eth)
  67. network[:interfaces][eth][:addresses].each do |key, info|
  68. # check if we got an ipv6 address and if its in global scope
  69. if info['family'] == 'inet6' && info['scope'] == 'Global'
  70. rackspace[name] = key
  71. break # break when we found an address
  72. end
  73. end
  74. end
  75. # Get the rackspace region
  76. #
  77. def get_region()
  78. status, stdout, stderr = run_command(:no_status_check => true, :command => "xenstore-ls vm-data/provider_data")
  79. if status == 0
  80. stdout.split("\n").each do |line|
  81. rackspace[:region] = line.split[2].delete('\"') if line =~ /^region/
  82. end
  83. end
  84. rescue Ohai::Exceptions::Exec
  85. Ohai::Log.debug("Unable to find xenstore-ls, cannot capture region information for Rackspace cloud")
  86. end
  87. # Adds rackspace Mash
  88. if looks_like_rackspace?
  89. rackspace Mash.new
  90. get_ip_address(:public_ip, :eth0)
  91. get_ip_address(:private_ip, :eth1)
  92. get_region()
  93. # public_ip + private_ip are deprecated in favor of public_ipv4 and local_ipv4 to standardize.
  94. rackspace[:public_ipv4] = rackspace[:public_ip]
  95. get_global_ipv6_address(:public_ipv6, :eth0)
  96. rackspace[:public_hostname] = "#{rackspace[:public_ip].gsub('.','-')}.static.cloud-ips.com"
  97. rackspace[:local_ipv4] = rackspace[:private_ip]
  98. get_global_ipv6_address(:local_ipv6, :eth1)
  99. rackspace[:local_hostname] = hostname
  100. end