Class: OsCtld::Cli::Supervisor

Inherits:
Object
  • Object
show all
Defined in:
lib/osctld/cli/supervisor.rb

Defined Under Namespace

Classes: Options

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts



16
17
18
# File 'lib/osctld/cli/supervisor.rb', line 16

def opts
  @opts
end

Class Method Details

.runObject



7
8
9
10
11
12
13
14
# File 'lib/osctld/cli/supervisor.rb', line 7

def self.run
  s = new
  s.parse

  return Cli::Daemon.run(s.opts) unless s.supervise?

  s.supervise
end

Instance Method Details

#cleanupObject



99
100
101
102
103
104
105
106
107
# File 'lib/osctld/cli/supervisor.rb', line 99

def cleanup
  File.unlink(Daemon::SOCKET) if File.exist?(Daemon::SOCKET)

  Dir.glob(File.join(RunState::USER_CONTROL_DIR, '*.sock')).each do |f|
    File.unlink(f)
  end

  File.unlink(Migration::SOCKET) if File.exist?(Migration::SOCKET)
end

#parseObject



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
# File 'lib/osctld/cli/supervisor.rb', line 18

def parse
  @opts = Options.new(true, :stdout, 'daemon')

  OptionParser.new do |opts|
    opts.on('--[no-]supervisor', 'Toggle osctld supervisor process (enabled by default)') do |v|
      @opts.supervisor = v
    end

    opts.on('-l', '--log LOGGER', %w(syslog stdout)) do |v|
      @opts.log = v.to_sym
    end

    opts.on(
      '--log-facility FACILITY',
      'Syslog facility, see man syslog(3), lowercase without LOG_ prefix'
    ) do |v|
      @opts.log_facility = v
    end

    opts.on('-h', '--help', 'Prints help message and exit') do
      puts opts
      exit
    end
  end.parse!

  @opts
end

#superviseObject



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
# File 'lib/osctld/cli/supervisor.rb', line 50

def supervise
  Process.setproctitle('osctld: supervisor')
  OsCtl::Lib::Logger.setup(opts.log, facility: opts.log_facility)

  out_r, out_w = IO.pipe

  pid = Process.fork do
    out_r.close

    STDIN.close
    STDOUT.reopen(out_w)
    STDERR.reopen(out_w)

    Process.exec(
      File.expand_path($0),
      '--no-supervisor',
      '--log', opts.log.to_s,
      '--log-facility', opts.log_facility,
      pgroup: true
    )
  end

  out_w.close

  Signal.trap('CHLD') do
    out_r.close
  end

  %w(INT TERM HUP).each do |sig|
    Signal.trap(sig) do
      Process.kill(sig, pid)
    end
  end

  begin
    out_r.each_line do |line|
      OsCtl::Lib::Logger.log(:unknown, line)
    end

  rescue IOError
    # pass
  ensure
    _, status = Process.wait2(pid)
    cleanup

    exit(status.exitstatus || 0)
  end
end

#supervise?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/osctld/cli/supervisor.rb', line 46

def supervise?
  opts.supervisor
end