Class: Bcome::Node::Factory
- Inherits:
-
Object
- Object
- Bcome::Node::Factory
- Includes:
- Singleton
- Defined in:
- lib/objects/node/factory.rb
Constant Summary collapse
- CONFIG_PATH =
'bcome'
- DEFAULT_CONFIG_NAME =
'networks.yml'
- SERVER_OVERRIDE_CONFIG_NAME =
'server-overrides.yml'
- LOCAL_OVERRIDE_CONFIG_NAME =
'me.yml'
- INVENTORY_KEY =
'inventory'
- COLLECTION_KEY =
'collection'
- SUBSELECT_KEY =
'inventory-subselect'
- MERGE_KEY =
'inventory-merge'
- KUBE_CLUSTER =
'kube-cluster'
- BCOME_RC_FILENAME =
'.bcomerc'
Instance Attribute Summary collapse
-
#estate ⇒ Object
readonly
Returns the value of attribute estate.
Instance Method Summary collapse
- #bucket ⇒ Object
- #config_file_name ⇒ Object
- #config_path ⇒ Object
- #create_node(config, parent = nil) ⇒ Object
- #create_tree(context_node, views) ⇒ Object
- #estate_config ⇒ Object
- #init_tree ⇒ Object
- #is_running_deprecated_configs? ⇒ Boolean
- #is_valid_view_type?(view_type) ⇒ Boolean
- #klass_for_view_type ⇒ Object
- #load_estate_config ⇒ Object
- #load_local_data ⇒ Object
- #load_machines_data ⇒ Object
- #local_data ⇒ Object
- #local_data_path ⇒ Object
- #machines_data ⇒ Object
- #machines_data_for_namespace(namespace) ⇒ Object
- #machines_data_path ⇒ Object
- #reformat_config(config) ⇒ Object
- #validate_view(breadcrumb, data) ⇒ Object
Instance Attribute Details
#estate ⇒ Object (readonly)
Returns the value of attribute estate.
7 8 9 |
# File 'lib/objects/node/factory.rb', line 7 def estate @estate end |
Instance Method Details
#bucket ⇒ Object
22 23 24 |
# File 'lib/objects/node/factory.rb', line 22 def bucket @bucket ||= {} end |
#config_file_name ⇒ Object
40 41 42 |
# File 'lib/objects/node/factory.rb', line 40 def config_file_name @config_file_name || DEFAULT_CONFIG_NAME end |
#config_path ⇒ Object
32 33 34 |
# File 'lib/objects/node/factory.rb', line 32 def config_path ENV['CONF'] || "#{CONFIG_PATH}/#{config_file_name}" end |
#create_node(config, parent = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/objects/node/factory.rb', line 58 def create_node(config, parent = nil) raise Bcome::Exception::InvalidNetworkConfig, "missing config type for config #{config}" unless config[:type] klass = klass_for_view_type[config[:type]] raise Bcome::Exception::InvalidNetworkConfig, "invalid config type #{config[:type]}" unless klass node = klass.new(views: config, parent: parent) create_tree(node, config[:views]) if config[:views]&.any? parent.resources << node if parent # Load inventory resources as early as possible if node.is_a?(Bcome::Node::Inventory::Base) node.load_nodes unless node.nodes_loaded? end bucket[node.keyed_namespace] = node node end |
#create_tree(context_node, views) ⇒ Object
44 45 46 |
# File 'lib/objects/node/factory.rb', line 44 def create_tree(context_node, views) views.each { |config| create_node(config, context_node) } end |
#estate_config ⇒ Object
99 100 101 |
# File 'lib/objects/node/factory.rb', line 99 def estate_config @estate_config ||= reformat_config(load_estate_config) end |
#init_tree ⇒ Object
26 27 28 29 30 |
# File 'lib/objects/node/factory.rb', line 26 def init_tree raise ::Bcome::Exception::EmptyNamespaceTree, "no namespaces found in #{config_path}.\n\nPlease refer to the documentation for assistance at https://docs.bcome.com." if estate_config.nil? @estate = create_node(estate_config) @estate end |
#is_running_deprecated_configs? ⇒ Boolean
155 156 157 |
# File 'lib/objects/node/factory.rb', line 155 def is_running_deprecated_configs? File.exist?('bcome/config/platform.yml') end |
#is_valid_view_type?(view_type) ⇒ Boolean
95 96 97 |
# File 'lib/objects/node/factory.rb', line 95 def is_valid_view_type?(view_type) klass_for_view_type.keys.include?(view_type) end |
#klass_for_view_type ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/objects/node/factory.rb', line 85 def klass_for_view_type { COLLECTION_KEY => ::Bcome::Node::Collection, INVENTORY_KEY => ::Bcome::Node::Inventory::Defined, SUBSELECT_KEY => ::Bcome::Node::Inventory::Subselect, MERGE_KEY => ::Bcome::Node::Inventory::Merge, KUBE_CLUSTER => ::Bcome::Node::Kube::Estate } end |
#load_estate_config ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/objects/node/factory.rb', line 111 def load_estate_config config = YAML.load_file(config_path).deep_symbolize_keys config.deep_merge(local_data) rescue ArgumentError, Psych::SyntaxError => e raise Bcome::Exception::InvalidNetworkConfig, 'Invalid yaml in network config' + e. rescue Errno::ENOENT raise Bcome::Exception::DeprecationWarning if is_running_deprecated_configs? raise Bcome::Exception::MissingNetworkConfig, config_path end |
#load_local_data ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/objects/node/factory.rb', line 139 def load_local_data return {} unless File.exist?(local_data_path) begin config = YAML.load_file(local_data_path).deep_symbolize_keys rescue StandardError => e raise ::Bcome::Exception::Generic, "Error parsing configuration file #{local_data_path}" end return {} if config.nil? config rescue ArgumentError, Psych::SyntaxError => e raise Bcome::Exception::InvalidNetworkConfig, 'Invalid yaml in machines data config' + e. end |
#load_machines_data ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/objects/node/factory.rb', line 122 def load_machines_data return {} unless File.exist?(machines_data_path) config = YAML.load_file(machines_data_path).deep_symbolize_keys config rescue ArgumentError, Psych::SyntaxError => e raise Bcome::Exception::InvalidNetworkConfig, 'Invalid yaml in machines data config' + e. end |
#local_data ⇒ Object
131 132 133 |
# File 'lib/objects/node/factory.rb', line 131 def local_data @local_data ||= load_local_data end |
#local_data_path ⇒ Object
135 136 137 |
# File 'lib/objects/node/factory.rb', line 135 def local_data_path ENV['ME'] || "#{CONFIG_PATH}/#{LOCAL_OVERRIDE_CONFIG_NAME}" end |
#machines_data ⇒ Object
103 104 105 |
# File 'lib/objects/node/factory.rb', line 103 def machines_data @machines_data ||= load_machines_data end |
#machines_data_for_namespace(namespace) ⇒ Object
107 108 109 |
# File 'lib/objects/node/factory.rb', line 107 def machines_data_for_namespace(namespace) machines_data[namespace] || {} end |
#machines_data_path ⇒ Object
36 37 38 |
# File 'lib/objects/node/factory.rb', line 36 def machines_data_path "#{CONFIG_PATH}/#{SERVER_OVERRIDE_CONFIG_NAME}" end |
#reformat_config(config) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/objects/node/factory.rb', line 48 def reformat_config(config) conf = ::Bcome::ConfigFactory.new config.each do |crumb, data| validate_view(crumb, data) crumbs = Bcome::Parser::BreadCrumb.parse(crumb) conf.add_crumbs(crumbs, data) end conf.flattened end |
#validate_view(breadcrumb, data) ⇒ Object
79 80 81 82 83 |
# File 'lib/objects/node/factory.rb', line 79 def validate_view(, data) raise Bcome::Exception::InvalidNetworkConfig, "Missing namespace type for namespace '#{}'" unless data && data[:type] raise Bcome::Exception::InvalidNetworkConfig, "Invalid View Type '#{data[:type]}' for namespace '#{}'. Expecting View Type to be one of: #{klass_for_view_type.keys.join(', ')}" unless is_valid_view_type?(data[:type]) end |