From c53cab7b88b0eb16ec7c45c451c585d924e311ee Mon Sep 17 00:00:00 2001 From: Inex Code Date: Wed, 1 Dec 2021 17:51:55 +0300 Subject: [PATCH] Move PullRepositoryChanges to system resource --- selfprivacy_api/resources/services/update.py | 55 -------------------- selfprivacy_api/resources/system.py | 47 +++++++++++++++++ 2 files changed, 47 insertions(+), 55 deletions(-) delete mode 100644 selfprivacy_api/resources/services/update.py diff --git a/selfprivacy_api/resources/services/update.py b/selfprivacy_api/resources/services/update.py deleted file mode 100644 index 73698d5a..00000000 --- a/selfprivacy_api/resources/services/update.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env/python3 -"""Update dispatch module""" -import os -import subprocess -from flask_restful import Resource, reqparse - -from selfprivacy_api.resources.services import api - - -class PullRepositoryChanges(Resource): - def get(self): - """ - Pull Repository Changes - --- - tags: - - Update - security: - - bearerAuth: [] - responses: - 200: - description: Got update - 201: - description: Nothing to update - 401: - description: Unauthorized - 500: - description: Something went wrong - """ - - git_pull_command = ["git", "pull"] - - current_working_directory = os.getcwd() - os.chdir("/etc/nixos") - - git_pull_process_descriptor = subprocess.Popen( - git_pull_command, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - shell=False, - ) - - git_pull_process_descriptor.communicate()[0] - - os.chdir(current_working_directory) - - if git_pull_process_descriptor.returncode == 0: - return {"status": 0, "message": "Update completed successfully"} - elif git_pull_process_descriptor.returncode > 0: - return { - "status": git_pull_process_descriptor.returncode, - "message": "Something went wrong", - }, 500 - - -api.add_resource(PullRepositoryChanges, "/update") diff --git a/selfprivacy_api/resources/system.py b/selfprivacy_api/resources/system.py index ddce3be9..d13f1fcf 100644 --- a/selfprivacy_api/resources/system.py +++ b/selfprivacy_api/resources/system.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 """System management module""" +import os import subprocess import pytz from flask import Blueprint @@ -281,6 +282,51 @@ class PythonVersion(Resource): return subprocess.check_output(["python", "-V"]).decode("utf-8").strip() +class PullRepositoryChanges(Resource): + def get(self): + """ + Pull Repository Changes + --- + tags: + - System + security: + - bearerAuth: [] + responses: + 200: + description: Got update + 201: + description: Nothing to update + 401: + description: Unauthorized + 500: + description: Something went wrong + """ + + git_pull_command = ["git", "pull"] + + current_working_directory = os.getcwd() + os.chdir("/etc/nixos") + + git_pull_process_descriptor = subprocess.Popen( + git_pull_command, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + shell=False, + ) + + git_pull_process_descriptor.communicate()[0] + + os.chdir(current_working_directory) + + if git_pull_process_descriptor.returncode == 0: + return {"status": 0, "message": "Update completed successfully"} + elif git_pull_process_descriptor.returncode > 0: + return { + "status": git_pull_process_descriptor.returncode, + "message": "Something went wrong", + }, 500 + + api.add_resource(Timezone, "/configuration/timezone") api.add_resource(AutoUpgrade, "/configuration/autoUpgrade") api.add_resource(RebuildSystem, "/configuration/apply") @@ -289,3 +335,4 @@ api.add_resource(UpgradeSystem, "/configuration/upgrade") api.add_resource(RebootSystem, "/reboot") api.add_resource(SystemVersion, "/version") api.add_resource(PythonVersion, "/pythonVersion") +api.add_resource(PullRepositoryChanges, "/update")