Class: FactTree

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/opensecret/plugins.io/facts/fact.tree.rb

Overview

– – —————– – Fact Production – —————– – – The fact tree is tasked with fact production – (population). Fact production is done by [consuming] – – [1] - simple string, number of boolean facts – [2] - facts already produced – [3] - identity facts from command line and environment – [4] - software (behaviour) that derives facts – [5] - inherited (extended) facts from (OO) parents – – – – —————————————– – The 4 Universal (DevOps) Creation Facts – —————————————– – – No matter the (DevOps) eco-system being fabricated, these four – facts prevail and stand out. – – Every time a DevOps [eco-system] is created, cloned, or recovered, – a small cluster of core facts endure to define the instance and act – as the identification basis of all new eco-system resources. – – The 4 facts underpinning eco-system creation are – – [1] - [what] is being built – [2] - [when] did the building begin – [3] - [who] instigated it (and from) – [4] - [which] workstation. – – ————————————— – DevOps 4 Creational [Instance] Facts – ————————————— – – The core instance identities used and reused time and again relate to – – [1] - plugin (eg) wordpress.hub or jenkins.hub – [2] - time (eg) 18036.0952.065 – [3] - user (eg) peterpan – [4] - workstation (eg) laptop_susie or hp_desktop – –

Constant Summary collapse

@@eval_prefix =
"e>>"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fObject (readonly)

Returns the value of attribute f.



53
54
55
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 53

def f
  @f
end

#iObject (readonly)

Returns the value of attribute i.



53
54
55
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 53

def i
  @i
end

Class Method Details

.populateObject

– – Call instantiate() before calling populate(). – – This method assimilates any and all fact files – (recursively) found in two subdirectories – – [1] - “reusable facts” and – [2] - the [plugin] folder –



271
272
273
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 271

def self.populate

end

Instance Method Details

#add_dir_to_identity(key_directory) ⇒ Object

– – Set the main directory path. – This method adds the parameter path to – the identity map. After this method – executes you will be able to access the – directory with @i



96
97
98
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 96

def add_dir_to_identity key_directory
  @i[:dir] = key_directory
end

#add_fact(group_symbol, key_symbol, key_value) ⇒ Object

– —————————————- – # – Add a fact to the main 2D fact database. – # – —————————————- – #



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 316

def add_fact group_symbol, key_symbol, key_value

  nil_error_text = "Neither fact coordinates nor value can be nil. [group]=> #{group_symbol} [key]=> #{key_symbol} [value]=> #{key_value}"
  raise ArgumentError.new nil_error_text if group_symbol.nil? || key_symbol.nil? || key_value.nil?

  if @f.has_key? group_symbol then

    # --
    # -- This isn't the first fact within this group
    # -- so store the new fact key/value pair within
    # -- the group's namespace.
    # --
    @f[group_symbol][key_symbol] = key_value

  else

    # --
    # -- Create a new umbrella grouping against which
    # -- the new key-value pairing will be inserted.
    # --
    @f.store group_symbol, { key_symbol => key_value }

  end

  # --
  # -- The @s sibling hash is updated to reflect the
  # -- key-value pairs within the current group. This
  # -- allows @s to be used as shorthand within INI
  # -- file fact definition statements.
  # --
  @s = @f[group_symbol]

end

#assimilate_fact(group_str, key_str, input_value) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 428

def assimilate_fact group_str, key_str, input_value

  grp_symbol = group_str.gsub(".", "_").to_sym
  key_symbol = key_str.gsub(".", "_").to_sym

  Throw.if_nil input_value
  fact_val = input_value.strip

  begin

    unless discard_fact?( group_str, key_str, fact_val )
      eval_value = evaluate(get_fact_value(fact_val))
      add_fact grp_symbol, get_key_symbol(key_str), eval_value
    end

  rescue Exception => e

    log.fatal(ere) { "## ##################### #################################" }
    log.fatal(ere) { "## Fact Evaluation Error #################################" }
    log.fatal(ere) { "## ##################### #################################" }
    log.fatal(ere) { "## Fact Family => #{group_str}"                             }
    log.fatal(ere) { "## Fact Key    => #{key_str}"                               }
    log.fatal(ere) { "## Fact Stmt   => #{fact_val}"                              }
    log.fatal(ere) { "## ##################### #################################" }

    log.fatal(ere) { e.message }
    log.fatal(ere) { e.backtrace.inspect }

    raise e

  end

  unless @f.has_key? grp_symbol then

    log.debug(ere){ "# -- ---------------------------------------------------------- #" }
    log.debug(ere){ "# -- the [#{group_str}] silo facts." }
    log.debug(ere){ "# -- ---------------------------------------------------------- #" }

  end

  id_keystring = "#{grp_symbol}#{key_symbol}".downcase
  sensitive = id_keystring.includes_any? [ "secret", "password", "credential", "creds" ]
  print_value = "****************"
  print_value = eval_value unless sensitive

  # -- -------------------------------------------------------- -- #
  # -- Log the human readable (fixed-width) key and value pair. -- #
  # -- -------------------------------------------------------- -- #
  fw_key = sprintf '%-33s', "@f[:#{grp_symbol}][:#{key_symbol}]"
  log.debug(ere){ "#{fw_key} => #{print_value}" }

end

#assimilate_general_factsObject

– – –



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 117

def assimilate_general_facts

  facts_folder = fact_path("published.facts")

  factfiles_map = Files.in_folders( Array.new.push(facts_folder) )
  factfiles_map.each do |file_name, folder_name|

    log.info(ere) {"Assimilating facts from => [#{file_name}]"}
    factfile_path = File.join factfiles_map[file_name], file_name
    log.info(ere) {"Factfile location => [#{nickname factfile_path}]"}
    assimilate_ini_file factfile_path

  end

end

#assimilate_ini_file(ini_filepath) ⇒ Object

– ——————————————- – # – – # – Template – # – – # – The longest river in africa is without – # – doubt the @[africa|longest.river]. Now – # – @[south.america|most.spoken] is the – # – most common language in south america. – # – – # – The population of the americas – # – is @[americas|population] according to – the Harvard 2015 census. – – # – ——————————————- – # – – # – Ruby Code – # – – # – double_north_america_pop = @f[:population] * 2 – – # – ——————————————- – # – Parameters – # – factory_facts : instantiated 2D hash – # – ini_filepath : path to factfile to read – # – – # – Dependencies and Assumptions – # – the [inifile] gem is installed – # – file exists at the ini_filepath – # – factory_facts are instantiated – # – identity facts like @i exist – # – ——————————————- – #



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 381

def assimilate_ini_file ini_filepath

  fact_filename = File.basename ini_filepath
  log_begin fact_filename

  no_file = "No (ini) factfile found here => #{ini_filepath}"
  raise ArgumentError.new no_file unless File.exists? ini_filepath

  # --
  # -- Use the inifile gem to parse and read the fact
  # -- file contents into Ruby's map structures.
  # --
  begin

    map_facts = IniFile.load ini_filepath

  rescue Exception => e

    log.fatal(ere) { "## ############################ #################################" }
    log.fatal(ere) { "## Fact File Assimilation Error #################################" }
    log.fatal(ere) { "## ############################ #################################" }
    log.fatal(ere) { "## File  => #{ini_filepath}"                                       }
    log.fatal(ere) { "## ############################ #################################" }

    log.fatal(ere) { e.message }
    log.fatal(ere) { e.backtrace.inspect }

    exit

  end

  # -- ------------------------------------------------------------ -- #
  # -- Copy all ini file facts to the global eco dictionary using a -- #
  # -- format of ==]  group_id.key_id = value                       -- #
  # -- ------------------------------------------------------------ -- #
  map_facts.each do | group_str, key_str, input_value |

    assimilate_fact group_str, key_str, input_value

  end

  log_end fact_filename

end

#assimilate_instance_factsObject

– – Assimilate [[ hub-runtime.ini ]] – This factfile builds upon the (fundamental) handful of – identity facts. It derives the facts for – – - the plugin instance folder – - the instance stamp id keys – – The instance fact file is not allowed to harbour any – external dependencies. –



145
146
147
148
149
150
151
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 145

def assimilate_instance_facts

  instance_facts_file = fact_path("hub-runtime.ini")
  log.info(ere) { "Path to instance facts file => #{instance_facts_file}" }
  assimilate_ini_file instance_facts_file

end

#assimilate_plugin_factsObject

– – –



105
106
107
108
109
110
111
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 105

def assimilate_plugin_facts

  plugin_fact_filepath = File.join @i[:src_dir], "#{@i[:plugin]}.ini"
  log.info(ere) { "Plugin Factfile => #{plugin_fact_filepath}" }
  assimilate_ini_file plugin_fact_filepath

end

#assimilate_station_factsObject

– – Knowing and setting @i to our workstation is key. – This means that the other workstations can reference files in – each others drives without knowing which workstation is being – used (see ssh.keyfile for a ghost reference example). – – Now we can proceed to assimilate the entire station facts file. –



239
240
241
242
243
244
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 239

def assimilate_station_facts

  stations_factfile = fact_path "known-hosts.ini"
  assimilate_ini_file stations_factfile

end

#colonObject

– – colon => full colon character – Delivers a “:” colon character –



604
605
606
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 604

def colon
  return ":"
end

#discard_fact?(group_str, key_str, input_value) ⇒ Boolean

Returns:

  • (Boolean)


482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 482

def discard_fact? group_str, key_str, input_value

  is_if = key_str.strip.end_with? "(if)"
  is_unless = key_str.strip.end_with?("(un)") || key_str.strip.end_with?("(unless)")
  return false unless is_if || is_unless

  two_parts = input_value.split( @@eval_prefix )
  no_prefix = "Conditional must be followed by eval prefix => #{two_parts}"
  raise RuntimeError.new no_pefix unless two_parts.length == 2

  conditional_part = two_parts.first.strip
  no_curlies_msg = "Wrap conditional in curly braces => #{conditional_part}"
  is_wrapped = conditional_part.start_with?("{") && conditional_part.end_with?("}")
  raise RuntimeError.new no_curlies_msg unless is_wrapped

  conditional_stmt = conditional_part[1..-2].strip
  log.info(ere){ "Conditional Stmt => #{conditional_stmt}" }
  conditional_val = evaluate( @@eval_prefix + conditional_stmt )
  is_boolean = [true, false].include? conditional_val

  boolean_msg = "=> #{conditional_stmt} <= evaluated to [#{conditional_val}] not boolean."
  raise RuntimeError.new boolean_msg unless is_boolean

  return false if is_if && conditional_val
  return true if is_if && !conditional_val

  return true if is_unless && conditional_val
  return false if is_unless && !conditional_val

  raise RuntimeError.new "Unreachable if[#{is_if}] unless[#{is_unless}] => #{conditional_val}"

end

#dotObject

– – dot => period (full stop) – Deliver a “.” period character –



595
596
597
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 595

def dot
  return "."
end

#evaluate(string) ⇒ Object

– ————————————————————– – # – Eval and return result of parameter string if it is ruby code. – # – ————————————————————– – #



279
280
281
282
283
284
285
286
287
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 279

def evaluate string

  # -----> @todo raise a FactError here

  raise RuntimeError.new "Fact to Evaluate is Nil." if string.nil?
  return string unless string.start_with? @@eval_prefix
  return eval( string.gsub @@eval_prefix, "" )

end

#exists?(fact_reference) ⇒ Boolean

Returns:

  • (Boolean)


581
582
583
584
585
586
587
588
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 581

def exists? fact_reference

  log.info(ere) { "Does Fact Exist? => #{fact_reference}" }
  return false if fact_reference.nil?
  return false if fact_reference.strip.length == 0
  return true if fact_reference.strip.length > 0

end

#fact_path(fact_filename) ⇒ Object

– – Get the path to a fact file or folder within the – reusable facts source tree. – – The base directory is acquired by dropping down – three (3) directory levels from where this module – is and then going into reusable.facts and finally – appending the parameter path (file or folder). –



256
257
258
259
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 256

def fact_path fact_filename
  fact_basedir = File.dirname( File.dirname( File.dirname( __FILE__ ) ) )
  return File.join( File.join(fact_basedir,"reusable.facts"), fact_filename )
end

#get_fact_value(fact_value_string) ⇒ Object



571
572
573
574
575
576
577
578
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 571

def get_fact_value fact_value_string

  is_conditional = fact_value_string.start_with?("{") && fact_value_string.include?( @@eval_prefix )
  return fact_value_string unless is_conditional

  return fact_value_string.rpartition( @@eval_prefix )[1..-1].join

end

#get_key_symbol(dirty_key_string) ⇒ Object



559
560
561
562
563
564
565
566
567
568
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 559

def get_key_symbol dirty_key_string

  key_string = dirty_key_string.strip
  split_string = "(unless)"
  split_string = "(if)" if key_string.end_with? "(if)"
  split_string = "(un)" if key_string.end_with? "(un)"

  return key_string.split(split_string).first.gsub(".", "_").to_sym

end

#identify_my_workstationObject

– – ———————————– – Finding the Workstation (Category) – ———————————– – – The known hosts facts file is parsed with each – section [examined] by asking two (2) questions. – – [1] - is [username] this workstation’s username? – [2] - does [hostnames] include this hostname? – – The 1st section to answer [YES] to both questions – is deemed as our workstation. Its symbol header is – then used for assimilation - how? – – —————————————- – Assimilating Workstation Specific Facts – —————————————- – – We do NOT assimilate the whole file. – We simply assimilate the [SECTION] of the file – that is deemed to be OUR (this) workstation. – – ——————————————- – Dependency Constraints | Workstation Facts – ——————————————- – – (work)station facts can [only] depend on facts that – have been declared in [[ hub-runtime.ini ]]. No – other external fact dependencies are permitted. –



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 185

def identify_my_workstation

  workstation_factfile_name = "known-hosts.ini"
  log_begin_workstation_identification workstation_factfile_name
  stations_factfile = fact_path workstation_factfile_name
  log.info(ere) { "Workstations Facts File (Path) => #{nickname stations_factfile}" }

  our_user = Home.usr
  our_host = NetDns.instance.host_name
  log.info(ere){ "Station Search For => #{our_user}@#{our_host}" }

  workstations = IniFile.load stations_factfile
  workstations.each_section do |station|

    no_user = "Workstation [#{station}] has no [username] fact."
    raise RuntimeError.new no_user unless workstations[station].has_key? "username"

    has_hostnames = workstations[station].has_key? "hostnames"
    no_hostnames = "[#{station}] not a workstation as no [hostnames] fact - Skip!"
    log.warn(ere) { no_hostnames } unless has_hostnames
    next unless has_hostnames

    this_user = evaluate(workstations[station]["username"])
    these_hosts = evaluate(workstations[station]["hostnames"])
    match = this_user.eql?(our_user) && these_hosts.include?(our_host)

    log.info(ere){ "[#{station}] with #{this_user}@#{these_hosts} - No!" } unless match
    next unless match

    log.info(ere){ "[#{station}] with #{this_user}@#{these_hosts} - Matched!" }
    dup_msg = "Duplicate or repeat match disallowed! #{@i[:station]} and #{station}"
    raise RuntimeError.new dup_msg unless @i[:station].nil?

    @i[:station] = station
    @i[:workstation] = station.gsub(".", "_").to_sym

  end

  no_match_msg = "No workstation match made for [#{our_user}@#{our_host}]."
  raise RuntimeError.new no_match_msg if @i[:station].nil?
  log_end_workstation_identification workstation_factfile_name
  LogObject.map @i

end

#instantiate(plugin_id, plugin_folder) ⇒ Object

– – Initialize the internal fact database – which is exposed for fact consumption – via – – @i => [identity] facts (1D) – @n => (reserved perhaps 4 nested facts) – @f => [file facts] database (2D) – @a => (reserved) – @c => coded consumtion (in software) – @t => template placeholders (2D) – @s => sibling (same category) facts (1D) –



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 68

def instantiate plugin_id, plugin_folder

  @i = { :plugin  => plugin_id,
         :time    => Stamp.yyjjj_hhmm_sst,
         :user    => Home.usr,
         :src_dir => plugin_folder,
         :station => nil
       }

  @f = {}

  @s = {}

  @f.store symbol(plugin_id), {}
  @p = @f[symbol(plugin_id)]


end

#log_begin(the_filename) ⇒ Object



635
636
637
638
639
640
641
642
643
644
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 635

def log_begin the_filename

  log.info(ere) { "-                                                         -" }
  log.info(ere) { "# @@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@ #" }
  log.info(ere) { "# -- ------------------------------------------------- -- #" }
  log.info(ere) { "# -- [= BEGIN THE ASSIMILATION =] #{the_filename}"          }
  log.info(ere) { "# -- ------------------------------------------------- -- #" }
  log.info(ere) { "# @@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@ #" }

end

#log_begin_workstation_identification(the_filename) ⇒ Object



609
610
611
612
613
614
615
616
617
618
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 609

def log_begin_workstation_identification the_filename

  log.info(ere) { "-                                                         -" }
  log.info(ere) { "# @@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@ #" }
  log.info(ere) { "# -- ------------------------------------------------- -- #" }
  log.info(ere) { "# -- [= BEGIN WORKSTATION IDENTIFICATION =] #{the_filename}" }
  log.info(ere) { "# -- ------------------------------------------------- -- #" }
  log.info(ere) { "# @@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@ #" }

end

#log_end(the_filename) ⇒ Object



647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 647

def log_end the_filename

  log.info(ere) { "# @@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@ #" }
  log.info(ere) { "# -- ------------------------------------------------- -- #" }
  log.info(ere) { "# -- [= END ASSIMILATION =] #{the_filename}"                }
  log.info(ere) { "# -- ------------------------------------------------- -- #" }
  log.info(ere) { "# @@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@ #" }
  log.info(ere) { "-                                                         -" }
  log.info(ere) { "-                                                         -" }
  log.info(ere) { "-                                                         -" }

end

#log_end_workstation_identification(the_filename) ⇒ Object



621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 621

def log_end_workstation_identification the_filename

  log.info(ere) { "# @@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@ #" }
  log.info(ere) { "# -- ------------------------------------------------- -- #" }
  log.info(ere) { "# -- [= END WORKSTATION IDENTIFICATION =] #{the_filename}"   }
  log.info(ere) { "# -- ------------------------------------------------- -- #" }
  log.info(ere) { "# @@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@ #" }
  log.info(ere) { "-                                                         -" }
  log.info(ere) { "-                                                         -" }
  log.info(ere) { "-                                                         -" }

end

#plugin_symbol(append_string) ⇒ Object

– – Return the (key access) symbol that represents the – plugin id plus a string given in the parameter. – – ——— – Example – ——— – – Plugin Id => ide.hub – Append Str => host – – equals – – Key String => ide.hub.host – Key Symbol => ide_hub_host – – – So the symbol :ide_hub_host is returned. –



551
552
553
554
555
556
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 551

def plugin_symbol append_string

  key_string = @i[:plugin] + "." + append_string.strip
  return key_string.gsub(".", "_").to_sym

end

#symbol(from_string) ⇒ Object

– – Return the symbolic representation of the – parameter key. Periods are replaced with – underscores (as per the fact convention) – before conversion to a symbol. – – Nil is not handled. – String is stripped beforehand just in case. –



525
526
527
528
529
# File 'lib/opensecret/plugins.io/facts/fact.tree.rb', line 525

def symbol from_string

  return from_string.strip.gsub(".", "_").to_sym

end