Class: Bcome::Node::Server::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/objects/node/server/base.rb

Direct Known Subclasses

Dynamic::Base, Static

Constant Summary

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 inherited from Base

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

Methods included from LoadingBar::Handler

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

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?, #method_in_registry?, #method_is_available_on_node?, #new_line, #parents, #pretty_description, #resource_identifiers, #ssh_connect, #tree_descriptions, #visual_hierarchy, #workon

Methods included from Context

#current_context?, #irb_workspace=, #previous_irb_workspace=

Constructor Details

#initialize(*params) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
# File 'lib/objects/node/server/base.rb', line 6

def initialize(*params)
  super
  # Set the object_id - sub inventories dup servers into new collections. This allows us to spot duplicates when interacting with collections
  @origin_object_id = object_id
end

Dynamic Method Handling

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

Instance Attribute Details

#origin_object_idObject (readonly)

Returns the value of attribute origin_object_id.



4
5
6
# File 'lib/objects/node/server/base.rb', line 4

def origin_object_id
  @origin_object_id
end

Instance Method Details

#add_list_attributes(attrs) ⇒ Object



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

def add_list_attributes(attrs)
  @attribs = list_attributes.merge(attrs)
end

#cache_dataObject



238
239
240
241
242
243
244
245
# File 'lib/objects/node/server/base.rb', line 238

def cache_data
  d = { identifier: @original_identifier }
  d[:internal_ip_address] = internal_ip_address if internal_ip_address
  d[:public_ip_address] = public_ip_address if public_ip_address
  d[:description] = description if description
  d[:cloud_tags] = cloud_tags
  d
end

#close_ssh_connectionObject



150
151
152
# File 'lib/objects/node/server/base.rb', line 150

def close_ssh_connection
  ssh_driver.close_ssh_connection
end

#cloud_tagsObject



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

def cloud_tags
  @generated_tags ||= do_generate_cloud_tags
end

#do_generate_cloud_tagsObject



72
73
74
# File 'lib/objects/node/server/base.rb', line 72

def do_generate_cloud_tags
  raise 'Should be overidden'
end

#do_run(raw_commands) ⇒ Object



247
248
249
250
251
252
253
254
# File 'lib/objects/node/server/base.rb', line 247

def do_run(raw_commands)
  raw_commands = raw_commands.is_a?(String) ? [raw_commands] : raw_commands
  commands = raw_commands.collect { |raw_command| ::Bcome::Ssh::Command.new(node: self, raw: raw_command) }
  command_exec = ::Bcome::Ssh::CommandExec.new(commands)
  command_exec.execute!
  commands.each(&:unset_node)
  commands
end

#dup_with_new_parent(new_parent) ⇒ Object



41
42
43
44
45
# File 'lib/objects/node/server/base.rb', line 41

def dup_with_new_parent(new_parent)
  new_node = clone
  new_node.update_parent(new_parent)
  new_node
end

#dynamic_server?Boolean

Returns:

  • (Boolean)


280
281
282
# File 'lib/objects/node/server/base.rb', line 280

def dynamic_server?
  !static_server?
end

#enabled_menu_itemsObject



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

def enabled_menu_items
  (super + %i[get ssh tags pseudo_tty tunnel]) - %i[workon enable disable enable! disable!]
end

#execute_script(script_name) ⇒ Object



173
174
175
176
# File 'lib/objects/node/server/base.rb', line 173

def execute_script(script_name)
  command_result = ::Bcome::Ssh::ScriptExec.execute(self, script_name)
  command_result
end

#get(remote_path, local_path) ⇒ Object



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

def get(remote_path, local_path)
  ssh_driver.get(remote_path, local_path)
end

#has_description?Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/objects/node/server/base.rb', line 272

def has_description?
  !description.nil?
end

#has_no_ssh_connection?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/objects/node/server/base.rb', line 158

def has_no_ssh_connection?
  !has_ssh_connection?
end

#has_ssh_connection?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/objects/node/server/base.rb', line 154

def has_ssh_connection?
  ssh_driver.has_open_ssh_con?
end

#has_tagged_value?(key, values) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/objects/node/server/base.rb', line 63

def has_tagged_value?(key, values)
  matchers = { key: key, values: values }
  cloud_tags.has_key_and_value?(matchers)
end

#hostObject



16
17
18
# File 'lib/objects/node/server/base.rb', line 16

def host
  raise 'Should be overidden'
end

#is_same_machine?(other) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/objects/node/server/base.rb', line 12

def is_same_machine?(other)
  origin_object_id == other.origin_object_id
end

#list_attributesObject



223
224
225
# File 'lib/objects/node/server/base.rb', line 223

def list_attributes
  @attribs ||= set_list_attributes
end

#local_network?Boolean

Returns:

  • (Boolean)


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

def local_network?
  defined?(local_network) && local_network
end

#local_port_forward(start_port, end_port) ⇒ Object Also known as: tunnel



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

def local_port_forward(start_port, end_port)
  ssh_driver.local_port_forward(start_port, end_port)
end

#lsObject Also known as: lsa



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

def ls
  puts "\n" + visual_hierarchy.hierarchy + "\n"
  puts pretty_description
end

#machinesObject



80
81
82
# File 'lib/objects/node/server/base.rb', line 80

def machines
  [self]
end


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/objects/node/server/base.rb', line 100

def menu_items
  base_items = super.dup
  base_items[:tags] = {
    description: 'print out server tags/labels',
    group: :informational
  }
  base_items[:ssh] = {
    description: 'initiate an ssh connection to this server',
    group: :ssh
  }
  base_items[:get] = {
    description: 'Download a file or directory',
    console_only: false,
    usage: 'get "/remote/path", "/local/path"',
    terminal_usage: 'get "/remote/path" "/local/path"',
    group: :file
  }
  base_items[:pseudo_tty] = {
    description: 'Invoke a pseudo-tty session',
    console_only: false,
    usage: 'pseudo_tty "your command"',
    terminal_usage: 'pseudo_tty "your command"',
    group: :ssh
  }
  base_items[:tunnel] = {
    description: 'Create a Tunnel over SSH',
    console_only: false,
    usage: 'tunnel(local_port, destination_port)',
    terminal_usage: 'tunnel local_port destination_port',
    group: :ssh
  }

  base_items
end

#open_ssh_connection(ping = false) ⇒ Object



146
147
148
# File 'lib/objects/node/server/base.rb', line 146

def open_ssh_connection(ping = false)
  ssh_driver.ssh_connection(ping)
end

#origin_namespaceObject



219
220
221
# File 'lib/objects/node/server/base.rb', line 219

def origin_namespace
  parent.namespace
end

#pingObject



200
201
202
203
# File 'lib/objects/node/server/base.rb', line 200

def ping
  ping_result = ssh_driver.ping
  print_ping_result(ping_result)
end


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

def print_ping_result(ping_result = { success: true })
  result_string = ping_result[:success] ? 'success'.success : 'failed'.error

  pretty_ping_result = "\n#{namespace.bc_cyan}:\s#{result_string.bold}\n"
  pretty_ping_result += "Error:\s".bc_cyan + "#{ping_result[:error].message.bc_red}\n" if !ping_result[:success] && ping_result[:error]
  # pretty_ping_result += ping_result[:backtrace] if ping_result[:backtrace]
  pretty_ping_result += "config:\s".bc_cyan + JSON.pretty_generate(ssh_driver.pretty_ssh_config)
  puts pretty_ping_result
end

#pseudo_tty(command) ⇒ Object



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

def pseudo_tty(command)
  as_pseudo_tty = true
  ssh_cmd = ssh_driver.ssh_command(as_pseudo_tty)
  tty_command = "#{ssh_cmd} '#{command}'"
  execute_local(tty_command)
end

#put(local_path, remote_path, *_params) ⇒ Object



182
183
184
# File 'lib/objects/node/server/base.rb', line 182

def put(local_path, remote_path, *_params)
  ssh_driver.put(local_path, remote_path)
end

#put_str(string, remote_path, *_params) ⇒ Object



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

def put_str(string, remote_path, *_params)
  ssh_driver.put_str(string, remote_path)
end

#reopen_ssh_connectionObject



140
141
142
143
144
# File 'lib/objects/node/server/base.rb', line 140

def reopen_ssh_connection
  puts "Connecting\s".informational + identifier
  close_ssh_connection
  open_ssh_connection
end

#requires_description?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/objects/node/server/base.rb', line 88

def requires_description?
  false
end

#requires_type?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/objects/node/server/base.rb', line 92

def requires_type?
  false
end

#rsync(local_path, remote_path) ⇒ Object



178
179
180
# File 'lib/objects/node/server/base.rb', line 178

def rsync(local_path, remote_path)
  ssh_driver.rsync(local_path, remote_path)
end

#run(*raw_commands) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/objects/node/server/base.rb', line 256

def run(*raw_commands)
  raise ::Bcome::Exception::MethodInvocationRequiresParameter, "Please specify commands when invoking 'run'" if raw_commands.empty?

  do_run(raw_commands)
rescue IOError, Errno::EBADF
  reopen_ssh_connection
  do_run(raw_commands)
rescue Exception => e
  if e.message == 'Unexpected spurious read wakeup'
    reopen_ssh_connection
    do_run(raw_commands)
  else
    raise e
  end
end

#server?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/objects/node/server/base.rb', line 84

def server?
  true
end

#set_list_attributesObject



227
228
229
230
231
232
233
234
235
236
# File 'lib/objects/node/server/base.rb', line 227

def set_list_attributes
  attribs = {
    "identifier": :identifier,
    "internal ip": :internal_ip_address,
    "public ip": :public_ip_address,
    "host": :host
  }

  attribs
end

#set_network_configuration_overridesObject

Override a server’s namespace parameters. Enabled features such as specific SSH config for a particular server, i.e. overidding that of it’s parent  inventory namespace.



26
27
28
29
30
31
32
33
34
35
# File 'lib/objects/node/server/base.rb', line 26

def set_network_configuration_overrides
  overridden_attributes = ::Bcome::Node::Factory.instance.machines_data_for_namespace(namespace.to_sym)
  overridden_attributes.each do |override_key, override_value|
    singleton_class.class_eval do
      define_method(override_key) do
        override_value
      end
    end
  end
end

#set_view_attributesObject



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

def set_view_attributes
  super
end

#sshObject



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

def ssh
  ssh_driver.do_ssh
end

#static_server?Boolean

Returns:

  • (Boolean)


276
277
278
# File 'lib/objects/node/server/base.rb', line 276

def static_server?
  false
end

#tagsObject



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

def tags
  data_print_from_hash(cloud_tags.data, 'Tags')
end

#tags_hObject



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

def tags_h
  cloud_tags.data
end

#typeObject



76
77
78
# File 'lib/objects/node/server/base.rb', line 76

def type
  'server'
end

#update_identifier(new_identifier) ⇒ Object



68
69
70
# File 'lib/objects/node/server/base.rb', line 68

def update_identifier(new_identifier)
  @identifier = new_identifier
end

#update_parent(new_parent) ⇒ Object



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

def update_parent(new_parent)
  @parent = new_parent
end