-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpods.py
More file actions
106 lines (90 loc) · 4.1 KB
/
Copy pathpods.py
File metadata and controls
106 lines (90 loc) · 4.1 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
"""
Pod and event views.
"""
from rest_framework import status
from rest_framework.response import Response
from api import models, serializers
from api.exceptions import DryccException
from api.views.app import AppFilterViewSet
from api.tasks import scale_app, restart_app, delete_pod
class PodViewSet(AppFilterViewSet):
model = models.app.App
serializer_class = serializers.PodSerializer
def list(self, *args, **kwargs):
pods = self.get_app().list_pods(*args, **kwargs)
data = self.get_serializer(pods, many=True).data
# fake out pagination for now
pagination = {'results': data, 'count': len(data)}
return Response(pagination, status=status.HTTP_200_OK)
def describe(self, *args, **kwargs):
pod_name = kwargs["name"]
data = self.get_app().describe_pod(pod_name)
if len(data) == 0:
raise DryccException("this process not found")
# fake out pagination for now
pagination = {'results': data, 'count': len(data)}
return Response(pagination, status=status.HTTP_200_OK)
def destroy(self, request, **kwargs):
pod_names = request.data.get("pod_ids")
pod_names = pod_names.split(",")
for pod_name in set(pod_names):
delete_pod.delay(self.get_app(), **{"pod_name": pod_name})
return Response(status=status.HTTP_200_OK)
class PtypeViewSet(AppFilterViewSet):
model = models.app.App
serializer_class = serializers.PtypeSerializer
def list(self, *args, **kwargs):
deploys = self.get_app().list_deployments(*args, **kwargs)
data = self.get_serializer(deploys, many=True).data
# fake out pagination for now
pagination = {'results': data, 'count': len(data)}
return Response(pagination, status=status.HTTP_200_OK)
def describe(self, *args, **kwargs):
deployment_name = kwargs["name"]
data = self.get_app().describe_deployment(deployment_name)
if len(data) == 0:
raise DryccException("this procfile type not found")
# fake out pagination for now
pagination = {'results': data, 'count': len(data)}
return Response(pagination, status=status.HTTP_200_OK)
def restart(self, request, *args, **kwargs):
app = self.get_app()
ptypes = set(
[ptype for ptype in request.data.get("ptypes", "").split(",") if ptype])
ptypes = app.check_ptypes(ptypes)
for ptype in set(ptypes):
restart_app.delay(app, **{"type": ptype})
return Response(status=status.HTTP_204_NO_CONTENT)
def clean(self, request, *args, **kwargs):
app = self.get_app()
ptypes = set(
[ptype for ptype in request.data.get("ptypes", "").split(",") if ptype])
if not ptypes:
raise DryccException("ptypes is a required field")
latest_ptypes = [k for k, v in app.structure.items() if v != 0]
not_allow = [ptype for ptype in ptypes if ptype in latest_ptypes]
if not_allow:
raise DryccException(f'ptype {",".join(not_allow)} should not garbage.')
app.clean(ptypes=ptypes)
return Response(status=status.HTTP_204_NO_CONTENT)
def scale(self, request, **kwargs):
app = self.get_app()
scale_app.delay(app, request.user, request.data)
return Response(status=status.HTTP_204_NO_CONTENT)
class EventViewSet(AppFilterViewSet):
model = models.app.App
serializer_class = serializers.EventSerializer
def list(self, request, **kwargs):
ptype = request.query_params.get("ptype", None)
pod_name = request.query_params.get("pod_name", None)
if not any([ptype, pod_name]):
data = []
else:
ref_kind, ref_name = "Deployment", f"{self.get_app().id}-{ptype}"
if pod_name:
ref_kind, ref_name = "Pod", pod_name
events = self.get_app().list_events(ref_kind, ref_name)
data = self.get_serializer(events, many=True).data
# fake out pagination for now
pagination = {'results': data, 'count': len(data)}
return Response(pagination, status=status.HTTP_200_OK)