Skip to content

Commit 9cf1506

Browse files
committed
Add procedure to remove corrupted host statuses
1 parent 38b9c6c commit 9cf1506

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module Procedures::Foreman
2+
class RemoveCorruptedHostStatuses < ForemanMaintain::Procedure
3+
metadata do
4+
for_feature :foreman_database
5+
description 'Remove corrupted host statuses'
6+
end
7+
8+
def run
9+
corrupted = collect_host_status_with_type_nil
10+
if corrupted.empty?
11+
skip("No corrupted host status. Everything fine. Exit.")
12+
end
13+
14+
print("Detected corrupted host statuses:\n")
15+
corrupted.each do |s|
16+
print("Host #{s['host_name']} has corrupted host_status with id #{s['host_status_id']}\n")
17+
end
18+
19+
answer = ask_decision("Do you want to remove the empty host statuses", actions_msg: 'y(yes), q(quit)')
20+
abort! unless answer == :yes
21+
22+
with_spinner('Removing corrupted host statuses') do
23+
delete_host_status_with_type_nil
24+
end
25+
end
26+
27+
private
28+
29+
def collect_host_status_with_type_nil
30+
feature(:foreman_database).query(
31+
"SELECT hosts.name AS host_name, host_status.id AS host_status_id
32+
FROM hosts, host_status
33+
WHERE host_status.type IS NULL AND hosts.id = host_status.host_id"
34+
)
35+
end
36+
37+
def delete_host_status_with_type_nil
38+
feature(:foreman_database).psql(
39+
"DELETE FROM host_status WHERE type IS NULL"
40+
)
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)