Module: TddDeploy::Environ

Included in:
Base
Defined in:
lib/tdd_deploy/environ.rb

Defined Under Namespace

Classes: DataCache

Constant Summary collapse

ENV_FNAME =
'site_host_setup.env'

Instance Method Summary collapse

Instance Method Details

#capfileObject

Raises:

  • (::RuntimeError)


96
97
98
99
100
101
102
103
104
105
# File 'lib/tdd_deploy/environ.rb', line 96

def capfile
  raise ::RuntimeError.new('Attempt to access capfile data w/o capfile_paths defined') unless DataCache.env_hash['capfile_paths']
  unless DataCache.capfile
    DataCache.capfile = TddDeploy::Capfile.new
    DataCache.env_hash['capfile_paths'].each do |path|
      DataCache.capfile.load_recipes path
    end
  end
  DataCache.capfile
end

#clear_envObject

clears the environment hash. Really - it’s useless until re-initialized



243
244
245
# File 'lib/tdd_deploy/environ.rb', line 243

def clear_env
  DataCache.env_hash = {}
end

#env_defaultsObject

Hash of default values - which are hokey



201
202
203
# File 'lib/tdd_deploy/environ.rb', line 201

def env_defaults
  DataCache.env_defaults
end

#env_descObject



205
206
207
# File 'lib/tdd_deploy/environ.rb', line 205

def env_desc
  DataCache.env_desc
end

#env_hashObject

set up all the standard accessors lazy initialize DataCache.env_hash



80
81
82
83
# File 'lib/tdd_deploy/environ.rb', line 80

def env_hash
  read_env || reset_env unless defined?(DataCache.env_hash)
  DataCache.env_hash
end

#env_hash=(hash) ⇒ Object

Raises:

  • (::ArgumentError)


85
86
87
88
89
90
91
92
93
94
# File 'lib/tdd_deploy/environ.rb', line 85

def env_hash=(hash)
  raise ::ArgumentError.new("env_hash=(): arg must be a hash") unless hash.is_a? Hash
  if !(tmp = hash.keys - DataCache.env_types.keys).empty?
    raise ::ArgumentError.new("env_hash=(): Illegal Keys in value: #{tmp.join(',')}")
  elsif !(tmp = DataCache.env_types.keys - hash.keys).empty?
    raise ::ArgumentError.new("env_hash=(): Missing Keys in value: #{tmp.join(',')}")
  else
    DataCache.env_hash = hash
  end
end

#env_typesObject

Hash mapping environment variable to type



196
197
198
# File 'lib/tdd_deploy/environ.rb', line 196

def env_types
  DataCache.env_types
end

#hostsObject

accessors for all defined env variables



396
397
398
# File 'lib/tdd_deploy/environ.rb', line 396

def hosts
  (self.web_hosts.to_a + self.db_hosts.to_a + self.balance_hosts.to_a + self.app_hosts.to_a).uniq.sort
end

#hosts=(list) ⇒ Object



400
401
402
403
404
405
406
407
408
409
# File 'lib/tdd_deploy/environ.rb', line 400

def hosts=(list)
  if (self.web_hosts.nil? && self.db_hosts.nil?) || self.web_hosts == self.db_hosts
    self.web_hosts =
      self.db_hosts =
        self.app_hosts = self.str_to_list(list)
    self.balance_hosts = []
  else
    raise ::RuntimeError.new("Cannot assign value to 'hosts' if web_hosts &/or db_hosts already set.\n web_hosts: #{self.web_hosts}\n db_hosts: #{self.db_hosts}")
  end
end

#list_to_str(key) ⇒ Object

packs an array into a comma separated string



318
319
320
321
# File 'lib/tdd_deploy/environ.rb', line 318

def list_to_str key
  tmp = self.env_hash[key]
  tmp.is_a?(Array) ? tmp.join(',') : tmp.to_s
end

#migration_hostsObject



411
412
413
# File 'lib/tdd_deploy/environ.rb', line 411

def migration_hosts
  self.capfile.migration_host_list
end

#rationalize_host_list(host_list_or_list_name) ⇒ Object

takes the name of a list (as a string or symbol), a single string, or an array of host names. If it’s an array, then returns uniq-ified array of strings [to handle the uniion of lists]



417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/tdd_deploy/environ.rb', line 417

def rationalize_host_list(host_list_or_list_name)
  if host_list_or_list_name.is_a? String
    return self.respond_to?(host_list_or_list_name.to_sym) ? self.send(host_list_or_list_name.to_sym) :
      [host_list_or_list_name]
  elsif host_list_or_list_name.is_a? Symbol
    return self.respond_to?(host_list_or_list_name) ? self.send(host_list_or_list_name) :
          [host_list_or_list_name.to_s]
  elsif host_list_or_list_name.is_a? Array
    return host_list_or_list_name.map { |host| host.to_s }.uniq
  else
    raise ArgumentError.new("rationalize_host_list(#{host_list_or_list_name.inspect}) is invalid")
  end
end

#read_envObject

reads the environment from TddDeploy::Environ::ENV_FNAME (site_host_setup.env) if the file exists someplace between the current directory and the root of the filesystem



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/tdd_deploy/environ.rb', line 255

def read_env
  dir_path = Dir.pwd
  DataCache.env_hash ||= {}
  loop do
    path = File.join dir_path, TddDeploy::Environ::ENV_FNAME
    if File.exists? TddDeploy::Environ::ENV_FNAME
      line_no = 0
      if f = File.new(path, 'r')
        begin
          f.each do |line|
            line_no += 1
            if line =~ /^\s*(\w+)\s*=\s*(.*?)\s*$/
              key = $1.downcase
              if self.env_types.keys.include? key
                self.send "#{key}=".to_sym, $2
                # self.env_hash[key] = self.env_types[key] == :list ? self.str_to_list($2) : $2.to_s
              else
                raise ::ArugmentError.new("TddDeploy::Environ#read_env: Error in #{TddDeploy::Error::ENV_FNAME}: #{line_no}: Illegal Key: #{key}")
              end
            else
              raise ::ArugmentError.new("TddDeploy::Environ#read_env: Error in #{TddDeploy::Error::ENV_FNAME}: #{line_no}: Unmatched Line: #{line}}")
            end
          end
        ensure
          f.close
        end
        # add any missing env keys
        (self.env_types.keys - self.env_hash.keys).each do |key|
          case self.env_types[key]
          when :pseudo then next
          when :capfile then next
          when :list
            self.env_hash[key] = str_to_list(self.env_defaults[key])
          else
            self.env_hash[key] = self.env_defaults[key]
          end
        end
        return self.env_hash
      else
        raise ::RuntimeError.new("Unable to open #{path} for reading")
      end
    elsif dir_path.length <= 1
      # reached root level, so initialize to defaults and exit
      return nil
    else
      # move to parent directory
      dir_path = File.expand_path('..', dir_path)
    end
  end
  nil
end

#reset_envObject

reset_env resets env_hash to env_defaults



248
249
250
251
# File 'lib/tdd_deploy/environ.rb', line 248

def reset_env
  clear_env
  set_env self.env_defaults
end

#save_envObject

saves the current environment in the current working directory in the file ‘site_host_setup.env’ [aka TddDeploy::Environ::ENV_FNAME]



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/tdd_deploy/environ.rb', line 326

def save_env
  f = File.new(TddDeploy::Environ::ENV_FNAME, "w")
  self.env_types.keys.sort.each do |k|
    v = self.env_hash[k] || ''
    case self.env_types[k]
    when :int then f.write "#{k}=#{v}\n"
    when :string then f.write "#{k}=#{v}\n"
    when :list then
      f.write "#{k}=#{self.list_to_str(k)}\n" unless k == 'hosts'
    when :pseudo then next
    when :capfile then next
    else
      raise ::RuntimeError.new("unknown key: #{k}")
    end
  end
  f.close
end

#set_env(value_hash = {}) ⇒ Object

set_env(value_hash {}) - convenience method which sets values of the environment hash using a hash rather than one-at-a-time



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/tdd_deploy/environ.rb', line 211

def set_env(value_hash = {})
  DataCache.env_hash ||= {}
  value_hash.each do |k, v|
    k = k.to_s
    case self.env_types[k]
    when :int then DataCache.env_hash[k] = v.to_i
    when :string then DataCache.env_hash[k] = v.to_s
    when :list then DataCache.env_hash[k] = self.str_to_list(v)
    when :capfile then next
    when :pseudo then
      if k == 'hosts'
        if (tmp = DataCache.env_hash['web_hosts']) == DataCache.env_hash['db_hosts'] \
            &&  [] == DataCache.env_hash['balance_hosts'] \
            &&  tmp == DataCache.env_hash['app_hosts']
          DataCache.env_hash['web_hosts'] =
            DataCache.env_hash['db_hosts'] =
              DataCache.env_hash['app_hosts'] = self.str_to_list(v)
          DataCache.env_hash['balance_hosts'] = []
        else
          raise ::RuntimeError.new("#{self}#reset_env(): Cannot assign value to 'hosts' if web_hosts &/or db_hosts already set.\n web_hosts: #{DataCache.env_hash['web_hosts']}\n db_hosts: #{DataCache.env_hash['db_hosts']}")
          # raise RuntimeError.new("Cannot change hosts key if web_hosts != db_hosts")
        end
      else
        next
      end
    else
      raise ::ArgumentError.new("#{self}#reset_env(): Illegal environment key: #{k}")
    end
  end
end

#str_to_list(str) ⇒ Object

bursts comma/space separated string into a sorted, unique array



308
309
310
311
312
313
314
315
# File 'lib/tdd_deploy/environ.rb', line 308

def str_to_list str
  case
  when str.is_a?(String) then str.split(/[\s,]+/).uniq.sort
  when str.is_a?(Array) then str.uniq.sort
  else
    raise ::ArgumentError.new("str_to_list: #{str}")
  end
end