mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-29 15:31:28 +00:00
Change the ServiceStatus to match systemctl show
This commit is contained in:
parent
3a5d4d5e86
commit
bf1cd32895
|
@ -56,10 +56,12 @@ class ServiceStorageUsage(StorageUsageInterface):
|
||||||
|
|
||||||
@strawberry.enum
|
@strawberry.enum
|
||||||
class ServiceStatusEnum(Enum):
|
class ServiceStatusEnum(Enum):
|
||||||
RUNNING = "RUNNING"
|
ACTIVE = "ACTIVE"
|
||||||
DEGRADED = "DEGRADED"
|
RELOADING = "RELOADING"
|
||||||
ERROR = "ERROR"
|
INACTIVE = "INACTIVE"
|
||||||
STOPPED = "STOPPED"
|
FAILED = "FAILED"
|
||||||
|
ACTIVATING = "ACTIVATING"
|
||||||
|
DEACTIVATING = "DEACTIVATING"
|
||||||
OFF = "OFF"
|
OFF = "OFF"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -36,11 +36,13 @@ router = APIRouter(
|
||||||
|
|
||||||
|
|
||||||
def service_status_to_return_code(status: ServiceStatus):
|
def service_status_to_return_code(status: ServiceStatus):
|
||||||
if status == ServiceStatus.RUNNING:
|
"""Converts service status object to return code for
|
||||||
|
compatibility with legacy api"""
|
||||||
|
if status == ServiceStatus.ACTIVE:
|
||||||
return 0
|
return 0
|
||||||
elif status == ServiceStatus.ERROR:
|
elif status == ServiceStatus.FAILED:
|
||||||
return 1
|
return 1
|
||||||
elif status == ServiceStatus.STOPPED:
|
elif status == ServiceStatus.INACTIVE:
|
||||||
return 3
|
return 3
|
||||||
elif status == ServiceStatus.OFF:
|
elif status == ServiceStatus.OFF:
|
||||||
return 4
|
return 4
|
||||||
|
@ -317,13 +319,13 @@ async def rest_send_ssh_key(input: SshKeyInput):
|
||||||
"""Send the SSH key"""
|
"""Send the SSH key"""
|
||||||
try:
|
try:
|
||||||
create_ssh_key("root", input.public_key)
|
create_ssh_key("root", input.public_key)
|
||||||
except KeyAlreadyExists:
|
except KeyAlreadyExists as error:
|
||||||
raise HTTPException(status_code=409, detail="Key already exists")
|
raise HTTPException(status_code=409, detail="Key already exists") from error
|
||||||
except InvalidPublicKey:
|
except InvalidPublicKey as error:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail="Invalid key type. Only ssh-ed25519 and ssh-rsa are supported",
|
detail="Invalid key type. Only ssh-ed25519 and ssh-rsa are supported",
|
||||||
)
|
) from error
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"status": 0,
|
"status": 0,
|
||||||
|
@ -345,15 +347,15 @@ async def rest_get_ssh_keys(username: str):
|
||||||
async def rest_add_ssh_key(username: str, input: SshKeyInput):
|
async def rest_add_ssh_key(username: str, input: SshKeyInput):
|
||||||
try:
|
try:
|
||||||
create_ssh_key(username, input.public_key)
|
create_ssh_key(username, input.public_key)
|
||||||
except KeyAlreadyExists:
|
except KeyAlreadyExists as error:
|
||||||
raise HTTPException(status_code=409, detail="Key already exists")
|
raise HTTPException(status_code=409, detail="Key already exists") from error
|
||||||
except InvalidPublicKey:
|
except InvalidPublicKey as error:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail="Invalid key type. Only ssh-ed25519 and ssh-rsa are supported",
|
detail="Invalid key type. Only ssh-ed25519 and ssh-rsa are supported",
|
||||||
)
|
) from error
|
||||||
except UserNotFound:
|
except UserNotFound as error:
|
||||||
raise HTTPException(status_code=404, detail="User not found")
|
raise HTTPException(status_code=404, detail="User not found") from error
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"message": "New SSH key successfully written",
|
"message": "New SSH key successfully written",
|
||||||
|
@ -364,8 +366,8 @@ async def rest_add_ssh_key(username: str, input: SshKeyInput):
|
||||||
async def rest_delete_ssh_key(username: str, input: SshKeyInput):
|
async def rest_delete_ssh_key(username: str, input: SshKeyInput):
|
||||||
try:
|
try:
|
||||||
remove_ssh_key(username, input.public_key)
|
remove_ssh_key(username, input.public_key)
|
||||||
except KeyNotFound:
|
except KeyNotFound as error:
|
||||||
raise HTTPException(status_code=404, detail="Key not found")
|
raise HTTPException(status_code=404, detail="Key not found") from error
|
||||||
except UserNotFound:
|
except UserNotFound as error:
|
||||||
raise HTTPException(status_code=404, detail="User not found")
|
raise HTTPException(status_code=404, detail="User not found") from error
|
||||||
return {"message": "SSH key deleted"}
|
return {"message": "SSH key deleted"}
|
||||||
|
|
|
@ -103,10 +103,13 @@ def move_service(
|
||||||
progress=5,
|
progress=5,
|
||||||
)
|
)
|
||||||
service.stop()
|
service.stop()
|
||||||
# Wait for Nextcloud to stop, check every second
|
# Wait for the service to stop, check every second
|
||||||
# If it does not stop in 30 seconds, abort
|
# If it does not stop in 30 seconds, abort
|
||||||
for _ in range(30):
|
for _ in range(30):
|
||||||
if service.get_status() != ServiceStatus.RUNNING:
|
if service.get_status() not in (
|
||||||
|
ServiceStatus.ACTIVATING,
|
||||||
|
ServiceStatus.DEACTIVATING,
|
||||||
|
):
|
||||||
break
|
break
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
"""Generic service status fetcher using systemctl"""
|
"""Generic service status fetcher using systemctl"""
|
||||||
import subprocess
|
import subprocess
|
||||||
import typing
|
|
||||||
|
|
||||||
from selfprivacy_api.services.service import ServiceStatus
|
from selfprivacy_api.services.service import ServiceStatus
|
||||||
|
|
||||||
|
@ -8,22 +7,22 @@ from selfprivacy_api.services.service import ServiceStatus
|
||||||
def get_service_status(service: str) -> ServiceStatus:
|
def get_service_status(service: str) -> ServiceStatus:
|
||||||
"""
|
"""
|
||||||
Return service status from systemd.
|
Return service status from systemd.
|
||||||
Use command return code to determine status.
|
Use systemctl show to get the status of a service.
|
||||||
|
Get ActiveState from the output.
|
||||||
Return code 0 means service is running.
|
|
||||||
Return code 1 or 2 means service is in error stat.
|
|
||||||
Return code 3 means service is stopped.
|
|
||||||
Return code 4 means service is off.
|
|
||||||
"""
|
"""
|
||||||
service_status = subprocess.Popen(["systemctl", "status", service])
|
service_status = subprocess.check_output(["systemctl", "show", service])
|
||||||
service_status.communicate()[0]
|
if b"LoadState=not-found" in service_status:
|
||||||
if service_status.returncode == 0:
|
return ServiceStatus.OFF
|
||||||
return ServiceStatus.RUNNING
|
if b"ActiveState=active" in service_status:
|
||||||
elif service_status.returncode == 1 or service_status.returncode == 2:
|
return ServiceStatus.ACTIVE
|
||||||
return ServiceStatus.ERROR
|
if b"ActiveState=inactive" in service_status:
|
||||||
elif service_status.returncode == 3:
|
return ServiceStatus.INACTIVE
|
||||||
return ServiceStatus.STOPPED
|
if b"ActiveState=activating" in service_status:
|
||||||
elif service_status.returncode == 4:
|
return ServiceStatus.ACTIVATING
|
||||||
|
if b"ActiveState=deactivating" in service_status:
|
||||||
|
return ServiceStatus.DEACTIVATING
|
||||||
|
if b"ActiveState=failed" in service_status:
|
||||||
|
return ServiceStatus.FAILED
|
||||||
|
if b"ActiveState=reloading" in service_status:
|
||||||
|
return ServiceStatus.RELOADING
|
||||||
return ServiceStatus.OFF
|
return ServiceStatus.OFF
|
||||||
else:
|
|
||||||
return ServiceStatus.DEGRADED
|
|
||||||
|
|
|
@ -12,10 +12,12 @@ from selfprivacy_api.utils.block_devices import BlockDevice
|
||||||
class ServiceStatus(Enum):
|
class ServiceStatus(Enum):
|
||||||
"""Enum for service status"""
|
"""Enum for service status"""
|
||||||
|
|
||||||
RUNNING = "RUNNING"
|
ACTIVE = "ACTIVE"
|
||||||
DEGRADED = "DEGRADED"
|
RELOADING = "RELOADING"
|
||||||
ERROR = "ERROR"
|
INACTIVE = "INACTIVE"
|
||||||
STOPPED = "STOPPED"
|
FAILED = "FAILED"
|
||||||
|
ACTIVATING = "ACTIVATING"
|
||||||
|
DEACTIVATING = "DEACTIVATING"
|
||||||
OFF = "OFF"
|
OFF = "OFF"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue