central 0.1.2 → 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 +4 -4
- data/lib/central.rb +108 -45
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef3bac32fa508e44dc3d48d1dabb4b29bdf6e55e
|
4
|
+
data.tar.gz: 1e6a45c9cdf4fa3cc46f547e91a3512f73bb73f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
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
|
-
|
58
|
-
|
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
|
-
|
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 =
|
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 =
|
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 =
|
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
|
-
|
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 =
|
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 =
|
260
|
+
out = shell('git fetch',{:silent => silent})
|
198
261
|
if out.size > 0
|
199
262
|
puts out
|
200
263
|
end
|
201
|
-
out =
|
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 =
|
268
|
+
out = shell("git pull origin #{branch}",{:silent => silent})
|
206
269
|
else
|
207
|
-
out =
|
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 =
|
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 =
|
230
|
-
unless
|
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 =
|
326
|
+
out = shell("grep -Fx '#{source_line}' \"#{file}\"")
|
264
327
|
if out == ""
|
265
|
-
|
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 =
|
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
|