Class: Bcome::Node::Inventory::Defined

Inherits:
Base
  • Object
show all
Includes:
LoadingBar::Handler
Defined in:
lib/objects/node/inventory/defined.rb

Constant Summary collapse

MACHINES_CACHE_PATH =
'static-cache.yml'

Constants inherited from Base

Base::DEFAULT_IDENTIFIER

Constants included from Draw

Draw::BLIP, Draw::BOTTOM_ANCHOR, Draw::BOX_BOTTOM_LEFT, Draw::BOX_BOTTOM_RIGHT, Draw::BOX_HORIZONTAL_LINE, Draw::BOX_SIDE, Draw::BOX_TOP_LEFT, Draw::BOX_TOP_RIGHT, Draw::BRANCH, Draw::INGRESS, Draw::LEFT_PADDING, Draw::MID_SHIPS

Constants included from LocalMetaDataFactory

LocalMetaDataFactory::META_DATA_FILE_PATH_PREFIX

Instance Attribute Summary collapse

Attributes inherited from Base

#identifier, #params, #parent, #views

Instance Method Summary collapse

Methods included from LoadingBar::Handler

#cursor, #do_signal, #fork_process, #signal_failure, #signal_stop, #signal_success, #start_indicator, #stop_indicator, #wrap_indicator

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

Methods inherited from Base

#close_ssh_connections, #collection?, const_missing, #data_print_from_hash, #execute_local, #execute_script, #has_parent?, #has_proxy?, #hide?, #inspect, #inventory?, #invoke, #is_top_level_node?, #keyed_namespace, #list_attributes, #list_key, #method_missing, #namespace, #no_nodes?, #nodes_loaded!, #nodes_loaded?, #prompt_breadcrumb, #proxy, #proxy_chain, #proxy_chain_link, #put, #put_str, #recurse_resource_for_identifier, #requires_description?, #requires_type?, #resource_for_identifier, #resources, #rsync, #scoped_resources, #scp, #server?, #ssh_connect, #validate_attributes, #validate_identifier

Methods included from Tree

#build_tree, #deduce_tree_structure, #namespace_tree_line, #network_namespace_tree_data, #recurse_tree_lines, #routes, #routing_tree_data, #routing_tree_line, #to_nested_hash, #tree

Methods included from Draw

#box_it, #max_box_line_length

Methods included from RegistryManagement

#registry, #user_command_wrapper

Methods included from LocalMetaDataFactory

#do_create_metadata, #meta, #metadata, #metadata_for_namespace, #raw_metadata

Methods included from WorkspaceMenu

#item_spacing, #menu, #menu_group_names, #menu_item_spacing_length, #mode, #print_menu_items, #tab_spacing

Methods included from Attributes

#filters, #identifier, #network_data, #network_driver, #recurse_hash_data_for_instance_key, #ssh_data, #ssh_driver, #ssh_driver=

Methods included from WorkspaceCommands

#cd, #clear!, #disable, #disable!, #enable, #enable!, #interactive, #is_node_level_method?, #ls, #lsa, #method_in_registry?, #method_is_available_on_node?, #new_line, #parents, #ping, #pretty_description, #resource_identifiers, #run, #ssh_connect, #tree_descriptions, #visual_hierarchy, #workon

Methods included from Context

#current_context?, #irb_workspace=, #previous_irb_workspace=

Constructor Details

#initialize(*params) ⇒ Defined

Returns a new instance of Defined.



13
14
15
16
17
# File 'lib/objects/node/inventory/defined.rb', line 13

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::Node::Base

Instance Attribute Details

#dynamic_nodes_loadedObject (readonly)

Returns the value of attribute dynamic_nodes_loaded.



11
12
13
# File 'lib/objects/node/inventory/defined.rb', line 11

def dynamic_nodes_loaded
  @dynamic_nodes_loaded
end

Instance Method Details

#cacheObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/objects/node/inventory/defined.rb', line 69

def cache
  @answer = ::Bcome::Interactive::Session.run(self,
                                              :capture_input, terminal_prompt: 'Are 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.delete(namespace)
    data.delete(namespace.to_sym)

    data[namespace] = views[:static_servers]

    File.open(machines_cache_path, 'w') do |file|
      file.write data.to_yaml
    end
    puts "\nMachines have been cached to #{machines_cache_path} for node #{namespace}".informational
  else
    puts 'Nothing saved'.warning
  end
end

#cache_nodes_in_memoryObject



100
101
102
# File 'lib/objects/node/inventory/defined.rb', line 100

def cache_nodes_in_memory
  @cache_handler.do_cache_nodes!
end

#do_reloadObject



104
105
106
107
# File 'lib/objects/node/inventory/defined.rb', line 104

def do_reload
  resources.unset!
  load_dynamic_nodes
end

#enabled_menu_itemsObject



19
20
21
# File 'lib/objects/node/inventory/defined.rb', line 19

def enabled_menu_items
  super + %i[cache reload]
end

#fetch_server_listObject



133
134
135
136
137
# File 'lib/objects/node/inventory/defined.rb', line 133

def fetch_server_list
  return [] unless network_driver

  network_driver.fetch_server_list(filters)
end

#load_dynamic_nodesObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/objects/node/inventory/defined.rb', line 115

def load_dynamic_nodes
  raw_servers = fetch_server_list

  raw_servers ||= []

  raw_servers.each do |raw_server|
    if raw_server.is_a?(Google::Apis::ComputeBeta::Instance)
      resources << ::Bcome::Node::Server::Dynamic::Gcp.new_from_gcp_instance(raw_server, self)
    elsif raw_server.is_a?(Fog::Compute::AWS::Server)
      resources << ::Bcome::Node::Server::Dynamic::Ec2.new_from_fog_instance(raw_server, self)
    else
      raise Bcome::Exception::UnknownDynamicServerType, "Unknown dynamic server type #{raw_server.class}"
    end
  end

  resources.rename_initial_duplicate if resources.should_rename_initial_duplicate?
end

#load_machines_configObject



91
92
93
94
95
96
97
98
# File 'lib/objects/node/inventory/defined.rb', line 91

def load_machines_config
  config = YAML.load_file(machines_cache_path).deep_symbolize_keys
  config
rescue ArgumentError, Psych::SyntaxError
  raise Bcome::Exception::InvalidMachinesCacheConfig, 'Invalid yaml in config'
rescue Errno::ENOENT
  {}
end

#load_nodesObject



109
110
111
112
113
# File 'lib/objects/node/inventory/defined.rb', line 109

def load_nodes
  set_static_servers
  load_dynamic_nodes unless resources.any?
  nodes_loaded!
end

#machines_cache_pathObject



65
66
67
# File 'lib/objects/node/inventory/defined.rb', line 65

def machines_cache_path
  "#{::Bcome::Node::Factory::CONFIG_PATH}/#{MACHINES_CACHE_PATH}"
end


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/objects/node/inventory/defined.rb', line 23

def menu_items
  base_items = super.dup

  base_items[:cache] = {
    description: 'Cache the current tree state',
    console_only: false,
    group: :miscellany
  }

  base_items[:reload] = {
    description: "Restock this inventory from remote (hit 'cache' after to persist)",
    console_only: true,
    group: :miscellany
  }
  base_items
end

#raw_static_machines_from_cacheObject



61
62
63
# File 'lib/objects/node/inventory/defined.rb', line 61

def raw_static_machines_from_cache
  load_machines_config[namespace.to_sym]
end

#reloadObject



40
41
42
43
44
# File 'lib/objects/node/inventory/defined.rb', line 40

def reload
  resources.reset_duplicate_nodes!
  do_reload
  puts "\n\nDone. Hit 'ls' to see the refreshed inventory.\n".informational
end

#set_static_serversObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/objects/node/inventory/defined.rb', line 46

def set_static_servers
  cached_machines = raw_static_machines_from_cache

  if cached_machines&.any?
    print "\n"
    title = 'Loading' + "\sCACHE".bc_orange.bold + "\s" + namespace.to_s.underline
    wrap_indicator type: :basic, title: title, completed_title: '' do
      cached_machines.each do |server_config|
        resources << ::Bcome::Node::Server::Static.new(views: server_config, parent: self)
      end
      signal_success
    end
  end
end