Class: Origen::SiteConfig::Config
- Defined in:
- lib/origen/site_config/config.rb
Constant Summary collapse
- RESTRICTED_FROM_CENTRALIZED_VARIABLES =
%w(centralized_site_config centralized_site_config_cache_dir centralized_site_config_verify_ssl)
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
- #cached? ⇒ Boolean
- #cached_file ⇒ Object
- #centralized? ⇒ Boolean
- #fetch ⇒ Object (also: #refresh)
-
#find_val(val) ⇒ Object
(also: #[])
Finds the value from this config, or from one of its centralized configs (if applicable).
- #has_var?(var) ⇒ Boolean
- #inform_user_of_cached_file ⇒ Object
-
#initialize(path:, parent:, values: nil) ⇒ Config
constructor
A new instance of Config.
-
#load ⇒ Object
Loads the site config into memory.
- #loaded? ⇒ Boolean
- #local? ⇒ Boolean
- #needs_refresh? ⇒ Boolean
- #read_erb(erb) ⇒ Object
- #refresh_time ⇒ Object
- #remove_var(var) ⇒ Object
- #runtime? ⇒ Boolean
Constructor Details
#initialize(path:, parent:, values: nil) ⇒ Config
Returns a new instance of Config.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/origen/site_config/config.rb', line 11 def initialize(path:, parent:, values: nil) @parent = parent if path == :runtime path = "runtime_#{object_id}" @type = :runtime elsif path.start_with?('http') @path = path @type = :centralized else @path = path @type = :local end @contains_centralized = false @loaded = false if values @values = values @loaded = true else @values = nil load end end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
5 6 7 |
# File 'lib/origen/site_config/config.rb', line 5 def parent @parent end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
4 5 6 |
# File 'lib/origen/site_config/config.rb', line 4 def path @path end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/origen/site_config/config.rb', line 6 def type @type end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
7 8 9 |
# File 'lib/origen/site_config/config.rb', line 7 def values @values end |
Instance Method Details
#cached? ⇒ Boolean
60 61 62 |
# File 'lib/origen/site_config/config.rb', line 60 def cached? File.exist?(cached_file) end |
#cached_file ⇒ Object
56 57 58 |
# File 'lib/origen/site_config/config.rb', line 56 def cached_file @cached_file ||= Pathname(parent.centralized_site_config_cache_dir).join('cached_config') end |
#centralized? ⇒ Boolean
200 201 202 |
# File 'lib/origen/site_config/config.rb', line 200 def centralized? type == :centralized end |
#fetch ⇒ Object Also known as: refresh
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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/origen/site_config/config.rb', line 64 def fetch def inform_user_of_cached_file if cached? puts yellow('Origen: Site Config: Found previously cached site config. Using the older site config.') else puts yellow('Origen: Site Config: No cached file found. An empty site config will be used in its place.') end puts end if centralized? puts "Pulling centralized site config from: #{path}" begin # TODO: needs to be replaced with a net/http equivalent, can't use gems here. The reference # to HTTParty will raise an error until that is done, but it will be handled gracefully below. text = HTTParty.get(path, verify: parent.find_val('centralized_site_config_verify_ssl')) puts "Caching centralized site config to: #{cached_file}" unless Dir.exist?(cached_file.dirname) FileUtils.mkdir_p(cached_file.dirname) end File.open(cached_file, 'w+') do |f| f.write(text) end rescue SocketError => e puts red("Origen: Site Config: Unable to connect to #{path}") puts red('Origen: Site Config: Failed to retrieve centralized site config!') puts red("Error from exception: #{e.}") inform_user_of_cached_file rescue OpenSSL::SSL::SSLError => e puts red("Origen: Site Config: Unable to connect to #{path}") puts red('Origen: Site Config: Failed to retrieve centralized site config!') puts red("Error from exception: #{e.}") puts red('It looks like the error is related to SSL certification. If this is a trusted server, you can use') puts red("the site config setting 'centralized_site_config_verify_ssl' to disable verifying the SSL certificate.") inform_user_of_cached_file rescue Exception => e # Rescue anything else to avoid any un-caught exceptions causing Origen not to boot. # Print lots of red so that the users are aware that there's a problem, but don't ultimately want this # to render Origen un-bootable puts red("Origen: Site Config: Unexpected exception ocurred trying to either retrieve or cache the site config at #{path}") puts red('Origen: Site Config: Failed to retrieve centralized site config!') puts red("Class of exception: #{e.class}") puts red("Error from exception: #{e.}") inform_user_of_cached_file end text end end |
#find_val(val) ⇒ Object Also known as: []
Finds the value from this config, or from one of its centralized configs (if applicable)
187 188 189 |
# File 'lib/origen/site_config/config.rb', line 187 def find_val(val) @values[val] end |
#has_var?(var) ⇒ Boolean
182 183 184 |
# File 'lib/origen/site_config/config.rb', line 182 def has_var?(var) @values.key?(var) end |
#inform_user_of_cached_file ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/origen/site_config/config.rb', line 65 def inform_user_of_cached_file if cached? puts yellow('Origen: Site Config: Found previously cached site config. Using the older site config.') else puts yellow('Origen: Site Config: No cached file found. An empty site config will be used in its place.') end puts end |
#load ⇒ Object
Loads the site config into memory. Process the site config as an ERB, if indicated to do so (.erb file extension) After the initial load, any centralized site configs will be retreived (if needed), cached, and loaded.
122 123 124 125 126 127 128 129 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/origen/site_config/config.rb', line 122 def load def read_erb(erb) ERB.new(File.read(erb), 0, '%<>') end # YAML.safe_load is prefered # rubocop:disable Security/YAMLLoad if centralized? if !cached? if fetch erb = read_erb(cached_file) else # There was a problem fetching the config. Just use an empty string. # Warning message will come from #fetch erb = ERB.new('') end else erb = read_erb(cached_file) end @values = (YAML.load(erb.result) || {}) else if File.extname(path) == '.erb' erb = read_erb(path) @values = (YAML.load(erb.result) || {}) else @values = (YAML.load_file(path) || {}) end end # rubocop:enable Security/YAMLLoad unless @values.is_a?(Hash) puts red("Origen: Site Config: The config at #{path} was not parsed as a Hash, but as a #{@values.class}") puts red(' Please review the format of the this file.') puts red(' Alternatively, an error condition may have been received from the server.') puts red(" This site config is available at: #{cached_file}") puts red(' This config will not be loaded and will be replaced with an empty config.') puts @values = {} end if centralized? # check for restricted centralized config values RESTRICTED_FROM_CENTRALIZED_VARIABLES.each do |var| if @values.key?(var) val = @values.delete(var) puts red("Origen: Site Config: config variable #{var} is not allowed in the centralized site config and will be removed.") puts red(" Value #{val} will not be applied!") end end end @loaded = true @values end |
#loaded? ⇒ Boolean
192 193 194 |
# File 'lib/origen/site_config/config.rb', line 192 def loaded? @loaded end |
#local? ⇒ Boolean
196 197 198 |
# File 'lib/origen/site_config/config.rb', line 196 def local? type == :local end |
#needs_refresh? ⇒ Boolean
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/origen/site_config/config.rb', line 35 def needs_refresh? if centralized? if refresh_time < 0 false elsif cached? # If the refresh time is 0, this will always be true # Note the difference of time objects below will give the difference in seconds. (Time.now - cached_file.ctime) / 3600.0 > refresh_time else # If the cached file cannot be found, force a new fetch true end else false end end |
#read_erb(erb) ⇒ Object
123 124 125 |
# File 'lib/origen/site_config/config.rb', line 123 def read_erb(erb) ERB.new(File.read(erb), 0, '%<>') end |
#refresh_time ⇒ Object
52 53 54 |
# File 'lib/origen/site_config/config.rb', line 52 def refresh_time parent.find_val('centralized_site_config_refresh') end |
#remove_var(var) ⇒ Object
178 179 180 |
# File 'lib/origen/site_config/config.rb', line 178 def remove_var(var) @values.delete(var) end |
#runtime? ⇒ Boolean
204 205 206 |
# File 'lib/origen/site_config/config.rb', line 204 def runtime? type == :runtime end |