Class: MCollective::Util::BoltSupport

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/util/bolt_support.rb,
lib/mcollective/util/bolt_support/plan_runner.rb,
lib/mcollective/util/bolt_support/task_result.rb,
lib/mcollective/util/bolt_support/task_results.rb

Defined Under Namespace

Classes: PlanRunner, TaskResult, TaskResults

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.init_choriaBoltSupport

Configures MCollective and initialize the Bolt Support class

Returns:



35
36
37
38
39
# File 'lib/mcollective/util/bolt_support.rb', line 35

def self.init_choria
  Config.instance.loadconfig(Util.config_file_for_user) unless Config.instance.configured

  new
end

.loglevel"debug", ...

Converts the current Puppet loglevel to one we understand

Returns:

  • ("debug", "info", "warn", "error", "fatal")


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mcollective/util/bolt_support.rb', line 19

def self.loglevel
  case Puppet::Util::Log.level
  when :notice, :warning
    "warn"
  when :err
    "error"
  when :alert, :emerg, :crit
    "fatal"
  else
    Puppet::Util::Log.level.to_s
  end
end

Instance Method Details

#assign_playbook_name(scope) ⇒ Object

Assigns the playbook name based on the fact choria.plan



167
168
169
170
171
172
173
# File 'lib/mcollective/util/bolt_support.rb', line 167

def assign_playbook_name(scope)
  return unless scope
  return unless scope["facts"]["choria"]
  return unless scope["facts"]["choria"]["playbook"]

  playbook.["name"] = scope["facts"]["choria"]["playbook"]
end

#choriaObject



12
13
14
# File 'lib/mcollective/util/bolt_support.rb', line 12

def choria
  @_choria ||= Choria.new
end

#data_lock(scope, item, properties, &blk) ⇒ Object

Performs a block within a lock in a data store

Parameters:

  • scope (Puppet::Parser::Scope)

    scope to log against

  • item (String)

    the lock key

  • properties (Hash)

    the data source properties



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mcollective/util/bolt_support.rb', line 112

def data_lock(scope, item, properties, &blk)
  locked = false
  lock_path = "plan_store/%s" % item
  config = {"plan_store" => properties}

  playbook.logger.scope = scope
  assign_playbook_name(scope)
  playbook.data_stores.from_hash(config)
  playbook.data_stores.prepare

  playbook.data_stores.lock(lock_path)
  locked = true

  yield
ensure
  playbook.data_stores.release(lock_path) if locked
end

#data_read(scope, item, properties) ⇒ Object

Retrieves a data item from a data store

Parameters:

  • scope (Puppet::Parser::Scope)

    scope to log against

  • item (String)

    the item to fetch

  • properties (Hash)

    the data source properties



82
83
84
85
86
87
88
# File 'lib/mcollective/util/bolt_support.rb', line 82

def data_read(scope, item, properties)
  playbook.logger.scope = scope
  assign_playbook_name(scope)
  playbook.data_stores.from_hash("plan_store" => properties)
  playbook.data_stores.prepare
  playbook.data_stores.read("plan_store/%s" % item)
end

#data_write(scope, item, value, properties) ⇒ String

Writes a value to a data store

Parameters:

  • scope (Puppet::Parser::Scope)

    scope to log against

  • item (String)

    the item to fetch

  • value (String)

    the item to fetch

  • properties (Hash)

    the data source properties

Returns:

  • (String)

    the data that was written



97
98
99
100
101
102
103
104
105
# File 'lib/mcollective/util/bolt_support.rb', line 97

def data_write(scope, item, value, properties)
  config = {"plan_store" => properties}

  playbook.logger.scope = scope
  assign_playbook_name(scope)
  playbook.data_stores.from_hash(config)
  playbook.data_stores.prepare
  playbook.data_stores.write("plan_store/%s" % item, value)
end

#discover_nodes(scope, type, properties) ⇒ Object

Discovers nodes using playbook node sets

Parameters:

  • scope (Puppet::Parser::Scope)

    scope to log against

  • type (String)

    a known node set type like ‘terraform`

  • properties (Hash)

    properties valid for the node set type



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mcollective/util/bolt_support.rb', line 62

def discover_nodes(scope, type, properties)
  uses_properties = properties.delete("uses") || {}
  playbook.logger.scope = scope
  assign_playbook_name(scope)
  playbook.uses.from_hash(uses_properties)

  nodes.from_hash("task_nodes" => properties.merge(
    "type" => type,
    "uses" => uses_properties.keys
  ))

  nodes.prepare
  nodes["task_nodes"]
end

#nodesObject



53
54
55
# File 'lib/mcollective/util/bolt_support.rb', line 53

def nodes
  @_nodes ||= Playbook::Nodes.new(playbook)
end

#playbookPlaybook

Creates a configured instance of the Playbook

Returns:



44
45
46
47
48
49
50
51
# File 'lib/mcollective/util/bolt_support.rb', line 44

def playbook
  @_playbook ||= begin
    pb = Playbook.new(self.class.loglevel)
    pb.logger = Playbook::Puppet_Logger
    pb.set_logger_level
    pb
  end
end

#run_task(scope, type, properties) ⇒ BoltSupport::TaskResults

Runs a playbook task and return execution results

Parameters:

  • scope (Puppet::Parser::Scope)

    scope to log against

  • type (String)

    the task type

  • properties (Hash)

    properties passed to the task

Returns:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mcollective/util/bolt_support.rb', line 136

def run_task(scope, type, properties)
  task_properties = properties.reject {|k, _| k.start_with?("_") }
  playbook.logger.scope = scope
  assign_playbook_name(scope)

  tasks = playbook.tasks.load_tasks([type => task_properties], "tasks")

  playbook.tasks.run_task(tasks[0], "plan", false)

  result = tasks[0][:result]
  runner = tasks[0][:runner]

  execution_result = runner.to_execution_result([result.success, result.msg, result.data])

  results = execution_result.map do |node, result_properties|
    TaskResult.new(node, JSON.parse(result_properties.to_json))
  end

  task_results = TaskResults.new(results, nil)
  task_results.message = result.msg

  return task_results if result.success
  return task_results if properties.fetch("fail_ok", false)
  return task_results if properties["_catch_errors"]

  raise(result.msg)
end