1. pombredanne
  2. BlingNode

Source

BlingNode / django_ve_preperation.py

try:
	import common
except:	
	import initial_preperation
	initial_preperation.install_requirements()
	import common

"""
    RUN AS ROOT
"""

def main():
    ############################# THIS IS THE BEGGINNING!

    ############################# Check For root Access
    if not common.is_user_root():
        common.error_exit(msg="\nERROR: Run this script as root\n")

    ############################# Check For and Install EASY_INSTALL
    print "---- INSTALLING FOR EASY_INSTALL ----"

    if common.does_program_exist("/usr/bin/easy_install"):
        print "---- Easy_Install Already Exists. ----"
    else:
        common.command_line_call("""sudo apt-get install python-setuptools python-dev build-essential""")
        print "---- Easy_Install Has Been Installed ----"

    ############################# Check For and Install PIP
    print "---- INSTALLING FOR PIP ----"

    if common.does_program_exist("/usr/local/bin/pip") or common.does_program_exist("/usr/bin/pip"):
        print "---- PIP Already Exists. ----"
    else:
        if common.does_program_exist("/usr/bin/easy_install"):
            print "-- Easy_Install is Available. --"
            print "-- Installing PIP Now --"
            common.command_line_call("""sudo easy_install -U pip""")
            print "---- PIP Has Been Installed ----"
        else:
            print "---- !PIP NOT INSTALLED. CANNOT INSTALL PIP! ----"
            common.error_exit()

    ############################# Check For and Install VIRTUALENV Program

    print  "---- INSTALLING VIRTUALENV PROGRAM ----"

    if common.does_program_exist("/usr/local/bin/virtualenv") or common.does_program_exist("/usr/bin/virtualenv"):
        print "---- VirtualEnv Program Already Exists. ----"
    else:
        if common.does_program_exist("/usr/bin/pip") or common.does_program_exist("/usr/local/bin/pip"):
            print "-- PIP is Available. --"
            print "-- Installing VirtualEnv Program Now --"
            common.command_line_call("""sudo pip install -U virtualenv""")
            print "---- VirtualEnv Program Has Been Installed ----"
        else:
            print "---- !VIRTUALENV NOT INSTALLED. CANNOT INSTALL VIRTUALENV PROGRAM! ----"
            common.error_exit()

    ############################# Check for and Create Template Folder

    print  "---- CREATING TEMPLATE FOLDER ----"

    TEMPLATE_DIR="/var/lib/misc/blingnode/lib/virtual-env-templates"

    if common.does_directory_exist(TEMPLATE_DIR):
        print "---- Template Folder Already Exists. ----"
    else:
        print "-- Safe to Write Directory --"
        print "-- Making New Templates Folder --"
        common.command_line_call("""mkdir """+TEMPLATE_DIR)
        print "---- Created New Templates Folder ----"

    ############################# Check for and Create Handler File

    HANDLER_TEMP_FILE="/var/lib/misc/blingnode/lib/virtual-env-templates/myvirtualdjango.py"
    print "---- CREATING TEMPLATE HANDLER FILE ----"

    if common.does_directory_exist(TEMPLATE_DIR):
        print "-- The Template Folder is Available --"

        if common.does_file_exist(HANDLER_TEMP_FILE):
            print "---- Template Handler File Already Exists ----"

        else:
            print "-- Making Handler Template File --"

            #### Creating /var/lib/misc/blingnode/lib/virtual-env-templates/myvirtualdjango.py
            handler_file = open(HANDLER_TEMP_FILE, 'w')
            handler_content =   """activate_this = '/var/www/PROJECT_NAME/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from django.core.handlers.modpython import handler
"""
            handler_file.write(handler_content)
            handler_file.close()
            #### END

            print "---- Handler Template File Created ----"

    else:
        print "-- The Template Folder does NOT Exist - Cannot Create Template Files --"

    ############################# Check for and Create Handler File

    APACHE_TEMP_FILE="/var/lib/misc/blingnode/lib/virtual-env-templates/apache_template"

    print  "---- CREATING TEMPLATE APACHE FILE ----"

    if common.does_directory_exist(TEMPLATE_DIR):
        print "-- The Template Folder is Available --"

        if common.does_file_exist(APACHE_TEMP_FILE):
            print "---- Apache Template File Already Exists ----"
        else:
            print "-- Making Apache Template File --"

            #### Write contents to file
            apache_file = open(APACHE_TEMP_FILE, 'w')
            apache_content = """<VirtualHost *:80>
    ServerName PROJECT_NAME.DOMAIN

    DocumentRoot /var/www/PROJECT_NAME/env/www
    <Directory /var/www/PROJECT_NAME/env/www>
         AllowOverride All
    </Directory>

    <Location \"/\">
        SetHandler python-program
        PythonHandler myvirtualdjango
        SetEnv DJANGO_SETTINGS_MODULE settings
        PythonPath \"['/var/www/PROJECT_NAME/env','/var/www/PROJECT_NAME/env/www'] + sys.path\"
        PythonDebug On
    </Location>

</VirtualHost>"""
            apache_file.write(apache_content)
            apache_file.close()
            #### END

        print "---- Apache Template File Created ----"
    else:
        print "-- The Template Folder does NOT Exist - Cannot Create Template Files --"

    ################################## YOU'RE DONE!

    print "---- YOU'RE DONE! ----"
    print "HERE'S WHAT YOU NEED TO DO NOW:"
    print "ACTUALLY RUN THE django_ve_init.sh SCRIPT"

    ## when testing this script, use REMOVEME, and the following command to revert changes

if __name__ == "__main__":
    main()