mirror of https://github.com/docker/docker-py.git
Don't fail loading the configuration when .dockercfg doesn't exist
Signed-off-by: Maxime Petazzoni <max@signalfuse.com>
This commit is contained in:
parent
3aa51d52d0
commit
9871f8d9ac
|
@ -98,33 +98,36 @@ def encode_header(auth):
|
||||||
|
|
||||||
|
|
||||||
def load_config(root=None):
|
def load_config(root=None):
|
||||||
if root is None:
|
root = root or os.environ['HOME']
|
||||||
root = os.environ['HOME']
|
config = {
|
||||||
config_file = {
|
|
||||||
'Configs': {},
|
'Configs': {},
|
||||||
'rootPath': root
|
'rootPath': root
|
||||||
}
|
}
|
||||||
f = open(os.path.join(root, '.dockercfg'))
|
|
||||||
try:
|
config_file = os.path.join(root, '.dockercfg')
|
||||||
config_file['Configs'] = json.load(f)
|
if not os.path.exists(config_file):
|
||||||
for k, conf in six.iteritems(config_file['Configs']):
|
return config
|
||||||
conf['Username'], conf['Password'] = decode_auth(conf['auth'])
|
|
||||||
del conf['auth']
|
with open(config_file) as f:
|
||||||
config_file['Configs'][k] = conf
|
try:
|
||||||
except Exception:
|
config['Configs'] = json.load(f)
|
||||||
f.seek(0)
|
for k, conf in six.iteritems(config['Configs']):
|
||||||
buf = []
|
conf['Username'], conf['Password'] = decode_auth(conf['auth'])
|
||||||
for line in f:
|
del conf['auth']
|
||||||
k, v = line.split(' = ')
|
config['Configs'][k] = conf
|
||||||
buf.append(v)
|
except Exception:
|
||||||
if len(buf) < 2:
|
f.seek(0)
|
||||||
raise Exception("The Auth config file is empty")
|
buf = []
|
||||||
user, pwd = decode_auth(buf[0])
|
for line in f:
|
||||||
config_file['Configs'][INDEX_URL] = {
|
k, v = line.split(' = ')
|
||||||
'Username': user,
|
buf.append(v)
|
||||||
'Password': pwd,
|
if len(buf) < 2:
|
||||||
'Email': buf[1]
|
raise Exception("The Auth config file is empty")
|
||||||
}
|
user, pwd = decode_auth(buf[0])
|
||||||
finally:
|
config['Configs'][INDEX_URL] = {
|
||||||
f.close()
|
'Username': user,
|
||||||
return config_file
|
'Password': pwd,
|
||||||
|
'Email': buf[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return config
|
||||||
|
|
|
@ -465,6 +465,15 @@ class DockerClientTest(unittest.TestCase):
|
||||||
## PY SPECIFIC TESTS ##
|
## PY SPECIFIC TESTS ##
|
||||||
#######################
|
#######################
|
||||||
|
|
||||||
|
def test_load_config_no_file(self):
|
||||||
|
folder = tempfile.mkdtemp()
|
||||||
|
cfg = docker.auth.load_config(folder)
|
||||||
|
self.assertTrue(cfg is not None)
|
||||||
|
self.assertIn('Configs', cfg)
|
||||||
|
self.assertEquals(cfg['Configs'], {})
|
||||||
|
self.assertIn('rootPath', cfg)
|
||||||
|
self.assertEquals(cfg['rootPath'], folder)
|
||||||
|
|
||||||
def test_load_config(self):
|
def test_load_config(self):
|
||||||
folder = tempfile.mkdtemp()
|
folder = tempfile.mkdtemp()
|
||||||
f = open(os.path.join(folder, '.dockercfg'), 'w')
|
f = open(os.path.join(folder, '.dockercfg'), 'w')
|
||||||
|
|
Loading…
Reference in New Issue