kanrisuru 0.7.3 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8235592e090d7f6466a0e78d0d7b4eff7d31a2c47649aadd7cf822df11847642
4
- data.tar.gz: 20c8730f24d486687c1161f0da1dc330bf93ec7f273db14c258cd0a83ce766e8
3
+ metadata.gz: 4f5fe692ff53ac4de87ca38181765a63cf88f0beab4e44145c780ba9af934ede
4
+ data.tar.gz: 58acc02d34aff916670ab26c9659f7e40205d31492ec5f043ea51487df33cb62
5
5
  SHA512:
6
- metadata.gz: f8d141bf32e49bd47acb9adc6d4307264be9b6ee30f83a581f2c033bb662205610d6cdbd495f56d195a91df8bcd5501a4f72bdf681db20bcaf5f5d23527e244a
7
- data.tar.gz: 0a9b31637b3a15cd4928abbcb7e2edcfd925ec9c9f61b008a44e7ef80c39b083b1f16b103281c8b6ed81ea758144dc2ea3eb5b50223a8dbce121fcc868abf453
6
+ metadata.gz: e2b8134ef2351a86f88286a7ed50a1f768510c75f65ecbf9d9684f376606b46ea023f1b78723ae550183c7606e230d7152614d263b19f1a31c6bf6fe0cfa2cc3
7
+ data.tar.gz: f7382ab716a20185ec071f96b6793afa7fcc1e11d32a0dd3174455cb7a5ba19d3fb61aa0fbb7d5574f26af36d983b3fae4ee9ec5757f16ef2a21edc43d5d83cd
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## Kanrisuru 0.8.0 (August 18, 2021) ##
2
+ * Add last / lastb implementation in system core module.
3
+
1
4
  ## Kanrisuru 0.7.3 (August 9, 2021) ##
2
5
  * Fixed bug with zypper remove package, where the package names weren't being added to the linux command.
3
6
  * Test case added to ensure package is removed.
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'date'
4
+ require 'ipaddr'
4
5
 
5
6
  module Kanrisuru
6
7
  module Core
@@ -19,6 +20,7 @@ module Kanrisuru
19
20
  os_define :linux, :kstat
20
21
 
21
22
  os_define :linux, :lsof
23
+ os_define :linux, :last
22
24
 
23
25
  os_define :linux, :uptime
24
26
 
@@ -116,6 +118,19 @@ module Kanrisuru
116
118
  :name
117
119
  )
118
120
 
121
+ SessionDetail = Struct.new(
122
+ :tty,
123
+ :login_at,
124
+ :logout_at,
125
+ :ip_address,
126
+ :success
127
+ )
128
+
129
+ LoginUser = Struct.new(
130
+ :user,
131
+ :sessions
132
+ )
133
+
119
134
  def load_env
120
135
  command = Kanrisuru::Command.new('env')
121
136
  execute_shell(command)
@@ -271,6 +286,65 @@ module Kanrisuru
271
286
  Kanrisuru::Result.new(command, &:to_i)
272
287
  end
273
288
 
289
+ def last(opts = {})
290
+ command =
291
+ if opts[:failed_attempts]
292
+ Kanrisuru::Command.new('lastb')
293
+ else
294
+ Kanrisuru::Command.new('last')
295
+ end
296
+
297
+ command.append_flag('-i')
298
+ command.append_flag('-F')
299
+ command.append_arg('-f', opts[:file])
300
+
301
+ ## Some systems only use 1 space between user and TTY field
302
+ ## Add an additional space in output formatting for simple parsing
303
+ ## logic.
304
+ command | "sed 's/ / /'"
305
+
306
+ execute_shell(command)
307
+
308
+ Kanrisuru::Result.new(command) do |cmd|
309
+ lines = cmd.to_a
310
+
311
+ mapping = {}
312
+
313
+ lines.each do |line|
314
+ next if Kanrisuru::Util.blank?(line)
315
+ next if line.include?('wtmp') || line.include?('btmp')
316
+
317
+ line = line.gsub(' still logged in', '- still logged in') if line.include?('still logged in')
318
+
319
+ values = line.split(/\s{2,}/, 4)
320
+ user = values[0]
321
+ tty = values[1]
322
+ ip = IPAddr.new(values[2])
323
+
324
+ date_range = values[3]
325
+ login, logout = date_range.split(' - ')
326
+
327
+ login = parse_last_date(login) if login
328
+
329
+ logout = parse_last_date(logout) if logout
330
+
331
+ detail = SessionDetail.new
332
+ detail.tty = tty
333
+ detail.ip_address = ip
334
+ detail.login_at = login
335
+ detail.logout_at = logout
336
+
337
+ detail.success = !Kanrisuru::Util.present?(opts[:failed_attemps])
338
+
339
+ mapping[user] = LoginUser.new(user, []) unless mapping.key?(user)
340
+
341
+ mapping[user].sessions << detail
342
+ end
343
+
344
+ mapping.values
345
+ end
346
+ end
347
+
274
348
  def ps(opts = {})
275
349
  group = opts[:group]
276
350
  user = opts[:user]
@@ -567,6 +641,19 @@ module Kanrisuru
567
641
  line.split(char, 2)[1]
568
642
  end
569
643
 
644
+ def parse_last_date(string)
645
+ tokens = string.split
646
+
647
+ return if tokens.length < 4
648
+
649
+ month_abbr = tokens[1]
650
+ day = tokens[2]
651
+ timestamp = tokens[3]
652
+ year = tokens[4]
653
+
654
+ DateTime.parse("#{day} #{month_abbr} #{year} #{timestamp}")
655
+ end
656
+
570
657
  def parse_policy_abbr(value)
571
658
  case value
572
659
  when '-'
@@ -713,7 +713,7 @@ module Kanrisuru
713
713
  zypper_repos_opt(command, opts)
714
714
  zypper_package_type_opt(command, opts)
715
715
  zypper_solver_opts(command, opts)
716
-
716
+
717
717
  packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
718
718
  command << packages
719
719
 
@@ -159,7 +159,6 @@ module Kanrisuru
159
159
  os_method_names.each do |method_name|
160
160
  if namespace
161
161
  namespace_class.class_eval do
162
-
163
162
  define_method method_name do |*args, &block|
164
163
  unbound_method = nil
165
164
 
@@ -193,7 +192,7 @@ module Kanrisuru
193
192
  else
194
193
  define_method method_name do |*args, &block|
195
194
  unbound_method = nil
196
-
195
+
197
196
  host = self
198
197
  os_method_cache = host.instance_variable_get(:@os_method_cache) || {}
199
198
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kanrisuru
4
- VERSION = '0.7.3'
4
+ VERSION = '0.8.0'
5
5
  end
@@ -96,6 +96,21 @@ RSpec.describe Kanrisuru::Core::System do
96
96
  expect(result.cpus.length).to eq(host.cpu.cores)
97
97
  end
98
98
 
99
+ it 'gets login details' do
100
+ host.su('root')
101
+
102
+ result = host.last
103
+ expect(result).to be_success
104
+ end
105
+
106
+ it 'gets failed login attempts' do
107
+ host.su('root')
108
+
109
+ result = host.last(failed_attempts: true)
110
+ puts result.data
111
+ expect(result).to be_success
112
+ end
113
+
99
114
  it 'gets process details' do
100
115
  result = host.ps
101
116
 
@@ -162,10 +162,10 @@ RSpec.describe Kanrisuru::OsPackage do
162
162
 
163
163
  expect(cluster).to respond_to(:output)
164
164
  expect(cluster.output).to eq([
165
- {:host=>"ubuntu-host", :result=>"debian"},
166
- {:host=>"centos-host", :result=>"fedora"},
167
- {:host=>"opensuse-host", :result=>"sles"}
168
- ])
165
+ { host: 'ubuntu-host', result: 'debian' },
166
+ { host: 'centos-host', result: 'fedora' },
167
+ { host: 'opensuse-host', result: 'sles' }
168
+ ])
169
169
 
170
170
  expect(host1).to respond_to(:alias)
171
171
  expect(host2).to respond_to(:alias)
@@ -180,10 +180,10 @@ RSpec.describe Kanrisuru::OsPackage do
180
180
  expect(cluster).to respond_to(:alias)
181
181
  expect(cluster.alias).to respond_to(:output)
182
182
  expect(cluster.alias.output).to eq([
183
- {:host=>"ubuntu-host", :result=>"debian"},
184
- {:host=>"centos-host", :result=>"fedora"},
185
- {:host=>"opensuse-host", :result=>"sles"}
186
- ])
183
+ { host: 'ubuntu-host', result: 'debian' },
184
+ { host: 'centos-host', result: 'fedora' },
185
+ { host: 'opensuse-host', result: 'sles' }
186
+ ])
187
187
 
188
188
  host1.disconnect
189
189
  host2.disconnect
@@ -86,5 +86,17 @@ RSpec.describe Kanrisuru::Core::System do
86
86
  :inode,
87
87
  :name
88
88
  )
89
+ expect(Kanrisuru::Core::System::SessionDetail.new).to respond_to(
90
+ :tty,
91
+ :login_at,
92
+ :logout_at,
93
+ :ip_address,
94
+ :success
95
+ )
96
+
97
+ expect(Kanrisuru::Core::System::LoginUser.new).to respond_to(
98
+ :user,
99
+ :sessions
100
+ )
89
101
  end
90
102
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanrisuru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Mammina
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-09 00:00:00.000000000 Z
11
+ date: 2021-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec