#!/usr/bin/env ruby require 'gli' require 'testlab' include GLI::App program_desc %(TestLab #{TestLab::VERSION} - A framework for building lightweight virtual infrastructure using LXC) version Testlab::VERSION # desc 'Describe some switch here' # switch [:s,:switch] # desc 'Describe some flag here' # default_value 'the default' # arg_name 'The name of the argument' # flag [:f,:flagname] # desc 'Manage the test lab' # arg_name 'Describe arguments to lab here' # command :lab do |c| # # c.desc 'Describe a switch to lab' # # c.switch :s # # c.desc 'Describe a flag to lab' # # c.default_value 'default' # # c.flag :f # # c.action do |global_options,options,args| # # # Your command logic here # # # If you have any errors, just raise them # # # raise "that command made no sense" # # puts "lab command ran" # # end # end desc 'Create the test lab' command :create do |create| create.action do |global_options,options,args| @testlab.create end end desc 'Destroy the test lab' command :destroy do |destroy| destroy.action do |global_options,options,args| @testlab.destroy end end desc 'Online the test lab' command :up do |up| up.action do |global_options,options,args| @testlab.up end end desc 'Offline the test lab' command :down do |down| down.action do |global_options,options,args| @testlab.down end end desc 'Setup the test lab infrastructure' command :setup do |setup| setup.action do |global_options,options,args| @testlab.setup end end desc 'Teardown the test lab infrastructure' command :teardown do |teardown| teardown.action do |global_options,options,args| @testlab.teardown end end desc 'Display information on the status of the test lab' command :status do |status| status.action do |global_options,options,args| @testlab.status end end desc 'Manage nodes' arg_name 'Describe arguments to node here' command :node do |c| c.desc 'Node ID' c.flag [:i, :id] c.desc 'Open an SSH console to a node' c.command :ssh do |ssh| ssh.action do |global_options,options,args| help_now!('id is required') if options[:id].nil? node = @testlab.nodes.select{ |n| n.id.to_sym == options[:id].to_sym }.first node.ssh.console end end end desc 'Manage networks' arg_name 'Describe arguments to network here' command :network do |c| c.action do |global_options,options,args| puts "network command ran" end end desc 'Manage containers' arg_name 'Describe arguments to container here' command :container do |c| c.desc 'Container ID' c.flag [:i, :id] c.desc 'Open an SSH console to a container' c.command :ssh do |ssh| ssh.action do |global_options,options,args| help_now!('id is required') if options[:id].nil? container = @testlab.containers.select{ |n| n.id.to_sym == options[:id].to_sym }.first container.ssh.console end end end desc 'Manage routes' command :route do |c| c.desc 'Add routes to lab networks' c.command :add do |add| add.action do |global_options,options,args| @testlab.nodes.each do |node| node.route_setup(:add) @testlab.ui.stdout.puts("Added routes successfully!".green.bold) @testlab.ui.stdout.puts %x(netstat -nr | grep '#{node.ip}').strip end end end c.desc 'Delete routes to lab networks' c.command :del do |del| del.action do |global_options,options,args| @testlab.nodes.each do |node| node.route_setup(:del) @testlab.ui.stdout.puts("Deleted routes successfully!".red.bold) @testlab.ui.stdout.puts %x(netstat -nr | grep '#{node.ip}').strip end end end end pre do |global,command,options,args| # Pre logic here # Return true to proceed; false to abort and not call the # chosen command # Use skips_pre before a command to skip this block # on that command only log_file = File.join(Dir.pwd, "testlab.log") @logger = ZTK::Logger.new(log_file) @ui = ZTK::UI.new(:logger => @logger) @testlab = TestLab.new(:ui => @ui) message = TestLab::Utility.format_message("TestLab v#{TestLab::VERSION} Loaded".black.bold) @testlab.ui.stdout.puts(message) true end post do |global,command,options,args| # Post logic here # Use skips_post before a command to skip this # block on that command only end on_error do |exception| # Error logic here # return false to skip default error handling @ui.stderr.puts(["EXCEPTION:".red.bold, exception.inspect.red].join(' ')) @logger.fatal { exception.inspect } exception.backtrace.each do |line| @logger.logdev.write("#{line}\n") end false end exit run(ARGV)