#!/usr/bin/env ruby require "thor" require "baleen" def pid pid = `ps ax | grep -E "bin/baleen-server" | grep -v grep | awk '{print $1}'` pid.split("\n") end def running? if pid.size == 0 false elsif pid.first.to_i == Process.pid false else true end end class BaleenServerCli < Thor include Baleen include Baleen::Default desc "start", "Start Baleen server" option :docker_host, :desc => "Host of Docker" option :docker_port, :desc => "Port that Docker is listening to" option :port, :desc => "Port that baleen server is listening to" option :ci, :desc => "When specified, baleen-server will receive github post-receive" option :ci_port, :desc => "Port to accept github post-receive" option :project_file, :desc => "Path to project file (baleen.yml)" option :dir, :desc => "Specify the directory for baleen to save temp files, logs, caches, etc." option :log_level, :desc => "Specify log level" option :daemon, :desc => "Running Baleen on background", :type => :boolean def start if running? BL.error "baleen-server is already running" BL.error "Exiting..." exit end docker_host = options[:docker_host] || default_docker_host docker_port = options[:docker_port] || default_docker_port port = options[:port] || default_port dir = options[:dir] || default_dir project_file = options[:project_file] || default_project_file ci_port = options[:ci_port] || default_ci_port daemon = options[:daemon] || default_daemon ci = options[:ci] log_level = options[:log_level] config = Configuration.instance config.dir = dir config.debug = daemon ? false : true log_levels = {"debug" => DEBUG, "info" => INFO, "warn" => WARN, "error" => ERROR} if log_level.nil? config.log_level = default_log_level else unless log_levels.keys.include? log_level hl_error "Unknown log level: #{log_level}" exit 1 end config.log_level = log_levels[log_level] end BL.notice("Starting baleen-server...") BL.notice(" Listening on: #{port}") BL.notice(" Docker API: http://#{docker_host}:#{docker_port}").eol Process.daemon(true, false) if daemon Baleen::Server.new(docker_host, docker_port, port, project_file) Baleen::CiServer.run!(docker_host: docker_host, docker_port: docker_port, port: ci_port, log_level: log_level) if ci # Prevent main thread from exiting for Celluloid actors to continue running # See https://github.com/celluloid/celluloid/wiki/Frequently-Asked-Questions sleep end desc "stop", "Stop Baleen server" def stop if running? BL.warn "Stopping Baleen server..." `kill -9 #{pid.first}` else BL.warn "baleen-server is not running" end end desc "restart", "Restart Baleen server" def restart self.stop self.start end end BaleenServerCli.start