/files/private-chef-cookbooks/private-chef/libraries/helper.rb

https://github.com/chef-boneyard/opscode-omnibus · Ruby · 146 lines · 110 code · 20 blank · 16 comment · 7 complexity · 0f418f8203af508daae5013195ecbf91 MD5 · raw file

  1. require 'mixlib/shellout'
  2. class OmnibusHelper
  3. attr_reader :node
  4. def initialize(node)
  5. @node = node
  6. end
  7. def ownership
  8. owner = node['private_chef']['user']['username']
  9. group = owner
  10. {"owner" => owner, "group" => group}
  11. end
  12. # Normalizes hosts. If the host part is an ipv6 literal, then it
  13. # needs to be quoted with []
  14. def self.normalize_host(host_part)
  15. # Make this simple: if ':' is detected at all, it is assumed
  16. # to be a valid ipv6 address. We don't do data validation at this
  17. # point, and ':' is only valid in an URL if it is quoted by brackets.
  18. if host_part =~ /:/
  19. "[#{host_part}]"
  20. else
  21. host_part
  22. end
  23. end
  24. def normalize_host(host_part)
  25. self.class.normalize_host(host_part)
  26. end
  27. def vip_for_uri(service)
  28. normalize_host(node['private_chef'][service]['vip'])
  29. end
  30. def db_connection_uri
  31. db_protocol = "postgres"
  32. db_user = node['private_chef']['postgresql']['sql_user']
  33. db_password = node['private_chef']['postgresql']['sql_password']
  34. db_vip = vip_for_uri('postgresql')
  35. db_name = "opscode_chef"
  36. "#{db_protocol}://#{db_user}:#{db_password}@#{db_vip}/#{db_name}"
  37. end
  38. def bifrost_db_connection_uri
  39. db_protocol = "postgres"
  40. db_user = node['private_chef']['oc_bifrost']['sql_user']
  41. db_password = node['private_chef']['oc_bifrost']['sql_password']
  42. db_vip = vip_for_uri('postgresql')
  43. db_name = "bifrost"
  44. "#{db_protocol}://#{db_user}:#{db_password}@#{db_vip}/#{db_name}"
  45. end
  46. # This file is touched once initial bootstrapping of the system is
  47. # done.
  48. def self.bootstrap_sentinel_file
  49. "/var/opt/opscode/bootstrapped"
  50. end
  51. # Use the presence of a sentinel file as an indicator for whether
  52. # the server has already had initial bootstrapping performed.
  53. #
  54. # @todo: Is there a more robust way to determine this, i.e., based
  55. # on some functional aspect of the system?
  56. def self.has_been_bootstrapped?
  57. File.exists?(bootstrap_sentinel_file)
  58. end
  59. # Parse a config string as a memory value returning an integer in MB
  60. # units. Supported inputs (not case sensitive) are B, K/Kb, M/Mb,
  61. # G/Gb. Uses integer division so values in B and Kb must exceed 1Mb.
  62. def self.parse_mem_to_mb(mem_str)
  63. if mem_str.is_a?(Integer)
  64. return mem_str
  65. end
  66. regex = /(\d+)([GgmMkKbB]{0,2})/
  67. m = regex.match(mem_str)
  68. raise "bad arg" if !m
  69. raw_value = m[1].to_i
  70. units = m[2]
  71. value = case units
  72. when /^b$/i
  73. raw_value / (1024 * 1024)
  74. when /^kb?$/i
  75. raw_value / 1024
  76. when /^mb?$/i
  77. raw_value
  78. when "" # no units, assume Mb
  79. raw_value
  80. when /^gb?$/i
  81. raw_value * 1024
  82. else
  83. raise "unsupported memory units: #{mem_str}"
  84. end
  85. if value > 0
  86. value
  87. else
  88. raise "zero Mb value not allowed: #{mem_str}"
  89. end
  90. end
  91. def self.erl_atom_or_string(term)
  92. case term
  93. when Symbol
  94. term
  95. when String
  96. "\"#{term}\""
  97. else
  98. "undefined"
  99. end
  100. end
  101. def erl_atom_or_string(term)
  102. self.class.erl_atom_or_string(term)
  103. end
  104. def s3_url_caching(setting)
  105. case setting.to_s
  106. when "off"
  107. "off"
  108. when /m$/
  109. "{#{setting.chop}, minutes}"
  110. when /%$/
  111. "{#{setting.chop}, percent}"
  112. end
  113. end
  114. # OC-11540, fallback to ssl_port if non_ssl_port is disabled
  115. def internal_lb_url
  116. if node['private_chef']['nginx']['non_ssl_port'] == false
  117. "https://#{vip_for_uri('lb_internal')}:#{node['private_chef']['nginx']['ssl_port']}"
  118. else
  119. "http://#{vip_for_uri('lb_internal')}:#{node['private_chef']['nginx']['non_ssl_port']}"
  120. end
  121. end
  122. def ldap_authentication_enabled?
  123. node['private_chef'].attribute?('ldap') &&
  124. !(node['private_chef']['ldap'].nil? || node['private_chef']['ldap'].empty?)
  125. end
  126. end