commit 849a61fbc6316122e796c45804ce367dfe50f3dd
Author: Marc Dequènes (Duck) <Duck@DuckCorp.org>
Date:   Wed Mar 2 00:54:21 2011 +0100

    [evol] Librarian API work §1, using DI too

diff --git a/bin/librarian b/bin/librarian
index b057150..b3740e8 100755
--- a/bin/librarian
+++ b/bin/librarian
@@ -21,11 +21,12 @@
 # to allow in-place run for test
 $: << File.join(File.dirname(__FILE__), "..", "lib")
 
+require 'needle'
 require 'cyborghood/cyborg'
 
 
 module CyborgHood
-  module LibrarianHome
+  module LibrarianLand
     include I18nTranslation
     bindtextdomain("cyborghood_librarian", {:path => Config::L10N_DIR, :charset => "UTF-8"})
 
@@ -38,23 +39,36 @@ module CyborgHood
       def setup
         super
 
-        define_interface "0.1~" do
-          node "Gruik" do
-            node ["g1", "g2", "g3"] do
-              on_request do |request|
-                request.reply.results = {
-                  :plouf => ">>> #{node_name} <<<"
-                }
-              end
-            end
-          end
-        end
+        define_interface "0.1~"
       end
     end
-  end
+  end # LibrarianLand
 end
 
-bot = CyborgHood::LibrarianHome::Librarian.new
+
+reg = Needle::Registry.new
+reg.define do |b|
+  b.require 'cyborghood', CyborgHood
+  b.require 'cyborghood-librarian/land', CyborgHood::LibrarianLand
+
+  b.bot { CyborgHood::LibrarianLand::Librarian.new(b.librarian_land) }
+end
+
+ldap_config = reg.config.ldap.marshal_dump
+ldap_config[:logger] = reg.logger
+
+begin
+  ActiveLdap::Base.setup_connection(ldap_config)
+  # force testing a connection NOW (by default ActiveLdap is doing lazy connections)
+  # (the loaded schema will be useful soon anyway)
+  # it also tests search parameters (provided to setup_connection)
+  ActiveLdap::Base.find(:first)
+rescue
+  reg.logger.fatal "LDAP failure: " + $!
+  exit 1
+end
+
+bot = reg.bot
 
 trap('INT') do
   bot.ask_to_stop
diff --git a/bin/postman b/bin/postman
index c49c72a..82818ca 100755
--- a/bin/postman
+++ b/bin/postman
@@ -68,20 +68,6 @@ module CyborgHood
           end
         end
 
-        # setup LDAP
-        ldap_config = @config.ldap.marshal_dump
-        ldap_config[:logger] = logger
-        begin
-          ActiveLdap::Base.setup_connection(ldap_config)
-          # force testing a connection NOW (by default ActiveLdap is doing lazy connections)
-          # (the loaded schema will be useful soon anyway)
-          # it also tests search parameters (provided to setup_connection)
-          ActiveLdap::Base.find(:first)
-        rescue
-          logger.fatal "LDAP failure: " + $!
-          exit 1
-        end
-
         @imap = IMAP.new(@config.imap, @config.imap.min_check_interval)
 
         logger.info "Bot '#{self.human_name}' loaded"
diff --git a/bin/test_client b/bin/test_client
index 9a468b8..08e435c 100755
--- a/bin/test_client
+++ b/bin/test_client
@@ -39,12 +39,15 @@ module CyborgHood
         task "compare stuff" do
           ask "MapMaker", :info1, "/_cyborg_"
           ask "Librarian", :info2, "/_cyborg_"
-          ask "MapMaker", :zones, "/Zones"
+          ask "Librarian", :library, "/Records"
+          ask "Librarian", :persons, "/Records/persons"
+          ask "Librarian", :dns_domains, "/Records/dns_domains"
+          #ask "MapMaker", :zones, "/Zones"
           #ask "MapMaker", :wanted_failure, "/prout"
-          ask "MapMaker", :dns, "/Services/DNS"
-          ask "MapMaker", :dnssec, "/Services/DNSSEC"
-          ask "MapMaker", :zone_mp, "/Zones/milkypond.org"
-          ask "MapMaker", :dns_check, "/Services/DNS/check_config"
+          #ask "MapMaker", :dns, "/Services/DNS"
+          #ask "MapMaker", :dnssec, "/Services/DNSSEC"
+          #ask "MapMaker", :zone_mp, "/Zones/milkypond.org"
+          #ask "MapMaker", :dns_check, "/Services/DNS/check_config"
           #ask "MapMaker", :search, "/Zones/?"
           #ask "MapMaker", :search_master, "/Zones/?", {:master => true}
           #ask "MapMaker", :search_slave, "/Zones/?", {:master => false}
diff --git a/data/cyborghood/default_config/global.yaml b/data/cyborghood/default_config/global.yaml
index 1436c79..cf680de 100755
--- a/data/cyborghood/default_config/global.yaml
+++ b/data/cyborghood/default_config/global.yaml
@@ -8,7 +8,3 @@ i18n: {}
 botnet:
   connection_type: unix_socket
   params: {}
-ldap:
-  host: localhost
-  port: 389
-  scope: sub
diff --git a/data/cyborghood/default_config/librarian.yaml b/data/cyborghood/default_config/librarian.yaml
index cd21505..4f48cc7 100644
--- a/data/cyborghood/default_config/librarian.yaml
+++ b/data/cyborghood/default_config/librarian.yaml
@@ -1,2 +1,5 @@
 ---
-
+ldap:
+  host: localhost
+  port: 389
+  scope: sub
diff --git a/data/cyborghood/schema/global.yaml b/data/cyborghood/schema/global.yaml
index cdb0559..47b61a4 100644
--- a/data/cyborghood/schema/global.yaml
+++ b/data/cyborghood/schema/global.yaml
@@ -35,12 +35,3 @@ mapping:
         mapping:
           =:
             type: any
-  # temporary location
-  "ldap":
-    type: map
-    required: true
-    name: LdapMapping
-    mapping:
-      =:
-        type: any
-
diff --git a/data/cyborghood/schema/librarian.yaml b/data/cyborghood/schema/librarian.yaml
index 6c97556..d69a14e 100644
--- a/data/cyborghood/schema/librarian.yaml
+++ b/data/cyborghood/schema/librarian.yaml
@@ -1,4 +1,10 @@
 ---
 type: map
 mapping:
-  zzz: { type: str }
+  "ldap":
+    type: map
+    required: true
+    name: LdapMapping
+    mapping:
+      =:
+        type: any
diff --git a/lib/cyborghood-librarian/interface/0_base.rb b/lib/cyborghood-librarian/interface/0_base.rb
new file mode 100644
index 0000000..c45f8c4
--- /dev/null
+++ b/lib/cyborghood-librarian/interface/0_base.rb
@@ -0,0 +1,7 @@
+node 'Records' do
+  node ['persons', 'dns_domains'] do
+    on_request do |request|
+      request.reply.results = bot.services.records.send(node_name)
+    end
+  end
+end
diff --git a/lib/cyborghood-librarian/interface_dsl_addons.rb b/lib/cyborghood-librarian/interface_dsl_addons.rb
new file mode 100644
index 0000000..b5b09c8
--- /dev/null
+++ b/lib/cyborghood-librarian/interface_dsl_addons.rb
@@ -0,0 +1,2 @@
+
+-> ldap search /?
diff --git a/lib/cyborghood-librarian/land.rb b/lib/cyborghood-librarian/land.rb
new file mode 100644
index 0000000..0869ba9
--- /dev/null
+++ b/lib/cyborghood-librarian/land.rb
@@ -0,0 +1,35 @@
+#--
+# CyborgHood, a distributed system management software.
+# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#++
+
+require 'active_ldap'
+
+
+module CyborgHood
+  module LibrarianLand
+    def register_services(container)
+      container.namespace_define(:librarian_land) do |b|
+        b.records do
+          require 'cyborghood-librarian/records'
+          Records.new
+        end
+      end
+    end
+
+    module_function :register_services
+  end # LibrarianLand
+end
diff --git a/lib/cyborghood-librarian/records.rb b/lib/cyborghood-librarian/records.rb
new file mode 100644
index 0000000..3c2c979
--- /dev/null
+++ b/lib/cyborghood-librarian/records.rb
@@ -0,0 +1,37 @@
+#--
+# CyborgHood, a distributed system management software.
+# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#++
+
+Dir.glob(File.join(File.dirname(__FILE__), "records", "*.rb")).each do |f|
+  logger.debug "Loading record definition '#{f}'"
+  require f
+end
+
+
+module CyborgHood
+  module LibrarianLand
+    class Records
+      def persons
+        LdapPerson.find(:all, :attributes => ['uid']).collect{|i| i.uid }
+      end
+
+      def dns_domains
+        LdapDnsDomain.find(:all, :attributes => ['cn']).collect{|i| i.cn }
+      end
+    end
+  end # LibrarianLand
+end
diff --git a/lib/cyborghood-librarian/records/dns_domain.rb b/lib/cyborghood-librarian/records/dns_domain.rb
new file mode 100644
index 0000000..d3d55e0
--- /dev/null
+++ b/lib/cyborghood-librarian/records/dns_domain.rb
@@ -0,0 +1,33 @@
+#--
+# CyborgHood, a distributed system management software.
+# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#++
+
+
+module CyborgHood
+  module LibrarianLand
+    class LdapDnsDomain < ActiveLdap::Base
+      ldap_mapping :dn_attribute => 'cn', :prefix => '', :classes => ['genericDomain']
+
+      def managers
+        list = self.manager
+        return [] if list.nil?
+        return list.collect{|dn| dn.to_s } if list.is_a? Array
+        return [list.to_s]
+      end
+    end
+  end # LibrarianLand
+end
diff --git a/lib/cyborghood-librarian/records/person.rb b/lib/cyborghood-librarian/records/person.rb
new file mode 100644
index 0000000..293712c
--- /dev/null
+++ b/lib/cyborghood-librarian/records/person.rb
@@ -0,0 +1,26 @@
+#--
+# CyborgHood, a distributed system management software.
+# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#++
+
+
+module CyborgHood
+  module LibrarianLand
+    class LdapPerson < ActiveLdap::Base
+      ldap_mapping :dn_attribute => 'uid', :prefix => '', :classes => ['individual']
+    end
+  end # LibrarianLand
+end
diff --git a/lib/cyborghood.rb b/lib/cyborghood.rb
index e728600..17259c5 100644
--- a/lib/cyborghood.rb
+++ b/lib/cyborghood.rb
@@ -38,6 +38,10 @@ module CyborgHood
       b.config do
         Config.instance
       end
+
+      b.logger do
+        Logger.instance
+      end
     end
   end
 
diff --git a/lib/cyborghood/objects/ldap.rb b/lib/cyborghood/objects/ldap.rb
deleted file mode 100644
index 161032c..0000000
--- a/lib/cyborghood/objects/ldap.rb
+++ /dev/null
@@ -1,36 +0,0 @@
-#--
-# CyborgHood, a distributed system management software.
-# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-#++
-
-require 'active_ldap'
-
-module CyborgHood
-  class LdapDnsDomain < ActiveLdap::Base
-    ldap_mapping :dn_attribute => 'cn', :prefix => '', :classes => ['genericDomain']
-
-    def managers
-      list = self.manager
-      return [] if list.nil?
-      return list.collect{|dn| dn.to_s } if list.is_a? Array
-      return [list.to_s]
-    end
-  end
-
-  class LdapPerson < ActiveLdap::Base
-    ldap_mapping :dn_attribute => 'uid', :prefix => '', :classes => ['individual']
-  end
-end
