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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/xlogin/session.rb', line 73

def close
  @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



101
102
103
104
105
106
107
# File 'lib/xlogin/session.rb', line 101

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

#duplicateObject



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

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

#enable_log(log = $stdout) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/xlogin/session.rb', line 93

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'   => Regexp.union(*@template.prompt.map(&:first)),
      'FailEOF'  => true,
    )
  rescue => e
    retry if (max_retry -= 1) > 0
    raise e
  end
end

#log(text) ⇒ Object



89
90
91
# File 'lib/xlogin/session.rb', line 89

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

#puts(*args, &block) ⇒ Object



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

def puts(*args, &block)
  args = [instance_exec(*args, &@template.interrupt!)].flatten.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



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

def waitfor(*args, &block)
  args = [Regexp.union(*@template.prompt.map(&:first))] if args.empty?
  @mutex.synchronize { _waitfor(*args, &block) }
end