Module: Bcome::WorkspaceCommands

Included in:
Node::Base
Defined in:
lib/objects/modules/workspace_commands.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/objects/modules/workspace_commands.rb', line 159

def method_missing(method_sym, *arguments)
  unless method_is_available_on_node?(method_sym)
    raise NameError, "NameError: undefined local variable or method '#{method_sym}'"
  end

  if resource_identifiers.include?(method_sym.to_s)
    method_sym.to_s
  elsif instance_variable_defined?("@#{method_sym}")
    instance_variable_get("@#{method_sym}")
  else
    command = user_command_wrapper.command_for_console_command_name(method_sym)
    command.execute(self, arguments)
  end
end

Instance Method Details

#backObject



112
113
114
# File 'lib/objects/modules/workspace_commands.rb', line 112

def back
  exit
end

#cd(identifier) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/objects/modules/workspace_commands.rb', line 65

def cd(identifier)
  if resource = resources.for_identifier(identifier)
    if resource.parent.resources.is_active_resource?(resource)
      ::Bcome::Workspace.instance.set(current_context: self, context: resource)
    else
      puts "\nCannot enter context - #{identifier} is disabled. To enable enter 'enable #{identifier}'\n".error
    end
  else
    raise Bcome::Exception::InvalidBreadcrumb, "Cannot find a node named '#{identifier}'"
    puts "#{identifier} not found"
  end
end

#clear!Object



124
125
126
127
128
129
# File 'lib/objects/modules/workspace_commands.rb', line 124

def clear!
  # Clear any disabled selection at this level and at all levels below
  resources.clear!
  resources.each(&:clear!)
  nil
end

#disable(*ids) ⇒ Object



116
117
118
# File 'lib/objects/modules/workspace_commands.rb', line 116

def disable(*ids)
  ids.each { |id| resources.do_disable(id) }
end

#disable!Object



137
138
139
140
141
# File 'lib/objects/modules/workspace_commands.rb', line 137

def disable!
  resources.disable!
  resources.each(&:disable!)
  nil
end

#enable(*ids) ⇒ Object



120
121
122
# File 'lib/objects/modules/workspace_commands.rb', line 120

def enable(*ids)
  ids.each { |id| resources.do_enable(id) }
end

#enable!Object



143
144
145
146
147
# File 'lib/objects/modules/workspace_commands.rb', line 143

def enable!
  resources.enable!
  resources.each(&:enable!)
  nil
end

#interactiveObject



27
28
29
# File 'lib/objects/modules/workspace_commands.rb', line 27

def interactive
  ::Bcome::Interactive::Session.run(self, :interactive_ssh)
end

#is_node_level_method?(method_sym) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/objects/modules/workspace_commands.rb', line 155

def is_node_level_method?(method_sym)
  respond_to?(method_sym) || method_is_available_on_node?(method_sym)
end

#list_in_tree(tab, resources) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/objects/modules/workspace_commands.rb', line 49

def list_in_tree(tab, resources)
  resources.sort_by(&:identifier).each do |resource|
    next if resource.parent && !resource.parent.resources.is_active_resource?(resource)
    resource.load_nodes if resource.inventory? && !resource.nodes_loaded?
    print_tree_view_for_resource(tab, resource)
    list_in_tree("#{tab}\t", resource.resources)
  end
end

#ls(active_only = false) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/objects/modules/workspace_commands.rb', line 2

def ls(active_only = false)
  puts "\n\n" + visual_hierarchy.hierarchy + "\n"
  puts "\t" + "Available #{list_key}s:".title + "\n\n"

  iterate_over = active_only ? @resources.active : @resources

  if iterate_over.any?
    iterate_over.sort_by(&:identifier).each do |resource|
      is_active = @resources.is_active_resource?(resource)
      puts resource.pretty_description(is_active)
      puts "\n"
    end
  else
    puts "\tNo resources found".informational
  end

  new_line
  nil
end

#lsaObject



22
23
24
25
# File 'lib/objects/modules/workspace_commands.rb', line 22

def lsa
  show_active_only = true
  ls(show_active_only)
end

#method_in_registry?(method_sym) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/objects/modules/workspace_commands.rb', line 174

def method_in_registry?(method_sym)
  ::Bcome::Registry::CommandList.instance.command_in_list?(self, method_sym)
end

#method_is_available_on_node?(method_sym) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/objects/modules/workspace_commands.rb', line 178

def method_is_available_on_node?(method_sym)
  resource_identifiers.include?(method_sym.to_s) || instance_variable_defined?("@#{method_sym}") || method_in_registry?(method_sym) || respond_to?(method_sym)
end

#new_lineObject



194
195
196
# File 'lib/objects/modules/workspace_commands.rb', line 194

def new_line
  puts "\n"
end

#parentsObject



43
44
45
46
47
# File 'lib/objects/modules/workspace_commands.rb', line 43

def parents
  ps = []
  ps << [parent, parent.parents] if has_parent?
  ps.flatten
end

#pingObject



91
92
93
# File 'lib/objects/modules/workspace_commands.rb', line 91

def ping
  ::Bcome::Ssh::ConnectionHandler.connect(self, is_ping: true)
end

#pretty_description(is_active = true) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/objects/modules/workspace_commands.rb', line 95

def pretty_description(is_active = true)
  desc = ''
  list_attributes.each do |key, value|
    next unless respond_to?(value) || instance_variable_defined?("@#{value}")
    attribute_value = send(value)
    next unless attribute_value

    desc += "\t"
    desc += is_active ? key.to_s.resource_key : key.to_s.resource_key_inactive
    desc += "\s" * (12 - key.length)
    desc += is_active ? attribute_value.resource_value : attribute_value.resource_value_inactive
    desc += "\n"
    desc = desc unless is_active
  end
  desc
end


58
59
60
61
62
63
# File 'lib/objects/modules/workspace_commands.rb', line 58

def print_tree_view_for_resource(tab, resource)
  separator = '-'
  tree_item = tab.to_s + separator.resource_key + " #{resource.type.resource_key} \s#{resource.identifier.resource_value}"
  tree_item += ' (empty set)' if !resource.server? && !resource.resources.has_active_nodes?
  puts tree_item
end

#resource_identifiersObject

Helpers –



151
152
153
# File 'lib/objects/modules/workspace_commands.rb', line 151

def resource_identifiers
  resources.collect(&:identifier)
end

#run(*raw_commands) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/objects/modules/workspace_commands.rb', line 78

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

  ::Bcome::Ssh::ConnectionHandler.connect(self, {})

  machines.pmap do |machine|
    commands = machine.do_run(raw_commands)
    results[machine.namespace] = commands
  end
  results
end

#treeObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/objects/modules/workspace_commands.rb', line 31

def tree
  puts "\nTree view\n".title
  tab = ''
  parents.reverse.each do |p|
    print_tree_view_for_resource(tab, p)
    tab = "#{tab}\t"
  end
  print_tree_view_for_resource(tab, self)
  list_in_tree("#{tab}\t", resources)
  print "\n"
end

#tree_descriptionsObject



189
190
191
192
# File 'lib/objects/modules/workspace_commands.rb', line 189

def tree_descriptions
  d = parent ? parent.tree_descriptions + [description] : [description]
  d.flatten
end

#visual_hierarchyObject



182
183
184
185
186
187
# File 'lib/objects/modules/workspace_commands.rb', line 182

def visual_hierarchy
  tabs = 0
  hierarchy = ''
  tree_descriptions.each { |d| hierarchy += "#{"\s\s\s" * tabs}|- #{d}\n"; tabs += 1; }
  hierarchy
end

#workon(*ids) ⇒ Object



131
132
133
134
135
# File 'lib/objects/modules/workspace_commands.rb', line 131

def workon(*ids)
  resources.disable!
  ids.each { |id| resources.do_enable(id) }
  puts "\nYou are now working on '#{ids.join(', ')}\n".informational
end