/usr/lib/python2.7/dist-packages/simplestreams/openstack.py is in python-simplestreams 0.1.0~bzr341-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (C) 2013 Canonical Ltd.
#
# Author: Scott Moser <scott.moser@canonical.com>
#
# Simplestreams is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# Simplestreams is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Simplestreams. If not, see <http://www.gnu.org/licenses/>.
from keystoneclient.v2_0 import client as ksclient
import os
import re
OS_ENV_VARS = (
'OS_AUTH_TOKEN', 'OS_AUTH_URL', 'OS_CACERT', 'OS_IMAGE_API_VERSION',
'OS_IMAGE_URL', 'OS_PASSWORD', 'OS_REGION_NAME', 'OS_STORAGE_URL',
'OS_TENANT_ID', 'OS_TENANT_NAME', 'OS_USERNAME', 'OS_INSECURE'
)
def load_keystone_creds(**kwargs):
# either via arguments or OS_* values in environment, the kwargs
# that are required are:
# 'username', 'auth_url',
# ('auth_token' or 'password')
# ('tenant_id' or 'tenant_name')
ret = {}
for name in OS_ENV_VARS:
lc = name.lower()
# take off 'os_'
short = lc[3:]
if short in kwargs:
ret[lc] = kwargs.get(lc)
elif name in os.environ:
# take off 'os_'
ret[short] = os.environ[name]
if 'insecure' in ret:
if isinstance(ret['insecure'], str):
ret['insecure'] = (ret['insecure'].lower() not in
("", "0", "no", "off"))
else:
ret['insecure'] = bool(ret['insecure'])
missing = []
for req in ('username', 'auth_url'):
if not ret.get(req):
missing.append(req)
if not (ret.get('auth_token') or ret.get('password')):
missing.append("(auth_token or password)")
if not (ret.get('tenant_id') or ret.get('tenant_name')):
raise ValueError("(tenant_id or tenant_name)")
if missing:
raise ValueError("Need values for: %s" % missing)
return ret
def get_regions(client=None, services=None, kscreds=None):
# if kscreds had 'region_name', then return that
if kscreds and kscreds.get('region_name'):
return [kscreds.get('region_name')]
if client is None:
creds = kscreds
if creds is None:
creds = load_keystone_creds()
client = get_ksclient(**creds)
endpoints = client.service_catalog.get_endpoints()
if services is None:
services = list(endpoints.keys())
regions = set()
for service in services:
for r in endpoints.get(service, {}):
regions.add(r['region'])
return list(regions)
def get_ksclient(**kwargs):
pt = ('username', 'password', 'tenant_id', 'tenant_name', 'auth_url',
'cacert', 'insecure')
kskw = {k: kwargs.get(k) for k in pt if k in kwargs}
return ksclient.Client(**kskw)
def get_service_conn_info(service='image', client=None, **kwargs):
# return a dict with token, insecure, cacert, endpoint
if not client:
client = get_ksclient(**kwargs)
endpoint = _get_endpoint(client, service, **kwargs)
return {'token': client.auth_token, 'insecure': kwargs.get('insecure'),
'cacert': kwargs.get('cacert'), 'endpoint': endpoint,
'tenant_id': client.tenant_id}
def _get_endpoint(client, service, **kwargs):
"""Get an endpoint using the provided keystone client."""
endpoint_kwargs = {
'service_type': service,
'endpoint_type': kwargs.get('endpoint_type') or 'publicURL',
}
if kwargs.get('region_name'):
endpoint_kwargs['attr'] = 'region'
endpoint_kwargs['filter_value'] = kwargs.get('region_name')
endpoint = client.service_catalog.url_for(**endpoint_kwargs)
return _strip_version(endpoint)
def _strip_version(endpoint):
"""Strip a version from the last component of an endpoint if present"""
# Get rid of trailing '/' if present
if endpoint.endswith('/'):
endpoint = endpoint[:-1]
url_bits = endpoint.split('/')
# regex to match 'v1' or 'v2.0' etc
if re.match(r'v\d+\.?\d*', url_bits[-1]):
endpoint = '/'.join(url_bits[:-1])
return endpoint
|