class Bcome::Node::Factory

Constants

BCOME_RC_FILENAME
COLLECTION_KEY
CONFIG_PATH
DEFAULT_CONFIG_NAME
INVENTORY_KEY
KUBE_CLUSTER
LOCAL_OVERRIDE_CONFIG_NAME
MERGE_KEY
SERVER_OVERRIDE_CONFIG_NAME
SUBSELECT_KEY

Attributes

estate[R]

Public Instance Methods

bucket() click to toggle source
# File lib/objects/node/factory.rb, line 22
def bucket
  @bucket ||= {}
end
config_file_name() click to toggle source
# File lib/objects/node/factory.rb, line 40
def config_file_name
  @config_file_name || DEFAULT_CONFIG_NAME
end
config_path() click to toggle source
# File lib/objects/node/factory.rb, line 32
def config_path
  ENV['CONF'] || "#{CONFIG_PATH}/#{config_file_name}"
end
create_node(config, parent = nil) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/objects/node/factory.rb, line 99
def estate_config
  @estate_config ||= reformat_config(load_estate_config)
end
init_tree() click to toggle source
# 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?() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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.message
rescue Errno::ENOENT
  raise Bcome::Exception::DeprecationWarning if is_running_deprecated_configs?

  raise Bcome::Exception::MissingNetworkConfig, config_path
end
load_local_data() click to toggle source
# 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.message
end
load_machines_data() click to toggle source
# 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.message
end
local_data() click to toggle source
# File lib/objects/node/factory.rb, line 131
def local_data
  @local_data ||= load_local_data
end
local_data_path() click to toggle source
# File lib/objects/node/factory.rb, line 135
def local_data_path
  ENV['ME'] || "#{CONFIG_PATH}/#{LOCAL_OVERRIDE_CONFIG_NAME}"
end
machines_data() click to toggle source
# File lib/objects/node/factory.rb, line 103
def machines_data
  @machines_data ||= load_machines_data
end
machines_data_for_namespace(namespace) click to toggle source
# File lib/objects/node/factory.rb, line 107
def machines_data_for_namespace(namespace)
  machines_data[namespace] || {}
end
machines_data_path() click to toggle source
# File lib/objects/node/factory.rb, line 36
def machines_data_path
  "#{CONFIG_PATH}/#{SERVER_OVERRIDE_CONFIG_NAME}"
end
reformat_config(config) click to toggle source
# 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) click to toggle source
# File lib/objects/node/factory.rb, line 79
def validate_view(breadcrumb, data)
  raise Bcome::Exception::InvalidNetworkConfig, "Missing namespace type for namespace '#{breadcrumb}'" unless data && data[:type]

  raise Bcome::Exception::InvalidNetworkConfig, "Invalid View Type '#{data[:type]}' for namespace '#{breadcrumb}'. Expecting View Type to be one of: #{klass_for_view_type.keys.join(', ')}" unless is_valid_view_type?(data[:type])
end