#!/usr/bin/env ruby libdir = File.expand_path(File.dirname(__FILE__)) require "#{libdir}/../lib/sfpagent" opts = Trollop::options do version "sfpagent 0.1.1 (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." opt :stop, "Stop the daemon agent." opt :status, "Print the status of the daemon agent." opt :state, "Given a model, print the state of all modules. (Note: [model-file] should be specified.)" opt :execute, "Given a model, execute a plan in given file. (Note: [model-file] should be specified.)" opt :pretty, "Print the result in a pretty JSON format." opt :port, "Port number of the daemon agent should listen to.", :default => Sfp::Agent::DefaultPort opt :daemon, "Start the agent as a daemon.", :default => true 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] Sfp::Agent.start(opts) elsif opts[:stop] Sfp::Agent.stop 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