xlogin 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 100ba79a46574b64637b2dff6480b75353acc21d
4
- data.tar.gz: a9aa7094fed1a2a3d6b7515e4cfcb5bf94f211fb
3
+ metadata.gz: 2c07f2ff5ac2cd13434e68ad9c2f70354981a3a1
4
+ data.tar.gz: af17f5d674b0643f823edfb0e9eff31bc13531b4
5
5
  SHA512:
6
- metadata.gz: 80ba1c2c142dd80fe5c8291fe95baf927d063ee6089489957b6c57011851074de96f3cac6e5fa1f1bdf1fc3a6d4df958c54c29d345689f6239301542d61498f5
7
- data.tar.gz: 6deea9b37a0d41d3c2794be95a93d0b6a484f6495887d86b0b67b85b05dc8dcfc2a98e6109c19d10bcf1845a4cd05945c912c02d3516b19cc967462580143c7d
6
+ metadata.gz: c971df162507902afa1217092ecc92203960d392156daeafea936c3a0bb2faaf1594dcd67b7237abed1b8847ee74ba5a2b3a7a34e2b24ff7737cf981fad58aec
7
+ data.tar.gz: 57018f0aa2bcad19a8efb85c6865c128f00709328dcd75f09588302cb0f0b3aa06556c269a9a3df8b04a4070b9996c2e3c6ee4e2d64c2c2d466f99622705de0d
@@ -5,11 +5,14 @@ module Xlogin
5
5
 
6
6
  module FirmwareDelegator
7
7
  def run(uri, opts = {})
8
+ uri = URI(uri.to_s)
9
+
8
10
  if hostname = opts.delete(:delegate)
9
- delegatee = FirmwareFactory.new.find(hostname)
10
- firmware = FirmwareFactory[delegatee[:type]]
11
+ target = FirmwareFactory.new.get(hostname)
12
+ target_os = FirmwareFactory[target[:type]]
13
+ target_uri = URI(target[:uri])
11
14
 
12
- firmware.on_exec do |args|
15
+ target_os.on_exec do |args|
13
16
  if args['String'].strip =~ /^kill-session(?:\(([\s\w]+)\))?$/
14
17
  puts($1.to_s)
15
18
  close
@@ -19,12 +22,15 @@ module Xlogin
19
22
  end
20
23
  end
21
24
 
22
- login_method = firmware.instance_eval { @methods[:login] }
23
- firmware.bind(:login, &@methods[:login])
24
- firmware.bind(:delegate, &@methods[:delegate])
25
+ login = @methods[:login]
26
+ delegate = @methods[:delegate]
27
+
28
+ userinfo = uri.userinfo.dup
29
+ uri.userinfo = ''
25
30
 
26
- session = firmware.run(uri, opts.merge(delegatee[:opts]))
27
- session.delegate(URI(delegatee[:uri]), &login_method)
31
+ session = target_os.run(uri, opts)
32
+ session.instance_exec(*userinfo.split(':'), &login)
33
+ session.instance_exec(target_uri, opts, &delegate)
28
34
  session
29
35
  else
30
36
  session = super(uri, opts)
@@ -34,5 +40,32 @@ module Xlogin
34
40
 
35
41
  prepend FirmwareDelegator
36
42
 
43
+ ### Usage:
44
+ ## Write xloginrc file
45
+ #
46
+ # vyos 'vyos01', 'telnet://user:pass@host:port'
47
+ # consolesrv 'vyos01::console', 'telnet://console_user:console_pass@console_host:console_port', delegate: 'vyos01'
48
+ #
49
+ ## Write firmware definition
50
+ #
51
+ # require 'timeout'
52
+ # Xlogin.configure :consolesrv do |os|
53
+ # os.bind(:login) do |*args|
54
+ # username, password = *args
55
+ # waitfor(/login:\s*\z/) && puts(username)
56
+ # waitfor(/Password:\s*\z/) && puts(password)
57
+ # end
58
+ #
59
+ # os.bind(:delegate) do |uri, opts|
60
+ # begin
61
+ # waittime = 3
62
+ # Timeout.timeout(waittime) do
63
+ # login(*uri.userinfo.split(':'), opts)
64
+ # end
65
+ # rescue Timeout::Error
66
+ # end
67
+ # end
68
+ #end
69
+
37
70
  end
38
71
  end
@@ -58,9 +58,8 @@ module Xlogin
58
58
  define_method(open) do |arg = password, &block|
59
59
  send("original_#{open}", arg)
60
60
  if block
61
- resp = block.call
61
+ block.call
62
62
  cmd(close)
63
- resp
64
63
  end
65
64
  end
66
65
  alias_method :enable, open
@@ -74,7 +73,6 @@ module Xlogin
74
73
  userinfo: uri.userinfo,
75
74
  timeout: @timeout,
76
75
  prompts: @prompts,
77
- methods: @methods,
78
76
  }.merge(opts)
79
77
  )
80
78
  end
@@ -47,7 +47,11 @@ module Xlogin
47
47
  end
48
48
 
49
49
  def set(type, name, uri, opts = {})
50
- @database[name] = { type: type, uri: uri, opts: opts }
50
+ @database[name] = { name: name, type: type, uri: uri, opts: opts }
51
+ end
52
+
53
+ def get(name)
54
+ @database[name]
51
55
  end
52
56
 
53
57
  def list
@@ -62,7 +66,10 @@ module Xlogin
62
66
 
63
67
  opts = item[:opts] || {}
64
68
  opts = opts.merge(args).reduce({}) { |a, (k, v)| a.merge(k.to_s.downcase.to_sym => v) }
65
- firmware.run(item_uri, opts)
69
+
70
+ session = firmware.run(item_uri, opts)
71
+ session.name = item[:name]
72
+ session
66
73
  end
67
74
 
68
75
  def method_missing(name, *args, &block)
@@ -1,7 +1,8 @@
1
1
  Xlogin.configure :vyos do |os|
2
2
  os.prompt(/[$#] (?:\e\[K)?\z/n)
3
3
 
4
- os.bind(:login) do |username, password|
4
+ os.bind(:login) do |*args|
5
+ username, password = *args
5
6
  waitfor(/login:\s/) && puts(username)
6
7
  waitfor(/Password:\s/) && puts(password)
7
8
  waitfor
@@ -4,9 +4,9 @@ require 'net/ssh/gateway'
4
4
  module Xlogin
5
5
  module Session
6
6
 
7
- alias_method :original_configure, :configure
8
- def configure(**opts)
9
- original_configure(**opts)
7
+ alias_method :original_configure_session, :configure_session
8
+ def configure_session(**opts)
9
+ original_configure_session(**opts)
10
10
 
11
11
  if uri = opts[:via]
12
12
  gateway = URI(uri)
@@ -59,7 +59,7 @@ module Xlogin
59
59
  def initialize(name, *args)
60
60
  @name = name
61
61
  @fail_on_error = true
62
- @silent = false
62
+ @silent = Rake.application.options.silent
63
63
  @lockfile = nil
64
64
  @logfile = nil
65
65
  @timeout = nil
@@ -76,11 +76,13 @@ module Xlogin
76
76
  @taskrunner = block
77
77
  end
78
78
 
79
- def safe_puts(*messages, io: $stdout)
79
+ def safe_puts(*messages, **opts)
80
+ opts[:io] ||= $stdout
81
+ return if @silent && !opts[:force]
80
82
  RakeTask.mutex.synchronize do
81
83
  messages.flat_map { |message| message.to_s.lines }.each do |line|
82
84
  line.gsub!("\r", "")
83
- io.puts (Rake.application.options.always_multitask)? "#{name}\t#{line}" : line
85
+ opts[:io].puts (Rake.application.options.always_multitask)? "#{name}\t#{line}" : line
84
86
  end
85
87
  end
86
88
  end
@@ -120,7 +122,7 @@ module Xlogin
120
122
 
121
123
  def run_task
122
124
  loggers = []
123
- loggers << $stdout unless silent || Rake.application.options.silent || Rake.application.options.always_multitask
125
+ loggers << $stdout unless silent || Rake.application.options.always_multitask
124
126
 
125
127
  if logfile
126
128
  mkdir_p(File.dirname(logfile), verbose: Rake.application.options.trace)
@@ -131,32 +133,31 @@ module Xlogin
131
133
  xlogin_opts[:log] = loggers unless loggers.empty?
132
134
  xlogin_opts[:timeout] = timeout if timeout
133
135
 
134
- @session = Xlogin.get(name, xlogin_opts)
135
- @session.extend(SessionExt)
136
+ begin
137
+ @session = Xlogin.get(name, xlogin_opts)
138
+ @session.extend(SessionExt)
136
139
 
137
- %i( safe_puts fail_on_error ).each do |name|
138
- method_proc = method(name)
139
- @session.define_singleton_method(name) { |*args| method_proc.call(*args) }
140
- end
140
+ method_proc = method(:safe_puts)
141
+ @session.define_singleton_method(:safe_puts) { |*args| method_proc.call(*args) }
141
142
 
142
- @taskrunner.call(@session) if @taskrunner
143
+ @taskrunner.call(@session) if @taskrunner && @session
144
+ rescue => e
145
+ raise e if fail_on_error
146
+ safe_puts(e, io: $stderr, force: true)
147
+ end
143
148
  end
144
149
 
145
150
  module SessionExt
146
151
  def cmd(*args)
147
- super(*args).tap do |message|
148
- safe_puts(message, io: $stdout) unless Rake.application.options.silent || !Rake.application.options.always_multitask
149
- break yield(message) if block_given?
150
- end
151
- rescue => e
152
- raise e if fail_on_error
153
- safe_puts("\n#{e}", io: $stderr)
154
- cmd('')
152
+ message = super(*args)
153
+ safe_puts(message, io: $stdout) if Rake.application.options.always_multitask
154
+ message = yield(message) if block_given?
155
+ message
155
156
  end
156
157
 
157
158
  def readline(command)
158
159
  prompt = StringIO.new
159
- safe_puts(cmd('').lines.last, io: prompt)
160
+ safe_puts(cmd('').lines.last, io: prompt, force: true)
160
161
 
161
162
  my_command = RakeTask.mutex.synchronize do
162
163
  Readline.pre_input_hook = lambda do
@@ -178,6 +179,10 @@ module Xlogin
178
179
  readline(command)
179
180
  end
180
181
  end
182
+
183
+ def sync(&block)
184
+ RakeTask.mutex.synchronize(&block) if block
185
+ end
181
186
  end
182
187
 
183
188
  end
@@ -3,16 +3,15 @@ require 'stringio'
3
3
  module Xlogin
4
4
  module Session
5
5
 
6
- attr_reader :name
6
+ attr_accessor :name
7
7
 
8
- def configure(**opts)
8
+ def configure_session(**opts)
9
9
  @name = opts[:node]
10
10
  @node = opts[:node]
11
11
  @port = opts[:port]
12
- @userinfo = opts[:userinfo].split(':')
12
+ @userinfo = opts[:userinfo].to_s.split(':')
13
13
  raise Xlogin::GeneralError.new('Argument error.') unless @node && @port
14
14
 
15
- @methods = opts[:methods] || {}
16
15
  @prompts = opts[:prompts] || [[/[$%#>] ?\z/n, nil]]
17
16
  @timeout = opts[:timeout] || 60
18
17
 
data/lib/xlogin/ssh.rb CHANGED
@@ -6,7 +6,7 @@ module Xlogin
6
6
  include Session
7
7
 
8
8
  def initialize(**opts)
9
- configure(opts.merge(port: opts[:port] || 22))
9
+ configure_session(opts.merge(port: opts[:port] || 22))
10
10
 
11
11
  begin
12
12
  username, password = *@userinfo
data/lib/xlogin/telnet.rb CHANGED
@@ -8,7 +8,7 @@ module Xlogin
8
8
  include Session
9
9
 
10
10
  def initialize(**opts)
11
- configure(opts.merge(port: opts[:port] || 23))
11
+ configure_session(opts.merge(port: opts[:port] || 23))
12
12
 
13
13
  super(
14
14
  'Host' => @node,
@@ -1,3 +1,3 @@
1
1
  module Xlogin
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xlogin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-21 00:00:00.000000000 Z
11
+ date: 2017-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler