|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from copy import deepcopy |
| 4 | + |
| 5 | +from ansible.module_utils.helpers import Helpers as helpers |
| 6 | + |
| 7 | +argument_spec = { |
| 8 | + 'console_sock': {'required': True, 'type': 'str'}, |
| 9 | + 'instance_config': {'required': True, 'type': 'dict'}, |
| 10 | + 'cartridge_defaults': {'required': False, 'type': 'dict', 'default': {}}, |
| 11 | + 'strict_mode': {'required': False, 'type': 'bool', 'default': False}, |
| 12 | +} |
| 13 | + |
| 14 | + |
| 15 | +def change_memory_size(control_console, param_name, new_value): |
| 16 | + if new_value is None: |
| 17 | + return False, None |
| 18 | + |
| 19 | + increase_memory_func_body = ''' |
| 20 | + local param_name, new_memory_size = ... |
| 21 | + local ok, err = pcall(box.cfg, { [param_name] = new_memory_size }) |
| 22 | + if not ok then |
| 23 | + if tostring(err):find("cannot decrease memory size at runtime") == nil then |
| 24 | + return nil, string.format('failed to set %s: %s', param_name, err) |
| 25 | + end |
| 26 | + end |
| 27 | + return ok |
| 28 | + ''' |
| 29 | + return control_console.eval_res_err(increase_memory_func_body, param_name, new_value) |
| 30 | + |
| 31 | + |
| 32 | +def change_dynamic_params(control_console, old_box_config, new_config, strict_mode=False): |
| 33 | + memory_sizes_to_change = {} |
| 34 | + box_params_to_change = {} |
| 35 | + incorrect_memory_changes = [] |
| 36 | + non_dynamic_params = [] |
| 37 | + |
| 38 | + for param_name, new_value in new_config.items(): |
| 39 | + old_box_value = old_box_config.get(param_name) |
| 40 | + |
| 41 | + if param_name in helpers.MEMORY_SIZE_BOX_CFG_PARAMS: |
| 42 | + if new_value > old_box_value: |
| 43 | + memory_sizes_to_change.update({param_name: new_value}) |
| 44 | + else: |
| 45 | + incorrect_memory_changes.append((param_name, old_box_value, new_value)) |
| 46 | + |
| 47 | + elif param_name in helpers.DYNAMIC_BOX_CFG_PARAMS: |
| 48 | + if new_value != old_box_value: |
| 49 | + box_params_to_change.update({param_name: new_value}) |
| 50 | + |
| 51 | + else: |
| 52 | + non_dynamic_params.append(param_name) |
| 53 | + |
| 54 | + if strict_mode: |
| 55 | + messages = [] |
| 56 | + if incorrect_memory_changes: |
| 57 | + messages.append('impossible to decrease memory sizes in runtime (%s)' % ', '.join( |
| 58 | + map(lambda change_info: "'%s' from '%s' to '%s'" % change_info, incorrect_memory_changes) |
| 59 | + )) |
| 60 | + if non_dynamic_params: |
| 61 | + messages.append("impossible to change '%s' in runtime" % ', '.join(non_dynamic_params)) |
| 62 | + if messages: |
| 63 | + return None, "Impossible to patch instance config: %s" % '; '.join(messages) |
| 64 | + |
| 65 | + memory_changed = False |
| 66 | + for param_name, new_value in memory_sizes_to_change.items(): |
| 67 | + changed, err = change_memory_size(control_console, param_name, new_value) |
| 68 | + if err is not None: |
| 69 | + return None, 'Failed to change memory size in runtime: %s' % err |
| 70 | + if changed: |
| 71 | + memory_changed = True |
| 72 | + |
| 73 | + box_changed = False |
| 74 | + if box_params_to_change: |
| 75 | + update_box_cfg_func_body = ''' |
| 76 | + box.cfg(...) |
| 77 | + return box.cfg |
| 78 | + ''' |
| 79 | + updated_config, _ = control_console.eval_res_err(update_box_cfg_func_body, box_params_to_change) |
| 80 | + box_changed = old_box_config != updated_config |
| 81 | + |
| 82 | + return memory_changed or box_changed, None |
| 83 | + |
| 84 | + |
| 85 | +def configure_box_cfg_params(console_sock, instance_config, defaults=None, strict_mode=False): |
| 86 | + control_console, err = helpers.get_control_console_if_started(console_sock, strict_mode) |
| 87 | + if err is not None: |
| 88 | + return None, err |
| 89 | + if not control_console: |
| 90 | + return False, None |
| 91 | + |
| 92 | + if not helpers.box_cfg_was_called(control_console): |
| 93 | + if strict_mode: |
| 94 | + return None, "Impossible to patch instance config: 'box.cfg' wasn't called" |
| 95 | + return False, None |
| 96 | + |
| 97 | + old_box_config = helpers.get_box_cfg(control_console) |
| 98 | + new_config = deepcopy(defaults or {}) |
| 99 | + new_config.update(instance_config) |
| 100 | + |
| 101 | + return change_dynamic_params(control_console, old_box_config, new_config, strict_mode) |
| 102 | + |
| 103 | + |
| 104 | +def patch_instance_in_runtime(params): |
| 105 | + console_sock = params['console_sock'] |
| 106 | + instance_config = params['instance_config'] |
| 107 | + cartridge_defaults = params['cartridge_defaults'] |
| 108 | + strict_mode = params['strict_mode'] |
| 109 | + |
| 110 | + changed, error = configure_box_cfg_params(console_sock, instance_config, cartridge_defaults, strict_mode) |
| 111 | + if error is not None: |
| 112 | + return helpers.ModuleRes(failed=True, msg=error) |
| 113 | + return helpers.ModuleRes(changed=changed) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + helpers.execute_module(argument_spec, patch_instance_in_runtime) |
0 commit comments