-
Couldn't load subscription status.
- Fork 7
YDBOPS-12647: New inventory and some fixes #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
74e2b56
055450a
9343804
0991ee3
ecb380b
dafeb53
868c6ec
ea80df2
ce3850e
e541412
ffc94e9
096982f
2b28cd8
81a917f
cf278d9
7792d30
bb4549f
21a1615
b5953ca
819f11f
aa70c51
7b3214b
be70f19
7122f41
ad1af93
63af4ed
33ac1f2
6093ea2
d78cc8b
b6af6c1
aa7f6fe
75806ea
7a31240
8bd7951
be39571
f6d5245
820eca8
e9d440a
52d13a0
89c804b
f343786
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ namespace: ydb_platform | |
| name: ydb | ||
|
|
||
| # The version of the collection. Must be compatible with semantic versioning | ||
| version: 1.2.0 | ||
| version: 1.2.5 | ||
|
|
||
| # The path to the Markdown (.md) readme file. This path is relative to the root of the collection | ||
| readme: README.md | ||
|
|
@@ -20,7 +20,6 @@ authors: | |
| - Eugene Arbatsky <[email protected]> elabpro | ||
| - Maksim Zinal <[email protected]> zinal | ||
|
|
||
|
|
||
| ### OPTIONAL but strongly recommended | ||
| # A short summary description of the collection | ||
| description: Collection for YDB cluster management. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| from ansible.plugins.inventory import BaseInventoryPlugin | ||
| import yaml | ||
| import copy | ||
| from ansible.errors import AnsibleError | ||
| # from ansible.template import Templar | ||
|
|
||
| DOCUMENTATION = r''' | ||
| name: ydb_inventory | ||
| plugin_type: inventory | ||
| short_description: YDB inventory from config.yaml | ||
| description: | | ||
| This inventory plugin fetches hosts from a custom source (config.yaml). | ||
| options: | ||
| plugin: | ||
| description: The name of the plugin (must be 'ydb_inventory') | ||
| required: true | ||
| choices: ['ydb_platform.ydb.ydb_inventory'] | ||
| ydb_config: | ||
| description: YDB config (file or dictionary) | ||
| required: true | ||
| env: | ||
| - name: INVENTORY_YDB_CONFIG | ||
| ''' | ||
|
|
||
| class InventoryModule(BaseInventoryPlugin): | ||
| NAME = 'ydb_inventory' | ||
|
|
||
| def verify_file(self, path): | ||
| # Проверяем, что это конфиг для нашего плагина | ||
| valid = super().verify_file(path) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Сюда стоит добавить валидацию расширения. По аналогии со сложившимися практиками, стоит проверять суффикс ydb_inventory.yaml, ydb_inventory.yml |
||
| return valid | ||
|
|
||
| def parse(self, inventory, loader, path, cache=True): | ||
| super().parse(inventory, loader, path, cache) | ||
| # Загружаем конфиг плагина (YAML-файл) | ||
| config = self._read_config_data(path) | ||
|
|
||
| ydb_config = config.get('ydb_config') | ||
| print(f"Reading inventory from {ydb_config}") | ||
|
|
||
| group = 'ydb' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не очень хорошо, что плагин создает группу с захардкоженным именем. |
||
| self.inventory.add_group(group) | ||
| brokers = [] | ||
|
|
||
| ydb_vars = self.inventory.groups['ydb'].get_vars() | ||
| try: | ||
| with open(ydb_config, "r") as file: | ||
| yaml_config = yaml.safe_load(file) | ||
|
|
||
| if 'config' in yaml_config: | ||
| """ V2 Config """ | ||
| yaml_config = yaml_config['config'] | ||
|
|
||
| self.inventory.groups['ydb'].set_variable('ydb_config_dict', yaml_config) | ||
|
|
||
| if 'ydb_dbname' not in ydb_vars and 'ydb_dynnodes' in ydb_vars: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кажется здесь плагин решает какую-то проблему определения дефолтов, которую inventory плагин не должен решать. Для этого у ansible есть другие механизмы. Особенно учитывая, что конфиг никак не используется для определения этих дефолтов |
||
| for dynnode in ydb_vars['ydb_dynnodes']: | ||
| if 'dbname' in dynnode: | ||
| self.inventory.groups['ydb'].set_variable('ydb_dbname',dynnode['dbname']) | ||
| break | ||
|
|
||
| if 'ydb_enforce_user_token_requirement' not in ydb_vars: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Думаю не стоит здесь (и далее, в похожих случаях) проверять существование переменной, нужно просто безусловно применять значение из конфига. |
||
| if 'domains_config' in yaml_config and 'security_config' in yaml_config['domains_config'] and 'enforce_user_token_requirement' in yaml_config['domains_config']['security_config']: | ||
| self.inventory.groups['ydb'].set_variable('ydb_enforce_user_token_requirement', yaml_config['domains_config']['security_config']['enforce_user_token_requirement']) | ||
| else: | ||
| self.inventory.groups['ydb'].set_variable('ydb_enforce_user_token_requirement', False) | ||
|
Comment on lines
+63
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ниже похожие цепочки and также можно сократить чуть полаконичнее, но это больше про предпочтения, наверное |
||
|
|
||
| if 'ydb_pool_kind' not in ydb_vars: | ||
| if 'default_disk_type' in yaml_config: | ||
| self.inventory.groups['ydb'].set_variable('ydb_pool_kind', yaml_config['default_disk_type'].lower()) | ||
| if 'domains_config' in yaml_config and 'domain' in yaml_config['domains_config']: | ||
| if 'storage_pool_types' in yaml_config['domains_config']['domain'][0]: | ||
| self.inventory.groups['ydb'].set_variable('ydb_pool_kind', yaml_config['domains_config']['domain'][0]['storage_pool_types'][0]['kind']) | ||
|
|
||
| if 'self_management_config' in yaml_config and 'enabled' in yaml_config['self_management_config'] and yaml_config['self_management_config']['enabled']: | ||
| self.inventory.groups['ydb'].set_variable('ydb_config_v2', True) | ||
|
|
||
| self.inventory.groups['ydb'].set_variable('ydb_config', yaml_config) | ||
|
|
||
| if 'ydb_domain' not in ydb_vars: | ||
| if 'domains_config' in yaml_config and 'domain' in yaml_config['domains_config']: | ||
| self.inventory.groups['ydb'].set_variable('ydb_domain',yaml_config['domains_config']['domain'][0]['name']) | ||
| else: | ||
| # Default domain is Root | ||
| self.inventory.groups['ydb'].set_variable('ydb_domain','Root') | ||
|
|
||
| # Read drives config | ||
| drive_configs = {} | ||
| drive_labels = {} | ||
| if 'ydb_disks' in ydb_vars: | ||
| for disk in ydb_vars['ydb_disks']: | ||
| drive_labels[disk['label']] = disk['name'] | ||
| if 'host_configs' in yaml_config: | ||
| for drive_config in yaml_config['host_configs']: | ||
| drive_configs[drive_config['host_config_id']] = copy.deepcopy(drive_config['drive']) | ||
| for i, item in enumerate(drive_config['drive']): | ||
| label = item['path'].split('/')[-1] | ||
| drive_configs[drive_config['host_config_id']][i]['label'] = label | ||
| if label in drive_labels: | ||
| drive_configs[drive_config['host_config_id']][i]['name'] = drive_labels[label] | ||
| else: | ||
| raise AnsibleError(f"Config parsing error, unable to find disk for label: {label}") | ||
| # Read hosts and define variables for them | ||
| if 'hosts' in yaml_config: | ||
| for host in yaml_config['hosts']: | ||
| self.inventory.add_host(host['host'], group=group) | ||
| # Set variables for hosts | ||
| for key, value in host.items(): | ||
| if key == 'host_config_id': | ||
| self.inventory.set_variable(host['host'], 'ydb_disks', drive_configs[value]) | ||
| else: | ||
| self.inventory.set_variable(host['host'], key, value) | ||
| if len(brokers) < 3: | ||
| brokers.append(host['host']) | ||
|
|
||
| if 'ydb_brokers' not in ydb_vars and len(brokers) > 0: | ||
| self.inventory.groups['ydb'].set_variable('ydb_brokers', brokers) | ||
|
|
||
| except Exception as e: | ||
| raise AnsibleError(f"Config parsing error: {str(e)}") | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
must be 'ydb_platform.ydb.ydb_inventory', согласно choices.
Вообще, в https://github.com/ansible-collections/community.general/tree/main/plugins/inventory тут используют что-то вроде:
description: Token that ensures this is a source file for the P(ydb_platform.ydb.ydb_inventory#inventory) plugin.или
The name of this plugin, it should always be set to V(ydb_platform.ydb.ydb_inventory) for this plugin to recognize it as its own.