#!/usr/bin/env ruby require 'yaml' require 'robot-controller' # bin/controller tool module RobotController # class CLI # meta command def self.run(args) if args.size == 0 || args.first == '--help' puts ' Usage: controller ( boot | quit ) controller ( start | status | stop | restart | log ) [worker] controller verify [--verbose] controller [--help] Example: % controller boot # start eyed and jobs % controller status # check on status of jobs % controller log dor_accessionWF_descriptive-metadata # view log for worker % controller verify # verify robots are running as configured % controller stop # stop jobs % controller quit # stop eyed Environment: EYE_BASEDIR - where bluepill stores its state (default: run/eye) EYE_LOGFILE - output log (default: log/eye.log) ROBOT_ENVIRONMENT - (default: development) ' exit(-1) end ENV['ROBOT_ENVIRONMENT'] ||= 'development' ENV['EYE_BASE_DIR'] ||= File.expand_path('run/eye') ENV['EYE_LOGFILE'] ||= File.expand_path('log/eye.log') fail 'eye requires config directory' unless File.directory?('config') cmd = 'eye' # cmd << " --dir #{ENV['EYE_BASE_DIR']}" # cmd << " --logger #{ENV['EYE_LOGFILE']}" case args.first.downcase when 'status' exec "#{cmd} info -j" exit(0) when 'boot' fn = 'config/eye.rb' # allow override fn = RobotController.eye_config unless File.file?(fn) if File.file?(fn) # puts "Loading #{fn}" exec "#{cmd} load #{fn}" # NOTREACHED end puts "ERROR: Cannot find eye configuration file for #{ENV['ROBOT_ENVIRONMENT']}" exit(-1) when 'verify' verbose = (args[1] == '--verbose') # load list of all possible robots robots = YAML.load_file('config/robots.yml') fail ArgumentError unless robots.is_a? Array # determine how many processes should be running for each robot running = {} robots.each do |robot| running[robot] = 0 end RobotController::Parser.load("robots_#{ENV['ROBOT_ENVIRONMENT']}.yml").each do |h| running[h[:robot]] = 0 if running[h[:robot]].nil? running[h[:robot]] += h[:n] end # verify that all robots running are known to the config/robots.yml running.each_key do |robot| unless robots.include?(robot) puts "ERROR: '#{robot}' robot is unknown to the suite. Check config/robots.yml" running.delete(robot) end end # verify suite verify = RobotController::Verify.new(running) begin statuses = verify.verify rescue => e puts "ERROR: Cannot run verification for any robots. #{e.class}: #{e.message}" exit(-1) end # print output for each status ok = true puts 'ERROR: No robots to verify?' if statuses.size == 0 statuses.each_pair do |robot, status| case status[:state] when :up puts "OK: #{robot} is up (#{status[:running]} running)" if verbose when :not_enabled if status[:running] == 0 puts "OK: #{robot} is not enabled (0 running)" if verbose else puts "ERROR: #{robot} is not enabled (but #{status[:running]} running)" end when :down ok = false puts "ERROR: #{robot} is down (#{status[:running]} of #{running[robot]} running)" else ok = false puts "ERROR: #{robot} cannot be verified (state=#{status[:state]})" end end puts 'OK' if ok && !verbose && statuses.size > 0 exit(0) else exec "#{cmd} #{args.join(' ')}" # NOTREACHED end end end end RobotController::CLI.run(ARGV)