class Expectacle::Thrower

Thrower logic(command list operation)

Thrower logic(command list operation)

Thrower logic(command list operation)

Public Instance Methods

preview_parameter(hosts, commands) click to toggle source

Preview all parameters for all hosts. @param [Array<Hash>] hosts Host parameters (read from host list file). @param [Array<String>] commands Commands (read from command list file).

# File lib/expectacle/thrower_preview.rb, line 11
def preview_parameter(hosts, commands)
  print YAML.dump(previewed_data(hosts, commands))
end
previewed_data(hosts, commands) click to toggle source

Preview all parameters for all hosts (for testing) @param [Array<Hash>] hosts Host parameters (read from host list file). @param [Array<String>] commands Commands (read from command list file).

# File lib/expectacle/thrower_preview.rb, line 18
def previewed_data(hosts, commands)
  @commands = commands
  hosts.map do |each|
    @host_param = each
    preview_command_for_host
  end
end
run_command_for_all_hosts(hosts, commands) click to toggle source

Run(exec) commands for all hosts. @param [Array<Hash>] hosts Host parameters (read from host list file). @param [Array<String>] commands Commands (read from command list file).

# File lib/expectacle/thrower.rb, line 15
def run_command_for_all_hosts(hosts, commands)
  hosts.each do |each|
    @commands = commands.dup # Notice: @commands will be decremented.
    @commands_len = @commands.length
    @auth_count = 0
    @host_param = each
    run_command_for_host
  end
end

Private Instance Methods

before_run_command() click to toggle source
# File lib/expectacle/thrower.rb, line 36
def before_run_command
  return unless @local_serial
  # for `cu` command
  @reader.expect(/^Connected\./, 1) do
    write_and_logging 'Send enter to connect serial', "\r\n", true
  end
end
check_auth_count() click to toggle source
# File lib/expectacle/thrower_utils.rb, line 10
def check_auth_count
  if @commands.length == @commands_len
    @auth_count += 1
  else
    @auth_count = 0
    @commands_len = @commands.length
  end
  return unless @auth_count > MAX_AUTH_COUNT
  @logger.error "Close: Too many auth retries (#{@auth_count} times)"
  @commands = []
  close_session
end
clear_auth_count() click to toggle source
# File lib/expectacle/thrower_utils.rb, line 23
def clear_auth_count
  @auth_count = 0
end
close_session() click to toggle source
# File lib/expectacle/thrower_utils.rb, line 27
def close_session
  write_and_logging 'Send break: ', 'exit'
  return unless @local_serial
  @logger.info 'Close IO for spawn command'
  @reader.close
  @writer.close
end
exec_by_mode(prompt) click to toggle source
# File lib/expectacle/thrower.rb, line 74
def exec_by_mode(prompt)
  case prompt
  when /#{@prompt[:command2]}/
    exec_in_privilege_mode
  when /#{@prompt[:command1]}/
    exec_in_normal_mode
  end
end
exec_by_sub_prompt(prompt) click to toggle source
# File lib/expectacle/thrower.rb, line 83
def exec_by_sub_prompt(prompt)
  case prompt
  when /#{@prompt[:yn]}/
    # it must match before sub_prompt
    write_and_logging 'Send yes: ', 'yes'
  when /#{@prompt[:sub1]}/, /#{@prompt[:sub2]}/
    write_and_logging 'Send return: ', ''
  end
end
exec_each_prompt(prompt) click to toggle source
# File lib/expectacle/thrower.rb, line 93
def exec_each_prompt(prompt)
  check_auth_count
  case prompt
  when /#{@prompt[:password]}/, /#{@prompt[:enable_password]}/
    write_and_logging 'Send password', embed_password, true
  when /#{@prompt[:username]}/
    write_and_logging 'Send username: ', embed_user_name
  when /#{@prompt[:command2]}/, /#{@prompt[:command1]}/
    exec_by_mode(prompt)
  when /#{@prompt[:yn]}/, /#{@prompt[:sub1]}/, /#{@prompt[:sub2]}/
    exec_by_sub_prompt(prompt)
  else
    @logger.error "Unknown prompt #{prompt}"
  end
end
exec_in_normal_mode() click to toggle source
# File lib/expectacle/thrower.rb, line 67
def exec_in_normal_mode
  exec_rest_commands do
    write_and_logging 'Send enable command: ', @prompt[:enable_command]
    @enable_mode = true
  end
end
exec_in_privilege_mode() click to toggle source
# File lib/expectacle/thrower.rb, line 59
def exec_in_privilege_mode
  exec_rest_commands do
    # Notice: @commands changed
    command = @commands.shift
    write_and_logging 'Send command: ', embed_command(command)
  end
end
exec_rest_commands() { || ... } click to toggle source
# File lib/expectacle/thrower.rb, line 51
def exec_rest_commands
  if !@commands.empty?
    yield
  else
    close_session
  end
end
preview_command_for_host() click to toggle source
# File lib/expectacle/thrower_preview.rb, line 28
def preview_command_for_host
  ready_to_open_host_session do |spawn_cmd|
    whole_previewed_parameters(spawn_cmd)
  end
end
previewed_commands() click to toggle source
# File lib/expectacle/thrower_preview.rb, line 47
def previewed_commands
  @commands.map { |cmd| embed_command(cmd) }
end
previewed_host_param() click to toggle source
# File lib/expectacle/thrower_preview.rb, line 34
def previewed_host_param
  host_param = @host_param.dup
  enable_mode = @enable_mode
  @enable_mode = false
  host_param[:username] = embed_user_name
  host_param[:password] = embed_password
  host_param[:ipaddr] = embed_ipaddr
  @enable_mode = true
  host_param[:enable] = embed_password
  @enable_mode = enable_mode
  host_param
end
run_command() click to toggle source
# File lib/expectacle/thrower.rb, line 44
def run_command
  do_on_interactive_process do |match|
    @logger.debug "Read: #{match}"
    exec_each_prompt match[1]
  end
end
run_command_for_host() click to toggle source
# File lib/expectacle/thrower.rb, line 27
def run_command_for_host
  ready_to_open_host_session do |spawn_cmd|
    open_interactive_process(spawn_cmd) do
      before_run_command
      run_command
    end
  end
end
whole_previewed_parameters(spawn_cmd) click to toggle source
# File lib/expectacle/thrower_preview.rb, line 51
def whole_previewed_parameters(spawn_cmd)
  {
    spawn_cmd: spawn_cmd,
    prompt: @prompt,
    host: previewed_host_param,
    commands: previewed_commands
  }
end