Beispiel #1
0
def is_port_in_use(host):
    _LOGGER.info("Checking if port used by Prestoserver is already in use..")
    try:
        portnum = lookup_port(host)
    except Exception:
        _LOGGER.info("Cannot find port from config.properties. " "Skipping check for port already being used")
        return 0
    with settings(hide("warnings", "stdout"), warn_only=True):
        output = run("netstat -an |grep %s |grep LISTEN" % str(portnum))
    if output:
        _LOGGER.info("Presto server port already in use. Skipping " "server start...")
        warn("Server failed to start on %s. Port %s already in use" % (env.host, str(portnum)))
    return output
Beispiel #2
0
def is_port_in_use(host):
    _LOGGER.info("Checking if port used by Prestoserver is already in use..")
    try:
        portnum = lookup_port(host)
    except Exception:
        _LOGGER.info("Cannot find port from config.properties. "
                     "Skipping check for port already being used")
        return 0
    with settings(hide('warnings', 'stdout'), warn_only=True):
        output = run('netstat -an |grep %s |grep LISTEN' % str(portnum))
    if output:
        _LOGGER.info("Presto server port already in use. Skipping "
                     "server start...")
        warn('Server failed to start on %s. Port %s already in use' %
             (env.host, str(portnum)))
    return output
 def test_lookup_port_not_in_file(self, run_mock):
     run_mock.return_value = _AttributeString('')
     run_mock.return_value.failed = False
     port = lookup_port('any_host')
     self.assertEqual(port, 8080)
    def execute_query(self, sql, schema="default", catalog="hive"):
        """
        Execute a query connecting to Presto server using passed parameters.

        Client sends http POST request to the Presto server, page:
        "/v1/statement". Header information should
        include: X-Presto-Catalog, X-Presto-Schema,  X-Presto-User

        Args:
            sql: SQL query to be executed
            schema: Presto schema to be used while executing query
                (default=default)
            catalog: Catalog to be used by the server

        Returns:
            True or False exit status
        """
        if not sql:
            raise InvalidArgumentError("SQL query missing")

        if not self.server:
            raise InvalidArgumentError("Server IP missing")

        if not self.user:
            raise InvalidArgumentError("Username missing")

        if not self.port:
            self.port = lookup_port(self.server)

        self.clear_old_results()

        headers = {"X-Presto-Catalog": catalog,
                   "X-Presto-Schema": schema,
                   "X-Presto-User": self.user}
        answer = ''
        try:
            _LOGGER.info("Connecting to server at: " + self.server +
                         ":" + str(self.port) + " as user " + self.user)
            conn = HTTPConnection(self.server, self.port, False,
                                  URL_TIMEOUT_MS)
            conn.request("POST", "/v1/statement", sql, headers)
            response = conn.getresponse()

            if response.status != 200:
                conn.close()
                _LOGGER.error("Connection error: "
                              + str(response.status) + " " + response.reason)
                return False

            answer = response.read()
            conn.close()

            self.response_from_server = json.loads(answer)
            _LOGGER.info("Query executed successfully")
            return True
        except (HTTPException, socket.error) as e:
            _LOGGER.error("Error connecting to presto server at: " +
                          self.server + ":" + str(self.port) + ' ' + e.message)
            return False
        except ValueError as e:
            _LOGGER.error('Error connecting to Presto server: ' + e.message +
                          ' error from server: ' + answer)
            raise e
Beispiel #5
0
 def test_lookup_port_not_in_file(self, run_mock):
     run_mock.return_value = _AttributeString('')
     run_mock.return_value.failed = False
     port = lookup_port('any_host')
     self.assertEqual(port, 8080)
Beispiel #6
0
    def execute_query(self, sql, schema="default", catalog="hive"):
        """
        Execute a query connecting to Presto server using passed parameters.

        Client sends http POST request to the Presto server, page:
        "/v1/statement". Header information should
        include: X-Presto-Catalog, X-Presto-Schema,  X-Presto-User

        Args:
            sql: SQL query to be executed
            schema: Presto schema to be used while executing query
                (default=default)
            catalog: Catalog to be used by the server

        Returns:
            True or False exit status
        """
        if not sql:
            raise InvalidArgumentError("SQL query missing")

        if not self.server:
            raise InvalidArgumentError("Server IP missing")

        if not self.user:
            raise InvalidArgumentError("Username missing")

        if not self.port:
            self.port = lookup_port(self.server)

        self.clear_old_results()

        headers = {
            "X-Presto-Catalog": catalog,
            "X-Presto-Schema": schema,
            "X-Presto-User": self.user
        }
        answer = ''
        try:
            _LOGGER.info("Connecting to server at: " + self.server + ":" +
                         str(self.port) + " as user " + self.user)
            conn = HTTPConnection(self.server, self.port, False,
                                  URL_TIMEOUT_MS)
            conn.request("POST", "/v1/statement", sql, headers)
            response = conn.getresponse()

            if response.status != 200:
                conn.close()
                _LOGGER.error("Connection error: " + str(response.status) +
                              " " + response.reason)
                return False

            answer = response.read()
            conn.close()

            self.response_from_server = json.loads(answer)
            _LOGGER.info("Query executed successfully")
            return True
        except (HTTPException, socket.error) as e:
            _LOGGER.error("Error connecting to presto server at: " +
                          self.server + ":" + str(self.port) + ' ' + e.message)
            return False
        except ValueError as e:
            _LOGGER.error('Error connecting to Presto server: ' + e.message +
                          ' error from server: ' + answer)
            raise e