#!/usr/bin/env ruby require 'rubygems' require 'fileutils' require 'instalatron' require 'yaml' require 'mixlib/cli' def record_session(vm_name, session_name = "instalatron_rec_" + Time.now.strftime("%F_%H%M")) puts "Recording session #{session_name}\n\n" script = [] $stdout.sync = true screen_count = 1 Dir.mkdir session_name if not File.directory?(session_name) begin print "Press ENTER to grab screen..." $stdin.gets loop do step = {} img = Instalatron.detect_screen(vm_name) # Get step name puts "Screen captured\n\n" print "Step name: " step[:name] = $stdin.gets.strip.chomp # Capture key sequence print "Key sequence: " step[:sequence] = $stdin.gets.strip.chomp if step[:sequence].empty? step[:sequence] = "" end # Copy screenshot to session dir step[:image] = "#{step[:name].gsub(' ','_').downcase}.png" FileUtils.cp img, "#{session_name}/#{step[:image]}" script << step puts Instalatron.command_window(step[:sequence], vm_name) screen_count += 1 print "Press ENTER to grab screen (#{screen_count})..." $stdin.gets end rescue SystemExit, Interrupt puts "Aborting..." File.open "#{session_name}/script.yml", 'w' do |f| f.puts script.to_yaml end end end class MyCLI include Mixlib::CLI option :vm_name, :short => "-n NAME", :long => "--vm-name NAME", :description => "Virtual Machine Name", :default => "instalatron_#{Time.now.strftime('%s')}" option :iso_file, :short => "-i ISO", :long => "--iso-file ISO", :description => "ISO file to boot the VM with" option :destroy_vm, :long => "--destroy-vm", :description => "Destroy the VM after running the tests" option :vm_memory, :long => "--vm-memory MEM", :description => "VM Memory", :default => 512 option :version, :long => "--version", :short => "-v", :description => "Instalatron Version", :proc => Proc.new { puts Instalatron::VERSION; exit 0 } option :help, :short => "-h", :long => "--help", :description => "Show this message", :on => :tail, :boolean => true, :show_options => true, :exit => 0 end def usage(cli) $stderr.puts cli.opt_parser.help exit 1 end def required_option(cli, opt) if cli.config[opt].nil? $stderr.puts "\n#{opt.to_s} argument requied.\n\n" $stderr.puts cli.opt_parser.help exit 1 end return cli.config[opt] end cli = MyCLI.new cli.parse_options vm_name = cli.config[:vm_name] iso_file = required_option(cli, :iso_file) if not File.exist?(iso_file) $stderr.puts "Invalid ISO file.\n\n" usage(cli) end session_name = "instalatron_rec_" + Time.now.strftime("%F_%H%M") # Create VBox VM first $stdout.puts "Creating VM #{vm_name}..." Instalatron.create_vm :vm_memory => cli.config[:vm_memory], :vm_name => vm_name, :iso_file => iso_file record_session vm_name, session_name