Class: Moose::Inventory::Operations::GroupCleanup

Inherits:
Object
  • Object
show all
Includes:
OperationEventSupport
Defined in:
lib/moose_inventory/operations/group_cleanup.rb

Overview

Recursively cleans up orphaned groups and their dependent relations.

Constant Summary collapse

AUTOMATIC_GROUP =
'ungrouped'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, emitter:) ⇒ GroupCleanup

Returns a new instance of GroupCleanup.



16
17
18
19
20
# File 'lib/moose_inventory/operations/group_cleanup.rb', line 16

def initialize(context:, emitter:)
  @context = context
  @emitter = emitter
  @dry_run = false
end

Instance Attribute Details

#dry_runObject

Returns the value of attribute dry_run.



14
15
16
# File 'lib/moose_inventory/operations/group_cleanup.rb', line 14

def dry_run
  @dry_run
end

Instance Method Details

#delete_orphaned_group(group, events, ignored_parent: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/moose_inventory/operations/group_cleanup.rb', line 22

def delete_orphaned_group(group, events, ignored_parent: nil)
  return if group.name == AUTOMATIC_GROUP
  return unless orphaned_after_planned_removal?(group, ignored_parent)

  emit(events, :recursively_delete_orphaned_group, name: group.name)
  group.children_dataset.each do |child|
    emit(events, :removing_recursive_child_association, parent: group.name, child: child.name)
    group.remove_child(child) unless dry_run
    emit(events, :ok, indent: 6)
    delete_orphaned_group(child, events, ignored_parent: group)
  end
  destroy_group(group, events, indent: 4)
end

#destroy_group(group, events, indent:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/moose_inventory/operations/group_cleanup.rb', line 36

def destroy_group(group, events, indent:)
  group.hosts_dataset.each do |host|
    next unless host.groups_dataset.one?

    emit(events, :adding_automatic_group_to_host, host: host[:name], indent: indent)
    host.add_group(context.automatic_group) unless dry_run
    emit(events, :ok, indent: indent + 2)
  end

  emit(events, :destroying_group, name: group.name, indent: indent)
  unless dry_run
    group.remove_all_groupvars
    group.remove_all_hosts
    group.destroy
  end
  emit(events, :ok, indent: indent + 2)
end