Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use uwsgi instead of gunicorn #246

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
FROM python:2.7-alpine

RUN apk update && apk add --no-cache python-dev gcc git g++ make libffi-dev openssl-dev libxml2 libxml2-dev libxslt libxslt-dev
ENV APP_EXPOSED_PORT 5000

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
RUN apk update && apk add --no-cache python-dev gcc git g++ make libffi-dev openssl-dev pcre-dev libxml2 libxml2-dev libxslt libxslt-dev curl

COPY requirements.txt /usr/src/app/
ENV APP_ROOT /usr/src/app

RUN mkdir -p $APP_ROOT
WORKDIR $APP_ROOT

COPY requirements.txt $APP_ROOT
RUN pip install --no-cache-dir -r requirements.txt

COPY . /usr/src/app
COPY . $APP_ROOT

RUN PBR_VERSION=0.0.0 pip install .

EXPOSE 5000
ENTRYPOINT [ "python" ]
EXPOSE $APP_EXPOSED_PORT

CMD [ "netman/main.py", "--host=0.0.0.0"]
HEALTHCHECK --interval=5s --timeout=3s CMD curl --fail http://localhost:$APP_EXPOSED_PORT/healthcheck
ENTRYPOINT uwsgi --ini uwsgi.ini
1 change: 1 addition & 0 deletions constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ requests==2.21.0
selectors2==2.0.1 # via ncclient
six==1.12.0 # via bcrypt, cryptography, ncclient, pynacl
urllib3==1.24.1 # via requests
uwsgi==2.0.18
werkzeug==0.14.1 # via flask
12 changes: 11 additions & 1 deletion netman/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# limitations under the License.

import argparse
import json
from logging import DEBUG, getLogger

from flask import request
from flask import make_response, request
from flask.app import Flask

from adapters.threading_lock_factory import ThreadingLockFactory
Expand All @@ -40,6 +41,15 @@ def log_request():
logger.debug("Headers : " + ", ".join(["{0}={1}".format(h[0], h[1]) for h in request.headers]))


@app.route('/healthcheck')
def healthcheck():
return make_response((
json.dumps(dict(running=True)),
200,
{'Content-Type': 'application/json'}
))


lock_factory = ThreadingLockFactory()
switch_factory = FlowControlSwitchFactory(MemoryStorage(), lock_factory)
real_switch_factory = RealSwitchFactory()
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ netaddr>=0.7.13
ncclient>=0.5.0
requests>=2.6.2
pyeapi
uwsgi
1 change: 1 addition & 0 deletions test-constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ sphinxcontrib-httpdomain==1.7.0
tftpy==0.8.0 # via fake-switches
twisted[conch]==16.6.0 # via fake-switches, mockssh
urllib3==1.24.1
uwsgi==2.0.18
werkzeug==0.14.1
zope.interface==4.6.0 # via twisted
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ gunicorn>=19.4.5
flake8==3.4.1
futures; # Runtime dependency of gunicorn
jabstract
uwsgi
2 changes: 1 addition & 1 deletion tests/system_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _start(self, path):
params = self._popen_params(path)

logging.info("starting netman : \"{}\"".format('" "'.join(params)))
self.proc = subprocess.Popen(params, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
self.proc = subprocess.Popen(params)
self._wait_until_port_is_opened(self.port)

def _stop(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import sys
from hamcrest import assert_that, less_than
from hamcrest import is_
import os
from os.path import dirname, join

from tests.system_tests import NetmanTestApp, create_session, get_available_switch


class GunicornCompatibilityTest(unittest.TestCase):
def test_run_netman_with_a_gunicorn_wrapper(self):
with GunicornNetmanTestApp() as partial_client:
class UwsgiCompatibilityTest(unittest.TestCase):
def test_run_netman_with_a_uwsgi_wrapper(self):
with UwsgiNetmanTestApp() as partial_client:
client = partial_client(get_available_switch("cisco"))

create_session(client, "my_session")
Expand All @@ -20,7 +20,7 @@ def test_run_netman_with_a_gunicorn_wrapper(self):
assert_that(result.status_code, is_(204), result.text)

def test_parameters_can_be_passed_through_the_command_line(self):
with GunicornNetmanTestApp() as partial_client:
with UwsgiNetmanTestApp() as partial_client:
client = partial_client(get_available_switch("brocade"))
start_time = time.time()

Expand All @@ -34,11 +34,10 @@ def test_parameters_can_be_passed_through_the_command_line(self):
assert_that(time.time() - start_time, is_(less_than(3)))


class GunicornNetmanTestApp(NetmanTestApp):
class UwsgiNetmanTestApp(NetmanTestApp):
def _popen_params(self, path):
gunicorn_executable = os.path.join(os.path.dirname(sys.executable), 'gunicorn')
params = [gunicorn_executable,
"netman.main:load_app(session_inactivity_timeout={})".format(self.session_inactivity_timeout),
"--bind", "{}:{}".format(self.ip, self.port),
uwsgi_executable = join(dirname(sys.executable), 'uwsgi')
params = [uwsgi_executable, "--module", "netman.main:app",
"--http", "{}:{}".format(self.ip, self.port),
"--threads", "2"]
return params
19 changes: 19 additions & 0 deletions uwsgi.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[uwsgi]

# Flask wsgi file
module = netman.main:app

# process-related settings
master = true
processes = 1
threads = 2
lazy-apps = true
# clear environment on exit
vacuum = true

http = 0.0.0.0:$(APP_EXPOSED_PORT)
http-timeout = 60
no-orphans = true
die-on-term = true

log-drain = GET /healthcheck