mirror of
https://github.com/openSUSE/osc.git
synced 2025-02-04 10:36:17 +01:00
- fully implement accessing multiple API servers. "apisrv" in the config takes
a URL now, so the variable "scheme" which was needed in addition before becomes obsolete. For backward compatibility, a hostname (and scheme variable) are still accepted. Likewise, the auth sections in the config take a URL now, and a hostname:port to keep old config working. Furthermore, apisrv can be overridden by -A on the commandline. HTTP or HTTPS scheme is determined from the URL. Credentials must be configured in .oscrc.
This commit is contained in:
parent
36e4cc3140
commit
b58b6e5a3f
@ -41,29 +41,24 @@ class Osc(cmdln.Cmdln):
|
|||||||
default=conf.config['http_debug'],
|
default=conf.config['http_debug'],
|
||||||
help='debug HTTP traffic')
|
help='debug HTTP traffic')
|
||||||
self.optparser.add_option('-A', '--apisrv', dest='apisrv',
|
self.optparser.add_option('-A', '--apisrv', dest='apisrv',
|
||||||
default=conf.config['apisrv'],
|
metavar='URL',
|
||||||
metavar='host',
|
help='specify URL to access API server at')
|
||||||
help='use HOST as API server')
|
|
||||||
self.optparser.add_option('-S', '--scheme', dest='scheme',
|
|
||||||
default=conf.config['scheme'],
|
|
||||||
metavar='(http|https)',
|
|
||||||
help='use this protocol (default: %s)' % conf.config['scheme'])
|
|
||||||
|
|
||||||
(self.global_opts, self.myargs) = self.optparser.parse_args()
|
(self.global_opts, self.myargs) = self.optparser.parse_args()
|
||||||
|
|
||||||
# XXX version is printed twice otherwise...
|
# XXX version is printed twice otherwise...
|
||||||
self.optparser.version = ''
|
self.optparser.version = ''
|
||||||
|
|
||||||
|
# merge commandline options into the config
|
||||||
conf.config['http_debug'] = self.global_opts.http_debug
|
conf.config['http_debug'] = self.global_opts.http_debug
|
||||||
conf.config['apisrv'] = self.global_opts.apisrv
|
if self.global_opts.apisrv:
|
||||||
conf.config['scheme'] = self.global_opts.scheme
|
conf.config['scheme'], conf.config['apisrv'] = \
|
||||||
|
conf.parse_apisrv_url(conf.config['scheme'], self.global_opts.apisrv)
|
||||||
|
|
||||||
# finally, initialize urllib2 for to use the credentials for Basic Authentication
|
# finally, initialize urllib2 for to use the credentials for Basic Authentication
|
||||||
conf.init_basicauth(conf.config)
|
conf.init_basicauth(conf.config)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def do_init(self, subcmd, opts, *args):
|
def do_init(self, subcmd, opts, *args):
|
||||||
"""${cmd_name}: Initialize a directory as working copy
|
"""${cmd_name}: Initialize a directory as working copy
|
||||||
|
|
||||||
|
50
osc/conf.py
50
osc/conf.py
@ -17,8 +17,7 @@ After reading the config, urllib2 is initialized.
|
|||||||
|
|
||||||
The configuration dictionary could look like this:
|
The configuration dictionary could look like this:
|
||||||
|
|
||||||
{'apisrv': 'api.opensuse.org',
|
{'apisrv': 'https://api.opensuse.org/',
|
||||||
'scheme': 'http',
|
|
||||||
'user': 'poeml',
|
'user': 'poeml',
|
||||||
'pass': 'secret',
|
'pass': 'secret',
|
||||||
'auth_dict': {'api.opensuse.org': {'user': 'poeml', 'pass': 'secret'},
|
'auth_dict': {'api.opensuse.org': {'user': 'poeml', 'pass': 'secret'},
|
||||||
@ -40,7 +39,7 @@ import ConfigParser
|
|||||||
# it will hold the parsed configuration
|
# it will hold the parsed configuration
|
||||||
config = { }
|
config = { }
|
||||||
|
|
||||||
DEFAULTS = { 'apisrv': 'api.opensuse.org',
|
DEFAULTS = { 'apisrv': 'https://api.opensuse.org/',
|
||||||
'scheme': 'https',
|
'scheme': 'https',
|
||||||
'user': 'your_username',
|
'user': 'your_username',
|
||||||
'pass': 'your_password',
|
'pass': 'your_password',
|
||||||
@ -68,6 +67,10 @@ boolean_opts = ['http_debug', 'do_commits']
|
|||||||
new_conf_template = """
|
new_conf_template = """
|
||||||
[general]
|
[general]
|
||||||
|
|
||||||
|
# URL to access API server, e.g. %(apisrv)s
|
||||||
|
# you also need a section [%(apisrv)s] with the credentials
|
||||||
|
#apisrv = %(apisrv)s
|
||||||
|
|
||||||
# Downloaded packages are cached here. Must be writable by you.
|
# Downloaded packages are cached here. Must be writable by you.
|
||||||
#packagecachedir = %(packagecachedir)s
|
#packagecachedir = %(packagecachedir)s
|
||||||
|
|
||||||
@ -79,13 +82,6 @@ new_conf_template = """
|
|||||||
# /srv/oscbuild/%%(repo)s-%%(arch)s
|
# /srv/oscbuild/%%(repo)s-%%(arch)s
|
||||||
#build-root = %(build-root)s
|
#build-root = %(build-root)s
|
||||||
|
|
||||||
# use this API server (hostname[:port])
|
|
||||||
# (it needs a section [%(apisrv)s] with the credentials)
|
|
||||||
#apisrv = %(apisrv)s
|
|
||||||
|
|
||||||
# use this protocol to access the API server (http or https)
|
|
||||||
#scheme = https
|
|
||||||
|
|
||||||
# show HTTP traffic useful for debugging
|
# show HTTP traffic useful for debugging
|
||||||
#http_debug = 1
|
#http_debug = 1
|
||||||
|
|
||||||
@ -112,6 +108,13 @@ Make sure that it has a [general] section.
|
|||||||
|
|
||||||
cookiejar = None
|
cookiejar = None
|
||||||
|
|
||||||
|
def parse_apisrv_url(scheme, apisrv):
|
||||||
|
import urlparse
|
||||||
|
if apisrv.startswith('http://') or apisrv.startswith('https://'):
|
||||||
|
return urlparse.urlsplit(apisrv)[0:2]
|
||||||
|
else:
|
||||||
|
return scheme, apisrv
|
||||||
|
|
||||||
def init_basicauth(config):
|
def init_basicauth(config):
|
||||||
"""initialize urllib2 with the credentials for Basic Authentication"""
|
"""initialize urllib2 with the credentials for Basic Authentication"""
|
||||||
|
|
||||||
@ -175,8 +178,10 @@ def get_config():
|
|||||||
# note that it is not suited for credentials containing spaces
|
# note that it is not suited for credentials containing spaces
|
||||||
import netrc
|
import netrc
|
||||||
try:
|
try:
|
||||||
|
# FIXME: apisrv is a URL now
|
||||||
|
netrc_host = parse_apisrv_url(None, DEFAULTS['apisrv'])[1]
|
||||||
config['user'], account, config['pass'] = \
|
config['user'], account, config['pass'] = \
|
||||||
netrc.netrc().authenticators(DEFAULTS['apisrv'])
|
netrc.netrc().authenticators(netrc_host)
|
||||||
print >>sys.stderr, 'Read credentials from %s.' % os.path.expanduser('~/.netrc')
|
print >>sys.stderr, 'Read credentials from %s.' % os.path.expanduser('~/.netrc')
|
||||||
except (IOError, TypeError, netrc.NetrcParseError):
|
except (IOError, TypeError, netrc.NetrcParseError):
|
||||||
#
|
#
|
||||||
@ -208,15 +213,11 @@ def get_config():
|
|||||||
print >>sys.stderr, new_conf_template % DEFAULTS
|
print >>sys.stderr, new_conf_template % DEFAULTS
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# holds multiple usernames and passwords
|
|
||||||
# it is used by urlgrabber's mirror fetcher
|
|
||||||
auth_dict = { }
|
|
||||||
for host in [ x for x in cp.sections() if x != 'general' ]:
|
|
||||||
auth_dict[host] = { 'user': cp.get(host, 'user'),
|
|
||||||
'pass': cp.get(host, 'pass') }
|
|
||||||
|
|
||||||
config = dict(cp.items('general', raw=1))
|
config = dict(cp.items('general', raw=1))
|
||||||
|
|
||||||
|
config['scheme'], config['apisrv'] = \
|
||||||
|
parse_apisrv_url(config['scheme'], config['apisrv'])
|
||||||
|
|
||||||
for i in boolean_opts:
|
for i in boolean_opts:
|
||||||
try:
|
try:
|
||||||
if int(config.get(i)):
|
if int(config.get(i)):
|
||||||
@ -230,11 +231,14 @@ def get_config():
|
|||||||
if type(config['urllist']) == str:
|
if type(config['urllist']) == str:
|
||||||
config['urllist'] = [ i.strip() for i in config['urllist'].split(',') ]
|
config['urllist'] = [ i.strip() for i in config['urllist'].split(',') ]
|
||||||
|
|
||||||
|
# holds multiple usernames and passwords
|
||||||
|
auth_dict = { }
|
||||||
|
for url in [ x for x in cp.sections() if x != 'general' ]:
|
||||||
|
dummy, host = \
|
||||||
|
parse_apisrv_url(config['scheme'], url)
|
||||||
|
auth_dict[host] = { 'user': cp.get(url, 'user'),
|
||||||
|
'pass': cp.get(url, 'pass') }
|
||||||
|
|
||||||
# add the auth data we collected to the config dict
|
# add the auth data we collected to the config dict
|
||||||
config['auth_dict'] = auth_dict
|
config['auth_dict'] = auth_dict
|
||||||
|
|
||||||
# for easier access to the api server's credentials, copy them to the "top":
|
|
||||||
config['user'] = config['auth_dict'][config['apisrv']]['user']
|
|
||||||
config['pass'] = config['auth_dict'][config['apisrv']]['pass']
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user