require 'flydata/compatibility_check' module Flydata module Command class Sender < Base def self.slop_start Slop.new do on 'n', 'no-daemon', 'Start FlyData agent as a regular program' end end def start(options_or_show_final_message = {show_final_message: true}) # For backward compatibility. Use only as options going forward if options_or_show_final_message.kind_of? Hash options = options_or_show_final_message else options = {show_final_message: options_or_show_final_message} end # Check if process exist if process_exist? say("Process is still running. Please stop process first.") unless options[:quiet] return end # Ends orphan_proceses if there is any orphan_processes.each do |pid| Process.kill(:TERM, pid) end wait_until_server_ready(options) dp = flydata.data_port.get AgentCompatibilityCheck.new(dp).check # Start sender(fluentd) process say('Starting sender process.') unless options[:quiet] Dir.chdir(FLYDATA_HOME){ Kernel.system("bash #{File.dirname(__FILE__)}/../../../bin/serverinfo", :out => ["#{FLYDATA_HOME}/flydata.log",'a'], :err => ["#{FLYDATA_HOME}/flydata.log",'a']) daemon_opt = opts.no_daemon? ? "" : daemon_option Kernel.system("ruby `which fluentd` #{daemon_opt} -l #{FLYDATA_HOME}/flydata.log -c #{FLYDATA_HOME}/flydata.conf -p #{File.dirname(__FILE__)}/../fluent-plugins") } Kernel.sleep 5 wait_until_client_ready(options) #wait_until_logs_uploaded if options[:show_final_message] && !options[:quiet] data_port = flydata.data_port.get say("Go to your Dashboard! #{flydata.flydata_api_host}/data_ports/#{data_port['id']}") say < 0 end def kill_all(optiosn = {}) if Kernel.system("ps ax | grep 'flydata' | grep -v grep | awk '{print \"kill \" $1}' | sh") say("Done.") unless options[:quiet] return true else raise 'Something has gone wrong...' end end private # Return a list of fluentd parent processes run by the same user for the # same flydata.pid file but not the process managed by the file itself. CMD = %Q{test -f %s && ps -u `whoami` -o ppid,pid,args | grep '^ *1 ' | grep '\\%s' | egrep -v "\\b`cat %s`\\b"} def orphan_processes cmd = CMD % [ pid_file, daemon_option, pid_file ] result = `#{cmd}` orphan_pids = [] result.each_line do |line| orphan_pids << line.split[1].to_i end orphan_pids end def wait_until_server_ready(options = {}) retry_count = 10 1.upto(retry_count) do |i| return true if server_ready? say("Waiting for the server side to become active... (#{i}/#{retry_count})") unless options[:quiet] Kernel.sleep 30 end false end def wait_until_client_ready(options = {}) retry_count = 10 1.upto(retry_count) do |i| if client_ready? say("Done! Client is ready now.") unless options[:quiet] return true end if process_died? raise "Client could not been launched. Detail here #{FLYDATA_HOME}/flydata.log" end say("Waiting for the client side to become active... (#{i}/#{retry_count})") unless options[:quiet] Kernel.sleep 10 end raise "Somthing has gone wrong... Please try setup command again." end def wait_until_client_stop(options = {}) retry_count = 5 1.upto(retry_count) do |i| return true unless process_exist? say("Waiting for the client to stop... (#{i}/#{retry_count})") unless options[:quiet] Kernel.sleep 3 end false end def wait_until_logs_uploaded(options = {}) say('Starting to check the upload from your server.') unless options[:quiet] data_port = flydata.data_port.get data_port_id = data_port['id'] retry_count = 10 1.upto(retry_count) do |i| if uploaded_successfully?(data_port_id) say("Uploading your logs correctly.") unless options[:quiet] return true end say("Waiting logs uploading... (#{i}/#{retry_count})") unless options[:quiet] Kernel.sleep 30 end raise 'Cannot confirm that your logs exist on the FlyData server. Something has gone wrong..' end def server_ready? data_port = flydata.data_port.get data_port['server_status'] == 'active' end def client_ready? process_exist? end def process_died? # Returns true if the process is running !!(`tail -n 1 #{FLYDATA_HOME}/flydata.log` =~ /process died within/) end def uploaded_successfully?(data_port_id) res = flydata.get("/data_ports/#{data_port_id}/tail.json") res and res['logs'] and res['logs'].size > 0 end def client_buffer_empty?(options = {}) client_buffer = File.join(FLYDATA_HOME, 'buffer') say("Checking the client buffer #{client_buffer}") unless options[:quiet] Dir.glob("#{client_buffer}/*").empty? end def pid_file "#{FLYDATA_HOME}/flydata.pid" end def daemon_option "-d #{pid_file}" end end end end