summaryrefslogtreecommitdiff
path: root/bin/leap_ca_daemon
blob: 21e48ef19d47d47fa61d4012679bec0865e6252e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/ruby

#
# LEAP Client Certificate Generation Daemon
#

BASE_DIR = File.expand_path('../..', File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__)

begin
  #
  # try without rubygems (might be already loaded or not present)
  #
  require 'leap_ca/version'
rescue LoadError
  #
  # try with rubygems
  #
  require "#{BASE_DIR}/lib/leap_ca/version.rb"
  LeapCA::REQUIRE_PATHS.each do |path|
    path = File.expand_path(path, BASE_DIR)
    $LOAD_PATH.unshift path unless $LOAD_PATH.include?(path)
  end
  require 'rubygems'
  require 'leap_ca/version'
end

# Graceful Ctrl-C
Signal.trap("SIGINT") do
  puts "\nQuit"
  exit
end

# this changes later, so save the initial current directory
CWD = Dir.pwd

# handle --version ourselves
if ARGV.grep(/--version/).any?
  puts "leap_ca #{LeapCA::VERSION}, ruby #{RUBY_VERSION}"
  exit(0)
end

# --fill-pool will fill the pool and then exit
if ARGV.grep(/--once/).any? or ARGV.grep(/--run-once/).any?
  require 'leap_ca'
  pool = LeapCA::Pool.new(:size => LeapCA::Config.max_pool_size)
  pool.fill
  exit(0)
end

#
# Start the daemon
#
require 'daemons'
if ENV["USER"] == "root"
  options = {:app_name => 'leap_ca', :dir_mode => :system}  # this will put the pid file in /var/run
else
  options = {:app_name => 'leap_ca', :dir_mode => :normal, :dir => '/tmp'} # this will put the pid file in /tmp
end
Daemons.run("#{BASE_DIR}/lib/leap_ca_daemon.rb", options)