Skip to content

Commit cfa40d7

Browse files
authored
scripts: Add getenvvar tool
Setting the necessary required environment variables has proven relatively tricky to do. Add a tool that will start doing this for us. Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent 5d5024a commit cfa40d7

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

script/getenvvar

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Set environment variables required for the CI jobs by inspection of the
5+
clouds.yaml file. This is useful where you only have this file.
6+
7+
To set variables:
8+
9+
$ eval $(./script/getenvvar)
10+
11+
To unset them:
12+
13+
$ unset $(compgen -v | grep OS_)
14+
"""
15+
16+
import argparse
17+
from pathlib import Path
18+
import sys
19+
20+
import yaml
21+
22+
p = Path('~/.config/openstack/clouds.yaml').expanduser()
23+
parser = argparse.ArgumentParser()
24+
parser.add_argument(
25+
'cloud',
26+
help="Cloud to export credentials for",
27+
)
28+
29+
args = parser.parse_args()
30+
31+
with p.open() as fh:
32+
data = yaml.safe_load(fh)
33+
34+
if args.cloud not in data.get('clouds', {}) or {}:
35+
print(f'Could not find cloud {args.cloud} in {str(p)}', file=sys.stderr)
36+
sys.exit(1)
37+
38+
cloud = data['clouds'][args.cloud]
39+
40+
if 'auth' not in cloud:
41+
print(f'Missing auth section for cloud {cloud}', file=sys.stderr)
42+
sys.exit(1)
43+
44+
auth = cloud['auth']
45+
46+
if 'username' not in auth or 'password' not in auth:
47+
print('Only password authentication supported', file=sys.stderr)
48+
sys.exit(1)
49+
50+
# FIXME: This should work but does not, since the check for auth credentials
51+
# is just 'OS_USERNAME == admin'
52+
53+
# user_id = auth.get('user_id')
54+
# project_id = auth.get('project_id')
55+
# if not user_id or not project_id:
56+
# import openstack
57+
# conn = openstack.connect(args.cloud)
58+
# auth_ref = conn.config.get_auth().get_auth_ref(conn.session)
59+
#
60+
# if not user_id:
61+
# user_id = auth_ref.user_id
62+
#
63+
# if not project_id:
64+
# project_id = auth_ref.project_id
65+
#
66+
# result = f"""
67+
# unset OS_CLOUD
68+
# export OS_AUTH_URL={auth['auth_url']}
69+
# export OS_USERID={user_id}
70+
# export OS_PASSWORD={auth['password']}
71+
# export OS_PROJECT_ID={project_id}
72+
# export OS_REGION_NAME={cloud['region_name']}
73+
# """.strip()
74+
75+
result = f"""
76+
unset OS_CLOUD
77+
export OS_AUTH_URL={auth['auth_url']}
78+
export OS_USERNAME={auth['username']}
79+
export OS_PASSWORD={auth['password']}
80+
export OS_PROJECT_NAME={auth['project_name']}
81+
export OS_DOMAIN_ID={auth['user_domain_id']}
82+
export OS_REGION_NAME={cloud['region_name']}
83+
"""
84+
85+
print(result)

0 commit comments

Comments
 (0)