selfprivacy-rest-api/selfprivacy_api/resources/system.py

154 lines
3.8 KiB
Python
Raw Normal View History

2021-11-11 18:31:28 +00:00
#!/usr/bin/env python3
2021-11-16 16:14:01 +00:00
"""System management module"""
import subprocess
2021-11-11 18:31:28 +00:00
from flask import Blueprint
from flask_restful import Resource, Api
api_system = Blueprint("system", __name__, url_prefix="/system")
api = Api(api_system)
2021-11-16 16:14:01 +00:00
2021-11-11 18:31:28 +00:00
class RebuildSystem(Resource):
2021-11-16 16:14:01 +00:00
"""Rebuild NixOS"""
2021-11-11 18:31:28 +00:00
def get(self):
2021-11-16 16:14:01 +00:00
"""
Rebuild NixOS with nixos-rebuild switch
---
tags:
- System
security:
- bearerAuth: []
responses:
200:
description: System rebuild has started
401:
description: Unauthorized
"""
2021-11-17 10:36:26 +00:00
rebuild_result = subprocess.Popen(
["systemctl", "start", "sp-nixos-rebuild.service"], start_new_session=True
)
2021-11-16 16:14:01 +00:00
rebuild_result.communicate()[0]
return rebuild_result.returncode
2021-11-11 18:31:28 +00:00
class RollbackSystem(Resource):
2021-11-16 16:14:01 +00:00
"""Rollback NixOS"""
2021-11-11 18:31:28 +00:00
def get(self):
2021-11-16 16:14:01 +00:00
"""
Rollback NixOS with nixos-rebuild switch --rollback
---
tags:
- System
security:
- bearerAuth: []
responses:
200:
description: System rollback has started
401:
description: Unauthorized
"""
2021-11-17 10:36:26 +00:00
rollback_result = subprocess.Popen(
["systemctl", "start", "sp-nixos-rollback.service"], start_new_session=True
)
2021-11-16 16:14:01 +00:00
rollback_result.communicate()[0]
return rollback_result.returncode
2021-11-11 18:31:28 +00:00
class UpgradeSystem(Resource):
2021-11-16 16:14:01 +00:00
"""Upgrade NixOS"""
2021-11-11 18:31:28 +00:00
def get(self):
2021-11-16 16:14:01 +00:00
"""
Upgrade NixOS with nixos-rebuild switch --upgrade
---
tags:
- System
security:
- bearerAuth: []
responses:
200:
description: System upgrade has started
401:
description: Unauthorized
"""
2021-11-17 10:36:26 +00:00
upgrade_result = subprocess.Popen(
["systemctl", "start", "sp-nixos-upgrade.service"], start_new_session=True
)
2021-11-16 16:14:01 +00:00
upgrade_result.communicate()[0]
return upgrade_result.returncode
2021-11-11 18:31:28 +00:00
2021-11-17 10:36:26 +00:00
class RebootSystem(Resource):
"""Reboot the system"""
def get(self):
"""
Reboot the system
---
tags:
- System
security:
- bearerAuth: []
responses:
200:
description: System reboot has started
401:
description: Unauthorized
"""
subprocess.Popen(["reboot"], start_new_session=True)
return "System reboot has started"
2021-11-11 18:31:28 +00:00
class SystemVersion(Resource):
2021-11-16 16:14:01 +00:00
"""Get system version from uname"""
2021-11-11 18:31:28 +00:00
def get(self):
2021-11-16 16:14:01 +00:00
"""
Get system version from uname -a
---
tags:
- System
security:
- bearerAuth: []
responses:
200:
description: OK
401:
description: Unauthorized
"""
2021-11-11 18:31:28 +00:00
return {
"system_version": subprocess.check_output(["uname", "-a"])
.decode("utf-8")
.strip()
}
class PythonVersion(Resource):
2021-11-16 16:14:01 +00:00
"""Get python version"""
2021-11-11 18:31:28 +00:00
def get(self):
2021-11-16 16:14:01 +00:00
"""
Get python version used by this API
---
tags:
- System
security:
- bearerAuth: []
responses:
200:
description: OK
401:
description: Unauthorized
"""
2021-11-11 18:31:28 +00:00
return subprocess.check_output(["python", "-V"]).decode("utf-8").strip()
api.add_resource(RebuildSystem, "/configuration/apply")
api.add_resource(RollbackSystem, "/configuration/rollback")
api.add_resource(UpgradeSystem, "/configuration/upgrade")
2021-11-17 10:36:26 +00:00
api.add_resource(RebootSystem, "/reboot")
2021-11-11 18:31:28 +00:00
api.add_resource(SystemVersion, "/version")
api.add_resource(PythonVersion, "/pythonVersion")