2013-03-10 23:14:31 +00:00
|
|
|
#!/usr/bin/env ruby
|
2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2018-10-01 20:00:26 +00:00
|
|
|
begin
|
|
|
|
load File.expand_path('../bin/spring', __dir__)
|
|
|
|
rescue LoadError => e
|
2020-09-30 09:07:01 +00:00
|
|
|
raise if e.message.exclude?('spring')
|
2018-10-01 20:00:26 +00:00
|
|
|
end
|
2015-05-08 13:58:28 +00:00
|
|
|
|
2018-10-01 20:00:26 +00:00
|
|
|
dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
2015-08-18 08:50:12 +00:00
|
|
|
Dir.chdir dir
|
2017-04-27 07:34:40 +00:00
|
|
|
|
|
|
|
require 'bundler'
|
2018-10-01 20:00:26 +00:00
|
|
|
|
2015-08-18 08:50:12 +00:00
|
|
|
require File.join(dir, 'config', 'environment')
|
2018-10-01 20:00:26 +00:00
|
|
|
|
2017-05-08 14:05:38 +00:00
|
|
|
require 'eventmachine'
|
|
|
|
require 'em-websocket'
|
|
|
|
require 'json'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'optparse'
|
|
|
|
require 'daemons'
|
2016-05-16 17:49:06 +00:00
|
|
|
|
2017-05-08 14:05:38 +00:00
|
|
|
def before_fork
|
|
|
|
# remember open file handles
|
|
|
|
@files_to_reopen = []
|
|
|
|
ObjectSpace.each_object(File) do |file|
|
2018-05-04 14:05:10 +00:00
|
|
|
@files_to_reopen << file if !file.closed?
|
2017-05-08 14:05:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_fork(dir)
|
|
|
|
Dir.chdir dir
|
|
|
|
|
|
|
|
# Re-open file handles
|
|
|
|
@files_to_reopen.each do |file|
|
|
|
|
file.reopen file.path, 'a+'
|
|
|
|
file.sync = true
|
|
|
|
end
|
|
|
|
|
2018-10-01 20:00:26 +00:00
|
|
|
# Spring redirects STDOUT and STDERR to /dev/null
|
|
|
|
# before we get here. This causes the `reopen` lines
|
|
|
|
# below to fail because the handles are already
|
|
|
|
# opened for write
|
|
|
|
if defined?(Spring)
|
|
|
|
$stdout.close
|
|
|
|
$stderr.close
|
|
|
|
end
|
|
|
|
|
|
|
|
$stdout.reopen("#{dir}/log/websocket-server_out.log", 'w').sync = true
|
|
|
|
$stderr.reopen("#{dir}/log/websocket-server_err.log", 'w').sync = true
|
2017-05-08 14:05:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
before_fork
|
|
|
|
|
2012-07-23 22:22:23 +00:00
|
|
|
# Look for -o with argument, and -I and -D boolean arguments
|
2012-08-06 06:29:39 +00:00
|
|
|
@options = {
|
2015-04-27 13:42:53 +00:00
|
|
|
p: 6042,
|
|
|
|
b: '0.0.0.0',
|
|
|
|
s: false,
|
|
|
|
v: false,
|
|
|
|
d: false,
|
|
|
|
k: '/path/to/server.key',
|
|
|
|
c: '/path/to/server.crt',
|
2018-10-01 20:00:26 +00:00
|
|
|
i: "#{dir}/tmp/pids/websocket.pid"
|
2012-07-23 22:22:23 +00:00
|
|
|
}
|
2013-03-10 23:14:31 +00:00
|
|
|
|
2012-07-23 22:22:23 +00:00
|
|
|
OptionParser.new do |opts|
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.banner = 'Usage: websocket-server.rb start|stop [options]'
|
2012-07-23 22:22:23 +00:00
|
|
|
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-d', '--daemon', 'start as daemon') do |d|
|
2012-08-06 06:29:39 +00:00
|
|
|
@options[:d] = d
|
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-v', '--verbose', 'enable debug messages') do |d|
|
2013-03-10 23:14:31 +00:00
|
|
|
@options[:v] = d
|
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-p', '--port [OPT]', 'port of websocket server') do |p|
|
2012-08-06 06:29:39 +00:00
|
|
|
@options[:p] = p
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-b', '--bind [OPT]', 'bind address') do |b|
|
2018-03-15 18:21:04 +00:00
|
|
|
@options[:b] = IPAddr.new(b).to_s
|
2012-07-23 22:22:23 +00:00
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-s', '--secure', 'enable secure connections') do |s|
|
2012-08-06 06:29:39 +00:00
|
|
|
@options[:s] = s
|
2012-08-02 09:17:22 +00:00
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-i', '--pid [OPT]', 'pid, default is tmp/pids/websocket.pid') do |i|
|
2013-01-15 22:46:35 +00:00
|
|
|
@options[:i] = i
|
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-k', '--private-key [OPT]', '/path/to/server.key for secure connections') do |k|
|
2018-12-19 14:31:06 +00:00
|
|
|
options[:tls_options] ||= {}
|
|
|
|
options[:tls_options][:private_key_file] = k
|
2012-08-02 09:17:22 +00:00
|
|
|
end
|
2015-04-27 13:20:16 +00:00
|
|
|
opts.on('-c', '--certificate [OPT]', '/path/to/server.crt for secure connections') do |c|
|
2018-12-19 14:31:06 +00:00
|
|
|
options[:tls_options] ||= {}
|
|
|
|
options[:tls_options][:cert_chain_file] = c
|
2012-08-02 09:17:22 +00:00
|
|
|
end
|
2012-07-23 22:22:23 +00:00
|
|
|
end.parse!
|
|
|
|
|
2013-06-19 00:40:42 +00:00
|
|
|
if ARGV[0] != 'start' && ARGV[0] != 'stop'
|
2013-06-30 15:54:54 +00:00
|
|
|
puts "Usage: #{File.basename(__FILE__)} start|stop [options]"
|
2015-04-30 17:54:08 +00:00
|
|
|
exit
|
2013-06-19 00:40:42 +00:00
|
|
|
end
|
|
|
|
|
2013-03-10 23:14:31 +00:00
|
|
|
if ARGV[0] == 'stop'
|
2018-03-15 18:21:04 +00:00
|
|
|
pid = File.read(@options[:i]).to_i
|
|
|
|
puts "Stopping websocket server (pid: #{pid})"
|
2015-07-16 19:56:54 +00:00
|
|
|
|
2018-03-15 18:21:04 +00:00
|
|
|
# IMPORTANT: Use SIGTERM (15), not SIGKILL (9)
|
|
|
|
# Daemons.rb cleans up the PID file automatically on termination;
|
|
|
|
# SIGKILL ends the process immediately and bypasses cleanup.
|
|
|
|
# See https://major.io/2010/03/18/sigterm-vs-sigkill/ for more.
|
|
|
|
Process.kill(:SIGTERM, pid)
|
2013-03-10 23:14:31 +00:00
|
|
|
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2015-11-24 02:33:40 +00:00
|
|
|
if ARGV[0] == 'start' && @options[:d]
|
2018-03-15 18:21:04 +00:00
|
|
|
puts "Starting websocket server on #{@options[:b]}:#{@options[:p]} (secure: #{@options[:s]}, pidfile: #{@options[:i]})"
|
2015-07-16 19:56:54 +00:00
|
|
|
|
2018-03-15 18:21:04 +00:00
|
|
|
# Use Daemons.rb's built-in facility for generating PID files
|
|
|
|
Daemons.daemonize(
|
|
|
|
app_name: File.basename(@options[:i], '.pid'),
|
|
|
|
dir_mode: :normal,
|
2018-10-01 20:00:26 +00:00
|
|
|
dir: File.dirname(@options[:i])
|
2018-03-15 18:21:04 +00:00
|
|
|
)
|
2013-03-10 23:14:31 +00:00
|
|
|
|
2017-05-08 14:05:38 +00:00
|
|
|
after_fork(dir)
|
2013-03-10 23:14:31 +00:00
|
|
|
end
|
2013-01-15 22:33:51 +00:00
|
|
|
|
2018-12-19 14:31:06 +00:00
|
|
|
WebsocketServer.run(@options)
|