This repository was archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupper.rb
More file actions
executable file
·81 lines (62 loc) · 1.97 KB
/
Copy pathupper.rb
File metadata and controls
executable file
·81 lines (62 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env ruby
require 'json'
require 'open3'
require 'optparse'
# TODO: config option to specify command which lets check if update was executed or not (and what was updated at all)
class Updater
CONFIG_DIR = '.upper'
def initialize(verbose=false)
@base_dir = File.join Dir.home, CONFIG_DIR
@verbose = verbose
@tasks = {}
Dir.foreach(@base_dir) do |filename|
path = File.join @base_dir, filename
if File.file? path
config = JSON.load(File.new(path))
@tasks[filename] = {
:cmd => config["cmd"],
:hosts => config["hosts"]
}
end
end
end
def start!
# puts "================================================================================"
puts ">> Starting updates"
# puts "================================================================================"
@tasks.each do |name, config|
if config[:hosts]
config[:hosts].each { |host| run name, config[:cmd], host }
else
run name, config[:cmd]
end
end
end
private
def log(msg, prefix=nil)
prefix = " " if prefix.nil?
puts " #{prefix} #{msg}"
end
def run(name, cmd, host=nil)
log "Updating #{name}", "="
log "Command: #{cmd}" if @verbose
whole_cmd = (if host.nil? then cmd else `ssh #{host} "#{cmd}"` end).strip
Open3.popen3(whole_cmd) do |stdin, stdout, stderr, wait_thr|
stdin.close
stdout.each_line do |line|
log "#{line.strip}", "|"
end
unless wait_thr.value.success?
log "Error", "!"
end
end
end
end
verbose = false
OptionParser.new do |opts|
opts.on "-v", "--[no-]verbose", "Run verbosely" do |v|
verbose = true
end
end.parse!
updater = Updater.new verbose
updater.start!