selfprivacy-rest-api/selfprivacy_api/resources/services/gitea.py

88 lines
2.3 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
"""Gitea management module"""
import json
2021-11-16 16:14:01 +00:00
import portalocker
from flask_restful import Resource
2021-11-11 18:31:28 +00:00
from selfprivacy_api.resources.services import api
2021-11-16 16:14:01 +00:00
2021-11-11 18:31:28 +00:00
class EnableGitea(Resource):
2021-11-16 16:14:01 +00:00
"""Enable Gitea"""
2021-11-11 18:31:28 +00:00
def post(self):
2021-11-16 16:14:01 +00:00
"""
Enable Gitea
---
tags:
- Gitea
security:
- bearerAuth: []
responses:
200:
description: Gitea enabled
401:
description: Unauthorized
"""
with open(
"/etc/nixos/userdata/userdata.json", "r+", encoding="utf-8"
) as userdata_file:
portalocker.lock(userdata_file, portalocker.LOCK_EX)
try:
2021-11-16 16:14:01 +00:00
data = json.load(userdata_file)
if "gitea" not in data:
data["gitea"] = {}
data["gitea"]["enable"] = True
2021-11-16 16:14:01 +00:00
userdata_file.seek(0)
json.dump(data, userdata_file, indent=4)
userdata_file.truncate()
finally:
2021-11-16 16:14:01 +00:00
portalocker.unlock(userdata_file)
2021-11-11 18:31:28 +00:00
return {
"status": 0,
"message": "Gitea enabled",
}
class DisableGitea(Resource):
2021-11-16 16:14:01 +00:00
"""Disable Gitea"""
2021-11-11 18:31:28 +00:00
def post(self):
2021-11-16 16:14:01 +00:00
"""
Disable Gitea
---
tags:
- Gitea
security:
- bearerAuth: []
responses:
200:
description: Gitea disabled
401:
description: Unauthorized
"""
with open(
"/etc/nixos/userdata/userdata.json", "r+", encoding="utf-8"
) as userdata_file:
portalocker.lock(userdata_file, portalocker.LOCK_EX)
try:
2021-11-16 16:14:01 +00:00
data = json.load(userdata_file)
if "gitea" not in data:
data["gitea"] = {}
data["gitea"]["enable"] = False
2021-11-16 16:14:01 +00:00
userdata_file.seek(0)
json.dump(data, userdata_file, indent=4)
userdata_file.truncate()
finally:
2021-11-16 16:14:01 +00:00
portalocker.unlock(userdata_file)
2021-11-11 18:31:28 +00:00
return {
"status": 0,
"message": "Gitea disabled",
}
api.add_resource(EnableGitea, "/gitea/enable")
api.add_resource(DisableGitea, "/gitea/disable")