rcmd 1.5.8 → 1.6.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: db367fb9ee0a345d21efe9d34e2e1a82a825567d
4
- data.tar.gz: 45e96daee3ac72dd2a3cdda0f576adc8553aa193
3
+ metadata.gz: 43972a99b7fa833e8e8cb928e2b11a305bf4786f
4
+ data.tar.gz: 3c0b71cb37cfbbbb62e0ba008fe10f060a45a7c9
5
5
  SHA512:
6
- metadata.gz: 3fd26cf8b227ee8ace5e8fadc4b0f9591d67355d8c84ef0dbf3833bf099e1a0763ac6d63a3e96f2a69417abadac71942baeee8a5865366814b628296c4d4d1a0
7
- data.tar.gz: 0747e55128628ae496d87d734134e09677a2b2d7042a55c07e9a3e4bbf19cfef8464d7a89d1cd3ab14ca86300b53dc3fb372971855ef1f8ec2a37d55f2fc2c60
6
+ metadata.gz: 47a65fd1edfc56767f2180455b72e028accd70e4077080ad0549a7dc54eaf893e036c52a36556f6335ca75c1a6687af0475fd734aba9e57f95f2d019f3a28fa1
7
+ data.tar.gz: 5ee4881c3f0a8c6f5edc65b2fb36f685d29e8323a24d6476b7f302435f0ec42237d478c2568716196dab672e5a28b1a333764dadbcb42c6e59740128bd32f134
data/.rspec_status CHANGED
@@ -1,9 +1,9 @@
1
1
  example_id | status | run_time |
2
2
  ------------------------ | ------ | --------------- |
3
- ./spec/rcmd_spec.rb[1:1] | passed | 0.00064 seconds |
4
- ./spec/rcmd_spec.rb[1:2] | passed | 0.00101 seconds |
5
- ./spec/rcmd_spec.rb[1:3] | passed | 0.00088 seconds |
6
- ./spec/rcmd_spec.rb[1:4] | passed | 0.00012 seconds |
7
- ./spec/rcmd_spec.rb[1:5] | passed | 0.00012 seconds |
8
- ./spec/rcmd_spec.rb[1:6] | passed | 0.00178 seconds |
9
- ./spec/rcmd_spec.rb[1:7] | passed | 0.00015 seconds |
3
+ ./spec/rcmd_spec.rb[1:1] | passed | 0.00078 seconds |
4
+ ./spec/rcmd_spec.rb[1:2] | passed | 0.00116 seconds |
5
+ ./spec/rcmd_spec.rb[1:3] | passed | 0.00099 seconds |
6
+ ./spec/rcmd_spec.rb[1:4] | passed | 0.00018 seconds |
7
+ ./spec/rcmd_spec.rb[1:5] | passed | 0.00019 seconds |
8
+ ./spec/rcmd_spec.rb[1:6] | passed | 0.00133 seconds |
9
+ ./spec/rcmd_spec.rb[1:7] | passed | 0.00011 seconds |
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rcmd (1.5.8)
4
+ rcmd (1.6.0)
5
+ activerecord
5
6
  io-console
6
7
  net-ssh
7
8
  regexp-examples
@@ -11,10 +12,25 @@ PATH
11
12
  GEM
12
13
  remote: https://rubygems.org/
13
14
  specs:
15
+ activemodel (5.1.4)
16
+ activesupport (= 5.1.4)
17
+ activerecord (5.1.4)
18
+ activemodel (= 5.1.4)
19
+ activesupport (= 5.1.4)
20
+ arel (~> 8.0)
21
+ activesupport (5.1.4)
22
+ concurrent-ruby (~> 1.0, >= 1.0.2)
23
+ i18n (~> 0.7)
24
+ minitest (~> 5.1)
25
+ tzinfo (~> 1.1)
26
+ arel (8.0.0)
27
+ concurrent-ruby (1.0.5)
14
28
  diff-lcs (1.3)
15
29
  equatable (0.5.0)
16
30
  hitimes (1.2.6)
31
+ i18n (0.8.6)
17
32
  io-console (0.4.6)
33
+ minitest (5.10.3)
18
34
  necromancer (0.4.0)
19
35
  net-ssh (4.2.0)
20
36
  pastel (0.7.1)
@@ -36,6 +52,7 @@ GEM
36
52
  rspec-support (~> 3.6.0)
37
53
  rspec-support (3.6.0)
38
54
  thread (0.2.2)
55
+ thread_safe (0.3.6)
39
56
  timers (4.1.2)
40
57
  hitimes
41
58
  tty-color (0.4.2)
@@ -48,6 +65,8 @@ GEM
48
65
  tty-reader (~> 0.1.0)
49
66
  tty-reader (0.1.0)
50
67
  wisper (~> 2.0.0)
68
+ tzinfo (1.2.3)
69
+ thread_safe (~> 0.1)
51
70
  wisper (2.0.0)
52
71
 
53
72
  PLATFORMS
data/Rakefile CHANGED
@@ -1,6 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
  require 'rdoc/task'
4
+ require 'sqlite3'
4
5
 
5
6
  RSpec::Core::RakeTask.new(:spec)
6
7
 
@@ -10,7 +11,7 @@ desc "Open an IRB console with this gem loaded"
10
11
  task :console do
11
12
  require 'irb'
12
13
  require 'irb/completion'
13
- require 'rcmd' # You know what to do.
14
+ require 'rcmd'
14
15
  ARGV.clear
15
16
  IRB.start
16
17
  end
@@ -30,8 +31,40 @@ Rake::RDocTask.new do |rd|
30
31
  rd.main = 'README.md'
31
32
  rd.rdoc_files.include 'README.md', "lib/**/*\.rb", "exe/**/*"
32
33
 
33
- rd.options << '--inline-source'
34
34
  rd.options << '--line-numbers'
35
35
  rd.options << '--all'
36
36
  end
37
37
 
38
+ # desc 'generate and populate a test sqlite database'
39
+ # task :testdb do
40
+ # begin
41
+ # db = SQLite3::Database.new "testdb"
42
+ # db.execute "create table servers(id integer primary key autoincrement, hostname text not null, stype text not null, os_type text not null)"
43
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node1', 'dev', 'rhel6')"
44
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node2', 'dev', 'rhel6')"
45
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node3', 'dev', 'rhel6')"
46
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node4', 'dev', 'rhel7')"
47
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node5', 'dev', 'rhel6')"
48
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node6', 'web', 'rhel6')"
49
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node7', 'web', 'rhel6')"
50
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node8', 'web', 'rhel7')"
51
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node9', 'web', 'rhel7')"
52
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node10', 'admin', 'ubuntu-LTS')"
53
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node11', 'admin', 'ubuntu-LTS')"
54
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node12', 'admin', 'ubuntu')"
55
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node13', 'admin', 'ubuntu')"
56
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node14', 'compute', 'debiuan')"
57
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node15', 'compute', 'debiuan')"
58
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node16', 'compute', 'debiuan')"
59
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node17', 'compute', 'debiuan')"
60
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node18', 'db', 'slackware')"
61
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node19', 'db', 'slackware')"
62
+ # db.execute "INSERT INTO servers (hostname, stype, os_type) VALUES ( 'node20', 'db', 'slackware')"
63
+
64
+ # rescue SQLite3::Exception => e
65
+ # puts "Exception occurred"
66
+ # puts e
67
+ # ensure
68
+ # db.close if db
69
+ # end
70
+ # end
data/doc/rdocs/Rcmd.html CHANGED
@@ -277,7 +277,7 @@ the thread count. This prevents spawning of unneeded threads.</p>
277
277
 
278
278
 
279
279
  <div class="method-source-code" id="run_command-source">
280
- <pre><span class="ruby-comment"># File lib/rcmd.rb, line 77</span>
280
+ <pre><span class="ruby-comment"># File lib/rcmd.rb, line 80</span>
281
281
  <span class="ruby-keyword">def</span> <span class="ruby-constant">Rcmd</span>.<span class="ruby-identifier">run_command</span>()
282
282
  <span class="ruby-keyword">if</span> <span class="ruby-keyword">not</span> <span class="ruby-ivar">@command</span>
283
283
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;No command set for execution&quot;</span>)
@@ -0,0 +1,489 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html>
4
+ <head>
5
+ <meta charset="UTF-8">
6
+
7
+ <title>class Rcmd::DB - RDoc Documentation</title>
8
+
9
+ <script type="text/javascript">
10
+ var rdoc_rel_prefix = "../";
11
+ var index_rel_prefix = "../";
12
+ </script>
13
+
14
+ <script src="../js/jquery.js"></script>
15
+ <script src="../js/darkfish.js"></script>
16
+
17
+ <link href="../css/fonts.css" rel="stylesheet">
18
+ <link href="../css/rdoc.css" rel="stylesheet">
19
+
20
+
21
+
22
+ <body id="top" role="document" class="class">
23
+ <nav role="navigation">
24
+ <div id="project-navigation">
25
+ <div id="home-section" role="region" title="Quick navigation" class="nav-section">
26
+ <h2>
27
+ <a href="../index.html" rel="home">Home</a>
28
+ </h2>
29
+
30
+ <div id="table-of-contents-navigation">
31
+ <a href="../table_of_contents.html#pages">Pages</a>
32
+ <a href="../table_of_contents.html#classes">Classes</a>
33
+ <a href="../table_of_contents.html#methods">Methods</a>
34
+ </div>
35
+ </div>
36
+
37
+ <div id="search-section" role="search" class="project-section initially-hidden">
38
+ <form action="#" method="get" accept-charset="utf-8">
39
+ <div id="search-field-wrapper">
40
+ <input id="search-field" role="combobox" aria-label="Search"
41
+ aria-autocomplete="list" aria-controls="search-results"
42
+ type="text" name="search" placeholder="Search" spellcheck="false"
43
+ title="Type to search, Up and Down to navigate, Enter to load">
44
+ </div>
45
+
46
+ <ul id="search-results" aria-label="Search Results"
47
+ aria-busy="false" aria-expanded="false"
48
+ aria-atomic="false" class="initially-hidden"></ul>
49
+ </form>
50
+ </div>
51
+
52
+ </div>
53
+
54
+
55
+
56
+ <div id="class-metadata">
57
+
58
+ <div id="parent-class-section" class="nav-section">
59
+ <h3>Parent</h3>
60
+
61
+
62
+ <p class="link">ActiveRecord::Base
63
+
64
+ </div>
65
+
66
+
67
+
68
+ <!-- Method Quickref -->
69
+ <div id="method-list-section" class="nav-section">
70
+ <h3>Methods</h3>
71
+
72
+ <ul class="link-list" role="directory">
73
+
74
+ <li ><a href="#method-c-create_config">::create_config</a>
75
+
76
+ <li ><a href="#method-c-db_connect">::db_connect</a>
77
+
78
+ <li ><a href="#method-c-load_config">::load_config</a>
79
+
80
+ <li ><a href="#method-c-override_config_file">::override_config_file</a>
81
+
82
+ <li ><a href="#method-c-query_by_hostname">::query_by_hostname</a>
83
+
84
+ <li ><a href="#method-c-query_by_os">::query_by_os</a>
85
+
86
+ <li ><a href="#method-c-query_by_type">::query_by_type</a>
87
+
88
+ </ul>
89
+ </div>
90
+
91
+ </div>
92
+ </nav>
93
+
94
+ <main role="main" aria-labelledby="class-Rcmd::DB">
95
+ <h1 id="class-Rcmd::DB" class="class">
96
+ class Rcmd::DB
97
+ </h1>
98
+
99
+ <section class="description">
100
+
101
+ <p>This class provides a middle layer for allowing a database to be queried
102
+ for hosts. This is accomplished by using ActiveRecord for the middle layer
103
+ and thus should support any database ActiveRecord supports. Currently
104
+ however, only the following are properly configured for backend use in this
105
+ class:</p>
106
+
107
+ <p>Backend Databases:</p>
108
+ <ul><li>
109
+ <p>sqlite3</p>
110
+ </li><li>
111
+ <p>mysql</p>
112
+ </li><li>
113
+ <p>frontbase</p>
114
+ </li><li>
115
+ <p>firebird</p>
116
+ </li><li>
117
+ <p>db2</p>
118
+ </li><li>
119
+ <p>openbase</p>
120
+ </li><li>
121
+ <p>oracle</p>
122
+ </li><li>
123
+ <p>postgresql</p>
124
+ </li></ul>
125
+
126
+ <p>This is achieved in the <a
127
+ href="DB.html#method-c-db_connect">::db_connect</a> method, which matches
128
+ the specifed adapter with the appropriate &#39;require&#39; call by use of
129
+ a case statement. If you are needing/wanting to add another database to
130
+ this you will need to add your require code for the db backend there. As we
131
+ do this comparison, the various backends are not specified in the Gemspec
132
+ file as it is the responsibilty of the user to ensure the correct gems are
133
+ installed for the needed database.</p>
134
+
135
+ <p>This class relies on a yaml configuration file to be present and as such
136
+ the &#39;DB.create_config&#39; method is provided to create an example
137
+ configuration which can be then edited. The options in the config file
138
+ are:</p>
139
+
140
+ </section>
141
+
142
+
143
+
144
+
145
+ <section id="5Buntitled-5D" class="documentation-section">
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+ <section id="public-class-5Buntitled-5D-method-details" class="method-section">
156
+ <header>
157
+ <h3>Public Class Methods</h3>
158
+ </header>
159
+
160
+
161
+ <div id="method-c-create_config" class="method-detail ">
162
+
163
+ <div class="method-heading">
164
+ <span class="method-name">create_config</span><span
165
+ class="method-args">()</span>
166
+
167
+ <span class="method-click-advice">click to toggle source</span>
168
+
169
+ </div>
170
+
171
+
172
+ <div class="method-description">
173
+
174
+ <p>Create a base config file</p>
175
+
176
+
177
+
178
+
179
+ <div class="method-source-code" id="create_config-source">
180
+ <pre><span class="ruby-comment"># File lib/rcmd/db.rb, line 84</span>
181
+ <span class="ruby-keyword">def</span> <span class="ruby-constant">DB</span>.<span class="ruby-identifier">create_config</span>
182
+ <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;Function: #{@config_dir}&quot;</span>
183
+ <span class="ruby-keyword">unless</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">directory?</span>(<span class="ruby-ivar">@config_dir</span>)
184
+ <span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">mkdir_p</span>(<span class="ruby-ivar">@config_dir</span>)
185
+ <span class="ruby-keyword">end</span>
186
+
187
+ <span class="ruby-identifier">conf</span> = [ <span class="ruby-value">:adapter</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;sqlite3&quot;</span>, <span class="ruby-value">:host</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">nil</span>, <span class="ruby-value">:database</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&#39;:memory:&#39;</span>, <span class="ruby-value">:table</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&#39;servers&#39;</span>, <span class="ruby-value">:username</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">nil</span>, <span class="ruby-value">:password</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">nil</span>, <span class="ruby-value">:host_field</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&#39;hostname&#39;</span>, <span class="ruby-value">:type_field</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&#39;server_type&#39;</span>, <span class="ruby-value">:os_field</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&#39;osversion&#39;</span> ]
188
+
189
+ <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-ivar">@config_file</span>, <span class="ruby-string">&#39;w&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
190
+ <span class="ruby-identifier">f</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">conf</span>.<span class="ruby-identifier">to_yaml</span>)
191
+ <span class="ruby-keyword">end</span>
192
+ <span class="ruby-keyword">end</span></pre>
193
+ </div>
194
+
195
+ </div>
196
+
197
+
198
+
199
+
200
+ </div>
201
+
202
+
203
+ <div id="method-c-db_connect" class="method-detail ">
204
+
205
+ <div class="method-heading">
206
+ <span class="method-name">db_connect</span><span
207
+ class="method-args">()</span>
208
+
209
+ <span class="method-click-advice">click to toggle source</span>
210
+
211
+ </div>
212
+
213
+
214
+ <div class="method-description">
215
+
216
+ <p>Method for establishing a connection to the database backend according to
217
+ the configuration file. This is also where we set the &#39;require&#39;
218
+ the need gems to interact with the chosen <a href="DB.html">DB</a> adapter
219
+ and set the table_name variable of the class.</p>
220
+
221
+
222
+
223
+
224
+ <div class="method-source-code" id="db_connect-source">
225
+ <pre><span class="ruby-comment"># File lib/rcmd/db.rb, line 162</span>
226
+ <span class="ruby-keyword">def</span> <span class="ruby-constant">DB</span>.<span class="ruby-identifier">db_connect</span>
227
+ <span class="ruby-keyword">self</span>.<span class="ruby-identifier">load_config</span>
228
+
229
+ <span class="ruby-keyword">case</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:adapter</span>].<span class="ruby-identifier">downcase</span>
230
+ <span class="ruby-keyword">when</span> <span class="ruby-string">&quot;sqlite3&quot;</span>
231
+ <span class="ruby-identifier">require</span> <span class="ruby-string">&quot;sqlite3&quot;</span>
232
+ <span class="ruby-keyword">when</span> <span class="ruby-string">&quot;mysql&quot;</span>
233
+ <span class="ruby-identifier">require</span> <span class="ruby-string">&quot;mysql&quot;</span>
234
+ <span class="ruby-keyword">when</span> <span class="ruby-string">&quot;frontbase&quot;</span>
235
+ <span class="ruby-identifier">require</span> <span class="ruby-string">&quot;ruby-frontbase&quot;</span>
236
+ <span class="ruby-keyword">when</span> <span class="ruby-string">&quot;firebird&quot;</span>
237
+ <span class="ruby-identifier">require</span> <span class="ruby-string">&quot;fireruby&quot;</span>
238
+ <span class="ruby-keyword">when</span> <span class="ruby-string">&quot;db2&quot;</span>
239
+ <span class="ruby-identifier">require</span> <span class="ruby-string">&quot;ruby-db2&quot;</span>
240
+ <span class="ruby-keyword">when</span> <span class="ruby-string">&quot;openbase&quot;</span>
241
+ <span class="ruby-identifier">require</span> <span class="ruby-string">&quot;openbase&quot;</span>
242
+ <span class="ruby-keyword">when</span> <span class="ruby-string">&quot;oracle&quot;</span>
243
+ <span class="ruby-identifier">require</span> <span class="ruby-string">&quot;ruby-oci8&quot;</span>
244
+ <span class="ruby-keyword">when</span> <span class="ruby-string">&quot;postgresql&quot;</span>
245
+ <span class="ruby-identifier">require</span> <span class="ruby-string">&quot;ruby-postgres&quot;</span>
246
+ <span class="ruby-keyword">end</span>
247
+
248
+ <span class="ruby-keyword">if</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:adapter</span>].<span class="ruby-identifier">downcase</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;sqlite3&quot;</span>
249
+ <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">establish_connection</span>(<span class="ruby-value">:adapter</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:adapter</span>].<span class="ruby-identifier">downcase</span>,
250
+ <span class="ruby-value">:database</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:database</span>])
251
+ <span class="ruby-keyword">else</span>
252
+ <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">establish_connection</span>(
253
+ <span class="ruby-value">:adapter</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:adapter</span>].<span class="ruby-identifier">downcase</span>,
254
+ <span class="ruby-value">:host</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:host</span>],
255
+ <span class="ruby-value">:username</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:username</span>],
256
+ <span class="ruby-value">:password</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:password</span>],
257
+ <span class="ruby-value">:database</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:database</span>] )
258
+ <span class="ruby-keyword">end</span>
259
+
260
+ <span class="ruby-ivar">@table_name</span> = <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:table</span>]
261
+ <span class="ruby-keyword">end</span></pre>
262
+ </div>
263
+
264
+ </div>
265
+
266
+
267
+
268
+
269
+ </div>
270
+
271
+
272
+ <div id="method-c-load_config" class="method-detail ">
273
+
274
+ <div class="method-heading">
275
+ <span class="method-name">load_config</span><span
276
+ class="method-args">()</span>
277
+
278
+ <span class="method-click-advice">click to toggle source</span>
279
+
280
+ </div>
281
+
282
+
283
+ <div class="method-description">
284
+
285
+ <p>Method called by <a href="DB.html#method-c-db_connect">::db_connect</a> to
286
+ load the yaml configuration file and perform some basic sanity checks and
287
+ disabling of os/type queries if those fields are not in the configuration</p>
288
+
289
+
290
+
291
+
292
+ <div class="method-source-code" id="load_config-source">
293
+ <pre><span class="ruby-comment"># File lib/rcmd/db.rb, line 121</span>
294
+ <span class="ruby-keyword">def</span> <span class="ruby-constant">DB</span>.<span class="ruby-identifier">load_config</span>
295
+ <span class="ruby-keyword">unless</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">file?</span>(<span class="ruby-ivar">@config_file</span>)
296
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">RuntimeError</span>, <span class="ruby-node">&quot;No database configuration file found: #{@config_file}&quot;</span>
297
+ <span class="ruby-keyword">end</span>
298
+
299
+ <span class="ruby-ivar">@conf</span> = <span class="ruby-constant">YAML</span>.<span class="ruby-identifier">load_file</span>(<span class="ruby-ivar">@config_file</span>)[<span class="ruby-value">0</span>]
300
+
301
+ <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:adapter</span>]
302
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">RuntimeError</span>, <span class="ruby-string">&quot;No adapter specified for database backend&quot;</span>
303
+ <span class="ruby-keyword">end</span>
304
+
305
+ <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:type_field</span>]
306
+ <span class="ruby-ivar">@db_type_queries</span> = <span class="ruby-keyword">false</span>
307
+ <span class="ruby-keyword">end</span>
308
+
309
+ <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:os_field</span>]
310
+ <span class="ruby-ivar">@db_os_queries</span> = <span class="ruby-keyword">false</span>
311
+ <span class="ruby-keyword">end</span>
312
+
313
+ <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:adapter</span>].<span class="ruby-identifier">downcase</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;sqlite3&quot;</span>
314
+ <span class="ruby-keyword">if</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:host</span>].<span class="ruby-identifier">nil?</span>
315
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">RuntimeError</span>, <span class="ruby-string">&quot;No database host server specified&quot;</span>
316
+ <span class="ruby-keyword">end</span>
317
+ <span class="ruby-keyword">if</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:username</span>].<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:password</span>].<span class="ruby-identifier">nil?</span>
318
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">RuntimeError</span>, <span class="ruby-string">&quot;No DB credentials specified&quot;</span>
319
+ <span class="ruby-keyword">end</span>
320
+ <span class="ruby-keyword">end</span>
321
+
322
+ <span class="ruby-keyword">if</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:database</span>].<span class="ruby-identifier">nil?</span>
323
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">RuntimeError</span>, <span class="ruby-string">&quot;No database specified&quot;</span>
324
+ <span class="ruby-keyword">end</span>
325
+
326
+ <span class="ruby-keyword">if</span> <span class="ruby-ivar">@conf</span>[<span class="ruby-value">:table</span>].<span class="ruby-identifier">nil?</span>
327
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">RuntimeError</span>, <span class="ruby-string">&quot;Table not specified&quot;</span>
328
+ <span class="ruby-keyword">end</span>
329
+ <span class="ruby-keyword">end</span></pre>
330
+ </div>
331
+
332
+ </div>
333
+
334
+
335
+
336
+
337
+ </div>
338
+
339
+
340
+ <div id="method-c-override_config_file" class="method-detail ">
341
+
342
+ <div class="method-heading">
343
+ <span class="method-name">override_config_file</span><span
344
+ class="method-args">(path)</span>
345
+
346
+ <span class="method-click-advice">click to toggle source</span>
347
+
348
+ </div>
349
+
350
+
351
+ <div class="method-description">
352
+
353
+ <p>Method for overiding the location and name of the default config file</p>
354
+
355
+
356
+
357
+
358
+ <div class="method-source-code" id="override_config_file-source">
359
+ <pre><span class="ruby-comment"># File lib/rcmd/db.rb, line 79</span>
360
+ <span class="ruby-keyword">def</span> <span class="ruby-constant">DB</span>.<span class="ruby-identifier">override_config_file</span>(<span class="ruby-identifier">path</span>)
361
+ <span class="ruby-ivar">@config_file</span> = <span class="ruby-identifier">path</span>
362
+ <span class="ruby-keyword">end</span></pre>
363
+ </div>
364
+
365
+ </div>
366
+
367
+
368
+
369
+
370
+ </div>
371
+
372
+
373
+ <div id="method-c-query_by_hostname" class="method-detail ">
374
+
375
+ <div class="method-heading">
376
+ <span class="method-name">query_by_hostname</span><span
377
+ class="method-args">(term)</span>
378
+
379
+ <span class="method-click-advice">click to toggle source</span>
380
+
381
+ </div>
382
+
383
+
384
+ <div class="method-description">
385
+
386
+ <p>Performs “LIKE” queries on the host_field (hostname)</p>
387
+
388
+
389
+
390
+
391
+ <div class="method-source-code" id="query_by_hostname-source">
392
+ <pre><span class="ruby-comment"># File lib/rcmd/db.rb, line 98</span>
393
+ <span class="ruby-keyword">def</span> <span class="ruby-constant">DB</span>.<span class="ruby-identifier">query_by_hostname</span>(<span class="ruby-identifier">term</span>)
394
+ <span class="ruby-keyword">return</span> <span class="ruby-identifier">where</span>(<span class="ruby-node">&quot;#{@conf[:host_field]} LIKE ?&quot;</span>, <span class="ruby-identifier">term</span> ).<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span> <span class="ruby-identifier">r</span>[<span class="ruby-ivar">@conf</span>[<span class="ruby-value">:host_field</span>]] }
395
+ <span class="ruby-keyword">end</span></pre>
396
+ </div>
397
+
398
+ </div>
399
+
400
+
401
+
402
+
403
+ </div>
404
+
405
+
406
+ <div id="method-c-query_by_os" class="method-detail ">
407
+
408
+ <div class="method-heading">
409
+ <span class="method-name">query_by_os</span><span
410
+ class="method-args">(term)</span>
411
+
412
+ <span class="method-click-advice">click to toggle source</span>
413
+
414
+ </div>
415
+
416
+
417
+ <div class="method-description">
418
+
419
+ <p>Perform direct &#39;Hash style&#39; queries on the os_field</p>
420
+
421
+
422
+
423
+
424
+ <div class="method-source-code" id="query_by_os-source">
425
+ <pre><span class="ruby-comment"># File lib/rcmd/db.rb, line 103</span>
426
+ <span class="ruby-keyword">def</span> <span class="ruby-constant">DB</span>.<span class="ruby-identifier">query_by_os</span>(<span class="ruby-identifier">term</span>)
427
+ <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@db_os_queries</span>
428
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">RuntimeError</span>, <span class="ruby-string">&quot;OS based queries not supported in the configuration supplied&quot;</span>
429
+ <span class="ruby-keyword">end</span>
430
+ <span class="ruby-keyword">return</span> <span class="ruby-identifier">where</span>(<span class="ruby-ivar">@conf</span>[<span class="ruby-value">:os_field</span>] =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">term</span>).<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span> <span class="ruby-identifier">r</span>[<span class="ruby-ivar">@conf</span>[<span class="ruby-value">:host_field</span>]] }
431
+ <span class="ruby-keyword">end</span></pre>
432
+ </div>
433
+
434
+ </div>
435
+
436
+
437
+
438
+
439
+ </div>
440
+
441
+
442
+ <div id="method-c-query_by_type" class="method-detail ">
443
+
444
+ <div class="method-heading">
445
+ <span class="method-name">query_by_type</span><span
446
+ class="method-args">(term)</span>
447
+
448
+ <span class="method-click-advice">click to toggle source</span>
449
+
450
+ </div>
451
+
452
+
453
+ <div class="method-description">
454
+
455
+ <p>Perform direct &#39;Hash style&#39; queries on the type_field</p>
456
+
457
+
458
+
459
+
460
+ <div class="method-source-code" id="query_by_type-source">
461
+ <pre><span class="ruby-comment"># File lib/rcmd/db.rb, line 111</span>
462
+ <span class="ruby-keyword">def</span> <span class="ruby-constant">DB</span>.<span class="ruby-identifier">query_by_type</span>(<span class="ruby-identifier">term</span>)
463
+ <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@db_type_queries</span>
464
+ <span class="ruby-identifier">raise</span> <span class="ruby-constant">RuntimeError</span>, <span class="ruby-string">&quot;Type queries not supported in the configuration supplied&quot;</span>
465
+ <span class="ruby-keyword">end</span>
466
+ <span class="ruby-keyword">return</span> <span class="ruby-identifier">where</span>(<span class="ruby-ivar">@conf</span>[<span class="ruby-value">:type_field</span>] =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">term</span>).<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span> <span class="ruby-identifier">r</span>[<span class="ruby-ivar">@conf</span>[<span class="ruby-value">:host_field</span>]] }
467
+ <span class="ruby-keyword">end</span></pre>
468
+ </div>
469
+
470
+ </div>
471
+
472
+
473
+
474
+
475
+ </div>
476
+
477
+
478
+ </section>
479
+
480
+ </section>
481
+ </main>
482
+
483
+
484
+ <footer id="validator-badges" role="contentinfo">
485
+ <p><a href="http://validator.w3.org/check/referer">Validate</a>
486
+ <p>Generated by <a href="https://rdoc.github.io/rdoc">RDoc</a> 5.0.0.
487
+ <p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
488
+ </footer>
489
+
@@ -1,5 +1,6 @@
1
- Wed, 04 Oct 2017 13:18:27 +0000
1
+ Fri, 06 Oct 2017 20:18:52 +0000
2
2
  README.md Wed, 04 Oct 2017 13:14:39 +0000
3
- lib/rcmd.rb Wed, 04 Oct 2017 11:31:32 +0000
4
- lib/rcmd/version.rb Wed, 04 Oct 2017 12:27:06 +0000
5
- exe/rcmd Wed, 04 Oct 2017 12:55:00 +0000
3
+ lib/rcmd.rb Fri, 06 Oct 2017 19:05:45 +0000
4
+ lib/rcmd/db.rb Fri, 06 Oct 2017 20:07:48 +0000
5
+ lib/rcmd/version.rb Fri, 06 Oct 2017 19:04:22 +0000
6
+ exe/rcmd Fri, 06 Oct 2017 19:36:13 +0000
data/doc/rdocs/index.html CHANGED
@@ -72,6 +72,8 @@
72
72
 
73
73
  <li><a href="./Rcmd.html">Rcmd</a>
74
74
 
75
+ <li><a href="./Rcmd/DB.html">Rcmd::DB</a>
76
+
75
77
  </ul>
76
78
  </div>
77
79
 
Binary file
@@ -1 +1 @@
1
- var search_data = {"index":{"searchIndex":["optionparser","rcmd","run_command()","readme"],"longSearchIndex":["optionparser","rcmd","rcmd::run_command()",""],"info":[["OptionParser","","OptionParser.html","",""],["Rcmd","","Rcmd.html","","<p>This module is for parellel remote (SSH) execution of a single command\nstring on Multiple hosts. The …\n"],["run_command","Rcmd","Rcmd.html#method-c-run_command","()","<p>Main method for this module which should be called after the correct\nvariables have been set.\n<p>We iterate …\n"],["README","","README_md.html","","<p>Description\n<p>rcmd (remote command) is a command line utility written in Ruby for\nexecuting the same command …\n"]]}}
1
+ var search_data = {"index":{"searchIndex":["optionparser","rcmd","db","create_config()","db_connect()","load_config()","override_config_file()","query_by_hostname()","query_by_os()","query_by_type()","run_command()","readme"],"longSearchIndex":["optionparser","rcmd","rcmd::db","rcmd::db::create_config()","rcmd::db::db_connect()","rcmd::db::load_config()","rcmd::db::override_config_file()","rcmd::db::query_by_hostname()","rcmd::db::query_by_os()","rcmd::db::query_by_type()","rcmd::run_command()",""],"info":[["OptionParser","","OptionParser.html","",""],["Rcmd","","Rcmd.html","","<p>This module is for parellel remote (SSH) execution of a single command\nstring on Multiple hosts. The …\n"],["Rcmd::DB","","Rcmd/DB.html","","<p>This class provides a middle layer for allowing a database to be queried\nfor hosts. This is accomplished …\n"],["create_config","Rcmd::DB","Rcmd/DB.html#method-c-create_config","()","<p>Create a base config file\n"],["db_connect","Rcmd::DB","Rcmd/DB.html#method-c-db_connect","()","<p>Method for establishing a connection to the database backend according to\nthe configuration file. This …\n"],["load_config","Rcmd::DB","Rcmd/DB.html#method-c-load_config","()","<p>Method called by DB.db_connect to load the yaml configuration file and\nperform some basic sanity checks …\n"],["override_config_file","Rcmd::DB","Rcmd/DB.html#method-c-override_config_file","(path)","<p>Method for overiding the location and name of the default config file\n"],["query_by_hostname","Rcmd::DB","Rcmd/DB.html#method-c-query_by_hostname","(term)","<p>Performs “LIKE” queries on the host_field (hostname)\n"],["query_by_os","Rcmd::DB","Rcmd/DB.html#method-c-query_by_os","(term)","<p>Perform direct &#39;Hash style&#39; queries on the os_field\n"],["query_by_type","Rcmd::DB","Rcmd/DB.html#method-c-query_by_type","(term)","<p>Perform direct &#39;Hash style&#39; queries on the type_field\n"],["run_command","Rcmd","Rcmd.html#method-c-run_command","()","<p>Main method for this module which should be called after the correct\nvariables have been set.\n<p>We iterate …\n"],["README","","README_md.html","","<p>Description\n<p>rcmd (remote command) is a command line utility written in Ruby for\nexecuting the same command …\n"]]}}
Binary file
Binary file
@@ -51,11 +51,49 @@
51
51
  <li class="module">
52
52
  <a href="Rcmd.html">Rcmd</a>
53
53
  </li>
54
+ <li class="class">
55
+ <a href="Rcmd/DB.html">Rcmd::DB</a>
56
+ </li>
54
57
  </ul>
55
58
 
56
59
  <h2 id="methods">Methods</h2>
57
60
  <ul>
58
61
 
62
+ <li class="method">
63
+ <a href="Rcmd/DB.html#method-c-create_config">::create_config</a>
64
+ &mdash;
65
+ <span class="container">Rcmd::DB</span>
66
+
67
+ <li class="method">
68
+ <a href="Rcmd/DB.html#method-c-db_connect">::db_connect</a>
69
+ &mdash;
70
+ <span class="container">Rcmd::DB</span>
71
+
72
+ <li class="method">
73
+ <a href="Rcmd/DB.html#method-c-load_config">::load_config</a>
74
+ &mdash;
75
+ <span class="container">Rcmd::DB</span>
76
+
77
+ <li class="method">
78
+ <a href="Rcmd/DB.html#method-c-override_config_file">::override_config_file</a>
79
+ &mdash;
80
+ <span class="container">Rcmd::DB</span>
81
+
82
+ <li class="method">
83
+ <a href="Rcmd/DB.html#method-c-query_by_hostname">::query_by_hostname</a>
84
+ &mdash;
85
+ <span class="container">Rcmd::DB</span>
86
+
87
+ <li class="method">
88
+ <a href="Rcmd/DB.html#method-c-query_by_os">::query_by_os</a>
89
+ &mdash;
90
+ <span class="container">Rcmd::DB</span>
91
+
92
+ <li class="method">
93
+ <a href="Rcmd/DB.html#method-c-query_by_type">::query_by_type</a>
94
+ &mdash;
95
+ <span class="container">Rcmd::DB</span>
96
+
59
97
  <li class="method">
60
98
  <a href="Rcmd.html#method-c-run_command">::run_command</a>
61
99
  &mdash;
data/exe/rcmd CHANGED
@@ -4,6 +4,7 @@ require 'optparse'
4
4
  require 'regexp-examples'
5
5
  require 'rcmd'
6
6
 
7
+ =begin rdoc
7
8
  # This program is for executing a command on multiple nodes at the same time and accepts the following arguments.
8
9
  #
9
10
  # * -u, --username | Username for the connections
@@ -13,9 +14,9 @@ require 'rcmd'
13
14
  # * -q, --quiet | Boolean flag for suppressing stdout of executed commands
14
15
  # * -v, --version | Print version string and exit
15
16
 
16
-
17
+ =end
17
18
  # Set default options
18
- options = { :threads => 4, :nodes => nil, :environment => nil, :host_file => nil, :user => 'root', :password => false, :command => nil, :quiet => false, :version => false, :debug => false, :expression => nil, :database => false }
19
+ options = { :threads => 4, :nodes => nil, :environment => nil, :user => 'root', :password => false, :command => nil, :quiet => false, :version => false, :debug => false, :expression => nil, :database => false, :config => false, :type => nil, :host => nil, :os => nil }
19
20
 
20
21
  host_list = []
21
22
 
@@ -24,18 +25,24 @@ opts = OptionParser.new #:nodoc:
24
25
 
25
26
  opts.on('-u username', '--username username', String, "Username for SSH connections") { |v| options[:user] = v }
26
27
  opts.on('-n nodes', '--nodes x,y,z', Array, "Comma seperated list of nodes. use '-' for a space seperated list piped from another command") { |v| options[:nodes] = v }
27
- opts.on('-r regex', '--regexp regex', String, "Use Regex to build host list (ruby regexp)") { |v| options[:expression] = v}
28
- # opts.on('-d database', '--database', "Use hosts from database (~/.dbconf.yaml)") { |v| options[:database] = v}
28
+ opts.on('-r regex', '--regexp regex', Regexp, "Use Regex to build host list (ruby regexp)") { |v| options[:expression] = v}
29
29
  opts.on('-t threads', '--threads threads', Integer, "Number of threads to run") { |v| options[:threads] = v }
30
30
  opts.on('-c <command>', '--command <command>', String, "Quoted string containing the command to be run") { |v| options[:command] = v }
31
31
  opts.on('-q', '--quiet', "Suppress stdout of commands. stderr will still be displayed") { |v| options[:quiet] = v }
32
32
  opts.on('-v', '--version', "Print what version of the command is in use") { |v| options[:version] = v}
33
33
  opts.on('-D', '--debug', "Print debug information") { |v| options[:debug] = v }
34
+ opts.separator " "
35
+ opts.separator "Database Options"
36
+ opts.on('-C', '--create-config', "Create template dbconfig file in ~/.rcmd") { |v| options[:config] = v}
37
+ opts.on('-T server-type ', '--type server-type', "Database query on server type") { |v| options[:type] = v}
38
+ opts.on('-H hostname-pattern', '--host hostname-pattern', "Database query on hostname (sql like query)") { |v| options[:host] = v}
39
+ opts.on('-O operating-system', '--os operating-system", "Database query on Operating System string') { |v| options[:os] = v}
34
40
 
35
41
  # Process options
36
42
  begin
37
43
  opts.parse!(ARGV)
38
- unless options[:version] or options[:expression]
44
+ # need a smarter way then this ugly hack
45
+ unless options[:version] or options[:expression] or options[:config] or options[:type] or options[:host] or options[:os]
39
46
  mandatory = [:command, :nodes]
40
47
  missing = mandatory.select{ |param| options[param].nil? }
41
48
  raise OptionParser::MissingArgument, missing.join(', ') unless missing.empty?
@@ -48,8 +55,45 @@ end
48
55
 
49
56
  # Process Regex for node list generation
50
57
  if options[:expression]
51
- unless options[:database]
52
58
  options[:nodes] = Regexp.new(options[:expression]).examples(max_group_results: 500)
59
+ end
60
+
61
+ if options[:config]
62
+ Rcmd::DB.create_config
63
+ puts "Configuration file created. Please adjust accordingly"
64
+ exit
65
+ end
66
+
67
+ # Process database options
68
+ if options[:host]
69
+ begin
70
+ Rcmd::DB.db_connect
71
+ options[:nodes] = Rcmd::DB.query_by_hostname(options[:host])
72
+ rescue e
73
+ puts "Error:: #{e}"
74
+ exit
75
+ ensure
76
+ Rcmd::DB.connection.disconnect!
77
+ end
78
+ elsif options[:type]
79
+ begin
80
+ Rcmd::DB.db_connect
81
+ options[:nodes] = Rcmd::DB.query_by_type(options[:type])
82
+ rescue e
83
+ puts "Error:: #{e}"
84
+ exit
85
+ ensure
86
+ Rcmd::DB.connection.disconnect!
87
+ end
88
+ elsif options[:os]
89
+ begin
90
+ Rcmd::DB.db_connect
91
+ options[:nodes] = Rcmd::DB.query_by_os(options[:os])
92
+ rescue e
93
+ puts "Error:: #{e}"
94
+ exit
95
+ ensure
96
+ Rcmd::DB.connection.disconnect!
53
97
  end
54
98
  end
55
99
 
data/lib/rcmd.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "rcmd/version"
2
+ require "rcmd/db"
2
3
  require 'net/ssh'
3
4
  require 'thread'
4
5
 
@@ -16,7 +17,9 @@ module Rcmd
16
17
  STDERR.sync = true
17
18
  @queue = Queue.new
18
19
 
19
- # Self class for setting up accessors
20
+ =begin rdoc
21
+ Self class for setting up accessors
22
+ =end
20
23
  class << self
21
24
 
22
25
  # Prefered/requested number of threads to be used. (Manditory)
@@ -124,4 +127,3 @@ module Rcmd
124
127
  end
125
128
  end
126
129
  end
127
-
data/lib/rcmd/db.rb ADDED
@@ -0,0 +1,199 @@
1
+ require "rubygems"
2
+ require "active_record"
3
+ require "fileutils"
4
+ require "yaml"
5
+
6
+ module Rcmd #:notnew:
7
+
8
+ =begin rdoc
9
+
10
+ This class provides a middle layer for allowing a database to be
11
+ queried for hosts. This is accomplished by using ActiveRecord for the
12
+ middle layer and thus should support any database ActiveRecord
13
+ supports. Currently however, only the following are properly
14
+ configured for backend use in this class:
15
+
16
+ Backend Databases:
17
+ - sqlite3
18
+ - mysql
19
+ - frontbase
20
+ - firebird
21
+ - db2
22
+ - openbase
23
+ - oracle
24
+ - postgresql
25
+
26
+ This is achieved in the DB.db_connect method, which matches the
27
+ specifed adapter with the appropriate 'require' call by use of a case
28
+ statement. If you are needing/wanting to add another database to this
29
+ you will need to add your require code for the db backend there. As we
30
+ do this comparison, the various backends are not specified in the
31
+ Gemspec file as it is the responsibilty of the user to ensure the
32
+ correct gems are installed for the needed database.
33
+
34
+ This class relies on a yaml configuration file to be present and as
35
+ such the 'DB.create_config' method is provided to create an example
36
+ configuration which can be then edited. The options in the config
37
+ file are:
38
+
39
+ ---
40
+ - :adapter: sqlite3 # The adapter to be used by ActiveRecord
41
+ :host: servername # The dns/IP/server name that hosts the database
42
+ :database: ":memory:" # The database/sid name to use for the connection
43
+ :table: servers # What table should be queried in the database
44
+ :username: username # The username for the DB connection
45
+ :password: passowrd # The password for the connection
46
+ :host_field: hostname # What column/field is used for the server/host name
47
+ :type_field: stype # What column/field is used for server type (if any)
48
+ :os_field: os_type # What column/field is used for os version (if any)
49
+
50
+ The host_field is required in order to perform any queries. If the
51
+ os_field or type_fields are not specified then those respective
52
+ queries will not run and instead raise a RuntimeError; stating that
53
+ the configuration prevents those options.
54
+
55
+ The remaining fields are dependent on the database backend to
56
+ determine if they are needed or not.
57
+
58
+ Quick usage:
59
+
60
+ Rcmd::DB.db_connect
61
+ Rcmd::DB.query_by_{hostname|type|os}(query term)
62
+
63
+ Raw queries directly on the Rcmd::DB object can be used, to include
64
+ adding records, but this is outside the scope of the command and thus
65
+ not implemented.
66
+
67
+ =end rdoc
68
+
69
+ class DB < ActiveRecord::Base
70
+
71
+ @config_dir = File.expand_path("~/.rcmd")
72
+ @config_file = "#{@config_dir}/dbconfig.yml"
73
+ @conf = nil
74
+ @db_type_queries = true
75
+ @db_os_queries = true
76
+
77
+ # Method for overiding the location and name of the default config
78
+ # file
79
+ def DB.override_config_file(path)
80
+ @config_file = path
81
+ end
82
+
83
+ # Create a base config file
84
+ def DB.create_config
85
+ puts "Function: #{@config_dir}"
86
+ unless File.directory?(@config_dir)
87
+ FileUtils.mkdir_p(@config_dir)
88
+ end
89
+
90
+ conf = [ :adapter => "sqlite3", :host => nil, :database => ':memory:', :table => 'servers', :username => nil, :password => nil, :host_field => 'hostname', :type_field => 'server_type', :os_field => 'osversion' ]
91
+
92
+ File.open(@config_file, 'w') do |f|
93
+ f.write(conf.to_yaml)
94
+ end
95
+ end
96
+
97
+ # Performs "LIKE" queries on the host_field (hostname)
98
+ def DB.query_by_hostname(term)
99
+ return where("#{@conf[:host_field]} LIKE ?", term ).map { |r| r[@conf[:host_field]] }
100
+ end
101
+
102
+ # Perform direct 'Hash style' queries on the os_field
103
+ def DB.query_by_os(term)
104
+ unless @db_os_queries
105
+ raise RuntimeError, "OS based queries not supported in the configuration supplied"
106
+ end
107
+ return where(@conf[:os_field] => term).map { |r| r[@conf[:host_field]] }
108
+ end
109
+
110
+ # Perform direct 'Hash style' queries on the type_field
111
+ def DB.query_by_type(term)
112
+ unless @db_type_queries
113
+ raise RuntimeError, "Type queries not supported in the configuration supplied"
114
+ end
115
+ return where(@conf[:type_field] => term).map { |r| r[@conf[:host_field]] }
116
+ end
117
+
118
+ # Method called by DB.db_connect to load the yaml configuration
119
+ # file and perform some basic sanity checks and disabling of
120
+ # os/type queries if those fields are not in the configuration
121
+ def DB.load_config
122
+ unless File.file?(@config_file)
123
+ raise RuntimeError, "No database configuration file found: #{@config_file}"
124
+ end
125
+
126
+ @conf = YAML.load_file(@config_file)[0]
127
+
128
+ unless @conf[:adapter]
129
+ raise RuntimeError, "No adapter specified for database backend"
130
+ end
131
+
132
+ unless @conf[:type_field]
133
+ @db_type_queries = false
134
+ end
135
+
136
+ unless @conf[:os_field]
137
+ @db_os_queries = false
138
+ end
139
+
140
+ unless @conf[:adapter].downcase == "sqlite3"
141
+ if @conf[:host].nil?
142
+ raise RuntimeError, "No database host server specified"
143
+ end
144
+ if @conf[:username].nil? || @conf[:password].nil?
145
+ raise RuntimeError, "No DB credentials specified"
146
+ end
147
+ end
148
+
149
+ if @conf[:database].nil?
150
+ raise RuntimeError, "No database specified"
151
+ end
152
+
153
+ if @conf[:table].nil?
154
+ raise RuntimeError, "Table not specified"
155
+ end
156
+ end
157
+
158
+ # Method for establishing a connection to the database backend
159
+ # according to the configuration file. This is also where we set
160
+ # the 'require' the need gems to interact with the chosen DB
161
+ # adapter and set the table_name variable of the class.
162
+ def DB.db_connect
163
+ self.load_config
164
+
165
+ case @conf[:adapter].downcase
166
+ when "sqlite3"
167
+ require "sqlite3"
168
+ when "mysql"
169
+ require "mysql"
170
+ when "frontbase"
171
+ require "ruby-frontbase"
172
+ when "firebird"
173
+ require "fireruby"
174
+ when "db2"
175
+ require "ruby-db2"
176
+ when "openbase"
177
+ require "openbase"
178
+ when "oracle"
179
+ require "ruby-oci8"
180
+ when "postgresql"
181
+ require "ruby-postgres"
182
+ end
183
+
184
+ if @conf[:adapter].downcase == "sqlite3"
185
+ ActiveRecord::Base.establish_connection(:adapter => @conf[:adapter].downcase,
186
+ :database => @conf[:database])
187
+ else
188
+ ActiveRecord::Base.establish_connection(
189
+ :adapter => @conf[:adapter].downcase,
190
+ :host => @conf[:host],
191
+ :username => @conf[:username],
192
+ :password => @conf[:password],
193
+ :database => @conf[:database] )
194
+ end
195
+
196
+ @table_name = @conf[:table]
197
+ end
198
+ end
199
+ end
data/lib/rcmd/version.rb CHANGED
@@ -1,4 +1,4 @@
1
- module Rcmd #:notnew:
1
+ module Rcmd # :notnew:
2
2
  # +VERSION+ - Version number string
3
- VERSION = "1.5.8"
3
+ VERSION = "1.6.0"
4
4
  end
data/rcmd.gemspec CHANGED
@@ -3,6 +3,7 @@ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require "rcmd/version"
5
5
 
6
+
6
7
  Gem::Specification.new do |spec|
7
8
  spec.name = "rcmd"
8
9
  spec.version = Rcmd::VERSION
@@ -35,12 +36,9 @@ Gem::Specification.new do |spec|
35
36
  spec.add_runtime_dependency('io-console')
36
37
  spec.add_runtime_dependency('thread')
37
38
  spec.add_runtime_dependency('tty-prompt')
39
+ spec.add_runtime_dependency('activerecord')
38
40
  spec.add_runtime_dependency('regexp-examples')
39
41
  spec.add_development_dependency('rspec', '~> 3.0', '>= 2.0.0')
40
- # spec.add_development_dependency('activerecord', '~>4.0.0')
41
- # spec.add_development_dependency('sqlite3')
42
- # spec.add_development_dependency('nokogiri', '~>1.6.0')
43
- # spec.add_development_dependency('rails', '~>4.0.0')
44
42
  spec.add_development_dependency "bundler", "~> 1.15"
45
43
  spec.add_development_dependency "rake", "~> 10.0"
46
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rcmd
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.8
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Rose
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-04 00:00:00.000000000 Z
11
+ date: 2017-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: regexp-examples
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -151,6 +165,7 @@ files:
151
165
  - doc/rdocs/OptionParser.html
152
166
  - doc/rdocs/README_md.html
153
167
  - doc/rdocs/Rcmd.html
168
+ - doc/rdocs/Rcmd/DB.html
154
169
  - doc/rdocs/created.rid
155
170
  - doc/rdocs/css/fonts.css
156
171
  - doc/rdocs/css/rdoc.css
@@ -198,6 +213,7 @@ files:
198
213
  - doc/rdocs/table_of_contents.html
199
214
  - exe/rcmd
200
215
  - lib/rcmd.rb
216
+ - lib/rcmd/db.rb
201
217
  - lib/rcmd/version.rb
202
218
  - rcmd.gemspec
203
219
  homepage: http://www.gitlab.com/daibhidh/rcmd