Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
本番運用を想定したDjango settings.pyの書き方入門(OSC Fall 2021...
Search
Ryuji Tsutsui
October 23, 2021
Technology
1
1.1k
本番運用を想定したDjango settings.pyの書き方入門(OSC Fall 2021 version)
オープンソースカンファレンス2021 Fall
資料
Ryuji Tsutsui
October 23, 2021
Tweet
Share
More Decks by Ryuji Tsutsui
See All by Ryuji Tsutsui
本番運用を想定したDjango settings.pyの書き方入門
ryu22e
1
2.2k
os.makedirsの細かすぎるセキュリティの話
ryu22e
0
50
HashiCorpの新ツール 「Waypoint」をCloud Runで使ってみた
ryu22e
0
390
Pythonチュートリアルイベント「Python Boot Camp」の紹介
ryu22e
0
170
Pythonとコミュニティと私
ryu22e
0
97
Other Decks in Technology
See All in Technology
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
エンジニア人生の拡張性を高める 「探索型キャリア設計」の提案
tenshoku_draft
1
130
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
6
660
いざ、BSC討伐の旅
nikinusu
2
780
これまでの計測・開発・デプロイ方法全部見せます! / Findy ISUCON 2024-11-14
tohutohu
3
370
Evangelismo técnico: ¿qué, cómo y por qué?
trishagee
0
360
ドメインの本質を掴む / Get the essence of the domain
sinsoku
2
160
OCI Network Firewall 概要
oracle4engineer
PRO
0
4.2k
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
230
iOS/Androidで同じUI体験をネ イティブで作成する際に気をつ けたい落とし穴
fumiyasac0921
1
110
アジャイルチームがらしさを発揮するための目標づくり / Making the goal and enabling the team
kakehashi
3
130
OS 標準のデザインシステムを超えて - より柔軟な Flutter テーマ管理 | FlutterKaigi 2024
ronnnnn
0
190
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Visualization
eitanlees
145
15k
Site-Speed That Sticks
csswizardry
0
28
Rails Girls Zürich Keynote
gr2m
94
13k
How to train your dragon (web standard)
notwaldorf
88
5.7k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
BBQ
matthewcrist
85
9.3k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Building Applications with DynamoDB
mza
90
6.1k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Transcript
ຊ൪ӡ༻Λఆͨ͠ Django settings.pyͷॻ͖ ํೖ Ryuji Tsutsui Open Source Conference 2021
Online/Fallࢿྉ 2021/10/23
ࣗݾհ • Ryuji Tsutsui@ryu22e • Ұൠࣾஂ๏ਓPyCon JP Association͔Βདྷ·ͨ͠ • ීஈຊͷϑΟϯςοΫܥاۀͰDjangoΛͬͯWebαʔϏε
Λ࡞͍ͬͯ·͢ • Djangoྺ8͙Β͍ • Python Boot CampίΞελοϑͱͯ͠׆ಈ͍ͯ͠·͢
ຊͷએ ٕज़ධࣾץʰPython࣮ફϨγϐʱΛࣥචத 20221݄ൃച༧ఆ ϖʔδ: ະఆ ֹۚ: ະఆ ʮPythonϥΠϒϥϦݫબϨγϐʯΛେ෯ʹՃ චɺվగ!!!
ࠓ͢͜ͱ • ຊ൪ڥͰ͑ΔDjangoΞϓϦέʔγϣϯͷઃܭʹ͍ͭͯɺ Έͳ͞ΜͱϊϋΛز͔ͭڞ༗͠·͢ • ࠓճsettings.pyʢDjangoͷઃఆϑΝΠϧʣͷॻ͖ํʹ͍ͭͯ͠ ·͢
ओʹ3ʹ͍ͭͯ͠·͢ • 1. ֤ڥ༻ͷઃఆ • 2. ൿີͷͷॻ͖ํ • 3. ϩάઃఆ
͜Μͳਓ͚ͷηογϣϯͰ͢ • ʮDjangoͷνϡʔτϦΞϧ͙Β͍ͬͨ͜ͱ͕͋Δ͚Ͳຊ൪ڥ Ͱಈ͔͢ΞϓϦέʔγϣϯΛ࡞ͬͨ͜ͱͳ͍ʯͱ͍͏ਓ͚Ͱ͢
ʢ·͓ͣ͞Β͍ʣ settings.pyͬͯԿ͚ͩͬʁ
settings.pyͱʁ $ django-admin startproject django_example $ cd django_example $ tree
django_example django_example !"" __init__.py !"" asgi.py !"" settings.py !"" urls.py #"" wsgi.py %KBOHPϓϩδΣΫτશମͰࢀরͰ͖Δάϩʔόϧͳ ఆΛఆٛͰ͖Δ
settings.pyͷॻ͖ํ DEBUG = True LANGUAGE_CODE = 'ja' TIME_ZONE = 'UTC'
ී௨ʹ1ZUIPOͷఆΛॻ͚ͩ͘
settings.pyʹॻ͍ͨઃఆΛݺͼग़͢ํ๏ >>> from django.conf import settings >>> settings.DEBUG True >>>
settings.LANGUAGE_CODE 'ja' EKBOHPDPOGTFUUJOHTΛܦ༝ͯ͠ݺͼ·͢
1. ֤ڥ༻ͷઃఆ
͜Μͳ࣌Ͳ͏͢Ε͍͍ͷͩΖ͏…ʁ INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles',
'debug_toolbar', # ։ൃڥ͔͠Θͳ͍ઃఆ ] ALLOWED_HOSTS = [ 'example.com', # ຊ൪ڥ͔͠Θͳ͍ઃఆ ] ։ൃڥɾຊ൪ڥͲͪΒʹͱͬͯෆཁͳઃఆ ؚ͕·Ε͍ͯΔʜ
ΞϓϦىಈલʹෆཁͳߦΛίϝϯτΞτ͢Δʁ INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles',
# 'debug_toolbar', # ։ൃڥ͔͠Θͳ͍ઃఆ ] ALLOWED_HOSTS = [ 'example.com', # ຊ൪ڥ͔͠Θͳ͍ઃఆ ] ϛε͕͋Γͦ͏ͳͷͰΓͨ͘ͳ͍
༗ޮແޮΛΓସ͑ΔΦϓγϣϯΛ༻ҙ͢Δʁ MODE = ... # ڥม͔MODEΛఆٛͨ͠ผͷϑΝΠϧΛಡΈࠐΉ INSTALLED_APPS = [ 'django.contrib.admin',
'django.contrib.auth', # ʢলུʣ ] if MODE == 'local': INSTALLED_APPS += ['debug_toolbar'] ίϝϯτΞτΑΓΦϖϛεͷՄ ೳੑͦ͏͚ͩͲʜ
༗ޮແޮΛΓସ͑ΔΦϓγϣϯΛ༻ҙ͢Δʁ if MODE == 'local': INSTALLED_APPS.append('debug_toolbar') if MODE == 'production':
ALLOWED_HOSTS = ['example.com'] elif MODE == 'staging': ALLOWED_HOSTS = ['staging.example.com'] else: ALLOWED_HOSTS = ['*'] if MODE == 'staging': ... Φϓγϣϯ͕૿͑Δͱઃఆ͕ෳࡶԽ ͦ͠͏
͜Μͳ࣌ઃఆϑΝΠϧΛڥ͝ͱʹ͚· ͠ΐ͏
͜ͷΑ͏ͳߏ͕͓קΊ django_example #"" settings # settings.py͕͋ͬͨॴʹ͜ΕΛ࡞Δ !"" __init__.py !"" base.py
# ͯ͢ͷڥͰ͏ڞ௨ͷઃఆ !"" local.py # ։ൃڥ༻ͷઃఆ !"" production.py # ຊ൪ڥ༻ͷઃఆ #"" staging.py # εςʔδϯάڥ༻ͷઃఆ TFUUJOHTQZফ͢
֤ϑΝΠϧͷ༰ INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', # ʢলུʣ ] ROOT_URLCONF
= 'django_example.urls' WSGI_APPLICATION = 'django_example.wsgi.application' base.pyʢڞ௨ͷઃఆʣ
from .base import * DEBUG = False ALLOWED_HOSTS = [
'staging.example.com', ] ֤ϑΝΠϧͷ༰ from .base import * DEBUG = False ALLOWED_HOSTS = [ 'example.com', ] from .base import * DEBUG = True INSTALLED_APPS += ['debug_toolbar'] stagings.pyʢεςʔδϯάڥ༻ʣ local.pyʢ։ൃڥ༻ʣ production.pyʢຊ൪ڥ༻ʣ ͜͜Ͱڞ௨ͷઃఆ ΛಡΈࠐΉ
Ͳ͏ͬͯ͏ઃఆϑΝΠϧΛΓସ͑Δ͔ • ڥมDJANGO_SETTINGS_MODULEΛ͍·͠ΐ͏ • ྫ͑ɺຊ൪ڥͳΒ͜͏͠·͢ • DJANGO_SETTINGS_MODULE="django_example.settings.production" • ϑΝΠϧύεͰͳͯ͘Pythonͷimportจʹॻ͘ͱ͖ͷυοτͰܨ͛ͨॻ͖ ํͰ͋Δ͜ͱʹҙ͍ͯͩ͘͠͞
• Ұ࣌తʹ͏ઃఆϑΝΠϧΛΓସ͍͑ͨͳΒɺDjangoίϚϯυʹ--settings ΦϓγϣϯΛࢦఆ͠·͢
ڥมΛͲ͜ʹઃఆ͢Ε͍͍͔ʁ • ͍ͬͯΔϓϥοτϑΥʔϜʢΦϯϓϨ or ΫϥυʣʹΑͬͯҧ͏ ͷͰɺҰ֓ʹ͜Εɺͱ͍͏ͷݴ͑·ͤΜ͕ɺز͔ͭྫΛڍ͛·͢
ڥมͷઃఆํ๏ྫ • .bashrcʹʮexport DJANGO_SETTINGS_MODULE="..."ʯΛॻ͍͓ͯ ͘ • systemdͷEnvironmentFileΛར༻͢Δ • AWS Systems
Manager ύϥϝʔλετΞΛར༻͢Δ • Google Cloud Secret ManagerΛར༻͢Δ
manage.pyͷฤू͕ඞཁ #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" import
os import sys def main(): """Run administrative tasks.""" os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_example.settings') # ʢলུʣ ͜͜ΛʮEKBOHP@FYBNQMFTFUUJOHTMPDBMʯʹมߋ
2. ൿີͷͷॻ͖ํ
͜ΜͳઃఆΛॻ͍͍͚ͯͳ͍ʂ
ͪͳΈʹGitHubͰࠓͷίʔυΛެ։͢Δͱ͜Μͳϝʔϧ͕ಧ͖·͢
ൿີͷڥม͔ΒಡΈऔΔΑ͏ʹ import os AWS_ACCESS_KEY_ID = os.environ.get( "DJANGO_AWS_ACCESS_KEY_ID" ) ͨͩɺ͜ͷॻ͖ํͩͱڥมͷઃఆ࿙Εͷࡍʹ /POF͕ೖͬͯ͠·͏ͷͰɺ͏ͻͱඞཁ
ඞਢͷڥมͲ͏ͬͯॻ͘ʁ import os from django.core.exception import ImproperlyConfigured # os.environʹ֘͢Δ͕ͳ͚Εྫ֎Λૹग़͢ΔؔΛ࡞Δ def
get_env_variable(var_name): try: os.environ[var_name] except KeyError: # os.environʹ֘͢ΔΩʔ͕ͳ͍߹ʹԿͷΩʔ͕Γͳ͍͔Λྫ֎ϝοηʔδʹؚΉ error_msg = "Set the {} environment variable".format(var_name) raise ImproperlyConfigured(error_msg) Ҿ༻ݩʮ5XP4DPPQTPG%KBOHPYʯ ϖʔδ
͜ΜͳϥΠϒϥϦ͓קΊ • django-environ • PyPI: https://pypi.org/project/django-environ/ • EnvΫϥεܦ༝ͰڥมΛݺͼग़͢ͱɺઃఆ࿙ΕΛݕग़Ͱ͖· ͢ɻ •
bool, intͳͲܕมͬͯ͘Ε·͢ɻ
3. ϩάઃఆ
DjangoͷϩάઃఆͷΓํ • LOGGINGͱ͍͏߲ʹࣙॻܕͰઃఆΛॻ͖·͢ • ʢͬ͘͟Γઆ໌͢ΔͱʣϩάΛͲ͜ʹग़ྗ͢Δ͔Λ͜͜ͰܾΊ·͢ • ग़ྗઌͷྫ • ϝʔϧ •
ඪ४ग़ྗ • ϩʔΧϧϑΝΠϧ • ϩάϩʔςʔγϣϯʢݹ͍ϩάͷΞʔΧΠϒআʣͰ͖·͢ • ʮϩάϨϕϧʯʢDEBUG, INFO, WARNING, ERROR, CRITICALʣϩάग़ྗݩͷϞδϡʔϧʹ Αͬͯग़ྗઌΛΓସ͑Δ͜ͱͰ͖·͢
LOGGINGͷઃఆ্ɺग़ྗઌඪ४ग़ྗͷΈʹ ͢Δ • LOGGING্Ͱͷग़ྗઌͯ͢ඪ४ग़ྗ • ඪ४ग़ྗͷϩάΛͲ͏ѻ͏͔ɺΞϓϦͷ֎ʹ͋ΔϛυϧΣΞʹ ͤ·͠ΐ͏ • Fluentd •
Amazon CloudWatch Logs • Google Cloud Logging
cookiecutter-django͓קΊ • cookiecutterςϯϓϨʔτΛݩʹϓϩδΣΫτΛ࡞͢Δπʔϧ • URL: https://github.com/pydanny/cookiecutter-django • LOGGINGͷઃఆྫ͗͢ΔͷͰ͜ͷεϥΠυʹࡌͤ·ͤΜͰ͠ ͕ͨɺ͜ͷςϯϓϨʔτͰ࡞ͬͨϓϩδΣΫτʹॻ͍͍ͯΔ༰Λಡ ΜͰ͍ͩ͘͞
cookiecutter-djangoͷ͍ํ $ pip install cookiecutter # macOSͳΒbrew install cookiecutter Մ
$ cookiecutter https://github.com/pydanny/cookiecutter-django $ # ʢ৭ʑ࣭͞Ε·͕͢ɺͱΓ͋͑ͣͬͯΈΔ͚ͩͳΒશ෦EnterΛԡͯ͠OKʣ $ cd my_awesome_project $ ls config/settings __init__.py base.py local.py production.py test.py
·ͱΊ • settings.pyڥ͝ͱʹϑΝΠϧΛ͚ͯɺڥม DJANGO_SETTINGS_MODULEͰಡΉϑΝΠϧΛΓସ͑·͠ΐ͏ • ൿີͷΛsettings.pyʹʹॻ͘ͷઈରμϝɻڥม͔Βಡ·ͤ·͠ΐ ͏ɻdjango-environ͓קΊ • ϩάग़ྗγϯϓϧʹඪ४ग़ྗͷΈɻอଘઌΞϓϦέʔγϣϯͷ֎ʹ͋Δϛ υϧΣΞʹཧͤ͞·͠ΐ͏
• Ҏ্3ΛॳΊ͔ΒΈࠐΜͩςϯϓϨʔτcookiecutter-django͓קΊͰ͢
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝· ͨ͠