Skip to content

Commit 1d5ee59

Browse files
authored
Merge pull request #7 from mana-ysh/feat/iac
Feat/iac
2 parents 746a4fd + 7ddfc4f commit 1d5ee59

File tree

11 files changed

+256
-5
lines changed

11 files changed

+256
-5
lines changed

.github/workflows/pythonpackage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Install dependencies
2727
run: |
2828
python -m pip install --upgrade pip
29-
pip install cookiecutter flake8 pytest mypy pytest-cov
29+
pip install cookiecutter flake8 pytest mypy pytest-cov kamidana
3030
make create-project-no-input PROJECT_NAME=dummy
3131
cd dummy && pip install .
3232
- name: CI

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Python project template for Web API with cookiecutter
2+
3+
## Requirements
4+
5+
- cookiecutter
6+
- kamidana

cookiecutter.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"project_slug": "python_api_template",
3-
"description": "hoge"
3+
"description": "hoge",
4+
"_extensions": ["kamidana.extensions.NamingModuleExtension"]
45
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
.github/
2+
k8s/
3+
4+
# Byte-compiled / optimized / DLL files
5+
__pycache__/
6+
*.py[cod]
7+
*$py.class
8+
9+
# C extensions
10+
*.so
11+
12+
# Distribution / packaging
13+
.Python
14+
build/
15+
develop-eggs/
16+
dist/
17+
downloads/
18+
eggs/
19+
.eggs/
20+
lib/
21+
lib64/
22+
parts/
23+
sdist/
24+
var/
25+
wheels/
26+
pip-wheel-metadata/
27+
share/python-wheels/
28+
*.egg-info/
29+
.installed.cfg
30+
*.egg
31+
MANIFEST
32+
33+
# PyInstaller
34+
# Usually these files are written by a python script from a template
35+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
36+
*.manifest
37+
*.spec
38+
39+
# Installer logs
40+
pip-log.txt
41+
pip-delete-this-directory.txt
42+
43+
# Unit test / coverage reports
44+
htmlcov/
45+
.tox/
46+
.nox/
47+
.coverage
48+
.coverage.*
49+
.cache
50+
nosetests.xml
51+
coverage.xml
52+
*.cover
53+
*.py,cover
54+
.hypothesis/
55+
.pytest_cache/
56+
57+
# Translations
58+
*.mo
59+
*.pot
60+
61+
# Django stuff:
62+
*.log
63+
local_settings.py
64+
db.sqlite3
65+
db.sqlite3-journal
66+
67+
# Flask stuff:
68+
instance/
69+
.webassets-cache
70+
71+
# Scrapy stuff:
72+
.scrapy
73+
74+
# Sphinx documentation
75+
docs/_build/
76+
77+
# PyBuilder
78+
target/
79+
80+
# Jupyter Notebook
81+
.ipynb_checkpoints
82+
83+
# IPython
84+
profile_default/
85+
ipython_config.py
86+
87+
# pyenv
88+
.python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
98+
__pypackages__/
99+
100+
# Celery stuff
101+
celerybeat-schedule
102+
celerybeat.pid
103+
104+
# SageMath parsed files
105+
*.sage.py
106+
107+
# Environments
108+
.env
109+
.venv
110+
env/
111+
venv/
112+
ENV/
113+
env.bak/
114+
venv.bak/
115+
116+
# Spyder project settings
117+
.spyderproject
118+
.spyproject
119+
120+
# Rope project settings
121+
.ropeproject
122+
123+
# mkdocs documentation
124+
/site
125+
126+
# mypy
127+
.mypy_cache/
128+
.dmypy.json
129+
dmypy.json
130+
131+
# Pyre type checker
132+
.pyre/

{{ cookiecutter.project_slug }}/Makefile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
SRC_PORT := 4600
2+
DST_PORT := 8000
3+
14
##### docker
25
IMAGE_NAME := {{ cookiecutter.project_slug }}
36
CONTAINER_NAME := ${IMAGE_NAME}-container
4-
SRC_PORT := 4600
5-
DST_PORT := 8000
67

78
build-image:
89
docker build -t ${IMAGE_NAME} ./
@@ -13,6 +14,7 @@ run-container:
1314
-p ${SRC_PORT}:${DST_PORT} \
1415
${IMAGE_NAME}
1516

17+
1618
##### ci
1719
ci: typecheck test lint
1820

@@ -29,6 +31,20 @@ test:
2931
pytest -rf --cov=./{{ cookiecutter.project_slug }}
3032

3133

34+
##### k8s
35+
MANIFEST_PATH = $(shell pwd)/k8s
36+
ENVIRONMENT := local
37+
38+
deploy:
39+
kubectl apply -k ${MANIFEST_PATH}/overlays/${ENVIRONMENT}/
40+
41+
# TODO: prevent operation mistaken
42+
destory:
43+
kubectl delete -k ${MANIFEST_PATH}/overlays/${ENVIRONMENT}/
44+
45+
port-forward:
46+
kubectl port-forward service/{{ cookiecutter.project_slug | kebabcase }}-service ${SRC_PORT}:${DST_PORT}
47+
3248
##### application
3349
launch:
3450
uvicorn app:app --host 0.0.0.0 --port ${DST_PORT}
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
1-
# python-api-template
1+
# {{ cookiecutter.project_slug }}
2+
3+
## Requirements
4+
5+
- make
6+
- docker for mac
7+
- Please turn on `Enable Kubernetes` configulation if you want to deploy on local k8s
8+
- flake8, pytest, mypy
9+
10+
## quick start
11+
12+
1. build docker image
13+
```bash
14+
make build-image
15+
```
16+
17+
2. deploy on your local k8s cluster
18+
```bash
19+
make deploy
20+
```
21+
22+
3. port-forward to k8s service resource
23+
```bash
24+
make port-forward
25+
```
26+
27+
4. invoke health check endpoint
28+
```bash
29+
curl localhost:4600/health
30+
{"msg":"I'm healthy"}
31+
```
32+
33+
## test/lint/typecheck
34+
35+
```bash
36+
make ci
37+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: configmap
5+
data:
6+
APP_ENV: nonProduction
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: deployment
5+
spec:
6+
selector:
7+
matchLabels:
8+
app: {{ cookiecutter.project_slug | kebabcase }}
9+
replicas: 2
10+
template:
11+
spec:
12+
containers:
13+
- name: {{ cookiecutter.project_slug | kebabcase }}
14+
image: {{ cookiecutter.project_slug }}
15+
imagePullPolicy: Never
16+
livenessProbe:
17+
httpGet:
18+
port: 8000
19+
path: /health
20+
readinessProbe:
21+
httpGet:
22+
port: 8000
23+
path: /health
24+
ports:
25+
- containerPort: 8000
26+
envFrom:
27+
- configMapRef:
28+
name: configmap
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace: default
2+
namePrefix: "{{ cookiecutter.project_slug | kebabcase }}-"
3+
commonLabels:
4+
app: {{ cookiecutter.project_slug | kebabcase }}
5+
resources:
6+
- configmap.yaml
7+
- deployment.yaml
8+
- service.yaml
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: service
5+
spec:
6+
type: NodePort
7+
ports:
8+
- protocol: TCP
9+
port: 8000
10+
targetPort: 8000
11+
selector:
12+
app: {{ cookiecutter.project_slug | kebabcase }}

0 commit comments

Comments
 (0)