Class: Bcome::Node::Factory

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/objects/node/factory.rb

Constant Summary collapse

CONFIG_PATH =
'bcome'.freeze
DEFAULT_CONFIG_NAME =
'networks.yml'.freeze
SERVER_OVERRIDE_CONFIG_NAME =
'machines-data.yml'.freeze
LOCAL_OVERRIDE_CONFIG_NAME =
'me.yml'.freeze
INVENTORY_KEY =
'inventory'.freeze
COLLECTION_KEY =
'collection'.freeze
SUBSELECT_KEY =
'inventory-subselect'.freeze
BCOME_RC_FILENAME =
'.bcomerc'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#estateObject (readonly)

Returns the value of attribute estate.



5
6
7
# File 'lib/objects/node/factory.rb', line 5

def estate
  @estate
end

Instance Method Details

#bucketObject



17
18
19
# File 'lib/objects/node/factory.rb', line 17

def bucket
  @bucket ||= {}
end

#config_file_nameObject



34
35
36
# File 'lib/objects/node/factory.rb', line 34

def config_file_name
  @config_file_name ||= ENV['CONF'] ? ENV['CONF'] : DEFAULT_CONFIG_NAME
end

#config_pathObject



26
27
28
# File 'lib/objects/node/factory.rb', line 26

def config_path
  "#{CONFIG_PATH}/#{config_file_name}"
end

#create_node(config, parent = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/objects/node/factory.rb', line 52

def create_node(config, parent = nil)
  raise Bcome::Exception::InvalidNetworkConfig, 'missing config type' 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] && config[:views].any?
  parent.resources << node if parent

  bucket[node.keyed_namespace] = node

  node
end

#create_tree(context_node, views) ⇒ Object



38
39
40
# File 'lib/objects/node/factory.rb', line 38

def create_tree(context_node, views)
  views.each { |config| create_node(config, context_node) }
end

#estate_configObject



90
91
92
# File 'lib/objects/node/factory.rb', line 90

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

#init_treeObject



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

def init_tree
  @estate = create_node(estate_config)
  @estate
end

#is_running_deprecated_configs?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/objects/node/factory.rb', line 143

def is_running_deprecated_configs?
  File.exist?("bcome/config/platform.yml")
end

#is_valid_view_type?(view_type) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/objects/node/factory.rb', line 86

def is_valid_view_type?(view_type)
  klass_for_view_type.keys.include?(view_type)
end

#klass_for_view_typeObject



78
79
80
81
82
83
84
# File 'lib/objects/node/factory.rb', line 78

def klass_for_view_type
  {
    COLLECTION_KEY => ::Bcome::Node::Collection,
    INVENTORY_KEY => ::Bcome::Node::Inventory::Defined,
    SUBSELECT_KEY => ::Bcome::Node::Inventory::Subselect
  }
end

#load_estate_configObject



108
109
110
111
112
113
114
115
116
# File 'lib/objects/node/factory.rb', line 108

def load_estate_config
  config = YAML.load_file(config_path).deep_symbolize_keys
  return config
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



134
135
136
137
138
139
140
141
# File 'lib/objects/node/factory.rb', line 134

def load_local_data
  return {} unless File.exist?(local_data_path)
  config = YAML.load_file(local_data_path) 
  return {} if config.nil?
  return config
rescue ArgumentError, Psych::SyntaxError => e
  raise Bcome::Exception::InvalidNetworkConfig, 'Invalid yaml in machines data config' + e.message
end

#load_machines_dataObject



118
119
120
121
122
123
124
# File 'lib/objects/node/factory.rb', line 118

def load_machines_data
  return {} unless File.exist?(machines_data_path)    
  config = YAML.load_file(machines_data_path).deep_symbolize_keys
  return config
rescue ArgumentError, Psych::SyntaxError => e
  raise Bcome::Exception::InvalidNetworkConfig, 'Invalid yaml in machines data config' + e.message
end

#local_dataObject



126
127
128
# File 'lib/objects/node/factory.rb', line 126

def local_data
  @local_data ||= load_local_data
end

#local_data_pathObject



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

def local_data_path
  "#{CONFIG_PATH}/#{LOCAL_OVERRIDE_CONFIG_NAME}"
end

#machines_dataObject



94
95
96
# File 'lib/objects/node/factory.rb', line 94

def machines_data
  @machines_data ||= load_machines_data
end

#machines_data_for_namespace(namespace) ⇒ Object



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

def machines_data_for_namespace(namespace)
  machines_data[namespace] ? machines_data[namespace] : {}
end

#machines_data_pathObject



30
31
32
# File 'lib/objects/node/factory.rb', line 30

def machines_data_path
  "#{CONFIG_PATH}/#{SERVER_OVERRIDE_CONFIG_NAME}"
end

#reformat_config(config) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/objects/node/factory.rb', line 42

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

#rewrite_estate_config(data) ⇒ Object



102
103
104
105
106
# File 'lib/objects/node/factory.rb', line 102

def rewrite_estate_config(data)
  File.open(config_path, 'w') do |file|
    file.write data.to_yaml
  end
end

#validate_view(breadcrumb, data) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/objects/node/factory.rb', line 68

def validate_view(breadcrumb, data)
  unless data && data[:type]
    raise Bcome::Exception::InvalidNetworkConfig, "Missing namespace type for for namespace '#{breadcrumb}'"
  end

  unless is_valid_view_type?(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(', ')}"
  end
end