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

126 lines
3.1 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
"""
rebuild_result = subprocess.Popen(["nixos-rebuild", "switch"])
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
"""
rollback_result = subprocess.Popen(["nixos-rebuild", "switch", "--rollback"])
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
"""
upgrade_result = subprocess.Popen(["nixos-rebuild", "switch", "--upgrade"])
upgrade_result.communicate()[0]
return upgrade_result.returncode
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, "/upgrade")
api.add_resource(SystemVersion, "/version")
api.add_resource(PythonVersion, "/pythonVersion")