Class: Bcome::Stack::Base

Inherits:
Object show all
Includes:
CommandHelper, ContextFunctions, Orchestrator::Registry
Defined in:
lib/stack/base.rb

Direct Known Subclasses

Environment, Estate, Instance, Platform

Constant Summary

Constants included from Orchestrator::Registry

Orchestrator::Registry::CONFIGS_PATH

Constants included from ContextFunctions

ContextFunctions::CONFIGS_PATH

Instance Method Summary collapse

Methods included from Orchestrator::Registry

#config_for_registry, #do_load_recipes, #has_registry_config?, #load_recipe_registry, #recipe_registry, #registry_configs_path

Methods included from CommandHelper

#run_local

Methods included from ContextFunctions

#command_for_context, #context_cmd_config_path, #context_cmd_functions, #context_commands, #execute_command_for_context, #has_context_cmd_functions?, #has_context_functions_config?, #load_context_cmd_functions

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/stack/base.rb', line 32

def method_missing(method_sym, *arguments, &block)
  if command_for_context = command_for_context(method_sym)
    execute_command_for_context(command_for_context)
  elsif recipe = ::Bcome::Orchestrator::Recipe.by_identifier(method_sym.to_s, self)
    puts "Executing Command Recipe #{recipe.recipe_identifier}".informational
    puts "Description: #{recipe.description}".command
    recipe.execute!(machines)
    return
  else
    super
  end
end

Instance Method Details

#available_resources_options_stringObject



112
113
114
# File 'lib/stack/base.rb', line 112

def available_resources_options_string
  "Please select from one of: #{all_items.join(', ')}'"
end

#become(object) ⇒ Object



74
75
76
# File 'lib/stack/base.rb', line 74

def become(object)
  BECOME.set(object, self)
end

#become_identifierObject

Core workspace behaviour



70
71
72
# File 'lib/stack/base.rb', line 70

def become_identifier
 ::START_PROMPT
end

#construct_nodeObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stack/base.rb', line 45

def construct_node
  node_attributes = 

  node_attributes.merge!({
    collection_key => resources.collect(&:node)
  }) if respond_to?(:collection_key)
 
  this_node = node_level_klass.new(node_attributes)
  this_node.set_parent_reference(this_node, child_reference_key, collection_key) if respond_to?(:collection_key) && respond_to?(:child_reference_key)
  return this_node
end

#describeObject



116
117
118
119
120
121
122
123
# File 'lib/stack/base.rb', line 116

def describe
  if self.respond_to?(:do_describe)
    message = "\n\tCollection Key:".menu_item_cyan + "\s#{reference_key}\n\n#{self.do_describe}"
  else
    message = "\nNothing to describe. Use 'ls' or 'list' to see namespace options".headsup unless self.respond_to?(:do_describe)
  end
  puts message
end

#has_sub_nodes?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/stack/base.rb', line 12

def has_sub_nodes?
  return true
end

#highlight?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/stack/base.rb', line 108

def highlight?
  false  ## override in stack objects that should be highlighted within a list, e.g. instance objects at the environment level that have been selected to workon on
end

#initObject



61
62
63
64
# File 'lib/stack/base.rb', line 61

def init
  puts "loading resources #{self.class} "
  node
end

#machinesObject



8
9
10
# File 'lib/stack/base.rb', line 8

def machines
  node.machines
end


92
93
94
# File 'lib/stack/base.rb', line 92

def menu
  ::RENDER.menu(menu_items, self)
end


96
97
98
99
100
101
102
103
104
105
106
# File 'lib/stack/base.rb', line 96

def menu_items
  [
    { :command => "list / ls", :description => "List all available resources at the current context." },
    { :command => "describe", :description => "Describe the resource object at the current context." },
    { :command => "cd [Node Name]", :description => "Select a resource object, and switch to its context.", :usage => "cd 'YourServerName'" },
    { :command => "exit", :description => "Return to the previous context" },
    { :command => "exit!", :description => "Close all contexts, and exit Become."},
    { :command => "local", :description => "Execute a shell command on your local machine.", :usage => 'local "command"'},
    { :command => "machines", :description => "Return all servers below the current level to the console. These objects can be manipulated directly" }
  ]
end

#nodeObject



24
25
26
# File 'lib/stack/base.rb', line 24

def node
  @node ||= construct_node
end

#node_level_klassObject



57
58
59
# File 'lib/stack/base.rb', line 57

def node_level_klass
  raise "Should be overidden in derived stack type"
end

#recipesObject

Construction & Initialisation



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

def recipes
  @recipes ||= do_load_recipes
end

#respond_to_bcome_context_method?(method_sym) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/stack/base.rb', line 28

def respond_to_bcome_context_method?(method_sym)
  return (command_for_context(method_sym) || ::Bcome::Orchestrator::Recipe.by_identifier(method_sym.to_s, self)) ? true : false
end

#workon(identifier) ⇒ Object Also known as: w, cd



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/stack/base.rb', line 78

def workon(identifier)
  resource = resource_for_identifier(identifier)
 
  unless resource
    puts "\nNo matching #{collection_key} for identifier '#{identifier}'.".failure + "\n\n#{available_resources_options_string}" + "\n\n"
  else
    puts "\nFrom #{resource.reference_key}, working on '#{identifier}'.".informational + "\n\n" 
    puts "Hit 'menu' or 'ls' to see what you can do here.\n\n"
    become(resource)
  end
end