netid-tools 0.6.3 → 0.7.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.
data/README.md CHANGED
@@ -145,90 +145,97 @@ The NetID you want to check
145
145
  Version History
146
146
  ===============
147
147
 
148
- ## 0.6.3
148
+ ### 0.7.0
149
+
150
+ * [API] Convert netid-tools to use hash for initialization
151
+ * [API] Switch to response objects for all methods
152
+ * [Improvement/Experimental] Add ability to pre-load connections. See commit e328e1 for more details
153
+
154
+
155
+ ### 0.6.3
149
156
 
150
157
  * Spec fix
151
158
 
152
- ## 0.6.2
159
+ ### 0.6.2
153
160
 
154
161
  * Fix executable
155
162
 
156
- ## 0.6.1
163
+ ### 0.6.1
157
164
 
158
165
  * Add TravisCI
159
166
 
160
- ## 0.6.0
167
+ ### 0.6.0
161
168
 
162
169
  * Add tests; refactor various methods
163
170
  * Switch more commands to use Response objects rather than bare responses. Documentation pending.
164
171
 
165
- ## 0.5.5
172
+ ### 0.5.5
166
173
 
167
174
  * Switch process listing to individual class
168
175
 
169
- ## 0.5.4
176
+ ### 0.5.4
170
177
 
171
178
  * Switch table formatting on ua_check binary
172
179
 
173
- ## 0.5.3
180
+ ### 0.5.3
174
181
 
175
182
  * Remove debug function
176
183
 
177
- ## 0.5.2
184
+ ### 0.5.2
178
185
 
179
186
  * Expand cluster paths if available in quota output
180
187
 
181
- ## 0.5.0
188
+ ### 0.5.0
182
189
 
183
190
  * Switch to new SSH connection method; reuse already existing SSH connection for host if it exists
184
191
  * Tighten up code
185
192
  * Fix bugs
186
193
 
187
- ## 0.4.2
194
+ ### 0.4.2
188
195
 
189
196
  * Rename check_quota -> quota_check
190
197
 
191
- ## 0.4.0
198
+ ### 0.4.0
192
199
 
193
200
  * Switch most methods from Class->Instance
194
201
 
195
- ## 0.3.10
202
+ ### 0.3.10
196
203
 
197
204
  * Add -p flag for full process check on hosts.
198
205
 
199
- ## 0.3.9
206
+ ### 0.3.9
200
207
 
201
208
  * Add webtype check to list of returned results in ua_check executable
202
209
  * Add Netid.check_webtype method
203
210
 
204
- ## 0.3.8
211
+ ### 0.3.8
205
212
 
206
213
  * Remove ovid21.u.washington.edu from host list; retired system
207
214
 
208
- ## 0.3.7
215
+ ### 0.3.7
209
216
 
210
217
  * Allow validate_netid? to return true for NetIDs that have a hyphen in them
211
218
 
212
- ## 0.3.6
219
+ ### 0.3.6
213
220
 
214
221
  * Add concise mode via -c flag to ua_check (skips quota results)
215
222
  * Add OptionParser to ua_check
216
223
  * Tweak formatting for results
217
224
  * Allow multiple lookups per execption
218
225
 
219
- ## 0.3.3
226
+ ### 0.3.3
220
227
 
221
228
  * Fix broken code so that it properly displays localhome
222
229
 
223
- ## 0.3.2
230
+ ### 0.3.2
224
231
 
225
232
  * Added install notes
226
233
 
227
- ## 0.3.1
234
+ ### 0.3.1
228
235
 
229
236
  * Initial documented release
230
237
  * Swich to Apache License
231
238
 
232
- ## 0.3.0
239
+ ### 0.3.0
233
240
 
234
241
  Initial release
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.3
1
+ 0.7.0
data/bin/ua_check CHANGED
@@ -45,23 +45,24 @@ user.each do |netid|
45
45
  puts "#{netid}".bold.green
46
46
  puts "-"*netid.length
47
47
  results = 0
48
- checkr = Netid.new(netid,system_user)
48
+ checkr = Netid.new({netid: netid,system_user:system_user})
49
+ checkr.pre_load_ssh(*hosts)
49
50
  hosts.each do |host|
50
51
  result = checkr.check_for_mysql_presence(host)
51
- if result
52
+ if result.response
52
53
  results += 1
53
- puts "MySQLd detected on #{result[0]}:#{result[1]}".blue
54
+ puts "MySQLd detected on #{result.response[0]}:#{result.response[1]}".blue
54
55
  end
55
56
  end
56
57
 
57
58
  result = checkr.check_for_localhome
58
59
  if result
59
- puts "Localhome: #{result}".cyan
60
+ puts "Localhome: #{result.response}".cyan
60
61
  else
61
62
  puts "No localhome detected"
62
63
  end
63
64
 
64
- puts "Webtypes set: #{checkr.check_webtype}"
65
+ puts "Webtypes set: #{checkr.check_webtype.response}"
65
66
 
66
67
  puts "No MySQLds Detected".bold.blue if results == 0
67
68
 
data/lib/netid-tools.rb CHANGED
@@ -11,16 +11,25 @@ class Netid
11
11
 
12
12
  attr_accessor :netid, :system_user, :systems, :primary_host, :secondary_host
13
13
 
14
- def initialize(netid,system_user=nil,systems=nil,primary_host=nil,secondary_host=nil)
15
- @netid = netid
16
- @system_user = system_user || `whoami`.chomp
17
- @systems = systems || ["ovid01.u.washington.edu",
14
+ def initialize(options)
15
+
16
+ if options[:netid]
17
+ @netid = options[:netid]
18
+ else
19
+ raise "NetID required in options hash."
20
+ end
21
+
22
+ @system_user = options[:system_user] || `whoami`.chomp
23
+ @systems = options[:systems] || ["ovid01.u.washington.edu",
18
24
  "ovid02.u.washington.edu",
19
25
  "ovid03.u.washington.edu",
20
26
  "vergil.u.washington.edu"
21
27
  ]
22
- @primary_host = primary_host || "ovid02.u.washington.edu"
23
- @secondary_host = secondary_host || "vergil.u.washington.edu"
28
+ @primary_host = options[:primary_host] || "ovid02.u.washington.edu"
29
+ @secondary_host = options[:secondary_host] || "vergil.u.washington.edu"
30
+
31
+ @debug = options[:debug] || false
32
+
24
33
  end
25
34
 
26
35
  def validate_netid
@@ -36,19 +45,29 @@ class Netid
36
45
  end
37
46
 
38
47
  def check_for_mysql_presence(host)
48
+ response = GenericResponse.new
39
49
  command = "ps -F -U #{netid} -u #{netid}"
40
50
  result = run_remote_command(command,host)
41
51
  if result =~ /mysql/
42
52
  /port=(?<port>\d+)/ =~ result
43
- [host,port.to_i]
53
+ response.response = [host,port.to_i]
54
+ response
44
55
  else
45
- false
56
+ response.response = false
46
57
  end
58
+ response
59
+ end
60
+
61
+ # Experimental feature
62
+ def pre_load_ssh(*hosts)
63
+ threaded_connect(system_user,*hosts)
47
64
  end
48
65
 
49
66
  def get_processes(host)
50
67
  if /no such user/i =~ run_remote_command("id #{netid}",host)
51
- result = false
68
+ result = GenericResponse.new
69
+ result.response = false
70
+ result
52
71
  else
53
72
 
54
73
  command = "ps -o pid,user,cputime,nice,wchan,pcpu,pmem,rss,start_time,cmd --user #{netid}"
@@ -56,7 +75,7 @@ class Netid
56
75
  refined_processes = UnixProcesses.new(host)
57
76
 
58
77
  refined_processes.headers = raw_processes[0].split
59
- raw_processes.delete_at(0)
78
+ raw_processes.shift
60
79
 
61
80
  refined_processes.processes = raw_processes.map do |line|
62
81
  line = line.split
@@ -69,39 +88,30 @@ class Netid
69
88
  end
70
89
  refined_processes
71
90
  end
72
-
73
-
74
- # if /no such user/i =~ run_remote_command("id #{netid}",host)
75
- # result = nil
76
- # else
77
- # result = run_remote_command("ps -F --user=#{netid}",host).lines.map{|l| l.chomp}
78
- # result = remove_extra_processes(result)
79
- # end
80
- # if result.nil? || result.count == 1
81
- # false
82
- # else
83
- # result
84
- # end
85
91
  end
86
92
 
87
93
  def check_for_localhome
94
+ response = GenericResponse.new
88
95
  result = run_remote_command("cpw -poh #{netid}",primary_host)
89
96
  if result =~ /Unknown/
90
- false
97
+ response.response = false
91
98
  else
92
- result.chomp
99
+ response.response = result.chomp
93
100
  end
101
+ response
94
102
  end
95
103
 
96
104
  def check_webtype
97
- result = []
105
+ response = GenericResponse.new
98
106
  command = "webtype -user #{netid}"
99
- result = run_remote_command(command,primary_host).chomp.split
100
- if result[0] == "user"
101
- result = run_remote_command(command,secondary_host).chomp.split
107
+ command_result = run_remote_command(command,primary_host).chomp
108
+ if command_result =~ /user/
109
+ response.response = run_remote_command(command,secondary_host).chomp.split
102
110
  else
103
- result
111
+ response.response = command_result.chomp.split
104
112
  end
113
+ response.response = false if response.response.empty?
114
+ response
105
115
  end
106
116
 
107
117
 
@@ -117,8 +127,11 @@ class Netid
117
127
  result = QuotaResponse.new
118
128
 
119
129
  command_result = command_result.chomp.split("\n")
120
- command_result.delete_at(0) if command_result.first == ""
121
- command_result.delete_at(0) # remove uid line
130
+ if command_result.first == ""
131
+ command_result.shift(2)
132
+ else
133
+ command_result.shift
134
+ end
122
135
 
123
136
  result.headers = process_quota_headers(command_result)
124
137
  result.response = command_result.map do |line|
@@ -140,7 +153,7 @@ class Netid
140
153
 
141
154
  def process_quota_headers(quota_results)
142
155
  headings = quota_results.first.split
143
- quota_results.delete_at(0)
156
+ quota_results.shift
144
157
  headings
145
158
  end
146
159
 
@@ -168,5 +181,4 @@ class Netid
168
181
  end
169
182
  end
170
183
  end
171
-
172
184
  end
@@ -2,10 +2,36 @@ module SystemConnect
2
2
 
3
3
  private
4
4
 
5
+ def connections
6
+ unless @connections
7
+ @connections = []
8
+ end
9
+ @connections
10
+ end
11
+
12
+ def connections=(var)
13
+ @connections = var
14
+ end
15
+
5
16
  def connect(host,user)
6
17
  Net::SSH.start(host,user,{auth_methods: %w(publickey)})
7
18
  end
8
19
 
20
+ def threaded_connect(user,*hosts)
21
+ connection_objects = []
22
+ threads = []
23
+ hosts.each do |host|
24
+ threads << Thread.new do
25
+ connections << SystemSSH.new(host,connect(host,user))
26
+ end
27
+ end
28
+ threads.each do |thr|
29
+ thr.join
30
+ end
31
+ end
32
+
33
+
34
+
9
35
  def run_remote_command(command, host)
10
36
  connection = find_connection_for_host(host)
11
37
  connection.exec!(command)
@@ -28,9 +54,8 @@ module SystemConnect
28
54
  end
29
55
 
30
56
  def find_connection_for_host(host)
31
- @connections ||= []
32
- unless @connections.select{ |conn| conn.hostname == host}.empty?
33
- @connections.select{ |conn| conn.hostname == host}.first.connection_object
57
+ unless connections.select{ |conn| conn.hostname == host}.empty?
58
+ connections.select{ |conn| conn.hostname == host}.first.connection_object
34
59
  else
35
60
  lazy_load_connection_for_host(host)
36
61
  find_connection_for_host(host)
data/netid-tools.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "netid-tools"
8
- s.version = "0.6.3"
8
+ s.version = "0.7.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nikky Southerland"]
12
- s.date = "2013-06-28"
12
+ s.date = "2013-07-03"
13
13
  s.description = "Gem with various methods to support UW NetIDs"
14
14
  s.email = "nikky@uw.edu"
15
15
  s.executables = ["ua_check"]
@@ -10,10 +10,38 @@ describe Netid do
10
10
  end
11
11
 
12
12
  before do
13
- @netid = Netid.new('test')
13
+ @netid = Netid.new({netid:'test'})
14
14
  end
15
15
 
16
16
  context "sanity checks" do
17
+ it "raises on two or more arguments" do
18
+ expect do
19
+ Netid.new('doop','derp')
20
+ end.to raise_error
21
+ end
22
+ it "expects an options hash" do
23
+ Netid.new({netid: 'nikky'}).netid.should eq 'nikky'
24
+ end
25
+ it "requires a NetID in hash" do
26
+ expect do
27
+ Netid.new({system_user: 'derp'})
28
+ end.to raise_error
29
+ end
30
+ it "knows about the usual options" do
31
+ full_object = Netid.new({
32
+ netid: 'netid2',
33
+ system_user: 'bob',
34
+ systems: %w(sylvan.uw.edu),
35
+ primary_host: 'hiigara.cac',
36
+ secondary_host: 'uvb76.cac'
37
+ })
38
+
39
+ full_object.netid.should eq 'netid2'
40
+ full_object.system_user.should eq 'bob'
41
+ full_object.systems.should eq ["sylvan.uw.edu"]
42
+ full_object.primary_host.should eq "hiigara.cac"
43
+ full_object.secondary_host.should eq "uvb76.cac"
44
+ end
17
45
  it "responds to #netid" do
18
46
  @netid.should respond_to :netid
19
47
  @netid.netid.should eq 'test'
@@ -47,10 +75,10 @@ describe Netid do
47
75
  @netid.validate_netid.response.should eq true
48
76
  end
49
77
  it "returns a GenericResponse of false on invalid netid" do
50
- Netid.new('123test').validate_netid.response.should be_false
78
+ Netid.new({netid:'123test'}).validate_netid.response.should be_false
51
79
  end
52
80
  it "returns a GenericResponse with error message on invalid netid" do
53
- Netid.new('123test').validate_netid.error.should eq "Not a valid NetID"
81
+ Netid.new({netid:'123test'}).validate_netid.error.should eq "Not a valid NetID"
54
82
  end
55
83
  end
56
84
 
@@ -59,10 +87,10 @@ describe Netid do
59
87
  @netid.validate_netid?.should be_true
60
88
  end
61
89
  it "returns false on NetID that starts with number" do
62
- Netid.new('123test').validate_netid?.should be_false
90
+ Netid.new({netid:'123test'}).validate_netid?.should be_false
63
91
  end
64
92
  it "returns false on NetID that is too long" do
65
- Netid.new('abcdefghijklmnop').validate_netid?.should be_false
93
+ Netid.new({netid:'abcdefghijklmnop'}).validate_netid?.should be_false
66
94
  end
67
95
  end
68
96
 
@@ -85,18 +113,18 @@ describe Netid do
85
113
  --pid-file=/da23/d38/nikky/mysql/data/ovid02.u.washington.edu.pid
86
114
  --socket=/da23/d38/nikky/mysql.sock --port=5280"
87
115
  mock_system_response(valid_return)
88
- @netid.check_for_mysql_presence('fake.example.com').should eq ['fake.example.com', 5280]
116
+ @netid.check_for_mysql_presence('fake.example.com').response.should eq ['fake.example.com', 5280]
89
117
  end
90
118
  it "returns false with no valid result" do
91
119
  mock_system_response("")
92
- @netid.check_for_mysql_presence('fake.example.com').should be_false
120
+ @netid.check_for_mysql_presence('fake.example.com').response.should be_false
93
121
  end
94
122
  end
95
123
 
96
124
  context "#get_processes" do
97
125
  it "returns false if a user is not detected" do
98
126
  mock_system_response("no such user")
99
- @netid.get_processes('example.com').should be_false
127
+ @netid.get_processes('example.com').response.should be_false
100
128
  end
101
129
  it "returns a UnixProcesses object on success" do
102
130
  mock_system_response("exists","1\n2\n3")
@@ -134,30 +162,30 @@ describe Netid do
134
162
  context "#check_for_localhome" do
135
163
  it "returns the localhome location upon success" do
136
164
  mock_system_response("/ov03/dw21/derp")
137
- @netid.check_for_localhome.should eq ("/ov03/dw21/derp")
165
+ @netid.check_for_localhome.response.should eq ("/ov03/dw21/derp")
138
166
  end
139
167
  it "returns false if no result" do
140
168
  mock_system_response("user Unknown")
141
- @netid.check_for_localhome.should be_false
169
+ @netid.check_for_localhome.response.should be_false
142
170
  end
143
171
  end
144
172
 
145
173
  context "#check_webtype" do
146
174
  it "returns array of webtypes upon success" do
147
175
  mock_system_response("depts\ncourses")
148
- @netid.check_webtype.should eq %w(depts courses)
176
+ @netid.check_webtype.response.should eq %w(depts courses)
149
177
  end
150
- it "returns empty array if no webtypes found" do
178
+ it "returns false if no webtypes found" do
151
179
  mock_system_response("")
152
- @netid.check_webtype.should be_empty
180
+ @netid.check_webtype.response.should be_false
153
181
  end
154
182
  it "tries alternate host if primary returns no user found" do
155
183
  mock_system_response("user Unknown","depts\ncourses")
156
- @netid.check_webtype.should eq %w(depts courses)
184
+ @netid.check_webtype.response.should eq %w(depts courses)
157
185
  end
158
186
  it "returns empty array if no webtypes found on alternate host" do
159
187
  mock_system_response("user Unknown","")
160
- @netid.check_webtype.should eq %w()
188
+ @netid.check_webtype.response.should be_false
161
189
  end
162
190
 
163
191
  end
@@ -208,4 +236,5 @@ describe Netid do
208
236
  @netid.check_quota.response.should eq [%w(/cg32 usage quota limit n/a files limit), %w(/hw00/w00/ferp usage quota limit grace files limit)]
209
237
  end
210
238
  end
239
+
211
240
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netid-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-28 00:00:00.000000000 Z
12
+ date: 2013-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: colored
@@ -229,7 +229,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
229
229
  version: '0'
230
230
  segments:
231
231
  - 0
232
- hash: 2948455287693453037
232
+ hash: 3274686386078058042
233
233
  required_rubygems_version: !ruby/object:Gem::Requirement
234
234
  none: false
235
235
  requirements: