Skip to content

Commit fdf8729

Browse files
committed
initial commit
1 parent b312010 commit fdf8729

File tree

6 files changed

+111
-2
lines changed

6 files changed

+111
-2
lines changed

Diff for: Dockerfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM python:2.7.8
2+
3+
RUN mkdir -p /usr/src/app
4+
WORKDIR /usr/src/app
5+
COPY . /usr/src/app
6+
7+
RUN pip install -r requirements.txt
8+
9+
EXPOSE 3000
10+
11+
CMD [ "python","application.py"]

Diff for: README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
# python-mysql-sample
2-
Sample application in python and mysql
1+
This sample demonstrates how to setup continuous integration for a Python+MySQL project.
2+
3+
This sample is built for DaoCloud, a docker based continuous integration and deployment platform.
4+
5+
## Default DB Connection
6+
# MYSQL_USERNAME=root
7+
# MYSQL_PASSWORD # empty string
8+
# MYSQL_PORT_3306_TCP_ADDR=localhost
9+
# MYSQL_PORT_3306_TCP_PORT=3306
10+
# MYSQL_INSTANCE_NAME=daocloud
11+
12+
## Run Container
13+
docker run --link your_mysql:mysql -p 3000:3000 python-mysql-sample

Diff for: application.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import os
2+
import flask
3+
import MySQLdb
4+
5+
application = flask.Flask(__name__)
6+
application.debug = True
7+
8+
@application.route('/')
9+
def hello_world():
10+
storage = Storage()
11+
storage.populate()
12+
score = storage.score()
13+
return "Hello world, %d!" % score
14+
15+
class Storage():
16+
def __init__(self):
17+
self.db = MySQLdb.connect(
18+
user = os.getenv('MYSQL_USERNAME', 'root'),
19+
passwd = os.getenv('MYSQL_PASSWORD', ''),
20+
db = os.getenv('MYSQL_INSTANCE_NAME', 'daocloud'),
21+
host = os.getenv('MYSQL_PORT_3306_TCP_ADDR', 'localhost'),
22+
port = int(os.getenv('MYSQL_PORT_3306_TCP_PORT', '3306'))
23+
)
24+
25+
cur = self.db.cursor()
26+
cur.execute("DROP TABLE IF EXISTS scores")
27+
cur.execute("CREATE TABLE scores(score INT)")
28+
29+
def populate(self):
30+
cur = self.db.cursor()
31+
cur.execute("INSERT INTO scores(score) VALUES(1234)")
32+
33+
def score(self):
34+
cur = self.db.cursor()
35+
cur.execute("SELECT * FROM scores")
36+
row = cur.fetchone()
37+
return row[0]
38+
39+
if __name__ == "__main__":
40+
application.run(host='0.0.0.0', port=3000)

Diff for: daocloud.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
image: python2.7
2+
3+
services:
4+
- mysql
5+
6+
env:
7+
- MYSQL_INSTANCE_NAME=test
8+
# leave below env as default
9+
# - MYSQL_USERNAME=root
10+
# - MYSQL_PASSWORD #leave empty
11+
# - MYSQL_PORT_3306_TCP_ADDR=mysql
12+
# - MYSQL_PORT_3306_TCP_PORT=3306
13+
14+
script:
15+
- pip install -r requirements.txt
16+
- mkdir -p daocloud/testresults
17+
- mkdir -p daocloud/codecoverage
18+
- mysql -u root -h mysql -e 'create database if not exists test;'
19+
- nosetests test.py --with-xunit --xunit-file=daocloud/testresults/nosetests.xml
20+
- which python && coverage run --branch test.py
21+
- which python && coverage xml -o daocloud/codecoverage/coverage.xml test.py
22+
23+
notify:
24+
email:
25+
recipients:
26+
27+
on_success: on
28+
on_failure: off

Diff for: requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
nose
2+
coverage
3+
Flask
4+
MySQL-python

Diff for: test.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from application import Storage
3+
4+
class TestSuite(unittest.TestCase):
5+
def test(self):
6+
storage = Storage()
7+
storage.populate()
8+
score = storage.score()
9+
self.failIf(score != 1234)
10+
11+
def main():
12+
unittest.main()
13+
14+
if __name__ == "__main__":
15+
main()

0 commit comments

Comments
 (0)