Skip to content

Commit

Permalink
Merge pull request #112 from camptocamp/non_root
Browse files Browse the repository at this point in the history
Fix profiling to run as non-root
  • Loading branch information
Patrick Valsecchi authored Mar 20, 2018
2 parents 69dd8f4 + a2f74c6 commit 1a729b4
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 17 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ __pycache__
/c2cwsgiutils.egg-info/
/dist/
/reports/
/.idea/dbnavigator.xml
/.idea/inspectionProfiles
/.idea/checkstyle-idea.xml
/.idea/codeStyles/
/.idea/dbnavigator.xml
/.idea/inspectionProfiles
/.idea/misc.xml
/.idea/sonarlint
/docs/*/build/
/.mypy_cache
/.scannerwork
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def custom_check(request):

health_check = HealthCheck(config)
health_check.add_db_session_check(models.DBSession, at_least_one_model=models.Hello)
health_check.add_url_check('http://localhost/api/hello')
health_check.add_url_check('http://localhost:8080/api/hello')
health_check.add_custom_check('custom', custom_check, 2)
health_check.add_alembic_check(models.DBSession, '/app/alembic.ini', 3)
```
Expand Down
8 changes: 6 additions & 2 deletions acceptance_tests/app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ LABEL maintainer "[email protected]"
# Step #1 copy only the stuff needed to install the dependencies and run the script
WORKDIR /app

EXPOSE 80
EXPOSE 8080

ENV SQLALCHEMY_URL=postgresql://www-data:www-data@db:5432/test \
SQLALCHEMY_URL_SLAVE=postgresql://www-data:www-data@db_slave:5432/test
SQLALCHEMY_URL_SLAVE=postgresql://www-data:www-data@db_slave:5432/test \
GUNICORN_PARAMS="-b :8080 --worker-class gthread --threads 10 --workers 5"

# Step #2 copy the rest of the files (watch for the .dockerignore)
COPY . /app
Expand All @@ -23,3 +24,6 @@ RUN pip install --no-cache-dir -r requirements.txt -e . && \
c2cwsgiutils_genversion.py $GIT_HASH && \
flake8 c2cwsgiutils_app app_alembic && \
python -m compileall -q .

# www-data
USER 33
4 changes: 2 additions & 2 deletions acceptance_tests/app/c2cwsgiutils_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def main(_, **settings):
config.scan("c2cwsgiutils_app.services")
health_check = HealthCheck(config)
health_check.add_db_session_check(models.DBSession, at_least_one_model=models.Hello)
health_check.add_url_check('http://localhost/api/hello')
health_check.add_url_check(name="fun_url", url=lambda _request: 'http://localhost/api/hello')
health_check.add_url_check('http://localhost:8080/api/hello')
health_check.add_url_check(name="fun_url", url=lambda _request: 'http://localhost:8080/api/hello')
health_check.add_custom_check('fail', _failure, 2)
health_check.add_alembic_check(models.DBSession, '/app/alembic.ini', 1)

Expand Down
2 changes: 1 addition & 1 deletion acceptance_tests/app/c2cwsgiutils_app/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def tracking(request):
depth = int(request.matchdict.get('depth'))
result = {'request_id': request.c2c_request_id}
if depth > 0:
result['sub'] = requests.get('http://localhost/api/tracking/%d' % (depth - 1)).json()
result['sub'] = requests.get('http://localhost:8080/api/tracking/%d' % (depth - 1)).json()
return result


Expand Down
4 changes: 2 additions & 2 deletions acceptance_tests/tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ app:
SENTRY_URL: https://14bdb65de3f247c4a89cc7ed53ddec72:[email protected]/5
C2C_REDIS_URL: redis://redis:6379
PYTHONMALLOC: debug
GUNICORN_PARAMS: -b :80 --worker-class gthread --threads 10 --workers 1
GUNICORN_PARAMS: -b :8080 --worker-class gthread --threads 10 --workers 1
C2C_PROFILER_PATH: /api_profiler
C2C_PROFILER_MODULES: "c2cwsgiutils c2cwsgiutils_app sqlalchemy request"
links:
- db
- db_slave
- redis
ports:
- 8480:80
- 8480:8080

alembic_master:
image: camptocamp/c2cwsgiutils_test_app:latest
Expand Down
8 changes: 4 additions & 4 deletions acceptance_tests/tests/tests/test_health_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def test_ok(app_connection):
print('response=' + json.dumps(response))
assert response == {
'status': 200,
'successes': ['db_engine_sqlalchemy', 'db_engine_sqlalchemy_slave', 'http://localhost/api/hello',
'successes': ['db_engine_sqlalchemy', 'db_engine_sqlalchemy_slave', 'http://localhost:8080/api/hello',
'fun_url', 'alembic_app_alembic.ini'],
'failures': {},
'timings': response['timings'],
Expand All @@ -15,7 +15,7 @@ def test_ok(app_connection):
}
}
assert response['timings'].keys() == {
'db_engine_sqlalchemy', 'db_engine_sqlalchemy_slave', 'http://localhost/api/hello',
'db_engine_sqlalchemy', 'db_engine_sqlalchemy_slave', 'http://localhost:8080/api/hello',
'fun_url', 'alembic_app_alembic.ini'}


Expand All @@ -37,7 +37,7 @@ def test_failure(app_connection):
print('response=' + json.dumps(response))
assert response == {
'status': 500,
'successes': ['db_engine_sqlalchemy', 'db_engine_sqlalchemy_slave', 'http://localhost/api/hello',
'successes': ['db_engine_sqlalchemy', 'db_engine_sqlalchemy_slave', 'http://localhost:8080/api/hello',
'fun_url', 'alembic_app_alembic.ini'],
'failures': {
'fail': {
Expand All @@ -51,7 +51,7 @@ def test_failure(app_connection):
}
}
assert response['timings'].keys() == {
'db_engine_sqlalchemy', 'db_engine_sqlalchemy_slave', 'http://localhost/api/hello',
'db_engine_sqlalchemy', 'db_engine_sqlalchemy_slave', 'http://localhost:8080/api/hello',
'fun_url', 'alembic_app_alembic.ini', 'fail'}


Expand Down
7 changes: 5 additions & 2 deletions c2cwsgiutils/profiler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
from tempfile import gettempdir
from typing import Callable

LOG = logging.getLogger(__name__)
Expand All @@ -11,7 +12,9 @@ def filter_wsgi_app(application: Callable) -> Callable:
if _PATH != "":
import linesman.middleware
LOG.info("Enable WSGI filter for the profiler on %s", _PATH)
return linesman.middleware.ProfilingMiddleware(app=application, profiler_path=_PATH,
chart_packages=_MODULES)
linesman.middleware.ENABLED_FLAG_FILE = os.path.join(gettempdir(), 'linesman-enabled')
return linesman.middleware.ProfilingMiddleware(
app=application, profiler_path=_PATH, chart_packages=_MODULES,
filename=os.path.join(gettempdir(), 'linesman-graph-sessions.db'))
else:
return application
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup, find_packages


VERSION = '1.8.0'
VERSION = '1.8.1'
HERE = os.path.abspath(os.path.dirname(__file__))
INSTALL_REQUIRES = [
pkg.split('==')[0]
Expand Down

0 comments on commit 1a729b4

Please sign in to comment.