[PYTHON] I tried combining Fabric, Cuisine and Jinja2

It's a memo, so it's rough. I was able to do something like this

Thing you want to do

I'm developing multiple Django apps and deploying them using Fabric and Cuisine. There are variables that differ depending on the description and environment (development, staging, production) in settings.py that I do not want to put on github, so I want to reflect them from the local file to the remote server file

code

Template file

project_root/settings/production.py



#Common setting items are project in all environments_root/settings/common.I have it in common with py.

from sample_app.settings.common import *

SECRET_KEY = '{{ secret_key }}'

DEBUG = False

ALLOWED_HOSTS = ['127.0.0.1']

DATABASES = {
    'default': {
        'ENGINE': '{{ databases.default.engine }}',
        'NAME': '{{ databases.default.name }}',
        'USER': '{{ databases.default.user }}',
        'PASSWORD': '{{ databases.default.password }}',
        'HOST': '{{ databases.default.host }}',
        'PORT': {{ databases.default.port }},
    }
}

yaml file

The value is appropriate

secrets.yml


django:
  settings:
    production:
      secret_key: ''
      databases:
        default:
          engine: django.db.backends.mysql
          name: sample_app
          user: root
          password: ''
          host: 127.0.0.1
          port: 3306

The process of uploading a file

Rather than uploading, it is more correct to say that you are creating a file with the contents specified remotely.

from jinja2 import Environment, FileSystemLoader
from cuisine import file_write

secrets = yaml.load(file('secrets.yml'))
local_template_name = local_template_path.split('/')[-1]
local_template_dir = local_template_path.replace(local_template_name, '')

#The following 3 lines are the main processing. Get the string with the variable embedded in the template and create a file containing it on the remote server.
jinja2_env = Environment(loader=FileSystemLoader(local_template_dir))
content = jinja2_env.get_template(local_template_name).render(secrets['django']['settings']['production'])
file_write(remote_path, content.encode('utf-8'))

reference

https://gist.github.com/wrunk/1317933

Recommended Posts

I tried combining Fabric, Cuisine and Jinja2
I tried using jinja2
I compared blade and jinja2
I tried using Twitter api and Line api
I tried using PyEZ and JSNAPy. Part 2: I tried using PyEZ
I tried using PyEZ and JSNAPy. Part 1: Overview
I tried web scraping using python and selenium
I implemented DCGAN and tried to generate apples
I tried scraping
I tried object detection using Python and OpenCV
I tried playing with PartiQL and MongoDB connected
I tried Jacobian and partial differential with python
I tried function synthesis and curry with python
I tried AutoKeras
I tried papermill
I tried morphological analysis and vectorization of words
I tried django-slack
I tried Django
I tried spleeter
I tried cgo
[Introduction to PID] I tried to control and play ♬
I tried to read and save automatically with VOICEROID2 2
I tried pipenv and asdf for Python version control
I tried using google test and CMake in C
I tried adding post-increment to CPython. Overview and summary
I tried to automatically read and save with VOICEROID2
I tried adding system calls and scheduler to Linux
I tried to implement Grad-CAM with keras and tensorflow
Python: I tried a liar and an honest tribe
I tried to install scrapy on Anaconda and couldn't
I tried using parameterized
I tried using mimesis
I tried using anytree
I tried competitive programming
I tried running pymc
I tried ARP spoofing
I tried using aiomysql
I tried using Summpy
I tried Python> autopep8
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried PyCaret2.0 (pycaret-nightly)
I tried using openpyxl
I tried deep learning
I tried AWS CDK!
I tried using Ipython
I tried to debug.
I tried using PyCaret
I tried using cron
I tried Kivy's mapview
I tried using ngrok
I tried using face_recognition
I tried to paste
I tried using Jupyter
I tried moving EfficientDet
I tried shell programming
I tried using Heapq