This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
package-updater.rb
executable file
·167 lines (127 loc) · 3.69 KB
/
package-updater.rb
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/ruby
# A simple script that uses the new batch mode available in
# mcollective 1.3.3 and newer to do package updates:
#
# pacakge-updater.rb --batch 10
#
# This will give you the chance to clean yum cache everywhere
# and then it will:
#
# - use the checkupdates action to get a list of available
# updates on all machines
# - present you with a menu of available updates
# - the package you picked will be updated in batches of 10
# machines at a time
#
# This loops until nothing is left to update
#
# While mco 1.3.2 has batching support there's been some refinements
# that this script relies on, do not use it on older mcollectives
#
# R.I.Pienaar / rip@devco.net / @ripienaar / http://devco.net/
require 'mcollective'
include MCollective::RPC
STDOUT.sync = true
STDERR.sync = true
def err(msg)
STDERR.puts "EEE> #{msg}"
end
def msg(msg)
puts ">>>> #{msg}"
end
def ask(msg)
print "#{msg} (y/n) "
ans = STDIN.gets.chomp.downcase
ans == "y"
end
def get_updates_due(agent)
updates = {}
agent.reset
msg "Checking for updates on #{agent.discover.size} servers"
agent.checkupdates(:batch_size => 0) do |r, s|
begin
outdated = [s[:data][:outdated_packages]].compact.flatten
unless outdated.empty?
msg "Found %d updates for %s" % [outdated.size, s[:sender]]
outdated.each do |pkg|
name = pkg[:package]
updates[name] ||= []
updates[name] << s[:sender]
end
else
msg "Found no updates for %s" % [ s[:sender] ]
end
rescue => e
err("Failed to parse data: #{e}: #{r.pretty_inspect}")
end
end
updates
end
def print_list(updates)
updates.keys.sort.each_with_index do |pkg, idx|
puts "%3d> %s on %d hosts" % [idx, pkg, updates[pkg].size]
end
puts
puts " r> Refresh updates list"
puts " q> Quit"
end
def update_pkg(updates, selection, agent)
pkg = updates.keys.sort[selection]
puts
msg "Updating %s on %d servers in batches of %d" % [pkg, updates[pkg].size, agent.batch_size]
agent.discover :hosts => updates[pkg]
versions = {}
agent.update(:package => pkg).each_with_index do |resp, i|
puts if i == 0
status = resp[:data][:properties]
if resp[:statuscode] == 0
if status.include?(:version)
version = "#{status[:version]}-#{status[:release]}"
elsif status.include?(:ensure)
version = status[:ensure].to_s
end
versions.include?(version) ? versions[version] += 1 : versions[version] = 1
printf("%-40s version = %s-%s\n", resp[:sender], status[:name], version)
else
printf("%-40s error = %s\n", resp[:sender], resp[:statusmsg])
end
end
puts
msg "Versions: %s" % [ versions.keys.sort.map {|s| "#{versions[s]} * #{s}" }.join(", ") ]
puts
if versions.keys.size == 1
return pkg
else
err "Some updates of #{pkg} failed, got %d versions" % [ versions.keys.size ]
end
end
@agent = rpcclient("package")
if @agent.batch_size == 0
exit unless ask("Are you sure you wish to continue without specifying a batch size using --batch?")
end
printrpc(@agent.yum_clean(:batch_size => 0)) if ask("Would you like to clear the yum cache everywhere?")
updates_due = get_updates_due(@agent)
until updates_due.empty?
begin
print_list(updates_due)
puts
print "Pick a package to update: "
choice = STDIN.gets.chomp.downcase
if choice == "r"
updates_due = get_updates_due(@agent)
next
elsif choice == "q"
exit
end
pkg = Integer(choice)
updates_due.delete(update_pkg(updates_due, pkg, @agent))
rescue Interrupt
exit
rescue SystemExit
raise
rescue => e
err "#{e.class}: #{e}"
err e.backtrace.pretty_inspect
retry
end
end