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 | |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 17 | import argparse |
| 18 | import os.path |
Stephen Finucane | 85576bb | 2025-02-26 18:04:09 +0000 | [diff] [blame] | 19 | import sys |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 20 | |
| 21 | import yaml |
| 22 | |
| 23 | |
Stephen Finucane | 85576bb | 2025-02-26 18:04:09 +0000 | [diff] [blame] | 24 | class UpdateCloudsYaml: |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 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 |
yatin | 82c30cd | 2023-11-15 12:44:50 +0000 | [diff] [blame] | 33 | self._clouds = {} |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 34 | |
| 35 | self._cloud = args.os_cloud |
| 36 | self._cloud_data = { |
| 37 | 'region_name': args.os_region_name, |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 38 | 'auth': { |
| 39 | 'auth_url': args.os_auth_url, |
| 40 | 'username': args.os_username, |
Stephen Finucane | 85576bb | 2025-02-26 18:04:09 +0000 | [diff] [blame] | 41 | 'user_domain_id': 'default', |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 42 | 'password': args.os_password, |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 43 | }, |
| 44 | } |
Stephen Finucane | 85576bb | 2025-02-26 18:04:09 +0000 | [diff] [blame] | 45 | |
Monty Taylor | 5690582 | 2019-01-08 15:29:16 +0000 | [diff] [blame] | 46 | if args.os_project_name and args.os_system_scope: |
| 47 | print( |
Stephen Finucane | 85576bb | 2025-02-26 18:04:09 +0000 | [diff] [blame] | 48 | "WARNING: os_project_name and os_system_scope were both " |
| 49 | "given. os_system_scope will take priority." |
| 50 | ) |
| 51 | |
| 52 | if args.os_system_scope: # system-scoped |
Monty Taylor | 5690582 | 2019-01-08 15:29:16 +0000 | [diff] [blame] | 53 | self._cloud_data['auth']['system_scope'] = args.os_system_scope |
Stephen Finucane | 85576bb | 2025-02-26 18:04:09 +0000 | [diff] [blame] | 54 | elif args.os_project_name: # project-scoped |
| 55 | self._cloud_data['auth']['project_name'] = args.os_project_name |
| 56 | self._cloud_data['auth']['project_domain_id'] = 'default' |
| 57 | |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 58 | if args.os_cacert: |
| 59 | self._cloud_data['cacert'] = args.os_cacert |
| 60 | |
| 61 | def run(self): |
| 62 | self._read_clouds() |
| 63 | self._update_clouds() |
| 64 | self._write_clouds() |
| 65 | |
| 66 | def _read_clouds(self): |
| 67 | try: |
| 68 | with open(self._clouds_path) as clouds_file: |
yatin | 82c30cd | 2023-11-15 12:44:50 +0000 | [diff] [blame] | 69 | self._clouds = yaml.safe_load(clouds_file) |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 70 | except IOError: |
| 71 | # The user doesn't have a clouds.yaml file. |
| 72 | print("The user clouds.yaml file didn't exist.") |
yatin | 82c30cd | 2023-11-15 12:44:50 +0000 | [diff] [blame] | 73 | self._clouds = {} |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 74 | |
| 75 | def _update_clouds(self): |
yatin | 82c30cd | 2023-11-15 12:44:50 +0000 | [diff] [blame] | 76 | self._clouds.setdefault('clouds', {})[self._cloud] = self._cloud_data |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 77 | |
| 78 | def _write_clouds(self): |
| 79 | |
| 80 | if self._create_directory: |
| 81 | clouds_dir = os.path.dirname(self._clouds_path) |
| 82 | os.makedirs(clouds_dir) |
| 83 | |
| 84 | with open(self._clouds_path, 'w') as clouds_file: |
yatin | 82c30cd | 2023-11-15 12:44:50 +0000 | [diff] [blame] | 85 | yaml.dump(self._clouds, clouds_file, default_flow_style=False) |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def main(): |
| 89 | parser = argparse.ArgumentParser('Update clouds.yaml file.') |
| 90 | parser.add_argument('--file') |
| 91 | parser.add_argument('--os-cloud', required=True) |
| 92 | parser.add_argument('--os-region-name', default='RegionOne') |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 93 | parser.add_argument('--os-cacert') |
| 94 | parser.add_argument('--os-auth-url', required=True) |
| 95 | parser.add_argument('--os-username', required=True) |
| 96 | parser.add_argument('--os-password', required=True) |
Monty Taylor | 5690582 | 2019-01-08 15:29:16 +0000 | [diff] [blame] | 97 | parser.add_argument('--os-project-name') |
| 98 | parser.add_argument('--os-system-scope') |
Brant Knudson | e1fa070 | 2015-06-21 08:54:43 -0500 | [diff] [blame] | 99 | |
| 100 | args = parser.parse_args() |
| 101 | |
| 102 | update_clouds_yaml = UpdateCloudsYaml(args) |
| 103 | update_clouds_yaml.run() |
| 104 | |
| 105 | |
| 106 | if __name__ == "__main__": |
| 107 | main() |