#! /usr/bin/env ruby require 'rubygems' require 'puppetfactory' require 'optparse' require 'yaml' NAME = File.basename($PROGRAM_NAME) if File.exist? '/etc/puppetfactory/config.yaml' options = YAML.load_file('/etc/puppetfactory/config.yaml') rescue {} else options = YAML.load_file('/etc/puppetfactory.yaml') rescue {} end optparse = OptionParser.new { |opts| opts.banner = "Usage : #{NAME} [command] [target] When called with no arguments, this will start the PuppetFactory server. You can also pass arguments to invoke CLI commands. * ls | list * List all PuppetFactory users * new |create [password] * Create a new PuppetFactory user with an optional password * remove | delete * Delete a PuppetFactory user * repair | reprovision * Reprovision a PuppetFactory user container * redeploy * Delete a user's environment from disk and redeploy it with r10k * configprint * Display the value of all Puppetfactory options. This will send commands to the PuppetFactory server specified in /etc/puppetfactory/config.yaml, defaulting to localhost if unspecified. " opts.on("-s SERVER", "--server SERVER", "Remote PuppetFactory server.") do |arg| options[:server] = arg end opts.on("-d", "--debug", "Display extra debugging information.") do options[:debug] = true end opts.separator('') opts.on("-h", "--help", "Displays this help") do puts opts exit end } optparse.parse! options[:puppet] ||= '/opt/puppetlabs/bin/puppet' options[:confdir] ||= '/etc/puppetlabs/puppet' options[:codedir] ||= '/etc/puppetlabs/code' options[:environments] ||= "#{options[:codedir]}/environments" options[:root] ||= File.expand_path(File.join(File.dirname(__FILE__), '..')) options[:logfile] ||= '/var/log/puppetfactory' options[:templatedir] ||= "#{File.dirname(__FILE__)}/../templates" options[:port] ||= 8000 options[:bind] ||= '0.0.0.0' options[:user] ||= 'admin' options[:password] ||= 'admin' options[:session] ||= '12345' options[:master] ||= `hostname`.strip options[:usersuffix] ||= 'puppetfactory.vm' options[:usergroups] ||= [] options[:puppetcode] ||= '/var/opt/puppetcode' options[:modulepath] ||= :none options[:gitserver] ||= 'https://github.com' options[:gituser] ||= 'puppetlabs' options[:controlrepo] ||= 'control-repo.git' options[:repomodel] ||= :single options[:plugins] ||= [:ShellUser, :Logs] def validate(options, option, values) unless values.include? options[option] raise "#{option} must be one of #{values.inspect}" end end validate(options, :modulepath, [:readwrite, :readonly, :none]) validate(options, :repomodel, [:single, :peruser]) Puppetfactory::Helpers.configure(options) # why do I have to do this? This page implies I shouldn't. # https://github.com/sinatra/sinatra#logging $logger = Logger::new(options[:logfile]) $logger.level = options[:debug] ? Logger::DEBUG : Logger::INFO $logger.formatter = proc { |severity,datetime,progname,msg| "[#{datetime}] #{severity}: #{msg}\n" } # if no arguments, start up the server if ARGV.size == 0 Puppetfactory.run! options # opts = { # :Port => 8000, # :Host => '0.0.0.0', # :Logger => WEBrick::Log::new(options[:logfile], WEBrick::Log::INFO), # :ServerType => options[:debug] ? WEBrick::SimpleServer : WEBrick::Daemon, # :DocumentRoot => options[:docroot], # :SSLEnable => false, # # :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE, # # :SSLCertificate => OpenSSL::X509::Certificate.new( File.open(File.join(CERT_PATH, 'server.crt')).read), # # :SSLPrivateKey => OpenSSL::PKey::RSA.new( File.open(File.join(CERT_PATH, 'server.key')).read), # # :SSLCertName => [ [ "CN",WEBrick::Utils::getservername ] ] # }.merge(options) # # Rack::Handler::WEBrick.run(Puppetfactory, opts) do |server| # require 'pry' # binding.pry # # [:INT, :TERM].each { |sig| trap(sig) { server.stop } } # end else require 'puppetfactory/cli' puppetfactory = Puppetfactory::Cli.new(options) command, user, password = ARGV case command when 'ls', 'list' puppetfactory.list when 'new', 'create' puppetfactory.create(user, password) when 'remove', 'delete' puppetfactory.delete(user) when 'repair', 'reprovision' puppetfactory.repair(user) when 'redeploy' puppetfactory.redeploy(user) when 'configprint' puts options.to_yaml else puts "Unknown command: #{command}" puts "Run #{NAME} -h for usage." exit 1 end end