2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2019-04-30 07:31:47 +01:00
|
|
|
client.py: implementation of the VnodeClient class for issuing commands
|
2013-08-29 15:21:13 +01:00
|
|
|
over a control channel to the vnoded process running in a network namespace.
|
2019-06-07 16:59:16 +01:00
|
|
|
The control channel can be accessed via calls using the vcmd shell.
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2019-10-14 22:28:18 +01:00
|
|
|
from core import utils
|
2019-10-12 00:36:57 +01:00
|
|
|
from core.constants import VCMD_BIN
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
|
2019-10-23 17:31:07 +01:00
|
|
|
class VnodeClient:
|
2017-05-03 21:20:56 +01:00
|
|
|
"""
|
|
|
|
Provides client functionality for interacting with a virtual node.
|
|
|
|
"""
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def __init__(self, name, ctrlchnlname):
|
2017-05-03 21:20:56 +01:00
|
|
|
"""
|
|
|
|
Create a VnodeClient instance.
|
|
|
|
|
|
|
|
:param str name: name for client
|
|
|
|
:param str ctrlchnlname: control channel name
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.name = name
|
|
|
|
self.ctrlchnlname = ctrlchnlname
|
|
|
|
|
2018-02-27 18:48:01 +00:00
|
|
|
def _verify_connection(self):
|
|
|
|
"""
|
|
|
|
Checks that the vcmd client is properly connected.
|
|
|
|
|
|
|
|
:return: nothing
|
2018-03-01 17:17:58 +00:00
|
|
|
:raises IOError: when not connected
|
2018-02-27 18:48:01 +00:00
|
|
|
"""
|
|
|
|
if not self.connected():
|
2018-03-01 17:17:58 +00:00
|
|
|
raise IOError("vcmd not connected")
|
2018-02-27 18:48:01 +00:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def connected(self):
|
2017-05-03 21:20:56 +01:00
|
|
|
"""
|
|
|
|
Check if node is connected or not.
|
|
|
|
|
|
|
|
:return: True if connected, False otherwise
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2019-06-03 03:06:25 +01:00
|
|
|
return True
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2016-03-04 21:57:17 +00:00
|
|
|
def close(self):
|
2017-05-03 21:20:56 +01:00
|
|
|
"""
|
|
|
|
Close the client connection.
|
|
|
|
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2019-06-03 03:06:25 +01:00
|
|
|
pass
|
|
|
|
|
2019-10-12 00:36:57 +01:00
|
|
|
def create_cmd(self, args):
|
2019-10-18 02:59:50 +01:00
|
|
|
return f"{VCMD_BIN} -c {self.ctrlchnlname} -- {args}"
|
2016-03-04 21:57:17 +00:00
|
|
|
|
2019-12-20 17:57:34 +00:00
|
|
|
def check_cmd(self, args, wait=True, shell=False):
|
2018-03-01 17:17:58 +00:00
|
|
|
"""
|
2018-03-01 21:21:25 +00:00
|
|
|
Run command and return exit status and combined stdout and stderr.
|
2018-03-01 17:17:58 +00:00
|
|
|
|
2019-10-12 00:36:57 +01:00
|
|
|
:param str args: command to run
|
2019-10-10 23:25:12 +01:00
|
|
|
:param bool wait: True to wait for command status, False otherwise
|
2019-12-20 17:57:34 +00:00
|
|
|
:param bool shell: True to use shell, False otherwise
|
2018-03-02 21:57:50 +00:00
|
|
|
:return: combined stdout and stderr
|
|
|
|
:rtype: str
|
2018-03-03 00:22:20 +00:00
|
|
|
:raises core.CoreCommandError: when there is a non-zero exit status
|
2018-03-01 17:17:58 +00:00
|
|
|
"""
|
2018-02-27 18:48:01 +00:00
|
|
|
self._verify_connection()
|
2019-10-12 00:36:57 +01:00
|
|
|
args = self.create_cmd(args)
|
2019-12-20 17:57:34 +00:00
|
|
|
return utils.cmd(args, wait=wait, shell=shell)
|