Module: Smash::CloudPowers::Zenv

Overview

This module provides some environment variable management and functionality Hopefully it should provide us with some “Zen”, when dealing with normally annoying env issues. Meh, it probably won’t but I like the name, so it stays :} System ENV, dotenv ENV and instance variables are considered for now but this will also use elasticache/redis…some other stuff too, in the coming versions

Instance Method Summary collapse

Methods included from Helpers

#create_logger, #log_file, #logger

Methods included from PathHelp

#job_exist?, #job_home, #job_path, #job_require_path

Methods included from LogicHelp

#attr_map, #called_from, #instance_attr_accessor, #smart_retry, #update_message_body

Methods included from LangHelp

#deep_modify_keys_with, #find_and_remove, #format_error_message, #from_json, #modify_keys_with, #to_basic_hash, #to_camel, #to_hyph, #to_i_var, #to_pascal, #to_ruby_file_name, #to_snake, #valid_json?, #valid_url?

Instance Method Details

#env_vars(key = '') ⇒ Object

Search through the Dotenv variables for a key or if no key is given, return all the .env-vars and their values

Parameters

  • key String -



41
42
43
44
# File 'lib/cloud_powers/zenv.rb', line 41

def env_vars(key = '')
  return ENV if key.empty?
  ENV[to_snake(key).upcase]
end

#file_tree_search(name) ⇒ Object

Attempts to find a file by searching the current directory for the file then walking up the file tree and searching at each stop all the way up to the root directory

Parameters

  • name String - name of the file or directory to find

Returns Pathname - path to the file or directory given as the name parameter



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cloud_powers/zenv.rb', line 23

def file_tree_search(name)
  next_dir = Pathname.new(`pwd`.strip).parent
  current_dir = Pathname.new(`pwd`.strip)
  until(next_dir == current_dir) do
    path = Dir.glob("#{current_dir}/#{name}").first
    return current_dir unless path.nil?
    current_dir = next_dir
    next_dir = next_dir.parent
  end
  return nil
end

#i_vars(key = '') ⇒ Object

Search through the instance variables for a key or if no key is given, return all the i-vars and their values

Parameters [key <String]: The key to search for

Returns the value of the key searched for



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cloud_powers/zenv.rb', line 53

def i_vars(key = '')
  name = to_i_var(key)

  # if no key is given, return a +Hash+ of all i-var/value pairs
  if key.empty?
    return self.instance_variables.inject({}) do |r, v|
      r.tap { |h| h[name] = self.instance_variable_get(name) }
    end
  end

  self.instance_variable_get(name)
end

#project_rootObject

PROJECT_ROOT should be set as early as possible in this Node’s initilize method. This method tries to search for it, using #zfind() and if a ‘nil` result is returned from that search, `pwd` is used as the PROJECT_ROOT.

Returns Pathname - path to the project root or where ever `pwd` resolves to for the caller

Notes

  • TODO: improve this…it needs to find the gem’s method’s caller’s project

root or at least the gem’s method’s caller’s file’s location.

Example

# called from cerebrum/cerebrum.rb in /home/ubuntu
project_root
# => '/home/ubuntu/cerebrum/'
# or
# called from go_nuts.rb#begin_going_crazy():(line -999999999.9) in /Users/crazyman/.ssh/why/all/the/madness/
project_root
# => '/Users/crazyman/.ssh/why/all/the/madness/'


86
87
88
89
90
91
92
93
94
# File 'lib/cloud_powers/zenv.rb', line 86

def project_root
  if @project_root.nil?
    file_home = Pathname.new(
      caller_locations.first.path.strip.split(/\//).last).realdirpath.parent
    # path = (zfind('PROJECT_ROOT') or file_home)
    @project_root = Pathname.new(file_home)
  end
  @project_root
end

#project_root=(var) ⇒ Object

Manually set the @project_root i-var as a Pathname object.

Parameters

  • String|Pathname - new path to the project root

Returns Pathname - @project_root

Example

project_root
# => '/home/ubuntu/cerebrum/'
project_root = Pathname.new(`pwd`)
project_root == `pwd`
# => true


110
111
112
# File 'lib/cloud_powers/zenv.rb', line 110

def project_root=(var)
  @project_root = Pathname.new(var)
end

#system_vars(key = '') ⇒ Object

Search through the system environment variables for a key or if no key is given, return all the system-env-vars and their values

Parameters

  • key String - the key to search for

Returns

  • if a key is given as a parameter, String

  • if no key is given as a parameter, Hash

with this structure { key => value, … } is returned for all keys with a value. Keys with no value are ommitted from the result.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cloud_powers/zenv.rb', line 125

def system_vars(key = '')
  name = to_snake(key).upcase
  if key.empty?
    # Separate key-value pairs from the large string received by `ENV`
    separate_pairs = `ENV`.split(/\n/).map do |string_pair|
      string_pair.split('=')
    end
    # Separate key-value pairs from each other into a hash of
    # { key => value, other_key => other_value }
    #   * keys with no value are removed
    separate_pairs.inject({}) do |res, pair|
      res.tap { |h_res| h_res[pair.first] = pair.last unless (pair.first == pair.last) }
    end
  else
    Object::ENV.has_key?(name) ? Object::ENV.fetch(name) : nil
  end
end

#zfind(key) ⇒ Object

ZFind looks for the key in a preditermined order of importance:

  • i-vars are considered first becuase they might be tracking different locations for multiple jobs or something like that.

  • dotenv files are second because they were manually set, so for sure it’s important

  • System Env variables are up next. Hopefully by this time we’ve found our information but if not, it should “search” through the system env too.

Parameters

  • key String|Symbol - the key to search for

Returns

  • String

Notes

  • TODO: implement a search for all 3 that can find close matches



159
160
161
162
# File 'lib/cloud_powers/zenv.rb', line 159

def zfind(key)
  project_root if @project_root.nil?
  i_vars(key) || env_vars(key) || system_vars(key)
end