Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenient script for rapid development #3318

Open
raiaman15 opened this issue Sep 12, 2021 · 3 comments
Open

Convenient script for rapid development #3318

raiaman15 opened this issue Sep 12, 2021 · 3 comments

Comments

@raiaman15
Copy link

raiaman15 commented Sep 12, 2021

Description

This ticket is a suggestion (since I personally follow and I have seen in some non-Django based projects.

While working on some projects, using Django cookie-cutter, I had to refer to the cookie-cutter documentation for a number of commands (especially when working with docker). Hence I suggest a convenient script to cover all major functionalities with intuitive command names (example: python manage.py showmigrations => ./app showmigrations) and then the end-user can further extend this as needed.

Rationale

This could assist the developer in rapid development. Not only they would have the short commands (easy to remember) but also they can extend it further by clubbing 2 or more commands together. Example, taking database backup and uploading to AWS:

Initially we have 3 commands:

docker-compose -f local.yml exec postgres backup
docker cp "$(docker-compose -f local.yml ps -q postgres)":/backups ./backups
docker-compose -f production.yml run --rm awscli upload

Code in our convenience script:

backup_db() {
  docker-compose -f local.yml exec postgres backup
  docker cp "$(docker-compose -f local.yml ps -q postgres)":/backups ./backups
}

upload_backup_db_s3() {
  docker-compose -f production.yml run --rm awscli upload
}

End-user would get simpler commands:

./app backup_db
./app upload_backup_bd_s3
@foarsitter
Copy link
Collaborator

Your suggestion sounds like #1879, is that correct?

@raiaman15
Copy link
Author

raiaman15 commented Sep 12, 2021

@foarsitter Correct, the only difference is I am using shell scripts instead of makefiles.
Since many of us use some such scripts, the suggestion is to let the core developers finalize it.

For example, for a new project, I generally start with something like this:

#!/bin/sh

# Arguments
c1=$1  # Environment
c2=$2  # Command
c3=$3  # Command argument (optional)


# Help command
help() {
  echo "Usage: <environment> <command> [<command argument>]"
  echo "environment:"
  echo "* local"
  echo "* production"
  echo ""
  echo "command:"
  echo "* generate_certificates"
  echo "* build"
  echo "* init"
  echo "* up"
  echo "* down"
  echo "* logs"
  echo "* showmigrations"
  echo "* makemigrations"
  echo "* migrate"
  echo "* createsuperuser"
  echo "* backup_db"
  echo "* list_backup_db"
  echo "* restore_db"
  echo "* upload_backup_db_s3"
  echo "* download_backup_db_s3"
  echo "* pycodestyle"
  echo "* flake8"
  echo "* pylint"
  echo "* pytest"
  echo "* coverage"
  echo "* unittest"
  echo "* run_all_checks_and_tests"
}


# App commands
generate_certificates() {
  if [ "$c1" = "local" ]
  then
    mkdir certs && docker-compose -f "$c1".yml run django openssl req -newkey rsa:4096 -x509 -sha256 -days 3650 -nodes -out certs/ngo.support.local.crt -keyout certs/ngo.support.local.key -subj "/C=IN/ST=Uttar Pradesh/L=Noida/O=Infroid/OU=IT/CN=ngo.support.local" && echo "Certificates generated successfully."
  else
    echo "Operation aborted. Certificates can only be generated for 'local' environment."
  fi
}

build() {
  docker-compose -f "$c1".yml build
}

init() {
  echo "Do you confirm that the following are installed: docker, docker-compose, git, openssl and pre-commit (y/n)? "
  read -r answer
  if [ "$answer" = "y" ]
  then
    build
    generate_ssl_certificates
    git init
    pre-commit install
  else
    echo "Operation aborted. Please install docker, docker-compose, git, openssl and pre-commit."
  fi
}

up() {
  docker-compose -f "$c1".yml up
}

down() {
  docker-compose -f "$c1".yml down
}

logs() {
  docker-compose -f "$c1".yml logs -t
}


# Django commands
showmigrations() {
  docker-compose -f "$c1".yml run --rm django python manage.py showmigrations
}

makemigrations() {
  docker-compose -f "$c1".yml run --rm django python manage.py makemigrations
}

migrate() {
  docker-compose -f "$c1".yml run --rm django python manage.py migrate
}

createsuperuser() {
  docker-compose -f "$c1".yml run --rm django python manage.py createsuperuser
}


# Postgres commands
backup_db() {
  docker-compose -f local.yml exec postgres backup
  docker cp "$(docker-compose -f local.yml ps -q postgres)":/backups ./backups
}

list_backup_db() {
  docker-compose -f local.yml exec postgres backups
}

restore_db() {
  docker-compose -f local.yml exec postgres restore "$c3"
}

upload_backup_db_s3() {
  docker-compose -f production.yml run --rm awscli upload
}

download_backup_db_s3() {
  docker-compose -f production.yml run --rm awscli download "$c3"
}


# Code check commands
pycodestyle() {
  echo "PYCODESTYLE" && docker-compose -f "$c1".yml run --rm django pycodestyle ./ngo_support_core
}

flake8() {
  echo "FLAKE8" && docker-compose -f "$c1".yml run --rm django flake8
}

pylint() {
  echo "PYLINT" && docker-compose -f "$c1".yml run --rm django pylint ./ngo_support_core
}

pytest() {
  docker-compose -f "$c1".yml run --rm django pytest
}

coverage() {
  docker-compose -f local.yml run --rm django coverage run -m pytest
  docker-compose -f local.yml run --rm django coverage report
}

unittest() {
  docker-compose -f "$c1".yml run --rm django python manage.py test
}

run_all_checks_and_tests() {
  pycodestyle && echo "pycodestyle - No issue found"
  flake8 && echo "flake8 - No issue found"
  pylint && echo "pylint - No issue found"
  coverage && echo "coverage - No issue found"
  unittest && echo "unittest - No issue found"
}

case "$c1" in
  "local"|"production")
    $c2;;
  *)
    help;;
esac

@jgz
Copy link

jgz commented Sep 18, 2021

This is a really good start. I have something I built like this as well but you've got more of the common commands already built out.

Adding the $3 arg on to the end of commands like logs/up/down/etc.. would would fill in some holes as well.

Also a run bash and pythion manage.py shell are needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants