#!/usr/bin/env ruby require 'fileutils' require 'getoptlong' require 'yaml' require 'bcrypt' require 'base64' require "#{File.dirname(__FILE__)}/../lib/teamster-cli" DEFAULT_UNIX_SOCKET_FILE = '/tmp/teamster.app.sock' DEFAULT_APP_STATE_FILE = '/srv/my-site/teamster.app.state' CONFIG_FILE = 'conf/teamster.conf' VERSION = File.read(File.dirname(__FILE__) + '/../VERSION') class TeamsterApp include Teamster::CLI class << self def read_options_and_run read_options run_app end def read_options opts = GetoptLong.new( ["--debug", GetoptLong::NO_ARGUMENT ], ["--version", GetoptLong::NO_ARGUMENT ], ["--prod", GetoptLong::NO_ARGUMENT ], ["--help", GetoptLong::NO_ARGUMENT ], ["--overwrite", GetoptLong::NO_ARGUMENT ], ["--socket-file", GetoptLong::REQUIRED_ARGUMENT], ["--state-file", GetoptLong::REQUIRED_ARGUMENT]) @config = {}.tap do |hsh| opts.each do |opt, arg| opt = opt[/--([\w-]+)/, 1].gsub(/-/, "_").to_sym arg ||= true hsh[opt] = arg end end @config end def run_app if @config[:help] quit detailed_usage elsif @config[:version] quit show_version elsif ARGV.size > 0 command = ARGV.shift.to_sym args = ARGV send command, *args else quit usage end end private # --{ COMMANDS }-- # def init(*args) current_working_folder = Dir.pwd team_name = args[0] content = File.dirname(__FILE__) + '/../content' puts "Initializing Teamster in current folder." puts "Creating required content..." FileUtils.mkdir_p "conf" create_config team_name FileUtils.mkdir_p "data" create_user FileUtils.cp_r "#{content}/views", current_working_folder FileUtils.cp_r "#{content}/public", current_working_folder create_file "config.ru", "config_ru" FileUtils.mkdir_p "lib/teamster-modules" create_file "lib/teamster-modules.rb", "teamster_modules" puts "Teamster initialized!" puts "- Run \"teamster create \" to create a placeholder module." puts "- Run \"teamster start\" to start teamster!" end def start(*) puts "Starting teamster..." if File.exists?(CONFIG_FILE) if @config[:prod] socket_file = @config[:socket_file] || DEFAULT_UNIX_SOCKET_FILE state_file = @config[:state_file] || DEFAULT_APP_STATE_FILE Thread.new { exec "puma -d -b unix://#{socket_file} -S #{state_file}" } else Thread.new { exec "rackup -p 9292" } end else quit "\nERROR: Teamster not initialized!\nUnable to start Teamster. Please initialize first by running \"teamster init\"." end end def stop(*) puts "Stopping teamster..." state_file = @config[:state_file] || DEFAULT_APP_STATE_FILE if File.exists? state_file Thread.new { exec "pumactl -S #{state_file} stop" } else quit "\nERROR: state file does not exists: #{state_file}\nUnable to stop teamster." exit 1 end end def restart(*) puts "Restarting teamster..." state_file = @config[:state_file] || DEFAULT_APP_STATE_FILE if File.exists? state_file Thread.new { exec "pumactl -S #{state_file} restart" } else quit "\nERROR: state file does not exists: #{state_file}\nUnable to restart teamster." end end def create(*args) name = args[0] create_module_for name.downcase end def import(*args) name = args[0] import_module_for name.downcase end def export(*args) name = args[0] export_module_for name.downcase end # --{ HELPERS }-- # end end TeamsterApp.read_options_and_run