Skip to content
Dan Ryan edited this page Sep 13, 2012 · 5 revisions

A participant is responsible for performing tasks as specified by the definition. There are many types of providers. A participant can provision a server, send notifications, or even execute remote commands.

Attributes

Participant attributes vary depending on their purpose.

Example participant

require 'net/ssh'

module Participant::Remote
  class SSH < Participant
    register :ssh
    
    action :run do
      requires :host, :user, :key_data, :command
      
      resource.output = run_ssh(resource.command)
      return {}
    end

    def run_ssh(command)
      session = Net::SSH.start(resource.host, resource.user, { key_data: resource.key_data })
      output = nil

      session.open_channel do |ch|
        ch.request_pty
        ch.exec command do |ch, success|
          raise ArgumentError, "Cannot execute #{resource.command}" unless success
          ch.on_data do |ichannel, data|
            output = data
          end
        end
      end

      session.loop
      session.close
      return output if output
    end
  end
end

Usage in a definition

When a participant is registered in Mastermind with the register :name class method, the :name is converted to a regular expression:

/^(.+)_#{name}[s]?$/

providers and the requested action are looked up by this regex. Mastermind splits this reference and turns the captured string from the participant reference into the action that the participant will perform. In the case of create_ec2_server, Mastermind turns this participant reference and determines the participant to be ec2_server and the action to be create.

Sometimes a participant reference reads better when pluralized, so we'll always look for an optional s at the end of the reference and direct it to the singular participant name. Such is the case with list_chef_nodes, where the participant is actually called chef_node but list_chef_node would be misleading as it in fact returns an array of Chef nodes.

Clone this wiki locally