def deploy_project(project_name, env_type, repo):
    """ Deploys project to remote server, requires project name, environment
    type(development/production) and the repository of the project """
    create_virtual_env(project_name)
    with cd(project_name):
        run('mkdir %s' % SOURCE_DIRECTORY_NAME)
        with cd(SOURCE_DIRECTORY_NAME):
            run('git clone %s .' % repo)
            source_path = run('pwd')
            nginx_uwsgi_conf_name = '%s.production.nginx.uwsgi' % project_name
            uwsgi_conf_name = '%s.production.uwsgi' % project_name
            with settings(warn_only=True):
                sudo('ln -s %s.conf /etc/nginx/sites-enabled/' % os.path.join(source_path,
                                                                         nginx_uwsgi_conf_name))
                sudo('ln -s %s.conf /etc/init/' % os.path.join(source_path,
                                                           uwsgi_conf_name))
            local_settings_path = os.path.join(source_path, project_name,
                                               project_name, 'settings',
                                               'local.py')
            if generate_local_config(project_name, local_settings_path):
                update_project(project_name, env_type)
def startproject(name):
    """Creates new virtual environment, installs Django and creates new project
    with the specified name. Prompts the user to choose DB engine and tries
    to setup database/user with the project name and random password and
    updates local settings according to the choosen database. Also creates
    nginx conf file for local usage"""
    if env['host'] not in ['127.0.0.1', 'localhost']:
        print 'This task can be executed only on localhost'
        return
    check, message = check_project_name(name)
    if not check:
        print message
        exit(1)
    create_virtual_env(name, True)
    ve_path = os.path.abspath(name)
    source_path = os.path.join(ve_path, SOURCE_DIRECTORY_NAME)
    local('mkdir %s' % source_path)
    with lcd(name):
        with prefix('. %s' % ve_activate_prefix(name)):
            packages_file = os.path.join(source_path,
                                         'required_packages.txt')
            local('cp %s %s' % (os.path.join(FABFILE_LOCATION,
                                             'project_settings',
                                             'required_packages.txt'),
                                packages_file))
            local('pip install -r %s' % packages_file)
            project_root = os.path.join(source_path, name)
            local('mkdir %s' % project_root)
            create_django_project(name, project_root)
            create_uwsgi_files(name, ve_path)
            init_git_repository(source_path)
            manage_py_path = os.path.join(source_path, name, 'manage.py')
            local_settings_path = os.path.join(source_path, name, name,
                                               'settings', 'local.py')
            db_type_class = select_db_type()
            if db_type_class:
                db_type = db_type_class()
                if not os.path.exists(db_type.executable_path):
                    print 'Database executable not found. Skipping DB creation part.'
                    django_db_config = generate_django_db_config(db_type.engine)
                    local('echo "%s" >> %s' % (django_db_config,
                                               local_settings_path))
                else:
                    installed_packages = file(packages_file).read()
                    package_list_updated = False
                    for package in db_type.required_packages:
                        if package not in installed_packages:
                            local('echo "%s" >> %s' % (package, packages_file))
                            package_list_updated = True
                    if package_list_updated:
                        local('pip install -r %s' % packages_file)
                    password = db_type.create_db_and_user(name)
                    if password:
                        django_db_config = generate_django_db_config(db_type.engine,
                                                                name, name,
                                                                password)
                        local('echo "%s" >> %s' % (django_db_config,
                                                   local_settings_path))
                        grant = db_type.grant_privileges(name, name)
                        if grant:
                            local('python %s syncdb' % manage_py_path)
                        else:
                            print 'Unable to grant DB privileges'
                            exit(1)
                    else:
                        print ('Unable to complete DB/User creation.'
                               'Skipping DB settings update.')
                        local('echo "%s" >> %s' % (generate_django_db_config(db_type.engine),
                                                   local_settings_path))
            else:
                local('echo "%s" >> %s' % (generate_django_db_config(),
                                               local_settings_path))
            local('python %s collectstatic --noinput' % manage_py_path)
                run('python manage.py collectstatic --noinput')
    uwsgi_conf_name = '%s.%s.uwsgi' % (project_name, env_type)
    sudo('initctl reload-configuration')
    with settings(warn_only=True):
        result = sudo('initctl restart %s' % uwsgi_conf_name)
        if result.failed:
            result = sudo('initctl start %s' % uwsgi_conf_name)
            if result.failed:
                print 'Failed to restart/start job %s' % uwsgi_conf_name
    sudo('/etc/init.d/nginx restart')


def deploy_project(project_name, env_type, repo):
    """ Deploys project to remote server, requires project name, environment
    type(development/production) and the repository of the project """
    create_virtual_env(project_name)
    with cd(project_name):
        run('mkdir %s' % SOURCE_DIRECTORY_NAME)
        with cd(SOURCE_DIRECTORY_NAME):
            run('git clone %s .' % repo)
            source_path = run('pwd')
            nginx_uwsgi_conf_name = '%s.production.nginx.uwsgi' % project_name
            uwsgi_conf_name = '%s.production.uwsgi' % project_name
            with settings(warn_only=True):
                sudo('ln -s %s.conf /etc/nginx/sites-enabled/' % os.path.join(source_path,
                                                                         nginx_uwsgi_conf_name))
                sudo('ln -s %s.conf /etc/init/' % os.path.join(source_path,
                                                           uwsgi_conf_name))
            local_settings_path = os.path.join(source_path, project_name,
                                               project_name, 'settings',
                                               'local.py')