forked from atorlee/SUSE-Manager-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_rereg.py
executable file
·168 lines (150 loc) · 5.63 KB
/
system_rereg.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
#!/usr/bin/env python3
#
# system_rereg.py
#
# (c) 2018 SUSE Linux GmbH, Germany.
# GNU Public License. No warranty. No Support (only from SUSE Consulting
#
# Version: 2020-04-2
#
# Created by: SUSE Michael Brookhuis
#
# This script will re-register a system against a proxy.
#
# Releases:
# 2019-12-11 M.Brookhuis - Initial release
# 2020-04-02 M.Brookhuis - Minor changes
#
"""
This script re-register a system against a proxy
"""
import argparse
from argparse import RawTextHelpFormatter
import xmlrpc.client
import os
import datetime
import smtools
import time
__smt = None
def perform_rereg(server, proxy):
"""
Apply configuration
"""
smt.set_hostname(server)
sid = smt.get_server_id_nofatal()
print(server)
try:
entitlement = smt.client.system.getDetails(smt.session, sid)
except xmlrpc.client.Fault:
smt.log_error('Unable to retrieve entitlement data')
return
try:
rereg_key = smt.client.system.obtainReactivationKey(smt.session, sid)
except xmlrpc.client.Fault:
smt.log_error("Unable to generate reactivation key for {}.".format(server))
return
if entitlement.get('base_entitlement') == "salt_entitled":
script = """#!/bin/bash
#
# script to migrate minion connection to SUSE Manager Proxy from
# either a SUSE Manager Server or another SUSE Manager Proxy
rc=0
# determine config file in which the master is defined
master_conf="$(grep -l -e '^ *master:' /etc/salt/minion.d/*.conf /etc/salt/minion)"
if [ -n "$master_conf" ] ; then
# we found the file, switch to new proxy
sed -i -r 's/^( *master: *).*/\\1{proxy}/' $master_conf
# determine config file in which the activation key is defined
key_conf="$(grep -l -e '^ *activation_key:' /etc/salt/minion.d/*.conf /etc/salt/minion)"
if [ -n "$key_conf" ] ; then
# we found a file, remember original value and replace with re-activation key
actkey_restore=$(grep -e '^ *activation_key:' $key_conf)
sed -i -r 's/^( *)activation_key:.*/\\1management_key: {key}/' $key_conf
else
# no file found. Where is susemanager grain defined?
key_conf="$(grep -l -e '^ *susemanager:' /etc/salt/minion.d/*.conf /etc/salt/minion)"
if [ -n "$key_conf" ] ; then
# found it. Add re-activation key.
sed -i -r 's/(^ *)(susemanager:) */\\1\\2\\n\\1\\1management_key: {key}/' $key_conf
else
# not found either. Create file and add required grains
key_conf=$(mktemp -p /etc/salt/minion.d/ -t generated_minionconf_XXX --suffix=.conf)
cat > $key_conf << EOF
### generated by minion migration script $0 on $(date)
grains:
susemanager:
management_key: {key}
EOF
fi
fi
sleep 10
systemctl restart salt-minion.service
sleep 15
# do we have an original activation key?
if [ -n "$actkey_restore" ] ; then
# substitute re-activation key by original activation key
sed -i -r "s/^ *management_key:.*/$actkey_restore/" $key_conf
else
# just delete re-activation key
sed -i -r "/^ *management_key:.*/d" $key_conf
fi
else
echo "$0: minion configuration file not found. Aborting." >&2
rc=1
fi
exit $rc""".format(proxy=proxy, key=rereg_key)
else:
script = "#!/bin/bash\nrhnreg_ks --activationkey={} --serverUrl=https://{}/XMLRPC --force\n".format(rereg_key, proxy)
try:
smt.client.system.scheduleScriptRun(smt.session, sid, 'root', 'root', 6000, script, datetime.datetime.now())
except xmlrpc.client.Fault as e:
smt.log_error("unable to schedule a script run for server. Error: {}".format(e))
def rereg_server(args):
"""
start update process
"""
if args.server and args.file:
smt.fatal_error("please select only 1 option and not both --server and --file")
if args.server:
perform_rereg(args.server, args.proxy)
if args.file:
if os.path.exists(args.file):
try:
sf = open(args.file, "r")
except:
smt.fatal_error("Given file {} doesn't exists. aborting".format(args.file))
for line in sf:
perform_rereg(line.rstrip(), args.proxy)
time.sleep(5)
else:
smt.fatal_error("Given file {} doesn't exists. aborting".format(args.file))
def main():
"""
Main function
"""
global smt
parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter, description=('''\
Usage:
system_update.py
'''))
parser.add_argument('-s', '--server', help='name of the server to be re-registered.')
parser.add_argument('-p', '--proxy',
help='name of the proxy server the system should be registered against. Required')
parser.add_argument('-f', '--file',
help='file with list of servers to be re-registered. There should be 1 server per line')
parser.add_argument('--version', action='version', version='%(prog)s 0.0.2, April 2, 2020')
args = parser.parse_args()
smt = smtools.SMTools("system_update")
if not args.proxy:
smt.log_error("The option --proxy is mandatory. Exiting script")
smt.exit_program(1)
else:
smt.log_info("Start")
smt.suman_login()
smt.set_hostname(args.proxy)
dummy = smt.get_server_id()
# login to suse manager
rereg_server(args)
smt.close_program()
if __name__ == "__main__":
SystemExit(main())