#!/usr/bin/env ruby # Try to load external libs, helpers and constants begin require "rubygems" require "optparse" require 'daemons' require_relative "../lib/pushyd/conf" rescue LoadError raise "EXITING: some basic libs were not found" end include PushyDaemon # Handle configuration APP_ROOT = File.expand_path(File.dirname(__FILE__) + "/../") begin # Defaults cmd_config = nil cmd_logfile = nil cmd_env = "production" cmd_dump = false # Parse options and check compliance OptionParser.new do |opts| opts.banner = "Usage: #{File.basename $PROGRAM_NAME} [options] start|stop" opts.on("-l", "--log LOGFILE") { |path| cmd_logfile = File.expand_path(path)} opts.on("-c", "--config CONFIGFILE") { |path| cmd_config = File.expand_path(path)} opts.on("-e", "--environment ENV") { |env| cmd_env = env } opts.on("-d", "--dump") { cmd_dump = true } opts.on("", "--dev") { cmd_env = "development" } end.order!(ARGV) # Build Chamber-based configuration from Gemspec with initial context Conf.prepare root: APP_ROOT, gemspec: "pushyd", env: cmd_env, config: cmd_config, logfile: cmd_logfile rescue OptionParser::InvalidOption => e abort "EXITING: InvalidOption: #{e.message} \n #{e.backtrace.to_yaml}" rescue PushyDaemon::ConfigParseError => e abort "EXITING: ConfigParseError: #{e.message} \n #{e.backtrace.to_yaml}" rescue StandardError => e abort "EXITING: StandardError: #{e.message} \n #{e.backtrace.to_yaml}" end # Display final configuration puts "--- #{Conf.name} #{Conf.version}" puts "YAML Parser \t #{YAML.name}" puts "Environment \t #{Conf.env}" puts "Config files \t #{Conf.files}" puts "Loging to file \t #{Conf.log.logfile}" # Quit if config dump requested if cmd_dump puts Conf.dump exit(0) end # Run daemon run_options = { ontop: false, :backtrace => true, :multiple => false } Daemons.run_proc('pushy-daemon', run_options) do # Load code puts "--- load code" require_relative "../lib/pushyd" # Start daemon puts "--- start daemon" PushyDaemon::Daemon.run end