Exemplo n.º 1
0
    def post(self, request, id, format=None):
        param = request.DATA

        # 判断 post 的参数是否有 'files' 'username'
        # 并且 value 不能为空
        if len(param) == 2 and 'files' in param.keys() and \
                               'username' in param.keys():
            if param['files'] and param['username']:
                msg = {
                    'id': id,
                    'files': param['files'],
                    'username': param['username'],
                    'message_type': 'files_read_container'
                }
                s, m, r = send_message(msg)
                if s == 0:
                    return Response(r, status=status.HTTP_200_OK)
                else:
                    detail = {'detail': m}
                    return Response(detail, status=status.HTTP_400_BAD_REQUEST)
            else:
                detail = {'detail': 'Error: The wrong parameter!'}
                return Response(detail, status=status.HTTP_400_BAD_REQUEST)
        else:
            detail = {'detail': 'Error: The wrong parameter!'}
            return Response(detail, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
    def post(self, request, id, format=None):
        param = request.DATA

        # 判断 post 的参数是否有 'command'
        # 并且 value 不能为空
        if 'command' in param.keys():
            if param['command']:
                msg = {
                    'id': id,
                    'command': param['command'],
                    'wait': param['wait'],
                    'message_type': 'exec_container'
                }

                s, m, r = send_message(msg)
                if s == 0:
                    if "django_project_info" in msg.keys() and \
                       msg["django_project_info"]:
                        detail = {'detail': r}
                    else:
                        detail = {
                            'detail':
                            'Container %s command \
executed successfully' % r['cid']
                        }
                    return Response(detail, status=status.HTTP_200_OK)
                else:
                    detail = {'detail': m}
                    return Response(detail, status=status.HTTP_400_BAD_REQUEST)
            else:
                detail = {'detail': 'Error: The wrong parameter!'}
                return Response(detail, status=status.HTTP_400_BAD_REQUEST)
        else:
            detail = {'detail': 'Error: The wrong parameter!'}
            return Response(detail, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
 def delete(self, request, id, format=None):
     param = request.DATA
     try:
         if len(param) == 1 and 'dirs' in param.keys():
             if type(param.get('dirs')) is not list:
                 detail = {'detail': 'Error: Value must be a list!'}
                 return Response(detail, status=status.HTTP_400_BAD_REQUEST)
             cid = Container.objects.get(id=id).cid
             msg = {
                 'id': id,
                 'cid': cid,
                 'dirs': param.get('dirs'),
                 'message_type': 'dirs_delete_container'
             }
             s, m, r = send_message(msg)
             if s == 0:
                 return Response(r['dirs'], status=status.HTTP_200_OK)
             else:
                 detail = {'detail': m}
                 return Response(detail, status=status.HTTP_400_BAD_REQUEST)
         else:
             detail = {'detail': 'Error: The wrong parameter!'}
             return Response(detail, status=status.HTTP_400_BAD_REQUEST)
     except:
         detail = {'detail': 'Error: The wrong parameter!'}
         return Response(detail, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 4
0
 def get(self, request, id, format=None):
     cid = self.get_object(id)
     msg = {'id': id, 'cid': cid.cid, 'message_type': 'inspect_container'}
     s, m, r = send_message(msg)
     if s == 0:
         return Response(r['container_info'], status=status.HTTP_200_OK)
     else:
         detail = {'detail': m}
         return Response(detail, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 5
0
 def delete(self, request, id, format=None):
     msg = {'id': id, 'message_type': 'delete_container'}
     s, m, r = send_message(msg)
     if s == 0:
         detail = {'detail': 'Container %s has been deleted.' % r['cid']}
         return Response(detail, status=status.HTTP_200_OK)
     else:
         detail = {'detail': m}
         return Response(detail, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 6
0
 def get(self, request, id, format=None):
     msg = {'id': id, 'message_type': 'top_container'}
     s, m, r = send_message(msg)
     if s == 0:
         r.pop('id')
         r.pop('host')
         r.pop('port')
         r.pop('message_type')
         return Response(r, status=status.HTTP_200_OK)
     else:
         detail = {'detail': m}
         return Response(detail, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 7
0
 def post(self, request, id, format=None):
     if request.POST:
         return Response('Error: Do not need any parameters!',
                         status=status.HTTP_400_BAD_REQUEST)
     msg = {'id': id, 'message_type': 'unpause_container'}
     s, m, r = send_message(msg)
     if s == 0:
         detail = {'detail': 'Container %s unpause success.' % r['cid']}
         return Response(detail, status=status.HTTP_200_OK)
     else:
         detail = {'detail': m}
         return Response(detail, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 8
0
 def post(self, request, format=None):
     serializer = ContainerSerializer(data=request.DATA)
     if serializer.is_valid():
         serializer.data['message_type'] = 'create_container'
         serializer.data['username'] = request.DATA['username']
         s, m, r = send_message(serializer.data)
         if s == 0:
             return Response(r, status=status.HTTP_201_CREATED)
         else:
             detail = {'detail': m}
             return Response(detail, status=status.HTTP_400_BAD_REQUEST)
     else:
         return Response({'detail': str(serializer.errors)},
                         status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 9
0
    def get(self, request, id, format=None):
        param = request.DATA

        # 判断 get 的参数是否有 'dirs', 并且 value 不能为空
        if len(param) == 1 and 'dirs' in param.keys() and param['dirs']:
            msg = {
                'id': id,
                'dirs': param['dirs'],
                'message_type': 'files_list_container'
            }
            s, m, r = send_message(msg)
            if s == 0:
                return Response(r['dirs'], status=status.HTTP_200_OK)
            else:
                detail = {'detail': m}
                return Response(detail, status=status.HTTP_400_BAD_REQUEST)
        else:
            detail = {'detail': 'Error: The wrong parameter!'}
            return Response(detail, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 10
0
    def post(self, request, id, format=None):
        if request.POST:
            return Response('Error: Do not need any parameters!',
                            status=status.HTTP_400_BAD_REQUEST)

        param = request.DATA
        self.get_object(id)
        msg = {
            'id': id,
            'username': param['username'],
            'message_type': 'start_container'
        }
        s, m, r = send_message(msg)
        if s == 0:
            detail = {'detail': 'Container %s startup success.' % r['cid']}
            return Response(detail, status=status.HTTP_200_OK)
        else:
            detail = {'detail': m}
            return Response(detail, status=status.HTTP_404_NOT_FOUND)