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.
-
#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.
-
#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 |
#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
57 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 |
# File 'lib/mb/environment_manager.rb', line 57 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) job.set_status("Performing a chef client run on #{nodes.length} nodes") nodes.collect do |node| node_querier.future(:chef_run, node.public_hostname) 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
127 128 129 130 131 132 133 |
# File 'lib/mb/environment_manager.rb', line 127 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
140 141 142 143 144 145 146 147 |
# File 'lib/mb/environment_manager.rb', line 140 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
154 155 156 |
# File 'lib/mb/environment_manager.rb', line 154 def destroy(name) ridley.environment.delete(name) end |
#find(id) ⇒ Ridley::EnvironmentResource
Find an environment on the remote Chef server
114 115 116 117 118 119 120 |
# File 'lib/mb/environment_manager.rb', line 114 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
161 162 163 |
# File 'lib/mb/environment_manager.rb', line 161 def list ridley.environment.all end |
#nodes_for_environment(name) ⇒ Array(Ridley::NodeObject)
Returns an array of nodes for an environment
185 186 187 |
# File 'lib/mb/environment_manager.rb', line 185 def nodes_for_environment(name) ridley.search(:node, "chef_environment:#{name}") end |
#purge_nodes(name) ⇒ Object
Removes all nodes and clients from the Chef server for a given environment
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/mb/environment_manager.rb', line 168 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 |