Top Level Namespace

Defined Under Namespace

Modules: Instalatron Classes: MyCLI

Constant Summary collapse

Log =
Logger.new($stdout)

Instance Method Summary collapse

Instance Method Details

#capture_sequence(vm_name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'bin/instalatron-record', line 39

def capture_sequence(vm_name)
  sequence = ""
  loop do  
    b = read_char 
    capture = b
    if b.chomp.empty?
      capture = "<enter>"
    elsif b == "\t"
      capture = "<tab>"
    elsif b == " "
      capture = "<spacebar>"
    elsif b == "$"
      break
    #
    # to print special character strings to stdout:
    # puts "%.3d" % read_char
    #
    elsif b == "\e[C"
      capture = "<Right>"
    elsif b == "\e[D"
      capture = "<Left>"
    elsif b == "\e[B"
      capture = "<Down>"
    elsif b == "\e[A"
      capture = "<Up>"
    else
      capture = b
    end
    Instalatron.command_window(capture, vm_name)
    print capture
    sequence << capture
  end
  sequence
end

#monitor_session(vm_name, script) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'bin/instalatron-monitor', line 6

def monitor_session(vm_name, script)
  ctrlc_gap = 0
  basedir = File.dirname(script)
  script = YAML.load_file(script)
  require 'pp'
  script.each do |screen|
    ref_img = "#{basedir}/#{File.basename(screen[:image])}"
    print "Waiting for '#{screen[:name]}'... ".ljust(45)
    loop do 
      begin
      img = Instalatron.detect_screen(vm_name) 
      if Instalatron.same_image?(ref_img, img)
        puts "DETECTED"
        break
      end
      rescue Interrupt, SystemExit
        if Time.now.to_f - ctrlc_gap < 0.5
          puts "\n\nDouble Ctrl-c detected. Aborting."
          exit
        else
          ctrlc_gap = Time.now.to_f
        end
        puts "SKIP"
        break
      end
    end
  end
end

#play_session(vm_name, script, custom_seq = nil, key_press_delay = 0) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'bin/instalatron-play', line 9

def play_session(vm_name, script, custom_seq = nil, key_press_delay = 0)
  ctrlc_gap = 0
  basedir = File.dirname(script)
  if File.exist?(basedir + '/config.rb')
    include Instalatron
    begin
      require basedir + '/config.rb'
    rescue Exception => e
      raise Exception.new("Error loading config.rb file: #{e.message}")
    end

    begin
      tmpl = ERB.new(File.read(script))
      script = YAML.load(tmpl.result)
    rescue Exception => e
      raise Exception.new("Error rendering ERB template from script.yml: #{e.message}")
    end

  else
    script = YAML.load_file(script)
  end
  if custom_seq
    new_seq = script[0]
    new_seq[:sequence] = custom_seq
    script[0] = new_seq
  end
  step = 1
  script.each do |screen|
    ref_img = "#{basedir}/#{File.basename(screen[:image])}"
    loop do 
      begin
      img = Instalatron.detect_screen(vm_name) 
      if Instalatron.same_image?(ref_img, img)
        puts "Screen detected: #{screen[:name]}"
        Instalatron.command_window screen[:sequence], vm_name, key_press_delay
        break
      end
      rescue Interrupt, SystemExit
        if Time.now.to_f - ctrlc_gap < 0.5
          puts "\n\nDouble Ctrl-c detected. Aborting."
          return
        else
          ctrlc_gap = Time.now.to_f
        end
        puts "Skipping #{screen[:name]}"
        break
      end
    end
    step += 1
  end
end

#read_charObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'bin/instalatron-record', line 11

def read_char
  begin
    # save previous state of stty
    old_state = `stty -g`
    # disable echoing and enable raw (not having to press enter)
    system "stty raw -echo"
    c = STDIN.getc.chr
    # gather next two characters of special keys
    if(c=="\e")
      extra_thread = Thread.new{
        c = c + STDIN.getc.chr
        c = c + STDIN.getc.chr
      }
      # wait just long enough for special keys to get swallowed
      extra_thread.join(0.00001)
      # kill thread so not-so-long special keys don't wait on getc
      extra_thread.kill
    end
  rescue => ex
    puts "#{ex.class}: #{ex.message}"
    puts ex.backtrace
  ensure
    # restore previous state of stty
    system "stty #{old_state}"
  end
  return c
end

#record_session(vm_name, session_name = "instalatron_rec_" + Time.now.strftime("%F_%H%M")) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'bin/instalatron-record', line 74

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 (press $ to end): "
      step[:sequence] = capture_sequence(vm_name)
      
      # Copy screenshot to session dir
      step[:image] = "#{step[:name].gsub(' ','_').downcase}.png"
      FileUtils.cp img, "#{session_name}/#{step[:image]}"

      script << step
      puts 
      puts
      screen_count += 1
      print "Press ENTER to grab screen (#{screen_count})..."
      $stdin.gets
    end
  rescue SystemExit, Interrupt
    puts "\nAborting..."
    File.open "#{session_name}/script.yml", 'w' do |f|
      f.puts script.to_yaml
    end
  end
end

#required_option(cli, opt) ⇒ Object



61
62
63
64
65
66
67
68
# File 'bin/instalatron-play', line 61

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

#usageObject



70
71
72
73
# File 'bin/instalatron-play', line 70

def usage(cli)
  $stderr.puts cli.opt_parser.help
  exit 1
end