module ChefRunDeck::State

> This is the State controller. It manages State information

Attributes

state[RW]

> State Operations <= #

Public Instance Methods

add_state(node, user, params) click to toggle source

> Add Node to the State

# File lib/chef-rundeck/state.rb, line 52
def add_state(node, user, params)
  # => Create a Node-State Object
  (n = {}) && (n[:name] = node)
  n[:created] = DateTime.now
  n[:creator] = user
  n[:type] = params['type'] if params['type']
  # => Build the Updated State
  update_state(n)
  # => Return the Added Node
  find_state(node)
end
delete_state(node) click to toggle source

> Remove Node from the State

# File lib/chef-rundeck/state.rb, line 65
def delete_state(node)
  # => Find the Node
  existing = find_state(node)
  return 'Node not present in state' unless existing
  # => Delete the Node from State
  state.delete(existing)
  # => Write Out the Updated State
  write_state
  # => Return the Deleted Node
  existing
end
find_state(node) click to toggle source
# File lib/chef-rundeck/state.rb, line 30
def find_state(node)
  state.detect { |h| h[:name].casecmp(node).zero? }
end
update_state(hash) click to toggle source
# File lib/chef-rundeck/state.rb, line 34
def update_state(hash) # rubocop: disable AbcSize
  # => Check if Node Already Exists
  existing = find_state(hash[:name])
  if existing # => Update the Existing Node
    state.delete(existing)
    audit_string = [DateTime.now, hash[:creator]].join(' - ')
    existing[:last_modified] = existing[:last_modified].is_a?(Array) ? existing[:last_modified].take(5).unshift(audit_string) : [audit_string]
    hash = existing
  end

  # => Update the State
  state.push(hash)

  # => Write Out the Updated State
  write_state
end
write_state() click to toggle source
# File lib/chef-rundeck/state.rb, line 77
def write_state
  # => Sort & Unique State
  state.sort_by! { |h| h[:name].downcase }.uniq!

  # => Write Out the Updated State
  Util.write_json_config(Config.state_file, state)
end