Class: Bcome::Node::Factory

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#estateObject (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

#bucketObject



22
23
24
# File 'lib/objects/node/factory.rb', line 22

def bucket
  @bucket ||= {}
end

#config_file_nameObject



40
41
42
# File 'lib/objects/node/factory.rb', line 40

def config_file_name
  @config_file_name || DEFAULT_CONFIG_NAME
end

#config_pathObject



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_configObject



99
100
101
# File 'lib/objects/node/factory.rb', line 99

def estate_config
  @estate_config ||= reformat_config(load_estate_config)
end

#init_treeObject



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

Returns:

  • (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

Returns:

  • (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_typeObject



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_configObject



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.message
rescue Errno::ENOENT
  raise Bcome::Exception::DeprecationWarning if is_running_deprecated_configs?

  raise Bcome::Exception::MissingNetworkConfig, config_path
end

#load_local_dataObject



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.message
end

#load_machines_dataObject



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.message
end

#local_dataObject



131
132
133
# File 'lib/objects/node/factory.rb', line 131

def local_data
  @local_data ||= load_local_data
end

#local_data_pathObject



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_dataObject



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_pathObject



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(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