From 7f5f6019b2f9a46d676f29885cd2dd28f9d63533 Mon Sep 17 00:00:00 2001 From: "Simental Magana, Marcos" Date: Tue, 2 Feb 2016 15:58:53 -0600 Subject: [PATCH 1/3] Add function to check if a given port is open Signed-off-by: Simental Magana, Marcos --- clearstack/common/util.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/clearstack/common/util.py b/clearstack/common/util.py index 0ecddc2..dc51ccd 100644 --- a/clearstack/common/util.py +++ b/clearstack/common/util.py @@ -52,6 +52,13 @@ def _print_error_message(self, e, file_name): file_name)) +def port_open(port): + """Return True if given port is already open in localhost. + Return False otherwise.""" + sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + return (sck.connect_ex(('127.0.0.1', port)) == 0) + + def setup_debugging(debug, is_remote_host=True): if not os.path.isdir(LOG_DIR): os.makedirs(LOG_DIR) From 750ebed4c0bebdec5ee0bce6c8ddc6da2220e161 Mon Sep 17 00:00:00 2001 From: "Simental Magana, Marcos" Date: Tue, 2 Feb 2016 16:47:02 -0600 Subject: [PATCH 2/3] Add function to check status of a given systemd service. Returns 'active', 'failed' or 'inactive' strings, depending of the systemd service status Signed-off-by: Simental Magana, Marcos --- clearstack/common/util.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/clearstack/common/util.py b/clearstack/common/util.py index dc51ccd..117bce0 100644 --- a/clearstack/common/util.py +++ b/clearstack/common/util.py @@ -59,6 +59,12 @@ def port_open(port): return (sck.connect_ex(('127.0.0.1', port)) == 0) +def service_status(service): + """Return status of a given systemd service.""" + stdout, stderr = run_command("systemctl is-active " + str(service)) + return (stdout.strip().decode("utf-8")) + + def setup_debugging(debug, is_remote_host=True): if not os.path.isdir(LOG_DIR): os.makedirs(LOG_DIR) From a4526e74888c14478378eb63e64677da3acd557a Mon Sep 17 00:00:00 2001 From: "Simental Magana, Marcos" Date: Wed, 3 Feb 2016 14:12:45 -0600 Subject: [PATCH 3/3] Add function to check if systemd unit service is enabled Signed-off-by: Simental Magana, Marcos --- clearstack/common/util.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/clearstack/common/util.py b/clearstack/common/util.py index 117bce0..ee89954 100644 --- a/clearstack/common/util.py +++ b/clearstack/common/util.py @@ -65,6 +65,12 @@ def service_status(service): return (stdout.strip().decode("utf-8")) +def service_enabled(service): + """Return True if systemd service is enabled, False otherwise""" + stdout, stderr = run_command("systemctl is-enabled " + str(service)) + return (stdout.strip() == b"enabled") + + def setup_debugging(debug, is_remote_host=True): if not os.path.isdir(LOG_DIR): os.makedirs(LOG_DIR)