Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
avara1986 committed May 5, 2018
2 parents f6914c4 + ec2e9e3 commit 6a89da3
Show file tree
Hide file tree
Showing 17 changed files with 196 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ ADD . $APP_HOME
EXPOSE 5000
USER python

CMD ["gunicorn", "--worker-class", "eventlet", "--workers", "8", "--log-level", "INFO", "--bind", "0.0.0.0:5000", "manage:app"]
CMD ["gunicorn", "--worker-class", "gevent", "--workers", "8", "--log-level", "INFO", "--bind", "0.0.0.0:5000", "manage:app"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Test the image:

Push to Kubernetes:

kubectl create -f service.yaml
kubectl apply -f service.yaml


## How to contrib
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
# built documents.
#
# The short X.Y version.
version = u'0.0.1'
version = u'0.1'
# The full version, including alpha/beta/rc tags.
release = u'0.0.1'
release = u'0.1'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Content
:maxdepth: 4

installation
runinkubernetes
quickstart
structure
configuration
Expand Down
65 changes: 1 addition & 64 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,4 @@ Clone the project
Configure your project and the path of your MS. See :doc:`configuration </configuration>` section.

Configure your setup.py with your project information



Use MS with Docker
------------------
`Install docker <https://docs.docker.com/install/>`_


Use MS with Kubernetes localy
-----------------------------
Configure your service.yaml (TODO: create docs to configure kubernetes service.yaml)

* Installing Kubernetes...

.. code-block:: bash
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.9.0/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
* Installing Minikube...

.. code-block:: bash
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
Start minikube and set the environment of docker

.. code-block:: bash
minikube start
eval $(minikube docker-env)
kubectl config use-context minikube
If a shell isn't your friend. You could use kubernetes web panel to see your pods:

.. code-block:: bash
minikube dashboard
Create your image

.. code-block:: bash
docker build -t your-app:v1 .
Deploy your images localy:

.. code-block:: bash
kubectl create -f service.yaml
minikube service your-app
Clean your environment

.. code-block:: bash
kubectl delete service your-app
kubectl delete deployment your-app
docker rmi your-app:v1 -f
minikube stop
eval $(minikube docker-env -u)
minikube delete
Configure your setup.py with your project information
63 changes: 63 additions & 0 deletions docs/runinkubernetes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Run in kubernetes
=================

Use MS with Docker
------------------
`Install docker <https://docs.docker.com/install/>`_


Use MS with Kubernetes localy
-----------------------------
Configure your service.yaml (TODO: create docs to configure kubernetes service.yaml)

* Installing Kubernetes...

.. code-block:: bash
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.9.0/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
* Installing Minikube...

.. code-block:: bash
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
Start minikube and set the environment of docker

.. code-block:: bash
minikube start
eval $(minikube docker-env)
kubectl config use-context minikube
If a shell isn't your friend. You could use kubernetes web panel to see your pods:

.. code-block:: bash
minikube dashboard
Create your image

.. code-block:: bash
docker build -t template:v1 .
Deploy your images localy:

.. code-block:: bash
kubectl apply -f service.yaml
minikube service template
Clean your environment

.. code-block:: bash
kubectl delete template
kubectl delete template
docker rmi template:v1 -f
minikube stop
eval $(minikube docker-env -u)
minikube delete
51 changes: 43 additions & 8 deletions docs/structure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@ You have a project with this structure:
.. code-block:: bash
manager.py
requirements.txt
requirements-tests.txt
requirements-docker.txt
setup.py
tox.ini
myms
├ healthcheck
│ └ healthcheck.py
├ logger
│ └ logger.py
├ models
│ └ __init__.py
└ tracer
└ main.py
project
├ __init__.py
├ config.py
├ views
│ ├ __init__.py
│ ├ views.py
│ └ healthcheck.py
│ └ views.py
├ models
│ ├ __init__.py
│ └ models.py
Expand All @@ -25,26 +38,48 @@ manager.py
A Django style command line. Use this to start the application like:

.. code-block:: bash
python manage.py runserver
You can set the host and the port with:

.. code-block:: bash
python manage.py runserver -h 0.0.0.0 -p 8080
Common Structure
----------------

myms/healthcheck/healthcheck.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This views is usually used by Kubernetes, Eureka and other systems to check if our application is up and running

myms/logger/logger.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Print logger in JSON format to send to server like Elasticsearch. Inject span traces in logger

myms/models/__init__.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Initizalize `flask_sqlalchemy.SQLAlchemy object`

myms/tracer/main.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Create an injector `flask_opentracing.FlaskTracer` to use in our projects

Structure of a project
----------------------

project/__init__.py
-------------------
~~~~~~~~~~~~~~~~~~~
This file init the project with the funcion `create_app`. Initialize the Flask app, register `blueprints <http://flask.pocoo.org/docs/0.12/blueprints/>`_
and intialize all libraries like Swagger, database, the trace system...

project/config.py
-----------------
~~~~~~~~~~~~~~~~~
See :doc:`configuration </configuration>` section

project/views
-------------
~~~~~~~~~~~~~
use views.py or create your file. You must add after register the view blueprint in `project/views/__init__.py`.

project/views/healthcheck.py
----------------------------
This views is usually used by Kubernetes, Eureka and other systems to check if our application is up and running

18 changes: 16 additions & 2 deletions project/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: utf-8

import logging
import os

from flasgger import Swagger
Expand All @@ -9,12 +10,16 @@

from project.config import CONFIG
from pyms.healthcheck import healthcheck_blueprint
from pyms.logger import CustomJsonFormatter
from pyms.models import db
from pyms.tracer.main import TracerModule

__author__ = "Alberto Vara"
__email__ = "a.vara.1986@gmail.com"
__version__ = "0.0.1"
__version__ = "0.1"

logger = logging.getLogger('jaeger_tracing')
logger.setLevel(logging.DEBUG)

ENVIRONMENT = os.environ.get("ENVIRONMENT", "default")

Expand Down Expand Up @@ -107,10 +112,19 @@ def create_app():
app.register_blueprint(views_blueprint)
app.register_blueprint(healthcheck_blueprint)

# Inject Modules
# Inject Modules
if not app.config["TESTING"] and not app.config["DEBUG"]:
injector = Injector([TracerModule(app)])
log_handler = logging.StreamHandler()
formatter = CustomJsonFormatter('(timestamp) (level) (name) (module) (funcName) (lineno) (message)')
formatter.add_service_name(app.config["APP_NAME"])
tracer = TracerModule(app)
injector = Injector([tracer])
FlaskInjector(app=app, injector=injector)
formatter.add_trace_span(tracer.tracer)
log_handler.setFormatter(formatter)
app.logger.addHandler(log_handler)
app.logger.setLevel(logging.INFO)

with app.test_request_context():
db.create_all()
Expand Down
6 changes: 5 additions & 1 deletion project/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def _format_response(response: Text = "") -> Union[List, Dict]:
return json.loads(response)


class FlaskrTestCase(unittest.TestCase):
class ProjectTestCase(unittest.TestCase):

def setUp(self):
os.environ["ENVIRONMENT"] = "test"
Expand All @@ -25,6 +25,10 @@ def test_home(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 404)

def test_healthcheck(self):
response = self.client.get('/healthcheck')
self.assertEqual(response.status_code, 200)

def test_list_view(self):
response = self.client.get('{base_url}/'.format(base_url=self.base_url))
self.assertEqual(response.status_code, 200)
Expand Down
5 changes: 3 additions & 2 deletions project/views/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: utf-8
from __future__ import absolute_import, print_function, unicode_literals

from flask import request, jsonify
from flask import request, jsonify, current_app

from pyms.models import db
from project.models.models import Colors
Expand Down Expand Up @@ -57,7 +57,7 @@ def list_view():
]
}]
"""

current_app.logger.info("Return all color list")
query = Colors.query.all()

return jsonify([i.serialize for i in query])
Expand Down Expand Up @@ -107,6 +107,7 @@ def create_view():
]
}
"""
current_app.logger.info("Create color")
color = Colors(name=request.form["name"])
db.session.add(color)
db.session.commit()
Expand Down
15 changes: 2 additions & 13 deletions pyms/healthcheck/healthcheck.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import opentracing
import requests
from flask_opentracing import FlaskTracer

from pyms.healthcheck import healthcheck_blueprint


@healthcheck_blueprint.route('/healthcheck', methods=['GET'])
def healthcheck(tracer: FlaskTracer):
span = tracer.get_span()
headers = {}
tracer._tracer.inject(span, opentracing.Format.HTTP_HEADERS, headers)
result = requests.post(url="http://localhost:8081/oauth/login", data={
"username": "test",
"password": "1234"
}, headers=headers)
return result.content
def healthcheck():
return "OK"
1 change: 1 addition & 0 deletions pyms/logger/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from pyms.logger.logger import CustomJsonFormatter

0 comments on commit 6a89da3

Please sign in to comment.