def create_postgresql_user_and_db(mode): """ Generates the commands needed to create the database and its owner. Args: mode(str): Must be either 'dev' or 'prod', or the program will exit. Returns(str): The command to be executed. """ chosen_config = check_mode(mode) pretty_print("\nCreating postgresql user and database ...") command = ( "sudo -u postgres psql -c \\\"DROP DATABASE IF EXISTS " + chosen_config.DB_NAME + ";\\\" &&" "sudo -u postgres psql -c \\\"DROP ROLE IF EXISTS " + chosen_config.DB_USER + ";\\\" &&" "sudo -u postgres psql -c \\\"CREATE USER " + chosen_config.DB_USER + " WITH PASSWORD '" + chosen_config.DB_PASSWORD + "';\\\" &&" "sudo -u postgres psql -c \\\"CREATE DATABASE " + chosen_config.DB_NAME + " OWNER " + chosen_config.DB_USER + ";\\\" &&" "sudo -u postgres psql -c \\\"ALTER USER " + chosen_config.DB_USER + " CREATEDB;\\\"" ) return command
def create_virtualenv(mode): """ Generates the commands needed to create the virtual environment. Args: mode(str): Must be either 'dev' or 'prod', or the program will exit. Returns(str): The command to be executed. """ chosen_config = check_mode(mode) pretty_print("\nCreating virtualenv ...") command = ( "virtualenv --no-site-packages -p /usr/bin/python3.4 " + chosen_config.VIRTUALENV_DIR ) return command
def update_project(mode): """ Generates the commands needed to update the project from github. Args: mode(str): Must be either 'dev' or 'prod', or the program will exit. Returns(str): The command to be executed. """ chosen_config = check_mode(mode) pretty_print("\nUpdating the project ...") command = ( "cd " + chosen_config.PROJECT_DIR + "\n" "git pull origin master" ) return command
def download_project(mode): """ Generates the commands needed to download the project from github. Args: mode(str): Must be either 'dev' or 'prod', or the program will exit. Returns(str): The command to be executed. """ chosen_config = check_mode(mode) pretty_print("\nDownloading the project ...") project_parent_dir = os.path.split(chosen_config.PROJECT_DIR)[0] command = ( "mkdir -p " + project_parent_dir + "\n" "cd " + project_parent_dir + "\n" "git clone " + chosen_config.PROJECT_REPO_URL ) return command
def clean_test_data(mode): """ Cleans application test data. Args: mode(str): Must be either 'dev' or 'prod', or the program will exit. Returns(str): The command to be executed. """ chosen_config = check_mode(mode) pretty_print("\nCleaning test data ...") command = ( "deactivate 2> /dev/null\n" "source " + chosen_config.VIRTUALENV_DIR + "/bin/activate 2> /dev/null\n" "cd " + chosen_config.PROJECT_DIR + "/src\n" "python manage.py shell < ../devops/test_data/clean_test_data.py" ) return command
def migrate(mode): """ Generates the commands needed to migrate the database. Args: mode(str): Must be either 'dev' or 'prod', or the program will exit. Returns(str): The command to be executed. """ chosen_config = check_mode(mode) pretty_print("\nMigrating database ...") command = ( "deactivate 2> /dev/null\n" "source " + chosen_config.VIRTUALENV_DIR + "/bin/activate 2> /dev/null\n" "python " + chosen_config.PROJECT_DIR + "/src/manage.py migrate" ) return command
def export_project_keys(mode): """ Generates the commands needed to export the project keys as env vars. Args: mode(str): Must be either 'dev' or 'prod', or the program will exit. Returns(str): The command to be executed. """ chosen_config = check_mode(mode) pretty_print("\nExporting project keys ...") command = "" for key, value in chosen_config.__dict__.items(): if not key.startswith("__"): command += \ "echo 'export " + key + "=" + value + "'" \ " | sudo tee --append " + chosen_config.VIRTUALENV_DIR + \ "/bin/activate\n" return command
def create_app_group_and_user(mode): """ Generates the commands needed to create the group and user for the app. Args: mode(str): Must be either 'dev' or 'prod', or the program will exit. Returns(str): The command to be executed. """ chosen_config = check_mode(mode) pretty_print("\nCreating app group and user ...") command = ( "id -g " + chosen_config.APP_GROUP + " || sudo groupadd --system " + chosen_config.APP_GROUP + "\n" "id -u " + chosen_config.APP_USER + " || sudo useradd --system --gid " + chosen_config.APP_GROUP + " --shell /bin/bash --home " + chosen_config.PROJECT_DIR + " " + chosen_config.APP_USER ) return command
def install_python_requirements(mode): """ Generates the commands needed to install python requirements. Args: mode(str): Must be either 'dev' or 'prod', or the program will exit. Returns(str): The command to be executed. """ chosen_config = check_mode(mode) filename = "" if mode == "dev": filename = "development.txt" elif mode == "prod": filename = "production.txt" pretty_print("\nInstalling python requirements ...") command = ( "deactivate 2> /dev/null\n" "source " + chosen_config.VIRTUALENV_DIR + "/bin/activate 2> /dev/null\n" "cd " + chosen_config.PROJECT_DIR + "/requirements\n" "pip install -r " + filename ) return command