Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2
version: 1
jobs:
one:
docker:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
venv/
*.swp
app/__py*
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM smarman/alp_py37

ADD reqs.txt uwsgi.ini /etc/
ADD app /srv

WORKDIR /srv

RUN apk add --no-cache --virtual \
.build-deps \
gcc \
python3-dev \
build-base \
linux-headers && \
addgroup -S apiUser && adduser -S apiUser -G apiUser && \
python3.7 -m venv /etc/venv && source /etc/venv/bin/activate && \
pip install --upgrade pip && \
pip install -r /etc/reqs.txt && \
apk del .build-deps

USER apiUser

CMD ["/etc/venv/bin/uwsgi", "--ini", "/etc/uwsgi.ini"]
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# variables
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
mkfile_dir := $(dir $(mkfile_path))

restart:
docker restart pythonapi

dev-env: build run

build:
docker build -t webbase .

run:
docker run --name=pythonapi -p 5000:5000 -d --mount type=bind,source=${mkfile_dir}app/,target=/srv webbase
#docker run --name=pythonapi -p 5000:5000 -d webbase
sleep 5
open http://0.0.0.0:5000

rebuild:
docker rm -f pythonapi
docker run --name=pythonapi -p 5000:5000 -d --mount type=bind,source=${mkfile_dir}app/,target=/srv webbase

clean:
docker rm -f pythonapi
docker rmi webbase

attach:
docker exec -it pythonapi ash

logs:
docker logs -f pythonapi
4 changes: 2 additions & 2 deletions api.py → app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ def api_filter():

return jsonify(results)


app.run()
if __name__ == 'main':
app.run(host='0.0.0.0')
File renamed without changes.
5 changes: 5 additions & 0 deletions k8s/00-init.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: pythonapi
25 changes: 25 additions & 0 deletions k8s/01-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
namespace: pythonapi
name: pythonapi
spec:
replicas: 1
template:
metadata:
labels:
app: pythonapi
spec:
containers:
- name: pythonapi
image: smarman/pythonapi:latest
imagePullPolicy: Never
ports:
- containerPort: 5000
livenessProbe:
httpGet:
path: /health
port: 5000
initialDelaySeconds: 5
periodSeconds: 60

16 changes: 16 additions & 0 deletions k8s/02-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: Service
metadata:
name: pythonapi-svc
namespace: pythonapi
labels:
app: pythonapi
spec:
type: NodePort
ports:
- name: http
port: 5000
#targetPort: 5000
nodePort: 30087
selector:
app: pythonapi
102 changes: 102 additions & 0 deletions notes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
circleci:
- config:
- in .circleci dir
- build docker images, tests
- sample
version: 2
jobs:
build:
docker:
- image: python:3.5
working_directory: /code
steps:
- checkout
- run:
name: Hello World
command: echo "Hello World"

explained:
- first job will run inside docker container
in the "docker" section we specify the image to use
- 'working_directory' arbitrary dir where the code is checked out and tests are run
from
- 'steps' all the important work like building, testing, shipping etc
in this ex we simply echo "Hello World"
- git add .
- git commit -m "added circle conf"
git push orign <branch-name>

walkthrough:
- every config starts with a version key. Used to issue warnings ab9out breaking changes
- creting a build job
- a run is comprised of one or more jobs. because this job doesn't use workflows it must have a
'build' job
- 'working_directory' key to specify where a jobs 'steps' run. Default is ~/project, where project is a literal string
- Choose an Executor Type:
- steps run in a virtual env called an executor
- above 'docker' executor is used to specify a custom Docker image
- the first image listed becomes the 'primary container' all commands for a job execute in this contaner
!NOTE circleci/python:3.5 is a convenience image provided by CircleCi. Images are exenstions for official images
- Other services and set env vars
- use the 'environment' key to set environment variables for all commands in a container
- ex)
version: 2
jobs:
build:
working_directory: ~/circleci-demo-python-django
docker:
- image: circleci/python:3.6.4 ### every job must define an image for the docker executeor and subsequent jobs may define a diff image
environment:
PIPENV_VENV_IN_PROJECT: true
DATABASE_URL: postgresql://root@localhost/circle_test?sslmode=disable
- image: circleci/postgres:9.6.2 # ex of how to specify a service container
environment:
POSTGRES_USER: root
POSTGRES_DB: circle_test
- installing dependencies:
- after choosing containers for the job create 'steps' to run specific commands
- use 'checkout' step to checkout source code !DEFAULT checkout to dir listed as working_directory
- use 'run' to execute bash commands. Ex pipenv is used to creat a virtual env and install python
version: 2
jobs:
build:
# ...
steps:
- checkout
- run:
command: | # use pipenv to install dependencies
sudo pip install pipenv
pipenv install
- Cache Dependencies:
- save time between builds
- 'save_cache' step to cache certian files or dirs
- 'restore_cache' to restor cached files or directories
ex)
version: 2
jobs:
build:
# ...
steps:
- checkout
- run: sudo chown -R circleci:circleci /usr/local/bin
- run: sudo chown -R circleci:circleci /usr/local/lib/python3.6/site-packages
- restore_cache: # ensure this step occures before installing dependencies
key: deps9-{{ .Branch }}-{{ checksum "Pipfile.lock" }}
- run:
command: |
sudo pip install pipenv
pipenv install
- save_cache:
key: deps9-{{ .Branch }}-{{ checksum "Pipfile.lock" }}
paths:
- ".venv"
- "/usr/local/bin"
- "/usr/local/lib/python3.6/site-packages"
- run:
command: |
pipenv run python manage.py test




- docs: https://circleci.com/docs/2.0/
2 changes: 2 additions & 0 deletions reqs.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Click==7.0
Flask==1.0.2
itsdangerous==1.1.0
requests
Jinja2==2.10.1
MarkupSafe==1.1.1
Werkzeug==0.15.2
uwsgi==2.0.17
13 changes: 13 additions & 0 deletions uwsgi.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[uwsgi]
chdir = /srv
venv = /etc/venv
module = app:app
callable = app

master = true
processes = 4;
threads = 2;
vacuum = true;
enabled-threads = true

http-socket = 0.0.0.0:5000