Module: Xlogin::SessionModule

Included in:
Ssh, Telnet
Defined in:
lib/xlogin/spec.rb,
lib/xlogin/session.rb,
lib/xlogin/rake_task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/xlogin/session.rb', line 12

def name
  @name
end

Instance Method Details

#closeObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/xlogin/session.rb', line 77

def close
  logout if respond_to?(:logout)
  @mutex.synchronize do
    @loggers.each do |_, logger|
      next if logger.nil? || [$stdout, $stderr].include?(logger)
      logger.close
    end

    if @gateway
      @gateway.close(@port)
      @gateway.shutdown!
    end

    super
  end
end

#disable_log(log = $stdout) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/xlogin/session.rb', line 106

def disable_log(log = $stdout)
  @loggers.delete(log)
  if block_given?
    yield
    enable_log(log)
  end
end

#duplicateObject



63
64
65
# File 'lib/xlogin/session.rb', line 63

def duplicate
  @template.build(@uri, **@config.to_h)
end

#enable_log(log = $stdout) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/xlogin/session.rb', line 98

def enable_log(log = $stdout)
  @loggers.update(log => build_logger(log))
  if block_given?
    yield
    disable_log(log)
  end
end

#expect(*args) ⇒ Object Also known as: exp



45
46
47
# File 'lib/xlogin/spec.rb', line 45

def expect(*args)
  Expectation.new(self, *args)
end

#initialize(template, uri, **opts) ⇒ Object



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
# File 'lib/xlogin/session.rb', line 14

def initialize(template, uri, **opts)
  @uri  = uri
  @host = uri.host
  @port = uri.port
  @port ||= case @uri.scheme
            when 'ssh'    then 22
            when 'telnet' then 23
            end

  @name     = opts[:name] || @host
  @config   = OpenStruct.new(opts)
  @template = template
  @username, @password = uri.userinfo.to_s.split(':')

  ssh_tunnel(@config.via) if @config.via
  max_retry = @config.retry || 1

  @mutex   = Mutex.new
  @loggers = [@config.log].flatten.uniq.reduce({}) { |a, e| a.merge(e => build_logger(e)) }

  begin
    super(
      'Host'     => @host,
      'Port'     => @port,
      'Username' => @username,
      'Password' => @password,
      'Timeout'  => @config.timeout || @template.timeout || false,
      'Prompt'   => prompt_pattern,
      'FailEOF'  => true,
    )
  rescue => e
    retry if (max_retry -= 1) > 0
    raise e
  end
end

#log(text) ⇒ Object



94
95
96
# File 'lib/xlogin/session.rb', line 94

def log(text)
  @loggers.each { |_, logger| logger.syswrite(text) if logger }
end

#msg(text, prefix: "[INFO]", chomp: false, **color) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/xlogin/rake_task.rb', line 114

def msg(text, prefix: "[INFO]", chomp: false, **color)
  default_color = { color: :green }

  log("\n")
  log(Time.now.iso8601.colorize(**color) + ' ') if !Rake.application.options.always_multitask

  log("#{prefix} #{text}".colorize(**default_color.merge(color)))
  cmd('') unless chomp
end

#promptObject



54
55
56
57
# File 'lib/xlogin/session.rb', line 54

def prompt
  text = cmd('').to_s.lines.last
  text.chomp if text
end

#prompt_patternObject



59
60
61
# File 'lib/xlogin/session.rb', line 59

def prompt_pattern
  Regexp.union(*@template.prompt.map(&:first))
end

#puts(*args, &block) ⇒ Object



67
68
69
70
# File 'lib/xlogin/session.rb', line 67

def puts(*args, &block)
  args = args.flat_map { |arg| instance_exec(arg, &@template.interrupt!) }.compact if @template.interrupt!
  args.empty? ? super('', &block) : super(*args, &block)
end

#typeObject



50
51
52
# File 'lib/xlogin/session.rb', line 50

def type
  @template.name
end

#waitfor(*args, &block) ⇒ Object



72
73
74
75
# File 'lib/xlogin/session.rb', line 72

def waitfor(*args, &block)
  args = [prompt_pattern] if args.empty?
  @mutex.synchronize { _waitfor(*args, &block) }
end