-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharsa2.py
executable file
·179 lines (147 loc) · 8.72 KB
/
arsa2.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
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
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python
# arsa.py - a script for archiving and removing old
# Spacewalk, Red Hat Satellite or SUSE Manager actions.
#
# 2014 By Christian Stankowic
# <info at stankowic hyphen development dot net>
# https://github.com/stdevel
#
from optparse import OptionParser
import xmlrpclib
import os
import stat
import getpass
import time
#list of supported API levels
supportedAPI = ["11.1","12","13","13.0","14","14.0","15","15.0"]
if __name__ == "__main__":
#define description, version and load parser
desc='''%prog is used to archive completed actions and remove archived actions on Spacewalk, Red Hat Satellite and SUSE Manager. Login credentials are assigned using the following shell variables:
SATELLITE_LOGIN username
SATELLITE_PASSWORD password
It is also possible to create an authfile (permissions 0600) for usage with this script. The first line needs to contain the username, the second line should consist of the appropriate password.
If you're not defining variables or an authfile you will be prompted to enter your login information.
Checkout the GitHub page for updates: https://github.com/stdevel/arsa'''
parser = OptionParser(description=desc,version="%prog version 0.3")
#-a / --authfile
parser.add_option("-a", "--authfile", dest="authfile", metavar="FILE", default="", help="defines an auth file to use instead of shell variables")
#-s / --server
parser.add_option("-s", "--server", dest="server", metavar="SERVER", default="localhost", help="defines the server to use")
#-q / --quiet
parser.add_option("-q", "--quiet", action="store_false", dest="verbose", default=True, help="don't print status messages to stdout")
#-d / --debug
parser.add_option("-d", "--debug", dest="debug", default=False, action="store_true", help="enable debugging outputs")
#-r / --remove
parser.add_option("-r", "--remove", dest="removeAll", default=False, action="store_true", help="archives completed actions and removes all archived actions")
#-c / --remove-archived
parser.add_option("-c", "--remove-archived", dest="removeArch", default=False, action="store_true", help="removes all archived actions")
#-l / --list-only
parser.add_option("-l", "--list-only", dest="listonly", default=False, action="store_true", help="only lists actions that would be archived")
#-f / --include-failed
parser.add_option("-f", "--include-failed", dest="includeFailed", default=False, action="store_true", help="also include failed actions")
#parse arguments
(options, args) = parser.parse_args()
#define URL and login information
SATELLITE_URL = "http://"+options.server+"/rpc/api"
#setup client and key depending on mode
client = xmlrpclib.Server(SATELLITE_URL, verbose=options.debug)
if options.authfile:
#use authfile
if options.debug: print "DEBUG: using authfile"
try:
#check filemode and read file
filemode = oct(stat.S_IMODE(os.lstat(options.authfile).st_mode))
if filemode == "0600":
if options.debug: print "DEBUG: file permission ("+filemode+") matches 0600"
fo = open(options.authfile, "r")
s_username=fo.readline()
s_password=fo.readline()
key = client.auth.login(s_username, s_password)
else:
if options.verbose: print "ERROR: file permission ("+filemode+") not matching 0600!"
exit(1)
except OSError:
print "ERROR: file non-existent or permissions not 0600!"
exit(1)
elif "SATELLITE_LOGIN" in os.environ and "SATELLITE_PASSWORD" in os.environ:
#shell variables
if options.debug: print "DEBUG: checking shell variables"
key = client.auth.login(os.environ["SATELLITE_LOGIN"], os.environ["SATELLITE_PASSWORD"])
else:
#prompt user
if options.debug: print "DEBUG: prompting for login credentials"
s_username = raw_input("Username: ")
s_password = getpass.getpass("Password: ")
key = client.auth.login(s_username, s_password)
#check whether the API version matches the minimum required
api_level = client.api.getVersion()
if not api_level in supportedAPI:
print "ERROR: your API version ("+api_level+") does not support the required calls. You'll need API version 1.8 (11.1) or higher!"
exit(1)
else:
if options.debug: print "INFO: supported API version ("+api_level+") found."
#retrieve completed, already archived and failed actions
toArchive = []
completed_actions = client.schedule.listCompletedActions(key)
archived_actions = client.schedule.listArchivedActions(key)
failed_actions = client.schedule.listFailedActions(key)
#print actions
if options.debug: print "completed:\n"+`completed_actions`+"\narchived:\n"+`archived_actions`
#go through completed actions and remove them if wanted
if options.listonly: print "things I'd like to clean (completed):\n-------------------------------------"
for entry in completed_actions:
if options.verbose: print "Found completed action #"+`entry["id"]`+" ("+`entry["name"]`+")..."
toArchive.append(entry["id"])
#also clean-up already archived actions if wanted
if options.removeAll:
#remove archived actions
if options.listonly: print "\nthings I'd like to remove (archived):\n-------------------------------------"
for entry in archived_actions:
if options.verbose: print "Found archived action #"+`entry["id"]`+" ("+`entry["name"]`+")..."
toArchive.append(entry["id"])
#also clean-up failed actions if wanted
if options.includeFailed:
#remove failed actions
if options.listonly: print "\nthings I'd like to remove (failed):\n-----------------------------------"
for entry in failed_actions:
if options.verbose: print "Found failed action #"+`entry["id"]`+" ("+`entry["name"]`+")..."
toArchive.append(entry["id"])
#Only clean-up already archived actions
if options.removeArch:
# make sure the list only contains archived stuff
toArchive = []
#remove archived actions
if options.listonly: print "\nthings I'd like to remove (archived):\n-------------------------------------"
for entry in archived_actions:
if options.verbose: print "Found archived action #"+`entry["id"]`+" ("+`entry["name"]`+")..."
toArchive.append(entry["id"])
#archive (and remove) actions if wanted
if options.debug: print "\nINFO: toArchive:" + str(`toArchive`)
#removing duplicate entries
toArchive = list(set(toArchive))
#remove actions if listonly not set
if options.listonly == False:
if options.verbose: print "Archiving actions..."
#enable 100 actions-per-call workaround if we dug hundreds actions
if len(toArchive) > 100:
if options.verbose: print "Enabling workaround to archive/delete more than 100 actions..."
tempActions = []
for action in toArchive:
if len(tempActions) != 100:
tempActions.append(action)
else:
print tempActions
client.schedule.archiveActions(key,tempActions)
time.sleep(.5)
if options.removeAll or options.removeArch:
client.schedule.deleteActions(key,tempActions)
time.sleep(.5)
tempActions = []
else:
client.schedule.archiveActions(key,toArchive)
if options.removeAll or options.removeArch:
client.schedule.deleteActions(key,toArchive)
else:
if options.verbose: print "Stopping here as we don't really want to remove actions..."
#logout and exit
client.auth.logout(key)