示例#1
0
文件: rest.py 项目: noQ/PyRTS
    def unregisterapp(self, nodeid, name, **kwargs):
        """
            Unregister application   
            
                method call: unregisterapp(self,  nodeid, name, **kwargs)
                    -> where **kwargs : <optional parameters>
                
                mandatory parameters:
                    token  - auth token
                    nodeid - node ID
                    name   - aplication name. Example: XandZero
                
                Returns:
                    SUCCESS: returns JSON object
                    ERROR:
                            REST_EXCEPTION = -1
                            DATA_NOT_FOUND = -5
                
                Call example:
                      - Request:
                             {
                                "c": "6523",
                                "m": "unregisterapp",
                                "p": {
                                    "token": "603fccbc9a9ec13532d7f3259d184f5de35360da",
                                    "nodeid":"4fe1d3e16ee7225440000000",
                                    "name" : "XandZero"
                                } 
                            }                                  
                
                      - Response:
                        - SUCCESS:
                               {"c": "4534564", "r": "OK", "e": 0}

                        - ERROR:
                               {"m": "REST method exception: 'Unable to delete node'", "c": "4534564", "e": -7}
        """
        if name is None:
            raise ServerParametersNotFound("Application name is invalid")
        if nodeid is None:
            raise ServerParametersNotFound("Server Node ID is invalid")

        srv_node = check_node(nodeid)
        try:
            applications = srv_node.unregister_application(name)
            _apps = []
            for app in applications:
                _apps.append(MongoObject.to_json(app))
            return _apps
        except Exception as exc:
            traceback.print_exc()
            raise ServerException("Unable to add application!")
示例#2
0
文件: token.py 项目: noQ/PyRTS
    def GET(self):
        web.header("Content-Type", "application/json")
        """ read data from web and return web code """
        response_code, remote_addr, response_data = read_web_data(web)

        """ raise exception if authorization fail """
        if response_code == NOT_AUTHORIZED:
            return dict(error=get_error(NOT_AUTHORIZED))
        if remote_addr is None or len(remote_addr) == 0:
            return dict(error=get_error(NOT_AUTHORIZED))

        """ generate and save access token """
        access_token = generate_access_token()
        try:
            token_obj = AccessToken(
                address=remote_addr, token=access_token, expire_at=int(time.time() + one_year_in_seconds), is_valid=True
            )
            token_obj.save()
            """ convert mongo object in json """
            json_obj = MongoObject.to_json(token_obj)
            return json_obj
        except Exception as error:
            return {"error": str(error)}
示例#3
0
文件: rest.py 项目: noQ/PyRTS
    def registernode(self, controllerid, address, name, verify_node=False, serialize=False, **kwargs):
        """
            Register new node

                method call: registernode(self,  controllerid, address, name, verify_node=False, serialize=False, **kwargs)
                    -> where **kwargs : <optional parameters>
                
                mandatory parameters:
                    token        - auth token
                    controllerid - controller ID
                    address      - node IP address
                    name         - node name
                
                optional parameters:
                    verify_node  - Check node
                    serialize    - serialize object
                
                Returns:
                    SUCCESS: returns JSON object
                    ERROR:
                            REST_EXCEPTION = -1
                
                Call example:
                    - Request:
                        {
                            "c":"4534564",
                            "m":"registernode",
                            "p" :{
                                "token":"c01066836281a24534e335fe22052378f7f2f5300d",
                                "controllerid":"4fe17ad76ee72244f0000001",
                                "address":"192.168.101.13",
                                "name":"COSTIA-NODE1"
                            }
                       }           

                    - Response:
                        - SUCCESS:
                            {"c": "4534564",
                             "r": {
                                    "address": "192.168.101.13",
                                    "id": "4fe0a8a76ee7224068000000",
                                    "name": "ROUTER-SERVER-COSTIA"
                                    },
                             "e": 0
                            }        
                                            
                        - ERROR:
                               {"m": "REST method exception: 'Host 192.168.190.29:8008 is down!'", "c": "4534564", "e": -9}

        """
        port = None
        if address is None or name is None:
            raise ServerParametersNotFound("Node name or address not specified!")
        ''' check host '''
        if DOUBLE_DOT in address:
            (host, port) = address.split(":")
        else:
            host = address
        if port is None:
            port = DEFAULT_WEB_PORT

        if verify_node:
            if not checkHost(host, int(port)):
                raise HostIsDown("Host " + str(host) + ":" + str(port) + " is down!")
        
        ''' get controller based on id '''
        srv_controller = check_controller(controllerid)
        ports = kwargs[PARAM_PORTS]
        ''' create new node '''
        srv_node = Node(
                        address=address,
                        machine_name=name,
                        ports=ports,
                        last_ping=datetime.datetime.now(), 
                        is_active=True
                        )
        if kwargs.has_key(PARAM_SYSTEM):
            try:
                srv_properties = json.loads(kwargs[PARAM_SYSTEM])
                srv_node.info = srv_properties
            except:
                traceback.print_exc()
                raise ServerException("Server properties are invalid!")

        ''' register server node '''
        try:
            ''' save node '''
            srv_node.save()
            ''' update controller node. save only the reference '''
            node = srv_controller.add_node(srv_node)
            if serialize:
                return MongoObject.to_json(node)
            return node
        except Exception as exc:
            traceback.print_exc()
            raise ServerException("Unable to register node - error: " + str(exc))
示例#4
0
文件: rest.py 项目: noQ/PyRTS
    def serverlist(self, **kwargs):
        """
            Get servers (controllers) list
           
                method call: serverlist(self, ip_address, controller, **kwargs)
                    -> where **kwargs : <optional parameters>
                
                mandatory parameters:
                    token    - auth token
                
                Returns:
                    SUCCESS: returns JSON object
                    ERROR:
                            REST_EXCEPTION = -1
                            DATA_NOT_FOUND = -5
                
                Call example:
                    - Request:
                         {
                            "c":"4534564",
                            "m":"serverlist",
                            "p" :{
                                "token":"c010836281a24534e335fe22052378f7f2f5300d"
                            }
                        }                    

                    - Response:
                        - SUCCESS:
                            {
                                "c": "4534564",
                                "r": [{
                                    "name": "CONTROLLER-SERVER-COSTIA",
                                    "server_type": "controller",
                                    "created_at": 1340193417021,
                                    "is_controller": true,
                                    "address": "192.168.101.12", 
                                    "nodes": [{
                                        "last_ping": 1340194525548,
                                        "machine_name": "COSTIA-NODE1",
                                        "access_token": "92b422bddb295f0d4424a3703d030a2d23e2b001",
                                        "is_active": true,
                                        "register_at": 1340194525950,
                                        "address": "192.168.101.29"
                                    }],
                                    "id": "4fe190596ee7224b28000000"
                                }],
                                "e": 0
                            }
                                            
                        - ERROR:
                            {"r": "Unable to add controller", "e": -1}
        """
        try:
            ''' get all controllers and returns controller and nodes.
                raise exception DATA_NOT_FOUND if no server found 
            '''
            servers = controllers_list()
            if len(servers) > 0:
                srv_list = []
                for srv in servers:
                    srv_obj = dict(id=str(srv.id),
                                   name=srv.name,
                                   address=srv.address,
                                   is_controller=srv.is_controller,
                                   server_type=srv.server_type,
                                   created_at=date_to_int(srv.created_at)
                                   )
                    if len(srv.nodes) > 0:
                        srv_obj['nodes'] = MongoObject.to_json(srv.nodes)
                    srv_list.append(srv_obj)
                return srv_list
            raise DataNotFound("Server list empty")
        except Exception as exc:
            traceback.print_exc()
            raise ServerException("Unable to return server list - error: " + str(exc))