Skip to content

Commit 6a89da3

Browse files
committed
Merge branch 'develop'
2 parents f6914c4 + ec2e9e3 commit 6a89da3

File tree

17 files changed

+196
-115
lines changed

17 files changed

+196
-115
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ ADD . $APP_HOME
1616
EXPOSE 5000
1717
USER python
1818

19-
CMD ["gunicorn", "--worker-class", "eventlet", "--workers", "8", "--log-level", "INFO", "--bind", "0.0.0.0:5000", "manage:app"]
19+
CMD ["gunicorn", "--worker-class", "gevent", "--workers", "8", "--log-level", "INFO", "--bind", "0.0.0.0:5000", "manage:app"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Test the image:
2020

2121
Push to Kubernetes:
2222

23-
kubectl create -f service.yaml
23+
kubectl apply -f service.yaml
2424

2525

2626
## How to contrib

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@
5757
# built documents.
5858
#
5959
# The short X.Y version.
60-
version = u'0.0.1'
60+
version = u'0.1'
6161
# The full version, including alpha/beta/rc tags.
62-
release = u'0.0.1'
62+
release = u'0.1'
6363

6464
# The language for content autogenerated by Sphinx. Refer to documentation
6565
# for a list of supported languages.

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Content
2020
:maxdepth: 4
2121

2222
installation
23+
runinkubernetes
2324
quickstart
2425
structure
2526
configuration

docs/installation.rst

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,4 @@ Clone the project
88
99
Configure your project and the path of your MS. See :doc:`configuration </configuration>` section.
1010

11-
Configure your setup.py with your project information
12-
13-
14-
15-
Use MS with Docker
16-
------------------
17-
`Install docker <https://docs.docker.com/install/>`_
18-
19-
20-
Use MS with Kubernetes localy
21-
-----------------------------
22-
Configure your service.yaml (TODO: create docs to configure kubernetes service.yaml)
23-
24-
* Installing Kubernetes...
25-
26-
.. code-block:: bash
27-
28-
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/
29-
30-
* Installing Minikube...
31-
32-
.. code-block:: bash
33-
34-
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
35-
36-
37-
Start minikube and set the environment of docker
38-
39-
.. code-block:: bash
40-
41-
minikube start
42-
eval $(minikube docker-env)
43-
kubectl config use-context minikube
44-
45-
If a shell isn't your friend. You could use kubernetes web panel to see your pods:
46-
47-
.. code-block:: bash
48-
49-
minikube dashboard
50-
51-
Create your image
52-
53-
.. code-block:: bash
54-
55-
docker build -t your-app:v1 .
56-
57-
Deploy your images localy:
58-
59-
.. code-block:: bash
60-
61-
kubectl create -f service.yaml
62-
minikube service your-app
63-
64-
65-
Clean your environment
66-
67-
.. code-block:: bash
68-
69-
kubectl delete service your-app
70-
kubectl delete deployment your-app
71-
docker rmi your-app:v1 -f
72-
minikube stop
73-
eval $(minikube docker-env -u)
74-
minikube delete
11+
Configure your setup.py with your project information

docs/runinkubernetes.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Run in kubernetes
2+
=================
3+
4+
Use MS with Docker
5+
------------------
6+
`Install docker <https://docs.docker.com/install/>`_
7+
8+
9+
Use MS with Kubernetes localy
10+
-----------------------------
11+
Configure your service.yaml (TODO: create docs to configure kubernetes service.yaml)
12+
13+
* Installing Kubernetes...
14+
15+
.. code-block:: bash
16+
17+
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/
18+
19+
* Installing Minikube...
20+
21+
.. code-block:: bash
22+
23+
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
24+
25+
26+
Start minikube and set the environment of docker
27+
28+
.. code-block:: bash
29+
30+
minikube start
31+
eval $(minikube docker-env)
32+
kubectl config use-context minikube
33+
34+
If a shell isn't your friend. You could use kubernetes web panel to see your pods:
35+
36+
.. code-block:: bash
37+
38+
minikube dashboard
39+
40+
Create your image
41+
42+
.. code-block:: bash
43+
44+
docker build -t template:v1 .
45+
46+
Deploy your images localy:
47+
48+
.. code-block:: bash
49+
50+
kubectl apply -f service.yaml
51+
minikube service template
52+
53+
54+
Clean your environment
55+
56+
.. code-block:: bash
57+
58+
kubectl delete template
59+
kubectl delete template
60+
docker rmi template:v1 -f
61+
minikube stop
62+
eval $(minikube docker-env -u)
63+
minikube delete

docs/structure.rst

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@ You have a project with this structure:
66
.. code-block:: bash
77
88
manager.py
9+
requirements.txt
10+
requirements-tests.txt
11+
requirements-docker.txt
12+
setup.py
13+
tox.ini
14+
myms
15+
├ healthcheck
16+
│ └ healthcheck.py
17+
├ logger
18+
│ └ logger.py
19+
├ models
20+
│ └ __init__.py
21+
└ tracer
22+
└ main.py
923
project
1024
├ __init__.py
1125
├ config.py
1226
├ views
1327
│ ├ __init__.py
14-
│ ├ views.py
15-
│ └ healthcheck.py
28+
│ └ views.py
1629
├ models
1730
│ ├ __init__.py
1831
│ └ models.py
@@ -25,26 +38,48 @@ manager.py
2538
A Django style command line. Use this to start the application like:
2639

2740
.. code-block:: bash
41+
2842
python manage.py runserver
2943
3044
You can set the host and the port with:
3145

3246
.. code-block:: bash
47+
3348
python manage.py runserver -h 0.0.0.0 -p 8080
3449
50+
Common Structure
51+
----------------
52+
53+
myms/healthcheck/healthcheck.py
54+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55+
This views is usually used by Kubernetes, Eureka and other systems to check if our application is up and running
56+
57+
myms/logger/logger.py
58+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59+
Print logger in JSON format to send to server like Elasticsearch. Inject span traces in logger
60+
61+
myms/models/__init__.py
62+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63+
Initizalize `flask_sqlalchemy.SQLAlchemy object`
64+
65+
myms/tracer/main.py
66+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67+
Create an injector `flask_opentracing.FlaskTracer` to use in our projects
68+
69+
Structure of a project
70+
----------------------
71+
3572
project/__init__.py
36-
-------------------
73+
~~~~~~~~~~~~~~~~~~~
3774
This file init the project with the funcion `create_app`. Initialize the Flask app, register `blueprints <http://flask.pocoo.org/docs/0.12/blueprints/>`_
3875
and intialize all libraries like Swagger, database, the trace system...
3976

4077
project/config.py
41-
-----------------
78+
~~~~~~~~~~~~~~~~~
4279
See :doc:`configuration </configuration>` section
4380

4481
project/views
45-
-------------
82+
~~~~~~~~~~~~~
4683
use views.py or create your file. You must add after register the view blueprint in `project/views/__init__.py`.
4784

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

project/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# encoding: utf-8
22

3+
import logging
34
import os
45

56
from flasgger import Swagger
@@ -9,12 +10,16 @@
910

1011
from project.config import CONFIG
1112
from pyms.healthcheck import healthcheck_blueprint
13+
from pyms.logger import CustomJsonFormatter
1214
from pyms.models import db
1315
from pyms.tracer.main import TracerModule
1416

1517
__author__ = "Alberto Vara"
1618
__email__ = "a.vara.1986@gmail.com"
17-
__version__ = "0.0.1"
19+
__version__ = "0.1"
20+
21+
logger = logging.getLogger('jaeger_tracing')
22+
logger.setLevel(logging.DEBUG)
1823

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

@@ -107,10 +112,19 @@ def create_app():
107112
app.register_blueprint(views_blueprint)
108113
app.register_blueprint(healthcheck_blueprint)
109114

115+
# Inject Modules
110116
# Inject Modules
111117
if not app.config["TESTING"] and not app.config["DEBUG"]:
112-
injector = Injector([TracerModule(app)])
118+
log_handler = logging.StreamHandler()
119+
formatter = CustomJsonFormatter('(timestamp) (level) (name) (module) (funcName) (lineno) (message)')
120+
formatter.add_service_name(app.config["APP_NAME"])
121+
tracer = TracerModule(app)
122+
injector = Injector([tracer])
113123
FlaskInjector(app=app, injector=injector)
124+
formatter.add_trace_span(tracer.tracer)
125+
log_handler.setFormatter(formatter)
126+
app.logger.addHandler(log_handler)
127+
app.logger.setLevel(logging.INFO)
114128

115129
with app.test_request_context():
116130
db.create_all()

project/tests/test_views.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def _format_response(response: Text = "") -> Union[List, Dict]:
1010
return json.loads(response)
1111

1212

13-
class FlaskrTestCase(unittest.TestCase):
13+
class ProjectTestCase(unittest.TestCase):
1414

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

28+
def test_healthcheck(self):
29+
response = self.client.get('/healthcheck')
30+
self.assertEqual(response.status_code, 200)
31+
2832
def test_list_view(self):
2933
response = self.client.get('{base_url}/'.format(base_url=self.base_url))
3034
self.assertEqual(response.status_code, 200)

project/views/views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# encoding: utf-8
22
from __future__ import absolute_import, print_function, unicode_literals
33

4-
from flask import request, jsonify
4+
from flask import request, jsonify, current_app
55

66
from pyms.models import db
77
from project.models.models import Colors
@@ -57,7 +57,7 @@ def list_view():
5757
]
5858
}]
5959
"""
60-
60+
current_app.logger.info("Return all color list")
6161
query = Colors.query.all()
6262

6363
return jsonify([i.serialize for i in query])
@@ -107,6 +107,7 @@ def create_view():
107107
]
108108
}
109109
"""
110+
current_app.logger.info("Create color")
110111
color = Colors(name=request.form["name"])
111112
db.session.add(color)
112113
db.session.commit()

0 commit comments

Comments
 (0)