forked from dagwieers/mrepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gensystemid
executable file
·292 lines (242 loc) · 8.8 KB
/
gensystemid
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/python
import getopt
import getpass
import os
import sys
import urlparse
if os.path.exists('/usr/share/mrepo/up2date_client/'):
sys.path.insert(-1, '/usr/share/mrepo/')
sys.path.insert(-1, '/usr/share/mrepo/up2date_client/')
elif os.path.exists('/usr/share/rhn/up2date_client/'):
sys.path.insert(-1, '/usr/share/rhn/')
sys.path.insert(-1, '/usr/share/rhn/up2date_client/')
else:
print >> sys.stderr, 'rhnget: up2date libraries are not installed. Aborting execution'
sys.exit(1)
from up2date_client import config, rpcServer, up2dateErrors
from rhn import rpclib
global cfg, loginInfo
def checkrelease(release, arch):
### We are not sure about the architectures below :-/
releases = {
'6Workstation': ('i386', 'x86_64'),
'6Server': ('i386', 'ppc', 's390', 's390x', 'x86_64'),
'6ComputeNode': ('i386', 'ppc', 's390', 's390x', 'x86_64'),
'6Client': ('i386', 'x86_64'),
'5Server': ('i386', 'ia64', 'ppc', 's390', 's390x', 'x86_64'),
'5Client': ('i386', 'ia64', 'x86_64'),
'4AS': ('i386', 'ia64', 'ppc', 's390', 's390x', 'x86_64'),
'4ES': ('i386', 'ia64', 'x86_64'),
'4WS': ('i386', 'ia64', 'x86_64'),
'4Desktop': ('i386', 'x86_64'),
'3AS': ('i386', 'ia64', 'ppc', 's390', 's390x', 'x86_64'),
'3ES': ('i386', 'ia64', 'x86_64'),
'3WS': ('i386', 'ia64', 'x86_64'),
'3Desktop': ('i386', 'x86_64'),
'2.1AS': ('i386', 'ia64'),
'2.1ES': ('i386', ),
'2.1WS': ('i386', ),
'2.1AW': ('ia64', ),
}
if release not in releases.keys():
raise Exception, 'Release name %s is not a known RHN release.' % release
if arch not in releases[release]:
raise Exception, 'RHN release %s does not exist for architecture %s.' % (release, arch)
return True
def lowarch(arch):
archs = {
'i386': ['i486', 'i586', 'i686', 'athlon'],
'ia64': [],
'ppc': ['ppc64', 'ppc64pseries', 'ppc64iseries'],
'x86_64': [],
's390': [],
's390x': [],
}
for key in archs:
if arch == key:
return arch
elif arch in archs[key]:
return key
else:
print 'gensystemid: Architecture %s unknown' % arch
return None
class Options:
def __init__(self, args):
self.arch = None
self.hostname = None
self.paths = None
self.quiet = False
self.rhnpassword = None
self.rhnrelease = None
self.rhnusername = None
self.verbose = 1
try:
opts, args = getopt.getopt(args, 'a:hqp:r:u:v',
['arch=', 'hostname=', 'quiet', 'release=', 'help', 'verbose', 'version'])
except getopt.error, exc:
print 'gensystemid: %s, try gensystemid -h for a list of all the options' % str(exc)
sys.exit(1)
for opt, arg in opts:
if opt in ['-a', '--arch']:
self.arch = arg
elif opt in ['--hostname']:
self.hostname = arg
elif opt in ['-p', '--password']:
self.rhnpassword = arg
elif opt in ['-q', '--quiet']:
self.quiet = True
elif opt in ['-r', '--release']:
self.rhnrelease = arg
elif opt in ['-u', '--username']:
self.rhnusername = arg
elif opt in ['-h', '--help']:
self.usage()
self.help()
sys.exit(0)
elif opt in ['-v', '--verbose']:
self.verbose = self.verbose + 1
elif opt in ['--version']:
self.version()
sys.exit(0)
if self.quiet:
self.verbose = 0
if self.verbose >= 3:
print 'Verbosity set to level %d' % self.verbose
if not self.arch:
self.arch = lowarch(os.uname()[4])
print 'gensystemid: Architecture not supplied, using system architecture %s' % self.arch
if not self.hostname:
self.hostname = '%s-%s-%s-mrepo' % (os.uname()[1].split('.')[0], self.rhnrelease, lowarch(self.arch))
try:
checkrelease(self.rhnrelease, self.arch)
except Exception, e:
print 'gensystemid:', e
sys.exit(2)
if len(args) <= 0:
print 'gensystemid: no destination path given'
sys.exit(1)
self.paths = args
def version(self):
print 'gensystemid %s' % VERSION
print 'Written by Dag Wieers <[email protected]>'
print
print 'platform %s/%s' % (os.name, sys.platform)
print 'python %s' % sys.version
print
print 'build revision $Rev: 4107 $'
def usage(self):
print 'usage: gensystemid -r release [-a arch] [-p password] [-q] [-u username] [-v] dir1 dir2 ...'
def help(self):
print '''Generate a custom RHN systemid
gensystemid options:
-a, --arch=arch specify architecture (i386, x86_64, ppc, ia64)
-q, --quiet minimal output
-p, --password=password specify rhn password (asked when not given)
-r, --release=rhnrelease specify rhn release {2.1,3,4}{AS,ES,WS,Desktop} 5{Server,Client} 6{Server,Client,Workstation,ComputeNode}
-u, --username=username specify rhn username (asked when not given)
-v, --verbose increase verbosity
-vv, -vvv, -vvvv.. increase verbosity more
'''
cfg = {}
loginInfo = {}
def error(level, str):
"Output error message"
if level <= op.verbose:
sys.stdout.write('gensystemid: %s\n' % str)
def info(level, str):
"Output info message"
if level <= op.verbose:
sys.stdout.write('%s\n' % str)
def die(ret, str):
"Print error and exit with errorcode"
error(0, str)
sys.exit(ret)
def main():
if not op.rhnusername:
op.rhnusername = raw_input('RHN Username: ')
if not op.rhnpassword:
op.rhnpassword = getpass.getpass('RHN Password: ')
rhnsystemid = '/tmp/systemid'
if os.path.isfile(rhnsystemid):
os.remove(rhnsystemid)
info(5, 'Using RHN systemid from %s' % rhnsystemid)
cfg = {}
loginInfo = {}
cfg['systemIdPath'] = rhnsystemid
cfg = config.initUp2dateConfig()
cfg['systemIdPath'] = rhnsystemid
cfg['useRhn'] = 1
if op.rhnrelease:
cfg['versionOverride'] = op.rhnrelease
if op.arch:
cfg['forceArch'] = '%s-redhat-linux' % op.arch
if os.access('/var/log/up2date', os.W_OK):
cfg['logFile'] = '/var/log/up2date'
else:
cfg['logFile'] = os.path.expanduser('~/up2date.log')
### Get proxy information from environment and set up2date config accordingly
proxy = None
if os.environ.has_key('http_proxy'):
t, proxy, t, t, t, t = urlparse.urlparse(os.environ['http_proxy'])
elif os.environ.has_key('https_proxy'):
t, proxy, t, t, t, t = urlparse.urlparse(os.environ['https_proxy'])
if proxy:
cfg['enableProxy'] = 1
cfg['httpProxy'] = proxy
info(4, 'Setting proxy to %s' % proxy)
### FIXME: Implement proxy authentication
# if proxy.username and proxy.password:
# cfg['enableProxyAuth'] = 1
# cfg['proxyPassword'] = proxy.password
# cfg['proxyUser='] = proxy.username
if op.verbose >= 5:
cfg['debug'] = 10000
### FIXME: Insert correct release_name (eg. redhat-release-es) only for RHEL2.1
auth = {
'profile_name': op.hostname,
'os_release': op.rhnrelease,
'release_name': 'redhat-release',
'architecture': '%s-redhat-linux' % op.arch,
'username': op.rhnusername,
'password': op.rhnpassword,
'uuid': '',
'rhnuuid': '',
}
s = rpcServer.getServer()
try:
systemid = rpcServer.doCall(s.registration.new_user, op.rhnusername, op.rhnpassword)
except rpclib.Fault, f:
error(0, 'Error registering user. %s' % f.faultString)
sys.exit(1)
s = rpcServer.getServer()
try:
systemid = rpcServer.doCall(s.registration.new_system, auth)
except rpclib.Fault, f:
error(0, 'Error registering system. %s' % f.faultString)
sys.exit(1)
for path in op.paths:
file = os.path.join(path, 'systemid')
info(1, 'Writing out file %s' % file)
open(file, 'w').write(systemid)
### Unbuffered sys.stdout
sys.stdout = os.fdopen(1, 'w', 0)
sys.stderr = os.fdopen(2, 'w', 0)
### Workaround for python <= 2.2.1
try:
True, False
except NameError:
True = 1
False = 0
Yes = yes = On = on = True
No = no = Off = off = False
### Main entrance
if __name__ == '__main__':
op = Options(sys.argv[1:])
try:
main()
except KeyboardInterrupt, e:
die(6, 'Exiting on user request')
except OSError, e:
# print e.errno
die(7, 'OSError: %s' % e)
# vim:ts=4:sw=4:et