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

#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



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, 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)

    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

Parameters:

  • name (#to_s)

Returns:

  • (Ridley::EnvironmentResource)


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

Parameters:

  • path (#to_s)

Returns:

  • (Ridley::EnvironmentResource)


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

Parameters:

  • name (#to_s)

Returns:

  • (Ridley::EnvironmentResource, nil)


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

Parameters:

  • id (#to_s)

    identifier for the environment to find

Returns:

  • (Ridley::EnvironmentResource)

Raises:



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

#listArray<Ridley::EnvironmentResource>

Returns a list of environments present on the remote server

Returns:

  • (Array<Ridley::EnvironmentResource>)


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

Parameters:

  • name (String)

Returns:

  • (Array(Ridley::NodeObject))


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

Parameters:

  • name (String)


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