Class: MotherBrain::EnvironmentManager
- Inherits:
-
Object
- Object
- MotherBrain::EnvironmentManager
- Includes:
- Celluloid, MB::Mixin::Locks, MB::Mixin::Services, Logging
- Defined in:
- lib/mb/environment_manager.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#async_configure(id, options = {}) ⇒ JobTicket
Asynchronously configure a target environment with the given attributes.
- #async_examine_nodes(id, options = {}) ⇒ Object
-
#configure(job, id, options = {}) ⇒ Object
private
Configure a target environment with the given attributes.
-
#create(name) ⇒ Ridley::EnvironmentResource
Creates an environment.
-
#create_from_file(path) ⇒ Ridley::EnvironmentResource
Creates an environment from JSON contained in a file.
-
#destroy(name) ⇒ Ridley::EnvironmentResource?
Destroys an environment.
- #examine_nodes(job, id, options = {}) ⇒ Object
-
#find(id) ⇒ Ridley::EnvironmentResource
Find an environment on the remote Chef server.
-
#initialize ⇒ EnvironmentManager
constructor
A new instance of EnvironmentManager.
-
#list ⇒ Array<Ridley::EnvironmentResource>
Returns a list of environments present on the remote server.
-
#nodes_for_environment(name) ⇒ Array(Ridley::NodeObject)
Returns an array of nodes for an environment.
-
#purge_nodes(name) ⇒ Object
Removes all nodes and clients from the Chef server for a given environment.
Methods included from Logging
add_argument_header, dev, filename, #log_exception, logger, #logger, reset, set_logger, setup
Constructor Details
#initialize ⇒ EnvironmentManager
Returns a new instance of EnvironmentManager.
19 20 21 |
# File 'lib/mb/environment_manager.rb', line 19 def initialize log.debug { "Environment Manager starting..." } end |
Class Method Details
.instance ⇒ Celluloid::Actor(EnvironmentManager)
7 8 9 |
# File 'lib/mb/environment_manager.rb', line 7 def instance MB::Application[:environment_manager] or raise Celluloid::DeadActorError, "environment manager not running" end |
Instance Method Details
#async_configure(id, options = {}) ⇒ JobTicket
attributes will be set at the ‘default’ level and will be merged into the existing attributes of the environment
Asynchronously configure a target environment with the given attributes
37 38 39 40 41 42 |
# File 'lib/mb/environment_manager.rb', line 37 def async_configure(id, = {}) job = Job.new(:environment_configure) async(:configure, job, id, ) job.ticket end |
#async_examine_nodes(id, options = {}) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/mb/environment_manager.rb', line 109 def async_examine_nodes(id, = {}) job = Job.new(:examine_nodes) async(:examine_nodes, job, id, ) job.ticket end |
#configure(job, id, options = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Configure a target environment with the given attributes
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/mb/environment_manager.rb', line 58 def configure(job, id, = {}) = .reverse_merge( attributes: Hash.new, force: false ) node_successes_count = 0 node_successes = Array.new node_failures_count = 0 node_failures = Array.new environment = find(id) job.report_running("Finding environment #{environment.name}") chef_synchronize(chef_environment: environment.name, force: [:force], job: job) do job.set_status("Saving updated environment with - #{[:attributes]}") environment.default_attributes.deep_merge!([:attributes]) environment.save job.set_status("Searching for nodes in the environment") nodes = nodes_for_environment(environment.name) nodes = MB::NodeFilter.filter([:node_filter], nodes) if [:node_filter] job.set_status("Performing a chef client run on #{nodes.length} nodes") nodes.collect do |node| node_querier.future(:chef_run, node.public_hostname, connector: node_querier.connector_for_os(node.chef_attributes.os)) end.each do |future| begin response = future.value node_successes_count += 1 node_successes << response.host rescue RemoteCommandError => error node_failures_count += 1 node_failures << error.host end end end if node_failures_count > 0 job.report_failure("chef client run failed on #{node_failures_count} node(s) - #{node_failures.join(', ')}") else job.report_success("Finished chef client run on #{node_successes_count} node(s) - #{node_successes.join(', ')}") end rescue => ex job.report_failure(ex) ensure job.terminate if job && job.alive? end |
#create(name) ⇒ Ridley::EnvironmentResource
Creates an environment
160 161 162 163 164 165 166 |
# File 'lib/mb/environment_manager.rb', line 160 def create(name) ridley.environment.create(name: name) rescue Ridley::Errors::HTTPConflict abort EnvironmentExists.new(name) rescue => error abort error end |
#create_from_file(path) ⇒ Ridley::EnvironmentResource
Creates an environment from JSON contained in a file
173 174 175 176 177 178 179 180 |
# File 'lib/mb/environment_manager.rb', line 173 def create_from_file(path) abort FileNotFound.new(path) unless File.exist? path env = ridley.environment.from_file(path) ridley.environment.create(env) rescue Ridley::Errors::HTTPConflict abort EnvironmentExists.new(env.name) end |
#destroy(name) ⇒ Ridley::EnvironmentResource?
Destroys an environment
187 188 189 |
# File 'lib/mb/environment_manager.rb', line 187 def destroy(name) ridley.environment.delete(name) end |
#examine_nodes(job, id, options = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/mb/environment_manager.rb', line 116 def examine_nodes(job, id, = {}) environment = find(id) job.report_running("Finding environment #{environment.name}") nodes = nodes_for_environment(environment.name) job.set_status("Examining #{nodes.length} nodes") nodes.collect do |node| log.debug "About to execute on: \"#{node.public_hostname}\"" node_querier.future(:execute_command, node.public_hostname, "echo %time%") end.each do |future| begin response = future.value rescue RemoteCommandError => error log.warn "Examine command failed on: \"#{error.host}\"" log.warn " " + error. end end job.report_success("Completed on #{nodes.length} nodes.") ensure job.terminate if job && job.alive? end |
#find(id) ⇒ Ridley::EnvironmentResource
Find an environment on the remote Chef server
147 148 149 150 151 152 153 |
# File 'lib/mb/environment_manager.rb', line 147 def find(id) unless environment = ridley.environment.find(id) abort EnvironmentNotFound.new(id) end environment end |
#list ⇒ Array<Ridley::EnvironmentResource>
Returns a list of environments present on the remote server
194 195 196 |
# File 'lib/mb/environment_manager.rb', line 194 def list ridley.environment.all end |
#nodes_for_environment(name) ⇒ Array(Ridley::NodeObject)
Returns an array of nodes for an environment
218 219 220 |
# File 'lib/mb/environment_manager.rb', line 218 def nodes_for_environment(name) ridley.partial_search(:node, "chef_environment:#{name}", ["fqdn", "cloud.public_hostname", "name", "os"]) end |
#purge_nodes(name) ⇒ Object
Removes all nodes and clients from the Chef server for a given environment
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/mb/environment_manager.rb', line 201 def purge_nodes(name) nodes = nodes_for_environment(name) futures = [] nodes.each do |node| futures << ridley.client.future(:delete, node) futures << ridley.node.future(:delete, node) end futures.map(&:value) end |