Class: Server

Inherits:
Object
  • Object
show all
Includes:
Daemon
Defined in:
lib/arldap/server.rb

Constant Summary collapse

@@def_config_file_name =
File.expand_path(File.join(File.dirname(__FILE__), "..", "conf", "ldap-server.yml"))

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Daemon

#daemonize

Constructor Details

#initialize(config_file_name = nil) ⇒ Server

Returns a new instance of Server.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/arldap/server.rb', line 24

def initialize(config_file_name = nil)
  config_file_name ||= @@def_config_file_name

  @config = YAML.load(ERB.new(File.read(config_file_name)).result)

  self.logger = Logger.new(@config["log_file"] || File.join(@config["rails_dir"], *%w(log ldap-server.log)))
  self.logger.level = @config["debug"] ? Logger::DEBUG : Logger::INFO
  self.logger.datetime_format = "%H:%M:%S"
  self.logger.info ""
  @config["pid_file"] ||= @config["pid_file"] || File.join(@config["rails_dir"], *%w(log ldap-server.pid))
  @pidfile = PidFile.new(@config["pid_file"])
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



19
20
21
# File 'lib/arldap/server.rb', line 19

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



19
20
21
# File 'lib/arldap/server.rb', line 19

def logger
  @logger
end

#pidfileObject

Returns the value of attribute pidfile.



19
20
21
# File 'lib/arldap/server.rb', line 19

def pidfile
  @pidfile
end

Instance Method Details

#become_user(username = 'nobody', chroot = false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/arldap/server.rb', line 37

def become_user(username = 'nobody', chroot = false)
  user = Etc::getpwnam(username)

  Dir.chroot(user.dir) and Dir.chdir('/') if chroot

  Process::initgroups(username, user.gid)
  Process::Sys::setegid(user.gid)
  Process::Sys::setgid(user.gid)
  Process::Sys::setuid(user.uid)
end

#restartObject



116
117
118
119
120
# File 'lib/arldap/server.rb', line 116

def restart
  stop
  sleep 5
  start
end

#startObject



48
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/arldap/server.rb', line 48

def start
  pidfile.ensure_empty! "ERROR: It looks like I'm already running #{@config['pid_file']}.  Not starting."

  logger.info "Starting LDAP server"
  daemonize(logger)

  self.logger.info("Cannot load Rails. Exiting.") and exit 5 unless defined? RAILS_ROOT
  @config.symbolize_keys!

  logger.info "Became daemon with process id: #{$$}"
  begin
    pidfile.create
  rescue Exception => e
    logger.info "Exception caught while creating pidfile: #{e}"
    exit
  end

  trap("TERM") do
    logger.info("Received TERM signal.  Exiting.") if logger
    pidfile.remove if pidfile
    exit
  end

  begin
    # This is to ensure thread-safety
    # logger.debug "Setting allow_concurrency"
    # ActiveRecord::Base.allow_concurrency = true
  rescue Exception => e
    logger.info "Exception caught: #{e}"
    exit
  end

  klass = nil
  begin
    klass = @config[:active_record_model].constantize
    logger.info "Access to #{klass.count} #{@config[:active_record_model]} records"
  rescue Exception => e
    logger.info "Exception caught while loading #{@config[:active_record_model]}: #{e}"
    exit
  end

  s = LDAP::Server.new(
    :port             => @config[:port],
    :bindaddr         => @config[:bind_address],
    :nodelay          => @config[:tcp_nodelay],
    :listen           => @config[:prefork_threads],
    :namingContexts   => [@config[:basedn]],
    :user             => @config[:user],
    :group            => @config[:group],
    :operation_class  => ActiveRecordOperation,
    :operation_args   => [@config, klass, logger]
  )
  s.run_tcpserver
  logger.info "Listening on port #{@config[:port]}."

  s.join
end

#stopObject



106
107
108
109
110
111
112
113
114
# File 'lib/arldap/server.rb', line 106

def stop
  if @pidfile.pid
    puts "Sending TERM signal to process #{@pidfile.pid}" if @config[:debug]
    logger.info("Killing server at #{@pidfile.pid}")
    Process.kill("TERM", @pidfile.pid.to_i)
  else
     puts "Can't find pid.  Are you sure I'm running?"
   end
end