Skip to content
This repository was archived by the owner on Jan 1, 2024. It is now read-only.

Commit 07227b0

Browse files
authored
Fix needs_restart for non-bootstrapped instance (#131)
Before this patch needs_restart function didn't check if box.cfg was called or not. But if box.cfg wasn't called we have to restart instance if config was changed.
1 parent 85e2d87 commit 07227b0

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

library/cartridge_instance.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ansible.module_utils.helpers import ModuleRes, CartridgeException, cartridge_errcodes
55
from ansible.module_utils.helpers import get_control_console
66
from ansible.module_utils.helpers import dynamic_box_cfg_params, memory_size_box_cfg_params
7+
from ansible.module_utils.helpers import box_cfg_was_called
78

89
import os
910

@@ -37,12 +38,6 @@ def serialize_dict(d):
3738
return '{ %s }' % ', '.join(parts)
3839

3940

40-
def box_cfg_was_called(control_console):
41-
return control_console.eval('''
42-
return type(box.cfg) ~= 'function'
43-
''')
44-
45-
4641
def get_box_cfg(control_console):
4742
return control_console.eval('''
4843
return type(box.cfg) == 'table' and box.cfg or box.NULL

library/cartridge_needs_restart.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ansible.module_utils.helpers import ModuleRes, CartridgeException, cartridge_errcodes
55
from ansible.module_utils.helpers import get_control_console
66
from ansible.module_utils.helpers import dynamic_box_cfg_params
7+
from ansible.module_utils.helpers import box_cfg_was_called
78

89
import os
910

@@ -120,7 +121,7 @@ def needs_restart(params):
120121
if last_restart_time < package_update_time:
121122
return ModuleRes(success=True, changed=True)
122123

123-
# check if instance config was changed (except memtx_memory)
124+
# check if instance config was changed (except dynamic params)
124125
current_instance_conf = read_yaml_file_section(
125126
instance_conf_file,
126127
control_console,
@@ -130,7 +131,7 @@ def needs_restart(params):
130131
return ModuleRes(success=True, changed=True)
131132

132133
if not stateboard:
133-
# check if default config was changed (except memtx_memory)
134+
# check if default config was changed (except dynamic params)
134135
current_default_conf = read_yaml_file_section(
135136
default_conf_path,
136137
control_console,
@@ -140,7 +141,13 @@ def needs_restart(params):
140141
if check_conf_updated(new_default_conf, current_default_conf, dynamic_box_cfg_params):
141142
return ModuleRes(success=True, changed=True)
142143

144+
# if box.cfg wasn't called,
145+
if not box_cfg_was_called(control_console):
146+
return ModuleRes(success=True, changed=True)
147+
143148
current_cfg = get_current_cfg(control_console)
149+
if current_cfg is None:
150+
return ModuleRes(success=True, changed=True)
144151

145152
for param_name in dynamic_box_cfg_params:
146153
new_value = None
@@ -153,7 +160,7 @@ def needs_restart(params):
153160
# If current parameter wasn't changed to the new value,
154161
# it mean that instance should be restarted to apply change
155162
if new_value is not None:
156-
if current_cfg[param_name] != new_value:
163+
if current_cfg.get(param_name) != new_value:
157164
return ModuleRes(success=True, changed=True)
158165

159166
return ModuleRes(success=True, changed=False)

module_utils/helpers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,9 @@ def is_expelled(host_vars):
198198

199199
def is_stateboard(host_vars):
200200
return host_vars.get('stateboard') is True
201+
202+
203+
def box_cfg_was_called(control_console):
204+
return control_console.eval('''
205+
return type(box.cfg) ~= 'function'
206+
''')

unit/test_needs_restart.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,39 @@ def test_instance_not_started(self):
8585
self.assertTrue(res.success, msg=res.msg)
8686
self.assertTrue(res.changed)
8787

88+
def test_box_cfg_is_function(self):
89+
param_name = 'some-param'
90+
old_value = 'old-value'
91+
new_value = 'new-value'
92+
93+
self.instance.set_box_cfg_function()
94+
95+
self.instance.set_instance_config({
96+
param_name: old_value,
97+
})
98+
99+
# nothing changed
100+
res = call_needs_restart(
101+
control_sock=self.console_sock,
102+
config={
103+
param_name: old_value,
104+
},
105+
)
106+
107+
self.assertTrue(res.success, msg=res.msg)
108+
self.assertTrue(res.changed)
109+
110+
# param was changed
111+
res = call_needs_restart(
112+
control_sock=self.console_sock,
113+
config={
114+
param_name: new_value,
115+
},
116+
)
117+
118+
self.assertTrue(res.success, msg=res.msg)
119+
self.assertTrue(res.changed)
120+
88121
def test_code_was_updated(self):
89122
# code was updated today, socket yesterday - needs restart
90123
self.instance.set_path_mtime(self.instance.APP_CODE_PATH, self.instance.DATE_TODAY)
@@ -290,7 +323,7 @@ def test_app_config_changed(self, instance_type, memory_param_name):
290323
["memtx_memory"],
291324
["vinyl_memory"],
292325
])
293-
def test_both_app_and_instance_memtx_memory_changed(self, memory_param_name):
326+
def test_memory_size_changed(self, memory_param_name):
294327
current_memory_size = 100
295328
new_memory_size_instance = 200
296329
new_memory_size_app = 300

0 commit comments

Comments
 (0)