Federico Ressi | 21a10d3 | 2020-01-31 07:43:30 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 2 | |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | # Update the clouds.yaml file. |
| 16 | |
| 17 | |
| 18 | import argparse |
| 19 | import os.path |
| 20 | |
| 21 | import yaml |
| 22 | |
| 23 | |
| 24 | class UpdateCloudsYaml(object): |
| 25 | def __init__(self, args): |
| 26 | if args.file: |
| 27 | self._clouds_path = args.file |
| 28 | self._create_directory = False |
| 29 | else: |
| 30 | self._clouds_path = os.path.expanduser( |
| 31 | '~/.config/openstack/clouds.yaml') |
| 32 | self._create_directory = True |
Artem Goncharov | 67630d4 | 2023-06-18 14:46:06 +0200 | [diff] [blame^] | 33 | self._keyringrc_path = os.path.expanduser( |
| 34 | '~/.config/python_keyring/keyringrc.cfg') |
| 35 | self._config = {} |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 36 | |
| 37 | self._cloud = args.os_cloud |
| 38 | self._cloud_data = { |
| 39 | 'region_name': args.os_region_name, |
| 40 | 'identity_api_version': args.os_identity_api_version, |
Monty Taylor | 5bf6a94 | 2015-12-31 16:22:04 -0600 | [diff] [blame] | 41 | 'volume_api_version': args.os_volume_api_version, |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 42 | 'auth': { |
| 43 | 'auth_url': args.os_auth_url, |
| 44 | 'username': args.os_username, |
| 45 | 'password': args.os_password, |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 46 | }, |
| 47 | } |
Monty Taylor | 5690582 | 2019-01-08 15:29:16 +0000 | [diff] [blame] | 48 | if args.os_project_name and args.os_system_scope: |
| 49 | print( |
| 50 | "WARNING: os_project_name and os_system_scope were both" |
| 51 | " given. os_system_scope will take priority.") |
| 52 | if args.os_project_name and not args.os_system_scope: |
| 53 | self._cloud_data['auth']['project_name'] = args.os_project_name |
| 54 | if args.os_identity_api_version == '3' and not args.os_system_scope: |
Brant Knudson | 68e6ae6 | 2015-06-25 18:15:05 -0500 | [diff] [blame] | 55 | self._cloud_data['auth']['user_domain_id'] = 'default' |
| 56 | self._cloud_data['auth']['project_domain_id'] = 'default' |
Monty Taylor | 5690582 | 2019-01-08 15:29:16 +0000 | [diff] [blame] | 57 | if args.os_system_scope: |
| 58 | self._cloud_data['auth']['system_scope'] = args.os_system_scope |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 59 | if args.os_cacert: |
| 60 | self._cloud_data['cacert'] = args.os_cacert |
| 61 | |
| 62 | def run(self): |
| 63 | self._read_clouds() |
| 64 | self._update_clouds() |
| 65 | self._write_clouds() |
| 66 | |
| 67 | def _read_clouds(self): |
| 68 | try: |
| 69 | with open(self._clouds_path) as clouds_file: |
Artem Goncharov | 67630d4 | 2023-06-18 14:46:06 +0200 | [diff] [blame^] | 70 | self._config = yaml.safe_load(clouds_file) |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 71 | except IOError: |
| 72 | # The user doesn't have a clouds.yaml file. |
| 73 | print("The user clouds.yaml file didn't exist.") |
Artem Goncharov | 67630d4 | 2023-06-18 14:46:06 +0200 | [diff] [blame^] | 74 | if "cache" not in self._config: |
| 75 | # Enable auth (and only auth) caching. Currently caching into the |
| 76 | # file on FS is configured in `_write_clouds` function. |
| 77 | self._config["cache"] = {"auth": True} |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 78 | |
| 79 | def _update_clouds(self): |
Artem Goncharov | 67630d4 | 2023-06-18 14:46:06 +0200 | [diff] [blame^] | 80 | self._config.setdefault('clouds', {})[self._cloud] = self._cloud_data |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 81 | |
| 82 | def _write_clouds(self): |
| 83 | |
| 84 | if self._create_directory: |
| 85 | clouds_dir = os.path.dirname(self._clouds_path) |
| 86 | os.makedirs(clouds_dir) |
| 87 | |
| 88 | with open(self._clouds_path, 'w') as clouds_file: |
Artem Goncharov | 67630d4 | 2023-06-18 14:46:06 +0200 | [diff] [blame^] | 89 | yaml.dump(self._config, clouds_file, default_flow_style=False) |
| 90 | |
| 91 | # Enable keyring token caching |
| 92 | keyringrc_dir = os.path.dirname(self._keyringrc_path) |
| 93 | os.makedirs(keyringrc_dir, exist_ok=True) |
| 94 | |
| 95 | # Configure auth caching into the file on FS. We do not bother of any |
| 96 | # expiration since SDK is smart enough to reauth once the token becomes |
| 97 | # invalid. |
| 98 | with open(self._keyringrc_path, 'w') as keyringrc_file: |
| 99 | keyringrc_file.write("[backend]\n") |
| 100 | keyringrc_file.write( |
| 101 | "default-keyring=keyrings.alt.file.PlaintextKeyring\n") |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def main(): |
| 105 | parser = argparse.ArgumentParser('Update clouds.yaml file.') |
| 106 | parser.add_argument('--file') |
| 107 | parser.add_argument('--os-cloud', required=True) |
| 108 | parser.add_argument('--os-region-name', default='RegionOne') |
| 109 | parser.add_argument('--os-identity-api-version', default='3') |
Akihiro Motoki | e0b375c | 2018-12-14 17:29:27 +0900 | [diff] [blame] | 110 | parser.add_argument('--os-volume-api-version', default='3') |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 111 | parser.add_argument('--os-cacert') |
| 112 | parser.add_argument('--os-auth-url', required=True) |
| 113 | parser.add_argument('--os-username', required=True) |
| 114 | parser.add_argument('--os-password', required=True) |
Monty Taylor | 5690582 | 2019-01-08 15:29:16 +0000 | [diff] [blame] | 115 | parser.add_argument('--os-project-name') |
| 116 | parser.add_argument('--os-system-scope') |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 117 | |
| 118 | args = parser.parse_args() |
| 119 | |
| 120 | update_clouds_yaml = UpdateCloudsYaml(args) |
| 121 | update_clouds_yaml.run() |
| 122 | |
| 123 | |
| 124 | if __name__ == "__main__": |
| 125 | main() |