Class: Bcome::Node::Inventory::Defined
- Inherits:
-
Base
- Object
- Base
- Base
- Bcome::Node::Inventory::Defined
show all
- Defined in:
- lib/objects/node/inventory/defined.rb
Constant Summary
collapse
- MACHINES_CACHE_PATH =
'machines-cache.yml'.freeze
Constants inherited
from Base
Base::DEFAULT_IDENTIFIER
LocalMetaDataFactory::META_DATA_FILE_PATH_PREFIX
Instance Attribute Summary collapse
Attributes inherited from Base
#params
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#cloud_matches, #direct_invoke_all_servers, #direct_invoke_server, #inventory?, #list_key, #machine_by_identifier, #machines, #matches_for, #meta_matches, #override_server_identifier?, #resources, #ssh, #tags, #toggle_bootstrap
Methods inherited from Base
#bootstrap?, #close_ssh_connections, #collection?, const_missing, #data_print_from_hash, #execute_local, #execute_script, #has_parent?, #has_proxy?, #identifier=, #inventory?, #invoke, #is_top_level_node?, #keyed_namespace, #list_attributes, #list_key, #namespace, #no_nodes?, #nodes_loaded?, #open_ssh_connections, #pack_metadata, #prompt_breadcrumb, #proxy, #put, #recurse_resource_for_identifier, #requires_description?, #requires_type?, #resource_for_identifier, #resources, #rsync, #scp, #server?, #unpack_metadata, #validate_attributes, #validate_identifier
#registry, #user_command_wrapper
#do_create_metadata, #meta, #metadata, #metadata_for_namespace, #raw_metadata
#item_spacing, #menu, #menu_item_spacing_length, #mode, #tab_spacing
Methods included from Attributes
#description, #filters, #identifier, #network_data, #network_driver, #recurse_hash_data_for_instance_var, #ssh_data, #ssh_driver, #type
#back, #cd, #clear!, #disable, #disable!, #enable, #enable!, #interactive, #is_node_level_method?, #list_in_tree, #ls, #lsa, #method_in_registry?, #method_is_available_on_node?, #method_missing, #new_line, #parents, #ping, #pretty_description, #print_tree_view_for_resource, #resource_identifiers, #run, #tree, #tree_descriptions, #visual_hierarchy, #workon
Methods included from Context
#irb_workspace=, #is_current_context?, #previous_irb_workspace=
Constructor Details
#initialize(*params) ⇒ Defined
Returns a new instance of Defined.
11
12
13
14
15
|
# File 'lib/objects/node/inventory/defined.rb', line 11
def initialize(*params)
@load_machines_from_cache = false
@cache_handler = ::Bcome::Node::CacheHandler.new(self)
super
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Bcome::WorkspaceCommands
Instance Attribute Details
#dynamic_nodes_loaded ⇒ Object
Returns the value of attribute dynamic_nodes_loaded.
9
10
11
|
# File 'lib/objects/node/inventory/defined.rb', line 9
def dynamic_nodes_loaded
@dynamic_nodes_loaded
end
|
Class Method Details
.to_s ⇒ Object
5
6
7
|
# File 'lib/objects/node/inventory/defined.rb', line 5
def self.to_s
'inventory'
end
|
Instance Method Details
#cache_nodes_in_memory ⇒ Object
87
88
89
|
# File 'lib/objects/node/inventory/defined.rb', line 87
def cache_nodes_in_memory
@cache_handler.do_cache_nodes!
end
|
#do_reload ⇒ Object
91
92
93
94
|
# File 'lib/objects/node/inventory/defined.rb', line 91
def do_reload
resources.unset!
load_dynamic_nodes
end
|
17
18
19
|
# File 'lib/objects/node/inventory/defined.rb', line 17
def
super + %i[save reload]
end
|
#fetch_server_list ⇒ Object
112
113
114
115
|
# File 'lib/objects/node/inventory/defined.rb', line 112
def fetch_server_list
return [] unless network_driver
network_driver.fetch_server_list(filters)
end
|
#load_dynamic_nodes ⇒ Object
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/objects/node/inventory/defined.rb', line 101
def load_dynamic_nodes
raw_servers = fetch_server_list
raw_servers.each do |raw_server|
resources << ::Bcome::Node::Server::Dynamic.new_from_fog_instance(raw_server, self)
end
resources.rename_initial_duplicate if resources.should_rename_initial_duplicate?
end
|
#load_machines_config ⇒ Object
78
79
80
81
82
83
84
85
|
# File 'lib/objects/node/inventory/defined.rb', line 78
def load_machines_config
config = YAML.load_file(machines_cache_path).deep_symbolize_keys
return config
rescue ArgumentError, Psych::SyntaxError
raise Bcome::Exception::InvalidMachinesCacheConfig, 'Invalid yaml in config'
rescue Errno::ENOENT
return {}
end
|
#load_nodes ⇒ Object
96
97
98
99
|
# File 'lib/objects/node/inventory/defined.rb', line 96
def load_nodes
set_static_servers
load_dynamic_nodes unless @load_machines_from_cache
end
|
#machines_cache_path ⇒ Object
#mark_as_cached! ⇒ Object
53
54
55
56
57
|
# File 'lib/objects/node/inventory/defined.rb', line 53
def mark_as_cached!
data = ::Bcome::Node::Factory.instance.load_estate_config
data[namespace.to_sym][:load_machines_from_cache] = true
::Bcome::Node::Factory.instance.rewrite_estate_config(data)
end
|
21
22
23
24
25
26
27
28
29
|
# File 'lib/objects/node/inventory/defined.rb', line 21
def
base_items = super.dup
base_items[:reload] = {
description: "Restock this inventory from remote (hit 'save' after to persist)",
console_only: true
}
base_items
end
|
#raw_static_machines_from_cache ⇒ Object
45
46
47
|
# File 'lib/objects/node/inventory/defined.rb', line 45
def raw_static_machines_from_cache
load_machines_config[namespace.to_sym]
end
|
#reload ⇒ Object
31
32
33
34
35
|
# File 'lib/objects/node/inventory/defined.rb', line 31
def reload
resources.reset_duplicate_nodes!
do_reload
puts "\nDone. Hit 'ls' to see the refreshed inventory.\n".informational
end
|
#save ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/objects/node/inventory/defined.rb', line 59
def save
@answer = ::Bcome::Interactive::Session.run(self,
:capture_input, terminal_prompt: "\nAre you sure you want to cache these machines (saving will overwrite any previous selections) [Y|N] ? ")
if @answer && @answer == 'Y'
cache_nodes_in_memory
data = load_machines_config
data[namespace] = views[:static_servers]
File.open(machines_cache_path, 'w') do |file|
file.write data.to_yaml
end
mark_as_cached!
puts "Machines have been cached for node #{namespace}".informational
else
puts 'Nothing saved'.warning
end
end
|
#set_static_servers ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/objects/node/inventory/defined.rb', line 37
def set_static_servers
if raw_static_machines_from_cache
raw_static_machines_from_cache.each do |server_config|
resources << ::Bcome::Node::Server::Static.new(views: server_config, parent: self)
end
end
end
|