Class: MotherBrain::EnvironmentManager

Inherits:
Object
  • Object
show all
Includes:
Celluloid, MB::Mixin::Locks, MB::Mixin::Services, Logging
Defined in:
lib/mb/environment_manager.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

add_argument_header, dev, filename, #log_exception, logger, #logger, reset, set_logger, setup

Constructor Details

#initializeEnvironmentManager

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

.instanceCelluloid::Actor(EnvironmentManager)

Returns:

Raises:

  • (Celluloid::DeadActorError)

    if the environment manager has not been started



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

Note:

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

Parameters:

  • id (String)

    identifier of the environment to configure

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :attributes (Hash) — default: Hash.new

    a hash of attributes to merge with the existing attributes of an environment

  • :force (Boolean) — default: false

    force configure even if the environment is locked

Returns:



37
38
39
40
41
42
# File 'lib/mb/environment_manager.rb', line 37

def async_configure(id, options = {})
  job = Job.new(:environment_configure)
  async(:configure, job, id, options)

  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, options = {})
  job = Job.new(:examine_nodes)
  async(:examine_nodes, job, id, options)

  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

Parameters:

  • job (MB::Job)

    a job to update with progress

  • id (String)

    identifier of the environment to configure

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :attributes (Hash) — default: Hash.new

    a hash of attributes to merge with the existing attributes of an environment

  • :force (Boolean) — default: false

    force configure even if the environment is locked

  • :node_filter (String)

    limit chef-run to certain nodes - NOT RECOMMENDED



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, options = {})
  options = options.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: options[:force], job: job) do
    job.set_status("Saving updated environment with - #{options[:attributes]}")
    environment.default_attributes.deep_merge!(options[:attributes])
    environment.save

    job.set_status("Searching for nodes in the environment")
    nodes = nodes_for_environment(environment.name)
    nodes = MB::NodeFilter.filter(options[:node_filter], nodes) if options[: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

Parameters:

  • name (#to_s)

Returns:

  • (Ridley::EnvironmentResource)


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

Parameters:

  • path (#to_s)

Returns:

  • (Ridley::EnvironmentResource)


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

Parameters:

  • name (#to_s)

Returns:

  • (Ridley::EnvironmentResource, nil)


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, options = {})
  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.message
    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

Parameters:

  • id (#to_s)

    identifier for the environment to find

Returns:

  • (Ridley::EnvironmentResource)

Raises:



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

#listArray<Ridley::EnvironmentResource>

Returns a list of environments present on the remote server

Returns:

  • (Array<Ridley::EnvironmentResource>)


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

Parameters:

  • name (String)

Returns:

  • (Array(Ridley::NodeObject))


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

Parameters:

  • name (String)


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