mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-08 10:54:15 +02:00
[snap] improvement -> snapd.is_running() refactored
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
import re
|
|
||||||
import socket
|
import socket
|
||||||
import traceback
|
import traceback
|
||||||
from logging import Logger
|
from logging import Logger
|
||||||
@@ -12,8 +11,6 @@ from urllib3.connectionpool import HTTPConnectionPool
|
|||||||
from bauh.commons.system import run_cmd
|
from bauh.commons.system import run_cmd
|
||||||
|
|
||||||
URL_BASE = 'http://snapd/v2'
|
URL_BASE = 'http://snapd/v2'
|
||||||
RE_SNAPD_STATUS = re.compile('\s+')
|
|
||||||
RE_SNAPD_SERVICES = re.compile(r'snapd\.\w+.+')
|
|
||||||
|
|
||||||
|
|
||||||
class SnapdConnection(HTTPConnection):
|
class SnapdConnection(HTTPConnection):
|
||||||
@@ -112,25 +109,12 @@ class SnapdClient:
|
|||||||
|
|
||||||
|
|
||||||
def is_running() -> bool:
|
def is_running() -> bool:
|
||||||
output = run_cmd('systemctl list-units', print_error=False)
|
status = run_cmd('systemctl is-active snapd.service snapd.socket', print_error=False)
|
||||||
|
if not status:
|
||||||
if not output:
|
|
||||||
return False
|
return False
|
||||||
|
else:
|
||||||
|
for status in status.split('\n'):
|
||||||
|
if status.strip().lower() == 'active':
|
||||||
|
return True
|
||||||
|
|
||||||
snapd_services = RE_SNAPD_SERVICES.findall(output)
|
return False
|
||||||
|
|
||||||
snap_socket, socket_running, service, service_running = False, False, False, False
|
|
||||||
if snapd_services:
|
|
||||||
for service_line in snapd_services:
|
|
||||||
line_split = RE_SNAPD_STATUS.split(service_line)
|
|
||||||
|
|
||||||
running = line_split[3] in {'listening', 'running'}
|
|
||||||
|
|
||||||
if line_split[0] == 'snapd.service':
|
|
||||||
service = True
|
|
||||||
service_running = running
|
|
||||||
elif line_split[0] == 'snapd.socket':
|
|
||||||
snap_socket = True
|
|
||||||
socket_running = running
|
|
||||||
|
|
||||||
return snap_socket and socket_running and (not service or service_running)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user