Class: AdGear::Infrastructure::GroupManager::App

Inherits:
Thor
  • Object
show all
Includes:
Config, LDAP, Logging, Utils, Version
Defined in:
lib/app.rb

Overview

App The top of stack abstraction for this application. Read through the code to check the sequence of events.

Since:

  • 0.1.0

Constant Summary

Constants included from Version

Version::GEM_VERSION

Constants included from Logging

Logging::Log

Constants included from Config

Config::GLOBAL_CONFIG

Constants included from LDAP

LDAP::Binder

Instance Method Summary collapse

Methods included from Utils

compare_attributes, create_ops_list, diff_op_exist?, duplicate?, find_ou, sort_member, stringify_all_keys, symbolify_all_keys

Methods included from Logging

fatal

Methods included from Config

list_all_groups, list_func_groups, list_locations, list_org_groups, list_perm_groups, list_users

Methods included from LDAP

delete_item, extract_cn, get_item, list_all_groups, list_func_groups, list_groups, list_locations, list_org_groups, list_organizational_units, list_perm_groups, set_item, user_exists?

Instance Method Details

#applyObject

Applies remote changes

Since:

  • 0.1.0



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/app.rb', line 82

def apply
  ops_to_perform = diff
  Log.info("Creating #{ops_to_perform[:create].length} new entities")
  ops_to_perform[:create].each do |cn|
    LDAP.set_item(
      :create,
      ["cn=#{cn}", Utils.find_ou(cn), GLOBAL_CONFIG[:treebase]].join(', ')
    )
  end
  if ops_to_perform[:create].any? && ops_to_perform[:modify].any?
    sleep_time = GLOBAL_CONFIG[:settle_sleep]
    sleep sleep_time
    Log.info("Waiting #{sleep_time} seconds for ldap to propagate changes after object creation")
  end

  Log.info("Applying #{ops_to_perform[:modify].length} modifications to existing items")
  ops_to_perform[:modify].each do |i|
    LDAP.set_item(
      :modify, ["cn=#{i[:cn]}",
                Utils.find_ou(i[:cn]),
                GLOBAL_CONFIG[:treebase]].join(', '), i[:attrib], i[:value]
              )
  end

  if ops_to_perform[:delete]
    Log.info("Removing #{ops_to_perform[:delete].length} deprecated items")

    items_to_delete = ops_to_perform[:delete].map do |i|
      treebase = GLOBAL_CONFIG[:treebase]

      filter = Net::LDAP::Filter.construct("CN=#{i}*")

      target = nil
      Binder.search(base: treebase, filter: filter).each do |entry|
        next unless /^CN=#{i},OU=/ =~ entry.dn
        target = entry.dn
      end
      target
    end

    items_to_delete.each do |i|
      LDAP.delete_item(i)
      Log.debug(Binder.get_operation_result)
    end
  end

  Log.info('done')

  exit(0)
end

#diffObject

Displays the difference between local and remote.

Since:

  • 0.1.0



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/app.rb', line 49

def diff
  # get all local groups
  Log.info('Compiling all local groups')
  local_groups = Config.list_all_groups
  local_groups.each { |i| local_groups[i[0]] = Utils.symbolify_all_keys(i[1]) }
  local_groups = Utils.sort_member(local_groups)
  Log.debug(msg: 'local groups', local_groups: local_groups)

  Log.info('Compiling local users')
  users = Config.list_users
  Log.debug(msg: 'users', users: users)

  # get all local users and check if they exist remotely
  if options[:verify_users]
    Log.info("Verifying #{users.length} local users against remote")
    users.each { |dn| LDAP.user_exists?(dn) ? Log.debug("#{dn} exists") : raise("#{dn} does not exist") }
  end

  # get all remote groups
  Log.info('Compiling remote groups')
  remote_groups = LDAP.list_all_groups
  remote_groups = Utils.symbolify_all_keys(remote_groups)
  remote_groups = Utils.sort_member(remote_groups)
  Log.debug(msg: 'remote groups', remote_groups: remote_groups)

  ops_to_perform = Utils.create_ops_list(local_groups, remote_groups)
  Log.info(msg: 'Operations to perform', operations: ops_to_perform)
  ops_to_perform
end

Since:

  • 0.1.0



40
41
42
# File 'lib/app.rb', line 40

def print_version
  puts GEM_VERSION
end