Class: Bcome::Node::Base

Inherits:
Object
  • Object
show all
Includes:
Context, Attributes, LocalMetaDataFactory, RegistryManagement, WorkspaceCommands, WorkspaceMenu
Defined in:
lib/objects/node/base.rb

Direct Known Subclasses

Collection, Inventory::Base, Server::Base

Constant Summary collapse

DEFAULT_IDENTIFIER =
"bcome"

Constants included from LocalMetaDataFactory

LocalMetaDataFactory::META_DATA_FILE_PATH_PREFIX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_item_spacing_length, #menu_items, #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

Methods included from WorkspaceCommands

#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) ⇒ Base

Returns a new instance of Base.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/objects/node/base.rb', line 22

def initialize(params)
  @params = params
  @identifier = nil
  @description = nil
  @views = params[:views]
  @parent = params[:parent]
  @type = params[:type]
  @metadata = {}

  set_view_attributes if @views
  validate_attributes
  ::Bcome::Registry::Loader.instance.set_command_group_for_node(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Bcome::WorkspaceCommands

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



20
21
22
# File 'lib/objects/node/base.rb', line 20

def params
  @params
end

Class Method Details

.const_missing(constant) ⇒ Object



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

def self.const_missing(constant)
  ## Hook for direct access to node level resources by constant name where
  ## cd ServerName should yield the same outcome as cd "ServerName"
  set_context  = ::IRB.CurrentContext.workspace.main
  return (set_context.resource_for_identifier(constant.to_s)) ? constant.to_s : super
end

Instance Method Details

#bootstrap?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/objects/node/base.rb', line 36

def bootstrap?
  false
end

#close_ssh_connectionsObject



201
202
203
204
205
206
# File 'lib/objects/node/base.rb', line 201

def close_ssh_connections
  machines.pmap do |machine|
    machine.close_ssh_connection
  end
  return
end

#collection?Boolean

Returns:

  • (Boolean)


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

def collection?
  false
end

#data_print_from_hash(data, heading) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/objects/node/base.rb', line 220

def data_print_from_hash(data, heading)
  puts "\n#{heading.title}"
  puts ""

  if data.keys.any?
    data.each do |key, value|
      puts "#{key.to_s.resource_key}: #{value.to_s.informational}"
    end
  else
    puts "No values found".warning
  end
  puts ""
end

#enabled_menu_itemsObject



52
53
54
# File 'lib/objects/node/base.rb', line 52

def enabled_menu_items
  [:ls, :lsa, :workon, :enable, :disable, :enable!, :disable!, :run, :tree, :ping, :put, :rsync, :cd, :meta, :pack_metadata, :unpack_metadata, :registry, :interactive, :execute_script] 
end

#execute_local(command) ⇒ Object



215
216
217
218
# File 'lib/objects/node/base.rb', line 215

def execute_local(command)
  puts "(local) > #{command}"
  system(command)
end

#execute_script(script_name) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/objects/node/base.rb', line 86

def execute_script(script_name)
  results = {}
  machines.pmap do |machine|
    command = machine.execute_script(script_name)
    results[machine.namespace] = command
  end
  results
end

#has_parent?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/objects/node/base.rb', line 185

def has_parent?
  !@parent.nil?
end

#has_proxy?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/objects/node/base.rb', line 56

def has_proxy?
  ssh_driver.has_proxy?
end

#inventory?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/objects/node/base.rb', line 44

def inventory?
  false
end

#invoke(method_name, arguments = []) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/objects/node/base.rb', line 139

def invoke(method_name, arguments = [])
  if method_is_available_on_node?(method_name)
    if respond_to?(method_name)        
      # Invoke a method on node that's defined by the system
      begin
        if arguments && arguments.any?
          send(method_name, *arguments)
        else
          send(method_name)
        end
      rescue ArgumentError => e
        raise ::Bcome::Exception::ArgumentErrorInvokingMethodFromCommmandLine.new method_name + "error message - #{e.message}"
      end
    else
      # Invoke a user defined (registry) method
      command = user_command_wrapper.command_for_console_command_name(method_name.to_sym)
      command.execute(self, arguments)
    end
  else
    # Final crumb is neither a node level context nor an executable method on the penultimate node level context
    raise ::Bcome::Exception::InvalidBreadcrumb.new("Method '#{method_name}' is not available on bcome node of type #{self.class}, at namespace #{namespace}")
  end
end

#is_top_level_node?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/objects/node/base.rb', line 189

def is_top_level_node?
  !has_parent?
end

#keyed_namespaceObject



180
181
182
183
# File 'lib/objects/node/base.rb', line 180

def keyed_namespace
  splits = namespace.split(":") ; 
  splits[1..splits.size].join(":")
end

#list_attributesObject



193
194
195
196
197
198
199
# File 'lib/objects/node/base.rb', line 193

def list_attributes
  { 
    "Identifier": :identifier,
    "Description": :description,
    "Type": :type
  }
end

#list_keyObject



135
136
137
# File 'lib/objects/node/base.rb', line 135

def list_key
  :view
end

#namespaceObject



176
177
178
# File 'lib/objects/node/base.rb', line 176

def namespace
  "#{ parent ? "#{parent.namespace}:" : "" }#{identifier}"
end

#no_nodes?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/objects/node/base.rb', line 123

def no_nodes?
  !resources || resources.empty?
end

#nodes_loaded?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/objects/node/base.rb', line 127

def nodes_loaded?
  resources.any?
end

#open_ssh_connectionsObject



208
209
210
211
212
213
# File 'lib/objects/node/base.rb', line 208

def open_ssh_connections
  machines.pmap do |machine|
    machine.open_ssh_connection unless machine.has_ssh_connection?
  end
  return
end

#pack_metadataObject



95
96
97
# File 'lib/objects/node/base.rb', line 95

def 
  ::Bcome::Encryptor.instance.pack
end

#prompt_breadcrumbObject



172
173
174
# File 'lib/objects/node/base.rb', line 172

def prompt_breadcrumb
  "#{has_parent? ? "#{parent.prompt_breadcrumb}> " : "" }#{ is_current_context? ? (has_parent? ? identifier.terminal_prompt : identifier) : identifier}" 
end

#proxyObject



60
61
62
# File 'lib/objects/node/base.rb', line 60

def proxy
  ssh_driver.proxy
end

#put(local_path, remote_path) ⇒ Object



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

def put(local_path, remote_path)
  resources.active.each do |resource|
    resource.put(local_path, remote_path)
  end
  return
end

#recurse_resource_for_identifier(identifier) ⇒ Object



167
168
169
170
# File 'lib/objects/node/base.rb', line 167

def recurse_resource_for_identifier(identifier)
  resource = resource_for_identifier(identifier)
  return resource ? resource : (has_parent? ? parent.recurse_resource_for_identifier(identifier) : nil)
end

#requires_description?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/objects/node/base.rb', line 115

def requires_description?
  true
end

#requires_type?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/objects/node/base.rb', line 119

def requires_type?
  true
end

#resource_for_identifier(identifier) ⇒ Object



163
164
165
# File 'lib/objects/node/base.rb', line 163

def resource_for_identifier(identifier)
  resources.for_identifier(identifier)
end

#resourcesObject



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

def resources
  @resources ||= ::Bcome::Node::Resources::Base.new
end

#rsync(local_path, remote_path) ⇒ Object



72
73
74
75
76
77
# File 'lib/objects/node/base.rb', line 72

def rsync(local_path, remote_path)
  resources.active.each do |resource|
    resource.rsync(local_path, remote_path)
  end   
  return
end

#scp(local_path, remote_path) ⇒ Object

TODO - why not do these in parallel?



65
66
67
68
69
70
# File 'lib/objects/node/base.rb', line 65

def scp(local_path, remote_path)
  resources.active.each do |resource|
    resource.put(local_path, remote_path)
  end
  return
end

#server?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/objects/node/base.rb', line 48

def server?
  false
end

#unpack_metadataObject



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

def 
  ::Bcome::Encryptor.instance.unpack
end

#validate_attributesObject



103
104
105
106
107
# File 'lib/objects/node/base.rb', line 103

def validate_attributes
  validate_identifier 
  raise ::Bcome::Exception::MissingDescriptionOnView.new(@views.inspect) if requires_description? && !@description
  raise ::Bcome::Exception::MissingTypeOnView.new(@views.inspect) if requires_type? && !@type
end

#validate_identifierObject



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

def validate_identifier
  @identifier = DEFAULT_IDENTIFIER if is_top_level_node? && !@identifier && !is_a?(::Bcome::Node::Server::Base)
  raise ::Bcome::Exception::MissingIdentifierOnView.new(@views.inspect) unless @identifier
  raise ::Bcome::Exception::InvalidIdentifier.new("'#{@identifier}' contains whitespace") if @identifier =~ /\s/
end