-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathadmin.py
More file actions
116 lines (108 loc) · 3.88 KB
/
admin.py
File metadata and controls
116 lines (108 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import json
from aiohttp import web
from subprocess import Popen
from multidict import MultiDict
from .utils import ApiUtils
from . import goose_migration_template
from services.migration_service.migration_config import host, port, user, password, database_name
class AdminApi(object):
def __init__(self, app):
app.router.add_route("GET", "/version", self.version)
app.router.add_route("GET", "/ping", self.ping)
app.router.add_route("GET", "/db_schema_status", self.db_schema_status)
endpoints_enabled = int(os.environ.get("MF_MIGRATION_ENDPOINTS_ENABLED",
1))
if endpoints_enabled:
app.router.add_route("PATCH", "/upgrade", self.upgrade)
async def ping(self, request):
"""
---
description: This end-point allow to test that service is up.
tags:
- Admin
produces:
- 'text/plain'
responses:
"202":
description: successful operation. Return "pong" text
"405":
description: invalid HTTP Method
"""
return web.Response(text="pong")
async def version(self, request):
"""
---
description: This end-point returns the latest compatible version of the
metadata service
tags:
- Admin
produces:
- 'application/json'
responses:
"200":
description: successful operation. Return version text
"405":
description: invalid HTTP Method
"""
version = await ApiUtils.get_latest_compatible_version()
return web.Response(text=version)
async def upgrade(self, request):
"""
---
description: This end-point upgrades to the latest available version of
of the schema
tags:
- Admin
produces:
- 'text/plain'
responses:
"200":
description: successful operation. Return text
"500":
description: could not upgrade
"""
goose_version_cmd = goose_migration_template.format(
database_name, user, password, host, port,
"up"
)
p = Popen(goose_version_cmd, shell=True,
close_fds=True)
p.wait()
if p.returncode == 0:
return web.Response(text="upgrade success")
else:
return web.Response(text="upgrade failed", status=500)
async def db_schema_status(self, request):
"""
---
description: This end-point returns varius stats around
tags:
- Admin
produces:
- 'application/json'
responses:
"200":
description: successful operation. returns status of db schema and migrations
"500":
description: could not upgrade
"""
try:
version = await ApiUtils.get_goose_version()
migration_in_progress = await ApiUtils.is_migration_in_progress()
unapplied_migrations = ApiUtils.get_unapplied_migrations(version)
body = {
"is_up_to_date": len(unapplied_migrations) == 0,
"current_version": version,
"migration_in_progress": migration_in_progress,
"db_schema_versions": ApiUtils.list_migrations(),
"unapplied_migrations": unapplied_migrations
}
return web.Response(body=json.dumps(body).encode('utf8'),
headers=MultiDict({"Content-Type": "application/json"}))
except Exception as e:
body = {
"detail": repr(e)
}
return web.Response(status=500, body=json.dumps(body).encode('utf8'),
headers=MultiDict({"Content-Type": "application/json"}))