central 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/central.rb +108 -45
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32c1b221cc555abfdf1fca8f3f39de5614767049
4
- data.tar.gz: 68ed3f15a8d2733429f28b2ddd8d2fd8ff2b195f
3
+ metadata.gz: ef3bac32fa508e44dc3d48d1dabb4b29bdf6e55e
4
+ data.tar.gz: 1e6a45c9cdf4fa3cc46f547e91a3512f73bb73f9
5
5
  SHA512:
6
- metadata.gz: 851be63883d051ec8a528eb565ccd142f9c327677b1bb20f24b441ef2028ac33adaeb35ca96a0e9293bbee766e40262ee0244beb9ecc4dfef8753bbba3ab10a0
7
- data.tar.gz: f17fe82282dd9e3f266cc7dc24c52b89d0a8f122dab02ec383c2ae7b5d00aa14c6fb32bf63693d3a49267c9920f46c89d4a18074983ebea521c3bc8e150019e8
6
+ metadata.gz: 2bbfc1d14c95b8702d6d91c679ef1d005603046fdfca7f00895f198c00ef6102f2069ad634dbe2ee213070fd823bd74fe1dd71595346e5eb1f18c0ac9cb35677
7
+ data.tar.gz: 181ee4734b7ab716b2a6fb66c230d4478ee377a6ada8e46787b08941545aebc47c6e1272ad4607aa6ce718b4bd28007ee94624e0d068a52f3b40936aad7e601b
data/lib/central.rb CHANGED
@@ -5,6 +5,7 @@
5
5
 
6
6
  require 'erb'
7
7
  require 'socket'
8
+ require 'open3'
8
9
 
9
10
  # get hostname
10
11
  def hostname
@@ -24,40 +25,102 @@ def os
24
25
  end
25
26
  end
26
27
 
27
- # run command second time with sudo privileges if it previously failed because of insufficient permissions
28
- def sudo(command,sudo=false)
29
- if sudo
30
- sudo = 'sudo '
31
- else
32
- sudo = ''
33
- end
34
- command = sudo+command
35
- out = `#{command} 2>&1`
36
- # removing line feed
37
- if out.length > 0 && out[-1].ord == 10
38
- out = out[0...-1]
39
- end
40
- # removing cariage return
41
- if out.length > 0 && out[-1].ord == 13
42
- out = out[0...-1]
43
- end
44
- if out.downcase.end_with?('permission denied')
45
- if sudo
46
- STDERR.puts "Couldn't execute #{command} due to permission denied\nrun central with sudo privileges"
47
- exit 1
48
- else
49
- out = sudo(command,true)
28
+ def linux?
29
+ return os == 'linux'
30
+ end
31
+
32
+ def osx?
33
+ return os == 'osx'
34
+ end
35
+
36
+ def freebsd?
37
+ return os == 'freebsd'
38
+ end
39
+
40
+ def solaris?
41
+ return os == 'solaris'
42
+ end
43
+
44
+ # run shell command and get output, optionaly can print command running if verbose and if not silent will also print to stdout and stderr
45
+ $shell_return_value = nil
46
+ def shell(command,options={:verbose => false, :silent => true})
47
+ silent = options[:silent]
48
+ puts "Executing: #{command}" if options[:verbose]
49
+ stdout = ''
50
+ stdout_line = ''
51
+ stderr = ''
52
+ stderr_line = ''
53
+ Open3.popen3(command) do |i,o,e,t|
54
+ stdout_open = true
55
+ stderr_open = true
56
+ while stdout_open or stderr_open
57
+ if stdout_open
58
+ begin
59
+ ch = o.read_nonblock(1)
60
+ stdout.insert(-1,ch)
61
+ unless silent
62
+ stdout_line.insert(-1,ch)
63
+ if ch == "\n"
64
+ STDOUT.puts stdout_line
65
+ stdout_line = ''
66
+ end
67
+ end
68
+ rescue IO::WaitReadable
69
+ IO.select([o],nil,nil,0.1) unless stderr_open
70
+ rescue EOFError
71
+ stdout_open = false
72
+ end
73
+ end
74
+ if stderr_open
75
+ begin
76
+ ch = e.read_nonblock(1)
77
+ stderr.insert(-1,ch)
78
+ unless silent
79
+ stderr_line.insert(-1,ch)
80
+ if ch == "\n"
81
+ STDERR.puts stderr_line
82
+ stderr_line = ''
83
+ end
84
+ end
85
+ rescue IO::WaitReadable
86
+ IO.select([e],nil,nil,0.1) unless stdout_open
87
+ rescue EOFError
88
+ stderr_open = false
89
+ end
90
+ end
50
91
  end
92
+ $shell_return_value = t.value
93
+ end
94
+ if stderr == ''
95
+ return stdout
96
+ else
97
+ return stdout, stderr
51
98
  end
52
- return out
99
+ end
100
+
101
+ # run shell command with sudo prefix, acts same as shell
102
+ def sudo(command,options)
103
+ return shell('sudo '+command, options)
53
104
  end
54
105
 
55
106
  # function used to check that system has all required tools installed
56
107
  def check_tool(name,check)
57
- output = sudo("#{check} 2>&1 1>/dev/null").downcase
58
- if output.include?('command not found')
108
+ begin
109
+ output = shell(check)
110
+ if output.kind_of?(String)
111
+ output = output.downcase
112
+ if output == '' or output.include?('command not found')
113
+ STDERR.puts "#{name} not found, please install it to use central"
114
+ exit 1
115
+ end
116
+ elsif output.kind_of?(Array)
117
+ if output[0] == '' and output[1] == ''
118
+ STDERR.puts "#{name} not found, please install it to use central"
119
+ exit 1
120
+ end
121
+ end
122
+ rescue Errno::ENOENT
59
123
  STDERR.puts "#{name} not found, please install it to use central"
60
- exit 1
61
124
  end
62
125
  end
63
126
 
@@ -108,14 +171,14 @@ end
108
171
 
109
172
  # get full path of symlink
110
173
  def symlink_path(symlink)
111
- sudo("readlink \"#{abs(symlink)}\"")
174
+ shell("readlink \"#{abs(symlink)}\"").strip
112
175
  end
113
176
 
114
177
  # make directory including intermediate directories
115
178
  def mkdir(path)
116
179
  path = abs(path)
117
180
  unless dir_exists?(path)
118
- out = sudo("mkdir -p \"#{path}\"")
181
+ out = shell("mkdir -p \"#{path}\"")
119
182
  puts "Created directory: #{path}"
120
183
  end
121
184
  end
@@ -130,7 +193,7 @@ def rm(path,recursive=false)
130
193
  end
131
194
  is_dir = dir_exists?(path)
132
195
  is_symlink = symlink?(path)
133
- out = sudo("rm #{recursive}-f \"#{path}\"")
196
+ out = shell("rm #{recursive}-f \"#{path}\"")
134
197
  if is_dir
135
198
  puts "Removed directory: #{path}"
136
199
  elsif is_symlink
@@ -149,7 +212,7 @@ end
149
212
  def touch(path)
150
213
  path = abs(path)
151
214
  unless file_exists?(path)
152
- out = sudo("touch \"#{path}\"")
215
+ out = shell("touch \"#{path}\"")
153
216
  puts "Touched file: #{path}"
154
217
  end
155
218
  end
@@ -162,7 +225,7 @@ def chmod(path,permissions,recursive=false)
162
225
  else
163
226
  recursive = ''
164
227
  end
165
- sudo("chmod #{recursive}#{permissions} \"#{path}\"")
228
+ shell("chmod #{recursive}#{permissions} \"#{path}\"")
166
229
  end
167
230
 
168
231
  # symlink path
@@ -181,30 +244,30 @@ def symlink(from,to)
181
244
  STDERR.puts "Directory #{from} exists in place of symlink..."
182
245
  exit 1
183
246
  else
184
- out = sudo("ln -s \"#{to}\" \"#{from}\"")
247
+ out = shell("ln -s \"#{to}\" \"#{from}\"")
185
248
  puts "Created symlink: #{from} → #{to}"
186
249
  end
187
250
  end
188
251
 
189
252
  # git clone url into a path
190
- def git(url,path,branch=nil)
253
+ def git(url,path,branch=nil,silent=false)
191
254
  path = abs(path)
192
255
  if dir_exists?(path) && dir_exists?("#{path}/.git")
193
256
  cwd = pwd()
194
257
  chdir path
195
258
  out = nil
196
259
  if branch
197
- out = sudo('git fetch')
260
+ out = shell('git fetch',{:silent => silent})
198
261
  if out.size > 0
199
262
  puts out
200
263
  end
201
- out = sudo("git checkout #{branch}")
264
+ out = shell("git checkout #{branch}",{:silent => silent})
202
265
  unless out.downcase.include? 'is now at'
203
266
  puts out
204
267
  end
205
- out = sudo("git pull origin #{branch}")
268
+ out = shell("git pull origin #{branch}",{:silent => silent})
206
269
  else
207
- out = sudo('git pull')
270
+ out = shell('git pull',{:silent => silent})
208
271
  end
209
272
  unless out.downcase.include? "already up-to-date"
210
273
  puts out
@@ -217,17 +280,17 @@ def git(url,path,branch=nil)
217
280
  else
218
281
  branch = ''
219
282
  end
220
- out = sudo("git clone #{branch}#{url} \"#{path}\"")
283
+ out = shell("git clone #{branch}#{url} \"#{path}\"",{:silent => silent})
221
284
  puts out
222
285
  puts "Git repository cloned: #{url} → #{path}"
223
286
  end
224
287
  end
225
288
 
226
289
  # download url into a path using curl
227
- def curl(url,path)
290
+ def curl(url,path,verbose=false)
228
291
  path = abs(path)
229
- output = sudo("curl -s \"#{url}\"")
230
- unless $?.exitstatus == 0
292
+ output = shell("curl -s -S \"#{url}\"",{:verbose => verbose, :silent => true})
293
+ unless $shell_return_value.success?
231
294
  STDERR.puts "Couldn't download file from #{url}..."
232
295
  exit 1
233
296
  end
@@ -260,9 +323,9 @@ def source(file,source)
260
323
  file = abs(file)
261
324
  source = abs(source)
262
325
  source_line = "source \"#{source}\""
263
- out = sudo("grep -Fx '#{source_line}' \"#{file}\"")
326
+ out = shell("grep -Fx '#{source_line}' \"#{file}\"")
264
327
  if out == ""
265
- sudo("echo '#{source_line}' >> \"#{file}\"")
328
+ shell("echo '#{source_line}' >> \"#{file}\"")
266
329
  puts "Added source #{source} line to #{file}"
267
330
  end
268
331
  end
@@ -279,7 +342,7 @@ def ls(path,options={})
279
342
  if options.key?(:grep) && options[:grep].length > 0
280
343
  command += " | grep #{options[:grep]}"
281
344
  end
282
- output = sudo(command)
345
+ output = shell(command)
283
346
  if output.downcase.end_with?('no such file or directory')
284
347
  STDERR.puts "Couldn't ls directory #{path}..."
285
348
  exit 1
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: central
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Geurkov