class OpenShift::Utils::Environ

Class represents the OpenShift/Ruby analogy of C environ(7)

Public Class Methods

for_cartridge(cartridge_dir) click to toggle source

@param [String] cartridge_dir Home directory of the gear @return [Hash<String,String>] hash[Environment Variable] = Value

# File lib/openshift-origin-node/utils/environ.rb, line 34
def self.for_cartridge(cartridge_dir)
  load("/etc/openshift/env",
       File.join(Pathname.new(cartridge_dir).parent.to_path, '.env'),
       File.join(cartridge_dir, 'env'))
end
for_gear(gear_dir) click to toggle source

Load the combined cartridge environments for a gear @param [String] gear_dir Home directory of the gear @return [Hash<String,String>] hash[Environment Variable] = Value

# File lib/openshift-origin-node/utils/environ.rb, line 26
def self.for_gear(gear_dir)
  load("/etc/openshift/env",
       File.join(gear_dir, '.env'),
       File.join(gear_dir, '*', 'env'))
end
load(*dirs) click to toggle source

Read a Gear's + n number cartridge environment variables into a environ(7) hash @param [String] env_dir of gear to be read @return [Hash<String,String>] environment variable name: value

# File lib/openshift-origin-node/utils/environ.rb, line 43
def self.load(*dirs)
  dirs.each_with_object({}) do |env_dir, env|
    # add wildcard for globbing if needed
    env_dir += '/*' if not env_dir.end_with? '*'

    # Find, read and load environment variables into a hash
    Dir[env_dir].each do |file|
      next if file.end_with? '.erb'
      next unless File.file? file

      contents = nil
      File.open(file) do |input|
        begin
          contents = input.read.chomp
          next if contents.empty?

          index    = contents.index('=')
          contents = contents[(index + 1)..-1]
          contents.gsub!(%r\A["']|["']\Z/, '')
        rescue Exception => e
          NodeLogger.logger.info { "Failed to process: #{file} [#{input}]: #{e.message}" }
        end
      end
      env[File.basename(file)] = contents
    end
  end
end