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

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

Direct Known Subclasses

Dynamic, Static

Constant Summary

Constants inherited from Base

Base::DEFAULT_IDENTIFIER

Constants included from LocalMetaDataFactory

LocalMetaDataFactory::META_DATA_FILE_PATH_PREFIX

Instance Attribute Summary collapse

Attributes inherited from Base

#params

Instance Method Summary collapse

Methods inherited from Base

#close_ssh_connections, #collection?, const_missing, #data_print_from_hash, #execute_local, #has_parent?, #has_proxy?, #inventory?, #invoke, #is_top_level_node?, #keyed_namespace, #list_key, #namespace, #no_nodes?, #nodes_loaded?, #open_ssh_connections, #prompt_breadcrumb, #proxy, #recurse_resource_for_identifier, #resource_for_identifier, #resources, #scp, #validate_attributes, #validate_identifier

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, #mode, #tab_spacing

Methods included from Attributes

#description, #filters, #identifier, #network_data, #network_driver, #recurse_hash_data_for_instance_var, #ssh_data, #ssh_driver

Methods included from WorkspaceCommands

#back, #cd, #clear!, #disable, #disable!, #enable, #enable!, #interactive, #is_node_level_method?, #list_in_tree, #method_in_registry?, #method_is_available_on_node?, #method_missing, #new_line, #parents, #pretty_description, #print_tree_view_for_resource, #resource_identifiers, #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.



6
7
8
9
10
11
# 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
  @bootstrap = false
end

Dynamic Method Handling

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

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

#bootstrap?Boolean

Returns:

  • (Boolean)


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

def bootstrap?
  @bootstrap ? true : false
end

#cache_dataObject



181
182
183
184
185
186
187
188
# File 'lib/objects/node/server/base.rb', line 181

def cache_data
  d = { identifier: 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



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

def close_ssh_connection
  ssh_driver.close_ssh_connection
end

#cloud_tagsObject



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

def cloud_tags
  @generated_tags ||= do_generate_cloud_tags
end

#do_generate_cloud_tagsObject



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

def do_generate_cloud_tags
  raise "Should be overidden"
end

#do_run(raw_commands) ⇒ Object



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

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 {|c| c.unset_node }
  return commands
end

#dup_with_new_parent(new_parent) ⇒ Object



22
23
24
25
26
# File 'lib/objects/node/server/base.rb', line 22

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

#dynamic_server?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/objects/node/server/base.rb', line 213

def dynamic_server?
  !static_server?
end

#enabled_menu_itemsObject



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

def enabled_menu_items
  (super + [:get, :ssh, :tags]) - [:enable, :disable, :enable!, :disable!]
end

#execute_script(script_name) ⇒ Object



116
117
118
119
# File 'lib/objects/node/server/base.rb', line 116

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

#get(remote_path, local_path) ⇒ Object



129
130
131
# File 'lib/objects/node/server/base.rb', line 129

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

#has_description?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/objects/node/server/base.rb', line 205

def has_description?
  !@description.nil?
end

#has_no_ssh_connection?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/objects/node/server/base.rb', line 108

def has_no_ssh_connection?
  !has_ssh_connection?
end

#has_ssh_connection?Boolean

Returns:

  • (Boolean)


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

def has_ssh_connection?
  ssh_driver.has_open_ssh_con?
end

#has_tagged_value?(key, values) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#list_attributesObject



170
171
172
173
174
175
176
177
178
179
# File 'lib/objects/node/server/base.rb', line 170

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

  attribs.merge!("description": :description ) if has_description?
  attribs
end

#lsObject Also known as: lsa



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

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

#machinesObject



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

def machines
  [self]
end


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/objects/node/server/base.rb', line 77

def menu_items
  base_items = super.dup
  base_items[:tags] = {
    description: "print out remote EC2 tags"
  }
  base_items[:ssh] = {
    description: "initiate an ssh connection to this server",
  }
  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\""
  }


  base_items
end

#open_ssh_connectionObject



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

def open_ssh_connection
  ssh_driver.ssh_connection  
end

#pingObject



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

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


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/objects/node/server/base.rb', line 144

def print_ping_result(ping_result = { success: true })
  result = {
    namespace => {
      "connection" => ping_result[:success] ? "success" : "failed",
      "ssh_config" => ssh_driver.pretty_config_details
    }
  }

  unless ping_result[:success]
    result[namespace]["error"] = ping_result[:error].message
  end

  colour = ping_result[:success] ? :green : :red

  ap result, {
    :color => {
       hash:  colour,
       symbol: colour,
       string: colour,
       keyword: colour,
       variable: colour,
       array: colour
    }
  }
end

#put(local_path, remote_path) ⇒ Object



125
126
127
# File 'lib/objects/node/server/base.rb', line 125

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

#requires_description?Boolean

Returns:

  • (Boolean)


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

def requires_description?
  false
end

#requires_type?Boolean

Returns:

  • (Boolean)


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

def requires_type?
  false
end

#rsync(local_path, remote_path) ⇒ Object



121
122
123
# File 'lib/objects/node/server/base.rb', line 121

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

#run(*raw_commands) ⇒ Object



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

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

#server?Boolean

Returns:

  • (Boolean)


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

def server?
  true
end

#sshObject



112
113
114
# File 'lib/objects/node/server/base.rb', line 112

def ssh
  ssh_driver.do_ssh
end

#static_server?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/objects/node/server/base.rb', line 209

def static_server?
  false
end

#tagsObject



32
33
34
# File 'lib/objects/node/server/base.rb', line 32

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

#toggle_bootstrap(set_to = (@bootstrap ? false : true)) ⇒ Object



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

def toggle_bootstrap(set_to = (@bootstrap ? false : true))
  @bootstrap = set_to
  puts "Bootstrap is #{bootstrap? ? "on" : "off" }".informational
end

#typeObject



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

def type
  "server"
end

#update_identifier(new_identifier) ⇒ Object



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

def update_identifier(new_identifier)
  @identifier = new_identifier
end

#update_parent(new_parent) ⇒ Object



28
29
30
# File 'lib/objects/node/server/base.rb', line 28

def update_parent(new_parent)
  @parent = new_parent
end