Ensure same POSTGRES_USER is set across environments

+ get rid of env.example in favor of pre-generated .env.
This commit is contained in:
Nikita P. Shupeyko 2018-02-07 13:41:57 +03:00
parent b76c36229c
commit abc864bd2b
2 changed files with 74 additions and 43 deletions

View File

@ -22,7 +22,7 @@ try:
except NotImplementedError: except NotImplementedError:
using_sysrandom = False using_sysrandom = False
_PROJECT_DIR_PATH = os.path.realpath(os.path.curdir) PROJECT_DIR_PATH = os.path.realpath(os.path.curdir)
def remove_file(file_path): def remove_file(file_path):
@ -35,7 +35,7 @@ def remove_open_source_project_only_files():
'CONTRIBUTORS.txt', 'CONTRIBUTORS.txt',
] ]
for file_name in file_names: for file_name in file_names:
os.remove(os.path.join(_PROJECT_DIR_PATH, file_name)) os.remove(os.path.join(PROJECT_DIR_PATH, file_name))
def remove_gplv3_files(): def remove_gplv3_files():
@ -43,21 +43,21 @@ def remove_gplv3_files():
'COPYING', 'COPYING',
] ]
for file_name in file_names: for file_name in file_names:
os.remove(os.path.join(_PROJECT_DIR_PATH, file_name)) os.remove(os.path.join(PROJECT_DIR_PATH, file_name))
def remove_pycharm_files(): def remove_pycharm_files():
idea_dir_path = os.path.join(_PROJECT_DIR_PATH, '.idea') idea_dir_path = os.path.join(PROJECT_DIR_PATH, '.idea')
if os.path.exists(idea_dir_path): if os.path.exists(idea_dir_path):
shutil.rmtree(idea_dir_path) shutil.rmtree(idea_dir_path)
docs_dir_path = os.path.join(_PROJECT_DIR_PATH, 'docs', 'pycharm') docs_dir_path = os.path.join(PROJECT_DIR_PATH, 'docs', 'pycharm')
if os.path.exists(docs_dir_path): if os.path.exists(docs_dir_path):
shutil.rmtree(docs_dir_path) shutil.rmtree(docs_dir_path)
def remove_docker_files(): def remove_docker_files():
shutil.rmtree(os.path.join(_PROJECT_DIR_PATH, 'compose')) shutil.rmtree(os.path.join(PROJECT_DIR_PATH, 'compose'))
file_names = [ file_names = [
'local.yml', 'local.yml',
@ -65,7 +65,7 @@ def remove_docker_files():
'.dockerignore', '.dockerignore',
] ]
for file_name in file_names: for file_name in file_names:
os.remove(os.path.join(_PROJECT_DIR_PATH, file_name)) os.remove(os.path.join(PROJECT_DIR_PATH, file_name))
def remove_heroku_files(): def remove_heroku_files():
@ -74,7 +74,7 @@ def remove_heroku_files():
'runtime.txt', 'runtime.txt',
] ]
for file_name in file_names: for file_name in file_names:
remove_file(os.path.join(_PROJECT_DIR_PATH, file_name)) remove_file(os.path.join(PROJECT_DIR_PATH, file_name))
def remove_paas_files(): def remove_paas_files():
@ -87,7 +87,7 @@ def remove_paas_files():
none_paas_files_left &= False none_paas_files_left &= False
if none_paas_files_left: if none_paas_files_left:
remove_file(os.path.join(_PROJECT_DIR_PATH, 'requirements.txt')) remove_file(os.path.join(PROJECT_DIR_PATH, 'requirements.txt'))
def remove_grunt_files(): def remove_grunt_files():
@ -95,7 +95,7 @@ def remove_grunt_files():
'Gruntfile.js', 'Gruntfile.js',
] ]
for file_name in file_names: for file_name in file_names:
os.remove(os.path.join(_PROJECT_DIR_PATH, file_name)) os.remove(os.path.join(PROJECT_DIR_PATH, file_name))
def remove_gulp_files(): def remove_gulp_files():
@ -103,7 +103,7 @@ def remove_gulp_files():
'gulpfile.js', 'gulpfile.js',
] ]
for file_name in file_names: for file_name in file_names:
os.remove(os.path.join(_PROJECT_DIR_PATH, file_name)) os.remove(os.path.join(PROJECT_DIR_PATH, file_name))
def remove_packagejson_file(): def remove_packagejson_file():
@ -111,15 +111,15 @@ def remove_packagejson_file():
'package.json', 'package.json',
] ]
for file_name in file_names: for file_name in file_names:
os.remove(os.path.join(_PROJECT_DIR_PATH, file_name)) os.remove(os.path.join(PROJECT_DIR_PATH, file_name))
def remove_celery_app(): def remove_celery_app():
shutil.rmtree(os.path.join(_PROJECT_DIR_PATH, '{{ cookiecutter.project_slug }}', 'taskapp')) shutil.rmtree(os.path.join(PROJECT_DIR_PATH, '{{ cookiecutter.project_slug }}', 'taskapp'))
def append_to_gitignore(path): def append_to_project_gitignore(path):
gitignore_file_path = os.path.join(_PROJECT_DIR_PATH, '.gitignore') gitignore_file_path = os.path.join(PROJECT_DIR_PATH, '.gitignore')
with open(gitignore_file_path, 'a') as gitignore_file: with open(gitignore_file_path, 'a') as gitignore_file:
gitignore_file.write(path) gitignore_file.write(path)
gitignore_file.write(os.linesep) gitignore_file.write(os.linesep)
@ -150,56 +150,95 @@ def generate_random_string(length,
return ''.join([random.choice(symbols) for _ in range(length)]) return ''.join([random.choice(symbols) for _ in range(length)])
def replace_flag_with_random_string(file_path, def set_flag(file_path,
flag, flag,
*args, value=None,
**kwargs): *args,
random_string = generate_random_string(*args, **kwargs) **kwargs):
if random_string is None: if value is None:
import sys random_string = generate_random_string(*args, **kwargs)
sys.stdout.write( if random_string is None:
"We couldn't find a secure pseudo-random number generator on your system. " import sys
"Please, {} manually.".format(flag) sys.stdout.write(
) "We couldn't find a secure pseudo-random number generator on your system. "
random_string = flag "Please, make sure to manually {} later.".format(flag)
)
random_string = flag
value = random_string
with open(file_path, 'r+') as f: with open(file_path, 'r+') as f:
file_contents = f.read().replace(flag, random_string) file_contents = f.read().replace(flag, value)
f.seek(0) f.seek(0)
f.write(file_contents) f.write(file_contents)
f.truncate() f.truncate()
return value
def set_django_secret_key(file_path): def set_django_secret_key(file_path):
replace_flag_with_random_string( django_secret_key = set_flag(
file_path, file_path,
'!!!SET DJANGO_SECRET_KEY!!!', '!!!SET DJANGO_SECRET_KEY!!!',
length=50, length=50,
using_digits=True, using_digits=True,
using_ascii_letters=True using_ascii_letters=True
) )
return django_secret_key
def set_postgres_user(file_path): def set_postgres_user(file_path,
replace_flag_with_random_string( value=None):
postgres_user = set_flag(
file_path, file_path,
'!!!SET POSTGRES_USER!!!', '!!!SET POSTGRES_USER!!!',
value=value,
length=8, length=8,
using_ascii_letters=True using_ascii_letters=True
) )
return postgres_user
def set_postgres_password(file_path): def set_postgres_password(file_path):
replace_flag_with_random_string( postgres_password = set_flag(
file_path, file_path,
'!!!SET POSTGRES_PASSWORD!!!', '!!!SET POSTGRES_PASSWORD!!!',
length=42, length=42,
using_digits=True, using_digits=True,
using_ascii_letters=True using_ascii_letters=True
) )
return postgres_password
def initialize_dotenv(postgres_user):
# Initializing `env.example` first.
envexample_file_path = os.path.join(PROJECT_DIR_PATH, 'env.example')
set_django_secret_key(envexample_file_path)
set_postgres_user(envexample_file_path, value=postgres_user)
set_postgres_password(envexample_file_path)
# Renaming `env.example` to `.env`.
dotenv_file_path = os.path.join(PROJECT_DIR_PATH, '.env')
shutil.move(envexample_file_path, dotenv_file_path)
def initialize_localyml(postgres_user):
set_postgres_user(os.path.join(PROJECT_DIR_PATH, 'local.yml'), value=postgres_user)
def initialize_local_settings():
set_django_secret_key(os.path.join(PROJECT_DIR_PATH, 'config', 'settings', 'local.py'))
def initialize_test_settings():
set_django_secret_key(os.path.join(PROJECT_DIR_PATH, 'config', 'settings', 'test.py'))
def main(): def main():
postgres_user = generate_random_string(length=16, using_ascii_letters=True)
initialize_dotenv(postgres_user)
initialize_localyml(postgres_user)
initialize_local_settings()
initialize_test_settings()
if '{{ cookiecutter.open_source_license }}' == 'Not open source': if '{{ cookiecutter.open_source_license }}' == 'Not open source':
remove_open_source_project_only_files() remove_open_source_project_only_files()
elif '{{ cookiecutter.open_source_license}}' != 'GPLv3': elif '{{ cookiecutter.open_source_license}}' != 'GPLv3':
@ -207,6 +246,7 @@ def main():
if '{{ cookiecutter.use_pycharm }}'.lower() == 'n': if '{{ cookiecutter.use_pycharm }}'.lower() == 'n':
remove_pycharm_files() remove_pycharm_files()
if '{{ cookiecutter.use_docker }}'.lower() == 'n': if '{{ cookiecutter.use_docker }}'.lower() == 'n':
remove_docker_files() remove_docker_files()
@ -238,15 +278,6 @@ def main():
if '{{ cookiecutter.use_celery }}'.lower() == 'n': if '{{ cookiecutter.use_celery }}'.lower() == 'n':
remove_celery_app() remove_celery_app()
envexample_file_path = os.path.join(_PROJECT_DIR_PATH, 'env.example')
set_django_secret_key(os.path.join(_PROJECT_DIR_PATH, 'config', 'settings', 'local.py'))
set_django_secret_key(os.path.join(_PROJECT_DIR_PATH, 'config', 'settings', 'test.py'))
set_django_secret_key(envexample_file_path)
set_postgres_user(envexample_file_path)
set_postgres_password(envexample_file_path)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -15,7 +15,7 @@ services:
volumes: volumes:
- .:/app - .:/app
environment: environment:
- POSTGRES_USER={{cookiecutter.project_slug}} - POSTGRES_USER=!!!SET POSTGRES_USER!!!
- USE_DOCKER=yes - USE_DOCKER=yes
ports: ports:
- "8000:8000" - "8000:8000"
@ -29,7 +29,7 @@ services:
- postgres_data_local:/var/lib/postgresql/data - postgres_data_local:/var/lib/postgresql/data
- postgres_backup_local:/backups - postgres_backup_local:/backups
environment: environment:
- POSTGRES_USER={{cookiecutter.project_slug}} - POSTGRES_USER=!!!SET POSTGRES_USER!!!
{% if cookiecutter.use_mailhog == 'y' %} {% if cookiecutter.use_mailhog == 'y' %}
mailhog: mailhog:
image: mailhog/mailhog:v1.0.0 image: mailhog/mailhog:v1.0.0