#!/usr/bin/env ruby require File.dirname(__FILE__) + '/../lib/sfpagent' def version? File.read(File.dirname(__FILE__) + '/../VERSION').strip end opts = Trollop::options do version "sfpagent #{version?} (c) 2013 Herry" banner <<-EOS SFP Agent that provides a Ruby framework for managing system configurations. The configurations are modelled in SFP language. Usage: sfpagent [options] [model-file] [plan-file] where [options] are: EOS opt :start, "Start the agent. If --daemon option is set true, then the agent will start as a daemon.", :short => '-s' opt :stop, "Stop the daemon agent.", :short => '-t' opt :restart, "Restart the daemon agent.", :short => '-r' opt :status, "Print the status of the daemon agent.", :short => '-a' opt :port, "Port number of the daemon agent should listen to.", :short => '-p', :default => Sfp::Agent::DefaultPort opt :ssl, "Set the agent to use HTTPS instead of HTTP.", :default => false opt :certfile, "Certificate file for HTTPS.", :default => '' opt :keyfile, "Private key file for HTTPS.", :default => '' opt :modules_dir, "A directory that holds all SFP modules.", :default => '' end def parse(filepath) home_dir = File.expand_path(File.dirname(filepath)) parser = Sfp::Parser.new({:home_dir => home_dir}) parser.parse(File.read(filepath)) parser end model_file = ARGV[0].to_s plan_file = ARGV[1].to_s if opts[:start] if not Sfp::Agent.pid.nil? puts "Agent is already running with PID #{Sfp::Agent.pid}" else opts[:daemon] = true Sfp::Agent.start(opts) end elsif opts[:stop] Sfp::Agent.stop(opts) elsif opts[:restart] opts[:daemon] = true Sfp::Agent.stop if Sfp::Agent.pid.to_i > 0 Sfp::Agent.start(opts) elsif opts[:status] Sfp::Agent.status elsif opts[:state] abort "[model-file] is not specified!\nUse \"sfpagent -h\" for more details.\n" if model_file == '' abort "File #{model_file} is not exist!" if not File.exist?(model_file) opts[:daemon] = false opts = Sfp::Agent.check_config(opts) Sfp::Agent.load_modules(opts) state = Sfp::Runtime.new(parse(model_file)).get_state(true) puts JSON.pretty_generate(state) elsif opts[:execute] abort "[model-file] is not specified!\nUse \"sfpagent -h\" for more details.\n" if model_file == '' abort "[plan-file] is not specified!\nUse \"sfpagent -h\" for more details.\n" if plan_file == '' abort "File #{model_file} is not exist!" if not File.exist?(model_file) abort "File #{plan_file} is not exist!" if not File.exist?(plan_file) opts[:daemon] = false opts = Sfp::Agent.check_config(opts) Sfp::Agent.load_modules(opts) runtime = Sfp::Runtime.new(parse(model_file)) runtime.get_state puts (runtime.execute_plan(File.read(plan_file)) ? "Success!" : "Failed!") else Trollop::help end