Class: EcoFaculty

Inherits:
Object
  • Object
show all
Defined in:
lib/opensecret/commons/eco.faculty.rb

Overview

– – eco-system [provisioning] begins in earnest here. By making – a [super] call (at the beginning, middle or end) - eco-systems – can extend the functionality provided here. – – To prevent this code running, child classes must provide their – own provision along an (optional) alternative implementation. –

Direct Known Subclasses

EcoSystem

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#eco_id_strObject (readonly)

Returns the value of attribute eco_id_str.



13
14
15
# File 'lib/opensecret/commons/eco.faculty.rb', line 13

def eco_id_str
  @eco_id_str
end

Instance Method Details

#configure_aws_credentialsObject

– – Assimilate and configure aws cloud credentials. – Call this method only when it is certain that the – hub fact [aws.creds.dir] exists. – – By convention this method expects the decrypted – AWS user credential INI file to be inside the above – dir with name <<plugin.id>>.aws.credentials.ini –



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/opensecret/commons/eco.faculty.rb', line 80

def configure_aws_credentials

  # --
  # -- Before the rubber can hit the road - we must
  # -- first read in the secret credentials. To avoid
  # -- logging the secrets we read it up into its own
  # -- isolated mini fact database.
  # --
  # -- The aws creds database lives just long enough
  # -- to programmatical configure the AWS IAM user.
  # --
  FactTree.instance.assimilate_ini_file @c[:aws][:creds_path]
  aws_creds = FactTree.instance.f
  log.info(ere) { "AWS Region Key => #{aws_creds[@eco_id_sym][:aws_region_key]}" }

  # --
  # -- Now the rubber can hit the road and we configure
  # -- the AWS Ruby SDK with the IAM credentials that
  # -- have been recovered.
  # --
  Aws.use_bundled_cert!
  Aws.config.update({
    :access_key_id     => aws_creds[@eco_id_sym][:aws_access_key],
    :secret_access_key => aws_creds[@eco_id_sym][:aws_secret_key],
    :region            => aws_creds[@eco_id_sym][:aws_region_key]
  })

  # --
  # -- Temporarily add the AWS credentials to the environment
  # -- variables so that infrastructure provisioning tools like
  # -- Terraform can access the cloud cedentials.
  # --
  EnvVar.new.write_env_var "AWS_ACCESS_KEY_ID",     aws_creds[@eco_id_sym][:aws_access_key]
  EnvVar.new.write_env_var "AWS_SECRET_ACCESS_KEY", aws_creds[@eco_id_sym][:aws_secret_key]
  EnvVar.new.write_env_var "AWS_DEFAULT_REGION",    aws_creds[@eco_id_sym][:aws_region_key]

end

#db_fact_exists?(group_symbol, key_symbol) ⇒ Boolean

— ———————————————————- — # — Return [true] if a fact exists that — # — [1] DOES NOT ASSUME FACT IS A STRING — [1] carries the primary symbol key in 1st parameter — # — [2] carries the secondary symbol key in 2nd parameter — # — [3] is neither empty nor solely whitespace — # — ———————————————————- — #

Returns:

  • (Boolean)


270
271
272
273
274
# File 'lib/opensecret/commons/eco.faculty.rb', line 270

def db_fact_exists? group_symbol, key_symbol
  return false unless @c.has_key? group_symbol
  fact_value = @c[group_symbol][key_symbol]
  return !fact_value.nil?
end

#e_fact(key_symbol) ⇒ Object

— ———————————————- — # — Return the fact that is — # — [1] under the current eco plugin banner — # — [2] has the key symbol in the parameter — # — ———————————————- — #



221
222
223
224
225
# File 'lib/opensecret/commons/eco.faculty.rb', line 221

def e_fact key_symbol
  error_msg = "fact 4 group => #{@eco_id_sym} + key => #{key_symbol} not in db."
  raise ArgumentError.new "\n\n#{error_msg}\n\n" unless eco_fact_exists? key_symbol
  return @c[@eco_id_sym][key_symbol]
end

#eco_fact_exists?(key_symbol) ⇒ Boolean

— —————————————————— — # — Return [true] if a fact exists that is — # — [1] under the banner of the current plugin id — # — [2] with a key symbol matching the parameter — # — [3] is neither empty nor solely whitespace — # — —————————————————— — #

Returns:

  • (Boolean)


258
259
260
# File 'lib/opensecret/commons/eco.faculty.rb', line 258

def eco_fact_exists? key_symbol
  return db_fact_exists? @eco_id_sym, key_symbol
end

#get_eco_fact(key_symbol) ⇒ Object

— ———————————————- — # — Return the factthat is — # — [1] under the current eco plugin banner — # — [2] has the key symbol in the parameter — # — ———————————————- — #



209
210
211
212
213
# File 'lib/opensecret/commons/eco.faculty.rb', line 209

def get_eco_fact key_symbol
  error_msg = "fact 4 group => #{@eco_id_sym} + key => #{key_symbol} not in db."
  raise ArgumentError.new "\n\n#{error_msg}\n\n" unless eco_fact_exists? key_symbol
  return @c[@eco_id_sym][key_symbol]
end

#instantiate_runtime(core_db) ⇒ Object

– – This key (instantiation) behaviour – – 1 - [CREATES] the runtime plugin folder [then] – 2 - [COPIES] into it files from the plugin source folder [and] – 3 - [NOTIFY] (point) log utility to new (plugin dir) logfile – – Before continuing the fact assimilation process we now – know enough to instantiate the plugin runtime folder and – start logging to a file within it. –



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/opensecret/commons/eco.faculty.rb', line 130

def instantiate_runtime core_db

  Throw.if_exists core_db[:runtime][:dir]

  # --
  # -- Create the new runtime directory then
  # -- Copy asset templates into it.
  # -- Note that these templates contain unresolved fact directives.
  # --
  FileUtils.mkdir_p core_db[:runtime][:dir] unless File.exists? core_db[:runtime][:dir]
  FileUtils.copy_entry @plugin_path, core_db[:runtime][:dir]
  FactTree.instance.add_dir_to_identity core_db[:runtime][:dir]

  # --
  # -- Evaluate the absolute [logfile path] within
  # -- the eco system instance runtime directory.
  # --
  # -- Then tell the logging.rb MIXIN that the new logfile
  # -- at (logfile_path) is ready to be used.
  # -- This MIXIN affects all classes that have log write statements
  # --
  logfile_path = File.join core_db[:runtime][:dir], "#{core_db[:runtime][:dirname]}.log"
  log.info(ere) { "[LAST] - The last logging statement going to STDOUT but NOT logfile." }
  set_logfile_path logfile_path
  log.info(ere) { "[FIRST] - The first log going to [both] STDOUT and a logfile." }
  log.info(ere) { "logfile => #{logfile_path}" }
  log.info(ere) { "Log Level is now set @ => #{log.level.to_s}" }

end

#plugin_fact(key_symbol) ⇒ Object

— ———————————————- — # — Return the fact that is — # — [1] under the current eco plugin banner — # — [2] has the key symbol in the parameter — # — ———————————————- — #



233
234
235
236
237
# File 'lib/opensecret/commons/eco.faculty.rb', line 233

def plugin_fact key_symbol
  error_msg = "fact 4 group => #{@eco_id_sym} + key => #{key_symbol} not in db."
  raise ArgumentError.new "\n\n#{error_msg}\n\n" unless eco_fact_exists? key_symbol
  return @c[@eco_id_sym][key_symbol]
end

#plugin_fact_exists?(key_symbol) ⇒ Boolean

– – Return [true] if a fact exists that is – – [1] under the banner of the current plugin id – [2] with a key symbol matching the parameter – [3] is neither empty nor solely whitespace –

Returns:

  • (Boolean)


247
248
249
# File 'lib/opensecret/commons/eco.faculty.rb', line 247

def plugin_fact_exists? key_symbol
  return db_fact_exists? @eco_id_sym, key_symbol
end

#plugin_src_dirObject

– – Get the folder path to the eco-system [plugin] – source directory. – – Also see the plugin runtime directory which is – the runtime folder created by the method named – => EcoFaculty.instantiate_runtime – – ————- – Dependency – ————- – – The method :core_provisioning in the plugin must – exist and must have been “Required”. –



30
31
32
33
34
# File 'lib/opensecret/commons/eco.faculty.rb', line 30

def plugin_src_dir

  return File.dirname self.method(:core_provisioning).source_location.first

end

#provisionObject

– – Facts are the business of this eco-system behaviour class. – This provision method will collect the eco-system plugin id – and from that assimilate all known fact files. –



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/opensecret/commons/eco.faculty.rb', line 42

def provision

  @eco_id_str = SnapFlat.do self.class.name
  @eco_id_sym = @eco_id_str.gsub(".", "_").to_sym
  @plugin_path = plugin_src_dir

  FactTree.instance.instantiate @eco_id_str, @plugin_path
  FactTree.instance.assimilate_instance_facts

  instantiate_runtime FactTree.instance.f

  FactTree.instance.identify_my_workstation
  FactTree.instance.assimilate_station_facts
  FactTree.instance.assimilate_plugin_facts
  FactTree.instance.assimilate_general_facts

  @c = FactTree.instance.f
  @i = FactTree.instance.i
  @p = FactTree.instance.f[@eco_id_sym]

  # --
  # -- assimilate and configure aws cloud credentials
  # -- if the plugin fact [aws.creds.dir] exists.
  # --
  configure_aws_credentials if @c[:aws][:creds_exist?]

end

#read_block_facts(module_path, method_name, grp_sym, key_sym, key_val) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/opensecret/commons/eco.faculty.rb', line 348

def read_block_facts module_path, method_name, grp_sym, key_sym, key_val

  # -- ---------------------------------------------------------- -- #
  # -- Derive name of block db ini file using id and method name. -- #
  # -- ---------------------------------------------------------- -- #
  block_filepath = FactLocator.block_factdb_path module_path, method_name
  Throw.if_not_exists block_filepath

  # -- ----------------------------------------------------------- -- #
  # -- Initialize block database building upon the wider scoped @e -- #
  # -- ----------------------------------------------------------- -- #
  FactReader.new @c, block_filepath, grp_sym, key_sym, key_val

end

#read_propertiesObject

– – If eco plugins wish to update only [SOME] of the properties – within [application.properties] then they should set – – fact key => @c[@c[:id]] – fact val => “/path/to/application.properties – – The property file will be assimilated and the key / value – pairs will be attached under the :app_properties grouping – symbol. One o more facts can be updated (overwritten) and – later written back (see write_app_properties) –



298
299
300
301
302
303
# File 'lib/opensecret/commons/eco.faculty.rb', line 298

def read_properties

  return unless eco_fact_exists? :props_src_path
  @fact_db.assimilate_property_file e_fact(:props_src_path), :app_properties

end

#replace_placeholdersObject

– – Scan every file in the eco plugin directory in search of – fact placeholder keys contained in the fact database. – – Once found, replace them with the corresponding value. – – ———– – Warning – ———– – This behaviour is [NOT RECURSIVE]. – Only files in the folder are examined. – Folders in the folder are ignored. –



197
198
199
200
201
# File 'lib/opensecret/commons/eco.faculty.rb', line 197

def replace_placeholders

  Files.find_replace Refactor.flatten(@c), @c[:runtime][:dir]

end

#string_fact_exists?(group_symbol, key_symbol) ⇒ Boolean

— [1] ASSUMES THAT FACT IS A STRING — #

Returns:

  • (Boolean)


278
279
280
281
282
283
# File 'lib/opensecret/commons/eco.faculty.rb', line 278

def string_fact_exists? group_symbol, key_symbol
  return false unless @c.has_key? group_symbol
  fact_value = @c[group_symbol][key_symbol]
  return false if fact_value.nil?
  return fact_value.strip.length > 0
end

#write_propertiesObject

– – If eco plugins wish the properties in the database under – :app_properties to be written to create (or overwrite) – the app properties they should create – – fact key => @c[@c[:id]] – fact val => “/path/to/application.properties – – —————- – Warning (Array) – —————- – The [:props_dst_dirs] fact is an [ARRAY]. – The same property key/value pairs will be serialized into – each file url stated in the array. The [:props_dst_dirs] – is (in contrast) a simple single filename. – – —————————– – (Optional) - Src Properties – —————————– – Plugins do not need to set the :app_props_src_path key. – If they only set the (dst) key, any properties set in the – fact database will still be written out. –



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/opensecret/commons/eco.faculty.rb', line 329

def write_properties

  return unless eco_fact_exists? :props_dst_dirs
  property_files = e_fact :props_dst_dirs

  property_files.each do | property_dir |

    Dir.mkdir property_dir unless File.exists? property_dir
    Files.write_properties(
      @c[:app_properties],
      property_dir,
      e_fact(:props_dst_name)
    )

  end

end