forked from RedHatSatellite/sat6_scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_sync.py
135 lines (113 loc) · 5 KB
/
check_sync.py
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
#!/usr/bin/python
#title :check_sync.py
#description :Checks Satellite 6 repository sync status
#URL :https://github.com/RedHatSatellite/sat6_disconnected_tools
#author :Geoff Gatward <[email protected]>
#notes :This script is NOT SUPPORTED by Red Hat Global Support Services.
#license :GPLv3
#==============================================================================
"""
Checks the status of Sync tasks.
Draws the users attention to problems in the sync tasks that could lead to
inconsistent repository states.
Call with -l switch to loop until all sync tasks are complete, otherwise runs
as a one-shot check.
"""
import sys, os, argparse, time
import simplejson as json
import helpers
def check_running_tasks(clear):
"""
Check for any currently running Sync tasks
Checks for any Synchronize tasks in running/paused or Incomplete state.
"""
#pylint: disable-msg=R0912,R0914,R0915
# Clear the screen
if clear:
os.system('clear')
print helpers.HEADER + "Checking for running/paused yum sync tasks..." + helpers.ENDC
tasks = helpers.get_p_json(
helpers.FOREMAN_API + "tasks/", \
json.dumps(
{
"per_page": "100",
}
))
# From the list of tasks, look for any running export or sync jobs.
# If e have any we exit, as we can't export in this state.
running_sync = 0
for task_result in tasks['results']:
if task_result['state'] == 'running' and task_result['label'] != 'Actions::BulkAction':
if task_result['humanized']['action'] == 'Synchronize':
running_sync = 1
print helpers.BOLD + "Running: " + helpers.ENDC \
+ task_result['input']['repository']['name']
if task_result['state'] == 'paused' and task_result['label'] != 'Actions::BulkAction':
if task_result['humanized']['action'] == 'Synchronize':
running_sync = 1
print helpers.ERROR + "Paused: " + helpers.ENDC \
+ task_result['input']['repository']['name']
if not running_sync:
print helpers.GREEN + "None detected" + helpers.ENDC
# Check any repos marked as Sync Incomplete
print helpers.HEADER + "\nChecking for incomplete (stopped) yum sync tasks..." + helpers.ENDC
repo_list = helpers.get_json(
helpers.KATELLO_API + "/content_view_versions")
# Extract the list of repo ids, then check the state of each one.
incomplete_sync = 0
for repo in repo_list['results']:
for repo_id in repo['repositories']:
repo_status = helpers.get_json(
helpers.KATELLO_API + "/repositories/" + str(repo_id['id']))
if repo_status['content_type'] == 'yum':
if repo_status['last_sync'] is None:
if repo_status['library_instance_id'] is None:
# incomplete_sync = 1
# print helpers.ERROR + "Broken Repo: " + helpers.ENDC + repo_status['name']
print helpers.WARNING + "Never Synchronized: " + helpers.ENDC + repo_status['name']
elif repo_status['last_sync']['state'] == 'stopped':
if repo_status['last_sync']['result'] == 'warning':
incomplete_sync = 1
print helpers.WARNING + "Incomplete: " + helpers.ENDC + repo_status['name']
else:
msg = repo_status['name'] + " - last_sync: " + repo_status['last_sync']['ended_at']
helpers.log_msg(msg, 'DEBUG')
# If we have detected incomplete sync tasks, ask the user if they want to export anyway.
# This isn't fatal, but *MAY* lead to inconsistent repositories on the dieconnected sat.
if not incomplete_sync:
print helpers.GREEN + "No incomplete syncs detected\n" + helpers.ENDC
else:
print "\n"
# Exit the loop if both tests are clear
if not running_sync and not incomplete_sync:
sys.exit(0)
def main(args):
"""
Main Routine
"""
#pylint: disable-msg=R0914,R0915
parser = argparse.ArgumentParser(description='Checks status of yum repository sync tasks.')
# pylint: disable=bad-continuation
parser.add_argument('-l', '--loop', help='Loop check until all tasks complete', required=False,
action="store_true")
args = parser.parse_args()
# Check if there are any currently running tasks that will conflict with an export
# Loop until all tasks are compltete.
if args.loop:
try:
while True:
clear = True
check_running_tasks(clear)
time.sleep(5)
except KeyboardInterrupt:
print "End"
else:
clear = False
check_running_tasks(clear)
sys.exit(0)
if __name__ == "__main__":
try:
main(sys.argv[1:])
except KeyboardInterrupt, e:
print >> sys.stderr, ("\n\nExiting on user cancel.")
sys.exit(1)