Class: Bcome::Stack::Environment

Inherits:
Base show all
Includes:
BecomeObject, EnvironmentSSH, FogHelper, Functions, Bcome::Selections
Defined in:
lib/stack/environment.rb

Direct Known Subclasses

View

Constant Summary

Constants included from Functions

Functions::CONFIGS_PATH

Constants included from BecomeObject

BecomeObject::CONFIGS_PATH

Constants included from Orchestrator::Registry

Orchestrator::Registry::CONFIGS_PATH

Constants included from ContextFunctions

ContextFunctions::CONFIGS_PATH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Functions

#F, #cmd_config_path, #cmd_for_ref, #cmd_functions, #functions, #has_cmd_functions?, #has_functions_config?, #load_cmd_functions

Methods included from EnvironmentSSH

#bastion_ip_address, #bastion_server, #bootstrap_proxy_command, #execute_cmd, #execute_command, #execute_scp_download, #execute_scp_upload, #proxy, #proxy_command, #ssh_key_path, #ssh_mode_type, #ssh_mode_user, #ssh_nat_user, #ssh_user, #valid_ssh_modes

Methods included from FogHelper

#colour_code_for_server_status, #credentials_key, #ec2_filters, #ec2_tags, #filter_by_tags, #fog_client, #instance_matches_tags?, #reload!, #render_servers_not_returned_by_filtered_lookup, #servers, #servers_not_returned_by_filtered_lookup, #show_all_unfiltered_status?, #unfiltered_fog_lookup, #unfiltered_search_params, #unfiltered_servers

Methods included from Bcome::Selections

#add, #add!, #add_object, #clear!, #find_resource_by_identifier, #get, #in_resources?, #manage_object, #no_selections?, #no_selections_error, #object_in_selections?, #put, #remove, #remove_object, #run, #selections

Methods included from BecomeObject

#all_items, #become_identifier, #context_breadcrumb, #has_main_context?, #has_potential_resources?, #identifier_for, #is_stack_level_resource?, #list, #local_download_path, #main_context, #main_context=, #mini_list, #namespace, #previous_workspace_object, #previous_workspace_object=, #resources, #responds_to_list?

Methods inherited from Base

#available_resources_options_string, #become, #become_identifier, const_missing, #construct_node, #describe, #has_sub_nodes?, #highlight?, #init, #interactive, #menu, #method_missing, #node, #recipes, #respond_to_bcome_context_method?, #workon

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

Constructor Details

#initialize(meta_data, parent) ⇒ Environment

Returns a new instance of Environment.



22
23
24
25
26
# File 'lib/stack/environment.rb', line 22

def initialize(, parent)
  @meta_data = 
  @identifier = [:environment]
  @platform = parent
end

Dynamic Method Handling

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

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



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

def identifier
  @identifier
end

#meta_dataObject (readonly)

Returns the value of attribute meta_data.



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

def 
  @meta_data
end

#platformObject (readonly)

Returns the value of attribute platform.



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

def platform
  @platform
end

Class Method Details

.collection_from_config(parent, configuration) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/stack/environment.rb', line 11

def collection_from_config(parent, configuration)
  collection = []
  configuration.each do ||
    collection << new(, parent) if [:network].to_s == parent.identifier.to_s 
  end
  return collection
end

Instance Method Details

#child_reference_keyObject



194
195
196
# File 'lib/stack/environment.rb', line 194

def child_reference_key
  :environment 
end

#collection_keyObject



198
199
200
# File 'lib/stack/environment.rb', line 198

def collection_key
  :instances
end

#collection_klassObject



186
187
188
# File 'lib/stack/environment.rb', line 186

def collection_klass
  ::INSTANCE_STACK_KLASS
end

#config_dirObject



146
147
148
# File 'lib/stack/environment.rb', line 146

def config_dir
  "#{CONFIGS_PATH}/static_instances/"
end

#config_pathObject



150
151
152
# File 'lib/stack/environment.rb', line 150

def config_path
  "#{config_dir}#{platform.identifier}_#{identifier}-instances.yml"
end

#descriptionObject



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/stack/environment.rb', line 174

def description
  desc = "\tNode Id:".menu_item_cyan + "\s#{node.identifier}".menu_item_green + "\n"
  desc += "\tNet Lookup:".menu_item_cyan + "\s#{node.network_lookup_type}".menu_item_white + "\n"
  desc += "\tSSH Mode:".menu_item_cyan + "\s#{node.ssh_mode_type}".menu_item_white
  if has_views?
    views.each do |view|
      desc += "\n\t \\ #{"view".menu_item_cyan}: #{view.identifier.menu_item_green}"
    end
  end
  return desc + "\n\n"
end

#do_create_viewsObject



74
75
76
77
78
79
80
81
# File 'lib/stack/environment.rb', line 74

def do_create_views
  return [] unless [:sub_views]
  view_collection = []
  [:sub_views].each do |sub_view_data|
    view_collection << ::Bcome::Stack::View.new(@meta_data, @platform, self, sub_view_data)
  end
  return view_collection
end

#do_describeObject



170
171
172
# File 'lib/stack/environment.rb', line 170

def do_describe
  description
end

#do_load_resources(restock_inventory = false) ⇒ Object



132
133
134
135
136
# File 'lib/stack/environment.rb', line 132

def do_load_resources(restock_inventory = false)
  loaded_resources = manage_inventory(restock_inventory)
  node.set_instances(loaded_resources)
  return loaded_resources
end

#dynamic_network_lookup?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/stack/environment.rb', line 217

def dynamic_network_lookup?
  return network_lookup_type == "dynamic"
end

#has_views?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/stack/environment.rb', line 42

def has_views?
  views.any?
end

#list_viewsObject Also known as: lv



46
47
48
49
50
51
52
# File 'lib/stack/environment.rb', line 46

def list_views
  if has_views?
    ::RENDER.list_items("Sub-views", views)
  else
    puts "\n" + "This environment has no configured sub-views".warning + "\n\n"
  end
end

#load_and_take_stockObject



126
127
128
129
130
# File 'lib/stack/environment.rb', line 126

def load_and_take_stock
  loaded_resources = refresh_dynamic_inventory
  take_stock!(loaded_resources)
  loaded_resources
end

#load_from_static_inventoryObject



99
100
101
102
103
104
# File 'lib/stack/environment.rb', line 99

def load_from_static_inventory
  path = config_path
  raise "Missing instances.yml configuration for your environment. You've selected a static network lookup, and yet I cannot find #{config_path}".failure unless File.exist?(config_path)
  config = YAML.load_file(path)
  return collection_klass.collection_from_config(self, config)
end

#machinesObject



28
29
30
31
32
# File 'lib/stack/environment.rb', line 28

def machines
  node_machines = node.machines
  do_load_resources unless node_machines.any?
  return node.machines
end

#manage_inventory(restock_inventory = false) ⇒ Object



138
139
140
141
142
143
# File 'lib/stack/environment.rb', line 138

def manage_inventory(restock_inventory = false)
  return load_from_static_inventory unless dynamic_network_lookup? # No dynamic lookup, load from static
  return refresh_dynamic_inventory unless use_inventory_cache?   # Dynamic lookup, with no cache, just load from dynamic
  return load_and_take_stock if !static_inventory_is_set? || restock_inventory # Dynamic lookup and stock, if stock not set or we're explicitly re-stocking
  return load_from_static_inventory # otherwise just load from static.
end


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/stack/environment.rb', line 83

def menu_items
  super + [
    { :command => "add", :description => "Add a resource you wish to work on.", :usage => "add MyServerIdentifier, OR add [array, of, identifiers]" },
    { :command => "add!", :description => "Add all resources from the current context." },
    { :command => "remove", :description => "Remove a resource you no longer with to work on.", :usage => "remove MyServerIdentifier, OR remove [array, of, identifiers]" },
    { :command => "clear!", :description => "Remove all selected resources." },
    { :command => "selections", :description => "Show all added resources.", :usage => "selections / s"},
    { :command => "run", :description => "Execute a command on all selected resources", :usage => "run 'command'" },
    { :command => "get", :description => "Download from remote from all selected resources (recursive - uses rsync)", :usage => "get 'remote_path'" },
    { :command => "put", :description => "Copy files up to all selected resources (recursive - uses rsync)", :usage => "put 'local_path', 'remote_path'" },
    { :command => "sudo", :description => "Run 'get' and 'put' in sudo mode (assumes you have passwordless sudo setup)" },
    { :command => 'functions', :description => "List all available custom functions" },
    { :command => 'lv', :description => "List all configured sub-views" },
  ]
end

#network_lookupObject



206
207
208
209
# File 'lib/stack/environment.rb', line 206

def network_lookup
  raise "Missing network lookup in networks_environment configuration".failure unless @meta_data[:network_lookup]
  return @meta_data[:network_lookup]
end

#network_lookup_typeObject



211
212
213
214
215
# File 'lib/stack/environment.rb', line 211

def network_lookup_type
  type = network_lookup[:type]
  raise "Unknown network lookup type '#{type}" unless ["dynamic", "static"].include?(type)
  return type
end

#node_level_klassObject



202
203
204
# File 'lib/stack/environment.rb', line 202

def node_level_klass
  ::Bcome::Node::Environment
end

#reference_keyObject



190
191
192
# File 'lib/stack/environment.rb', line 190

def reference_key
  :environments
end

#refresh_dynamic_inventoryObject



106
107
108
109
# File 'lib/stack/environment.rb', line 106

def refresh_dynamic_inventory
  loaded_resources = collection_klass.collection_from_fog_data(self, servers)
  loaded_resources
end

#resource_for_identifier(identifier) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/stack/environment.rb', line 63

def resource_for_identifier(identifier)
  match = super
  unless match
    # Unless we've found a match amongst our server instances, look in our views
    matches = views.select {|view| view.identifier.to_sym == identifier.to_sym }
    raise "Retrieved more than one match for #{collection_key} '#{identifier}'. Selection is ambiguous" if matches.size > 1
    match = matches.first
  end
  return match
end

#resource_identifiersObject



55
56
57
# File 'lib/stack/environment.rb', line 55

def resource_identifiers
  @resource_identifiers ||= resource_identifiers_with_views
end

#resource_identifiers_with_viewsObject



59
60
61
# File 'lib/stack/environment.rb', line 59

def resource_identifiers_with_views
  return has_views? ? (resources.collect(&:identifier) << views.collect(&:identifier)).flatten : resources.collect(&:identifier)
end

#restockObject



112
113
114
115
116
# File 'lib/stack/environment.rb', line 112

def restock
  puts "\n/ Refreshing server inventory".menu_item_green
  take_stock!(collection_klass.collection_from_fog_data(self, servers))
  puts " Done. Inventory written to: #{config_path}\n".menu_item_green
end

#should_eager_load_children?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/stack/environment.rb', line 34

def should_eager_load_children?
  false
end

#static_inventory_is_set?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/stack/environment.rb', line 122

def static_inventory_is_set?
  File.exist?(config_path)
end

#take_stock!(resources) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/stack/environment.rb', line 154

def take_stock!(resources)
  `mkdir -p #{config_dir}`
  inventory = { :instances => [] }  
  resources.each do |resource|
    inventory[:instances] << {
      :identifier => resource.identifier,
      :external_network_interface_address => resource.external_network_interface_address,
      :public_ip_address => resource.public_ip_address,
      :role => resource.role
    }
  end
  File.open(config_path, "w") do |file|
    file.write inventory.to_yaml
  end
end

#use_inventory_cache?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/stack/environment.rb', line 118

def use_inventory_cache?
  (dynamic_network_lookup? && self.[:inventory_cache])
end

#viewsObject



38
39
40
# File 'lib/stack/environment.rb', line 38

def views
  @views ||= do_create_views
end