#!/usr/bin/env ruby ################################################################################ # # Author: Zachary Patten # Copyright: Copyright (c) Zachary Patten # License: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ################################################################################ require 'gli' require 'testlab' include GLI::App include TestLab::Utility::GLI include TestLab::Utility::Misc version TestLab::VERSION program_desc %(TestLab - A toolkit for building virtual computer labs) # program_long_desc %(Program Long Description) sort_help :manually default_command :help preserve_argv true commands_from 'commands' desc 'Show verbose output' default_value false switch [:v, :verbose] desc 'Quiet mode' default_value false switch [:q, :quiet] desc 'Path to Labfile: ${REPO}/Labfile' arg_name 'path/to/file' # default_value File.join(Dir.pwd, 'Labfile') flag [:l, :labfile] desc 'Path to Repository directory: ${PWD}' arg_name 'path/to/directory' default_value Dir.pwd flag [:r, :repo] desc 'Path to Configuration directory: ${REPO}/.testlab-$(hostname -s)' arg_name 'path/to/directory' # default_value File.join(Dir.pwd, ".testlab-#{TestLab.hostname}") flag [:c, :config] pre do |global,command,options,args| @testlab_start_time = Time.now.utc (global[:verbose] == true) and (ENV['LOG_LEVEL'] = 'DEBUG') log_file = File.join(global[:repo], "testlab-#{TestLab.hostname}.log") @logger = ZTK::Logger.new(log_file) @ui = ZTK::UI.new( :logger => @logger, :verbose => global[:verbose], :quiet => global[:quiet] ) @ui.logger.debug { "global(#{global.inspect})" } @ui.logger.debug { "options(#{options.inspect})" } @ui.logger.debug { "args(#{args.inspect})" } @testlab = TestLab.new( :ui => @ui, :labfile_path => global[:labfile], :config_dir => global[:config], :repo_dir => global[:repo] ) TestLab::Utility.log_header(@testlab).each { |line| @logger.info { line } } @testlab.boot if !@ui.quiet? message = format_message("TestLab v#{TestLab::VERSION} Loaded".black.bold) @testlab.ui.stdout.puts(message) @testlab.ui.logger.info { message } end true end post do |global,command,options,args| testlab_run_time = (Time.now.utc - @testlab_start_time) message = format_message("TestLab v#{TestLab::VERSION} Finished (%0.4f seconds)".black.bold % testlab_run_time) @testlab.ui.stdout.puts(message) @testlab.ui.logger.info { message } true end on_error do |exception| testlab_run_time = (Time.now.utc - @testlab_start_time) @ui.stderr.puts @ui.stderr.puts(format_message(["ERROR:".red, exception.inspect.red.bold].join(' '))) case exception when GLI::BadCommandLine, GLI::UnknownCommand, GLI::UnknownCommandArgument, GLI::UnknownGlobalArgument then command_regex = /Command '([\w]+)' / command = exception.message.scan(command_regex).flatten.first @ui.stderr.puts commands[:help] and commands[:help].execute({}, {}, (command.nil? ? [] : [command.to_s])) false else @logger.fatal { exception.inspect } exception.backtrace.each do |line| @logger.logdev.write("#{line}\n") end message = format_message("TestLab v#{TestLab::VERSION} Aborted (%0.4f seconds)".black.bold % testlab_run_time) @testlab.ui.stderr.puts(message) @testlab.ui.logger.info { message } false end end exit run(ARGV)