-
Notifications
You must be signed in to change notification settings - Fork 86
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
[sonic-host-services]: Support GCU and reload #1
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
86eb51f
Add dbus support for below commands:
ganglyu 4f01fb1
Merge branch 'master' into update_dbus
ganglyu 8faadef
Merge branch 'master' into update_dbus
ganglyu 4934701
Merge branch 'master' into update_dbus
qiluo-msft 0f1e190
Update for comment
ganglyu 8287786
Merge branch 'master' into update_dbus
qiluo-msft be6b250
Update dbus interface.
ganglyu 6c520d5
Fix LGTM alert.
ganglyu eff6c16
Use stdin to replace temp file.
ganglyu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Config command handler""" | ||
|
||
from host_modules import host_service | ||
import subprocess | ||
import os | ||
|
||
MOD_NAME = 'config' | ||
DEFAULT_CONFIG = '/etc/sonic/config_db.json' | ||
|
||
class Config(host_service.HostModule): | ||
""" | ||
DBus endpoint that executes the config command | ||
""" | ||
@host_service.method(host_service.bus_name(MOD_NAME), in_signature='s', out_signature='is') | ||
def reload(self, config_db_json): | ||
|
||
cmd = ['/usr/local/bin/config', 'reload', '-y'] | ||
config_db_json = config_db_json.strip() | ||
if config_db_json and len(config_db_json): | ||
config_file = '/tmp/config_db.json' | ||
ganglyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try: | ||
if (os.path.exists(config_file)): | ||
os.remove(config_file) | ||
with open(config_file, 'w') as fp: | ||
fp.write(config_db_json) | ||
except Exception as err: | ||
return -1, "Fail to create config file: %s"%str(err) | ||
cmd.append(config_file) | ||
|
||
result = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
msg = '' | ||
if result.returncode: | ||
lines = result.stderr.decode().split('\n') | ||
for line in lines: | ||
if 'Error' in line: | ||
msg = line | ||
break | ||
return result.returncode, msg | ||
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. Execute command and return error message code seems dupe in multiple places, suggest create a new method. |
||
|
||
@host_service.method(host_service.bus_name(MOD_NAME), in_signature='s', out_signature='is') | ||
def save(self, config_file): | ||
|
||
cmd = ['/usr/local/bin/config', 'save', '-y'] | ||
if config_file and config_file != DEFAULT_CONFIG: | ||
cmd.append(config_file) | ||
|
||
result = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
msg = '' | ||
if result.returncode: | ||
lines = result.stderr.decode().split('\n') | ||
for line in lines: | ||
if 'Error' in line: | ||
msg = line | ||
break | ||
return result.returncode, msg | ||
|
||
def register(): | ||
"""Return class and module name""" | ||
return Config, MOD_NAME | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
"""Generic config updater command handler""" | ||
|
||
from host_modules import host_service | ||
import subprocess | ||
|
||
MOD_NAME = 'gcu' | ||
|
||
class GCU(host_service.HostModule): | ||
""" | ||
DBus endpoint that executes the generic config updater command | ||
""" | ||
@host_service.method(host_service.bus_name(MOD_NAME), in_signature='s', out_signature='is') | ||
def apply_patch_db(self, patch_text): | ||
patch_file_path = '/tmp/config_db.patch' | ||
try: | ||
with open(patch_file_path, 'w') as fp: | ||
fp.write(patch_text) | ||
except Exception as err: | ||
return -1, "Fail to create patch file: %s"%str(err) | ||
|
||
cmd = ['/usr/local/bin/config', 'apply-patch', '-f', 'CONFIGDB', patch_file_path] | ||
ganglyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
result = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
msg = '' | ||
if result.returncode: | ||
lines = result.stderr.decode().split('\n') | ||
for line in lines: | ||
if 'Error' in line: | ||
msg = line | ||
break | ||
return result.returncode, msg | ||
|
||
@host_service.method(host_service.bus_name(MOD_NAME), in_signature='s', out_signature='is') | ||
def apply_patch_yang(self, patch_text): | ||
patch_file_path = '/tmp/config_yang.patch' | ||
try: | ||
with open(patch_file_path, 'w') as fp: | ||
fp.write(patch_text) | ||
except Exception as err: | ||
return -1, "Fail to create patch file: %s"%str(err) | ||
|
||
cmd = ['/usr/local/bin/config', 'apply-patch', '-f', 'SONICYANG', patch_file_path] | ||
ganglyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
result = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
msg = '' | ||
if result.returncode: | ||
lines = result.stderr.decode().split('\n') | ||
for line in lines: | ||
if 'Error' in line: | ||
msg = line | ||
break | ||
return result.returncode, msg | ||
|
||
@host_service.method(host_service.bus_name(MOD_NAME), in_signature='s', out_signature='is') | ||
def create_checkpoint(self, checkpoint_file): | ||
|
||
cmd = ['/usr/local/bin/config', 'checkpoint', checkpoint_file] | ||
|
||
result = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
msg = '' | ||
if result.returncode: | ||
lines = result.stderr.decode().split('\n') | ||
for line in lines: | ||
if 'Error' in line: | ||
msg = line | ||
break | ||
return result.returncode, msg | ||
|
||
@host_service.method(host_service.bus_name(MOD_NAME), in_signature='s', out_signature='is') | ||
def delete_checkpoint(self, checkpoint_file): | ||
|
||
cmd = ['/usr/local/bin/config', 'delete-checkpoint', checkpoint_file] | ||
|
||
result = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
msg = '' | ||
if result.returncode: | ||
lines = result.stderr.decode().split('\n') | ||
for line in lines: | ||
if 'Error' in line: | ||
msg = line | ||
break | ||
return result.returncode, msg | ||
|
||
def register(): | ||
"""Return class and module name""" | ||
return GCU, MOD_NAME | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[pytest] | ||
addopts = --cov=scripts --cov-report html --cov-report term --cov-report xml --ignore=tests/*/test*_vectors.py --junitxml=test-results.xml -vv | ||
addopts = --cov=scripts --cov=host_modules --cov-report html --cov-report term --cov-report xml --ignore=tests/*/test*_vectors.py --junitxml=test-results.xml -vv | ||
ganglyu marked this conversation as resolved.
Show resolved
Hide resolved
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import sys | ||
import os | ||
import pytest | ||
from unittest import mock | ||
from host_modules import config_engine | ||
|
||
class TestConfigEngine(object): | ||
@mock.patch("dbus.SystemBus") | ||
@mock.patch("dbus.service.BusName") | ||
@mock.patch("dbus.service.Object.__init__") | ||
def test_reload(self, MockInit, MockBusName, MockSystemBus): | ||
config_file = "/tmp/config_db.json" | ||
with mock.patch("subprocess.run") as mock_run: | ||
res_mock = mock.Mock() | ||
test_ret = 0 | ||
test_msg = b"Error: this is the test message\nHello world\n" | ||
attrs = {"returncode": test_ret, "stderr": test_msg} | ||
res_mock.configure_mock(**attrs) | ||
mock_run.return_value = res_mock | ||
config_db_json = "{}" | ||
config_stub = config_engine.Config(config_engine.MOD_NAME) | ||
ret, msg = config_stub.reload(config_db_json) | ||
call_args = mock_run.call_args[0][0] | ||
assert "reload" in call_args | ||
assert config_file in call_args | ||
assert ret == test_ret, "Return value is wrong" | ||
assert msg == "", "Return message is wrong" | ||
with mock.patch("subprocess.run") as mock_run: | ||
res_mock = mock.Mock() | ||
test_ret = 1 | ||
test_msg = b"Error: this is the test message\nHello world\n" | ||
attrs = {"returncode": test_ret, "stderr": test_msg} | ||
res_mock.configure_mock(**attrs) | ||
mock_run.return_value = res_mock | ||
config_db_json = "{}" | ||
config_stub = config_engine.Config(config_engine.MOD_NAME) | ||
ret, msg = config_stub.reload(config_db_json) | ||
call_args = mock_run.call_args[0][0] | ||
assert "reload" in call_args | ||
assert config_file in call_args | ||
assert ret == test_ret, "Return value is wrong" | ||
assert msg == "Error: this is the test message", "Return message is wrong" | ||
|
||
@mock.patch("dbus.SystemBus") | ||
@mock.patch("dbus.service.BusName") | ||
@mock.patch("dbus.service.Object.__init__") | ||
def test_save(self, MockInit, MockBusName, MockSystemBus): | ||
with mock.patch("subprocess.run") as mock_run: | ||
res_mock = mock.Mock() | ||
test_ret = 0 | ||
test_msg = b"Error: this is the test message\nHello world\n" | ||
attrs = {"returncode": test_ret, "stderr": test_msg} | ||
res_mock.configure_mock(**attrs) | ||
mock_run.return_value = res_mock | ||
config_file = "test.patch" | ||
config_stub = config_engine.Config(config_engine.MOD_NAME) | ||
ret, msg = config_stub.save(config_file) | ||
call_args = mock_run.call_args[0][0] | ||
assert "save" in call_args | ||
assert config_file in call_args | ||
assert ret == test_ret, "Return value is wrong" | ||
assert msg == "", "Return message is wrong" | ||
with mock.patch("subprocess.run") as mock_run: | ||
res_mock = mock.Mock() | ||
test_ret = 1 | ||
test_msg = b"Error: this is the test message\nHello world\n" | ||
attrs = {"returncode": test_ret, "stderr": test_msg} | ||
res_mock.configure_mock(**attrs) | ||
mock_run.return_value = res_mock | ||
config_file = "test.patch" | ||
config_stub = config_engine.Config(config_engine.MOD_NAME) | ||
ret, msg = config_stub.save(config_file) | ||
call_args = mock_run.call_args[0][0] | ||
assert "save" in call_args | ||
assert config_file in call_args | ||
assert ret == test_ret, "Return value is wrong" | ||
assert msg == "Error: this is the test message", "Return message is wrong" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import sys | ||
import os | ||
import pytest | ||
from unittest import mock | ||
from host_modules import gcu | ||
|
||
class TestGCU(object): | ||
@mock.patch("dbus.SystemBus") | ||
@mock.patch("dbus.service.BusName") | ||
@mock.patch("dbus.service.Object.__init__") | ||
def test_apply_patch_db(self, MockInit, MockBusName, MockSystemBus): | ||
with mock.patch("subprocess.run") as mock_run: | ||
res_mock = mock.Mock() | ||
test_ret = 0 | ||
test_msg = b"Error: this is the test message\nHello world\n" | ||
attrs = {"returncode": test_ret, "stderr": test_msg} | ||
res_mock.configure_mock(**attrs) | ||
mock_run.return_value = res_mock | ||
patch_text = "{}" | ||
gcu_stub = gcu.GCU(gcu.MOD_NAME) | ||
ret, msg = gcu_stub.apply_patch_db(patch_text) | ||
call_args = mock_run.call_args[0][0] | ||
assert "apply-patch" in call_args | ||
assert "CONFIGDB" in call_args | ||
assert '/tmp/config_db.patch' in call_args | ||
assert ret == test_ret, "Return value is wrong" | ||
assert msg == "", "Return message is wrong" | ||
with mock.patch("subprocess.run") as mock_run: | ||
res_mock = mock.Mock() | ||
test_ret = 1 | ||
test_msg = b"Error: this is the test message\nHello world\n" | ||
attrs = {"returncode": test_ret, "stderr": test_msg} | ||
res_mock.configure_mock(**attrs) | ||
mock_run.return_value = res_mock | ||
patch_text = "{}" | ||
gcu_stub = gcu.GCU(gcu.MOD_NAME) | ||
ret, msg = gcu_stub.apply_patch_db(patch_text) | ||
call_args = mock_run.call_args[0][0] | ||
assert "apply-patch" in call_args | ||
assert "CONFIGDB" in call_args | ||
assert '/tmp/config_db.patch' in call_args | ||
assert ret == test_ret, "Return value is wrong" | ||
assert msg == "Error: this is the test message", "Return message is wrong" | ||
|
||
@mock.patch("dbus.SystemBus") | ||
@mock.patch("dbus.service.BusName") | ||
@mock.patch("dbus.service.Object.__init__") | ||
def test_apply_patch_yang(self, MockInit, MockBusName, MockSystemBus): | ||
with mock.patch("subprocess.run") as mock_run: | ||
res_mock = mock.Mock() | ||
test_ret = 0 | ||
test_msg = b"Error: this is the test message\nHello world\n" | ||
attrs = {"returncode": test_ret, "stderr": test_msg} | ||
res_mock.configure_mock(**attrs) | ||
mock_run.return_value = res_mock | ||
patch_text = "{}" | ||
gcu_stub = gcu.GCU(gcu.MOD_NAME) | ||
ret, msg = gcu_stub.apply_patch_yang(patch_text) | ||
call_args = mock_run.call_args[0][0] | ||
assert "apply-patch" in call_args | ||
assert "SONICYANG" in call_args | ||
assert '/tmp/config_yang.patch' in call_args | ||
assert ret == test_ret, "Return value is wrong" | ||
assert msg == "", "Return message is wrong" | ||
with mock.patch("subprocess.run") as mock_run: | ||
res_mock = mock.Mock() | ||
test_ret = 1 | ||
test_msg = b"Error: this is the test message\nHello world\n" | ||
attrs = {"returncode": test_ret, "stderr": test_msg} | ||
res_mock.configure_mock(**attrs) | ||
mock_run.return_value = res_mock | ||
patch_text = "{}" | ||
gcu_stub = gcu.GCU(gcu.MOD_NAME) | ||
ret, msg = gcu_stub.apply_patch_yang(patch_text) | ||
call_args = mock_run.call_args[0][0] | ||
assert "apply-patch" in call_args | ||
assert "SONICYANG" in call_args | ||
assert '/tmp/config_yang.patch' in call_args | ||
assert ret == test_ret, "Return value is wrong" | ||
assert msg == "Error: this is the test message", "Return message is wrong" | ||
|
||
@mock.patch("dbus.SystemBus") | ||
@mock.patch("dbus.service.BusName") | ||
@mock.patch("dbus.service.Object.__init__") | ||
def test_create_checkpoint(self, MockInit, MockBusName, MockSystemBus): | ||
with mock.patch("subprocess.run") as mock_run: | ||
res_mock = mock.Mock() | ||
test_ret = 0 | ||
test_msg = b"Error: this is the test message\nHello world\n" | ||
attrs = {"returncode": test_ret, "stderr": test_msg} | ||
res_mock.configure_mock(**attrs) | ||
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. Suggest create a new method for dupe code. |
||
mock_run.return_value = res_mock | ||
cp_name = "test_name" | ||
gcu_stub = gcu.GCU(gcu.MOD_NAME) | ||
ret, msg = gcu_stub.create_checkpoint(cp_name) | ||
call_args = mock_run.call_args[0][0] | ||
assert "checkpoint" in call_args | ||
assert "delete-checkpoint" not in call_args | ||
assert cp_name in call_args | ||
assert ret == test_ret, "Return value is wrong" | ||
assert msg == "", "Return message is wrong" | ||
with mock.patch("subprocess.run") as mock_run: | ||
res_mock = mock.Mock() | ||
test_ret = 1 | ||
test_msg = b"Error: this is the test message\nHello world\n" | ||
attrs = {"returncode": test_ret, "stderr": test_msg} | ||
res_mock.configure_mock(**attrs) | ||
mock_run.return_value = res_mock | ||
cp_name = "test_name" | ||
gcu_stub = gcu.GCU(gcu.MOD_NAME) | ||
ret, msg = gcu_stub.create_checkpoint(cp_name) | ||
call_args = mock_run.call_args[0][0] | ||
assert "checkpoint" in call_args | ||
assert "delete-checkpoint" not in call_args | ||
assert cp_name in call_args | ||
assert ret == test_ret, "Return value is wrong" | ||
assert msg == "Error: this is the test message", "Return message is wrong" | ||
|
||
@mock.patch("dbus.SystemBus") | ||
@mock.patch("dbus.service.BusName") | ||
@mock.patch("dbus.service.Object.__init__") | ||
def test_delete_checkpoint(self, MockInit, MockBusName, MockSystemBus): | ||
with mock.patch("subprocess.run") as mock_run: | ||
res_mock = mock.Mock() | ||
test_ret = 0 | ||
test_msg = b"Error: this is the test message\nHello world\n" | ||
attrs = {"returncode": test_ret, "stderr": test_msg} | ||
res_mock.configure_mock(**attrs) | ||
mock_run.return_value = res_mock | ||
cp_name = "test_name" | ||
gcu_stub = gcu.GCU(gcu.MOD_NAME) | ||
ret, msg = gcu_stub.delete_checkpoint(cp_name) | ||
call_args = mock_run.call_args[0][0] | ||
assert "delete-checkpoint" in call_args | ||
assert cp_name in call_args | ||
assert ret == test_ret, "Return value is wrong" | ||
assert msg == "", "Return message is wrong" | ||
with mock.patch("subprocess.run") as mock_run: | ||
res_mock = mock.Mock() | ||
test_ret = 1 | ||
test_msg = b"Error: this is the test message\nHello world\n" | ||
attrs = {"returncode": test_ret, "stderr": test_msg} | ||
res_mock.configure_mock(**attrs) | ||
mock_run.return_value = res_mock | ||
cp_name = "test_name" | ||
gcu_stub = gcu.GCU(gcu.MOD_NAME) | ||
ret, msg = gcu_stub.delete_checkpoint(cp_name) | ||
call_args = mock_run.call_args[0][0] | ||
assert "delete-checkpoint" in call_args | ||
assert cp_name in call_args | ||
assert ret == test_ret, "Return value is wrong" | ||
assert msg == "Error: this is the test message", "Return message is wrong" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why strip? Is it a valid user intention to provide the argument with extra blanks? #Closed
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.
Still have the question.
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.
Now I use len(config_db_json.strip()) to detect string with only spaces, and config_db_json is still the same.