def generate_local_config(name, local_settings_path):
    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.'
            return False
        else:
            password = db_type.create_db_and_user(name)
            if password:
                django_db_config = generate_django_db_config(db_type.engine,
                                                        name, name,
                                                        password)
                run('echo "%s" >> %s' % (django_db_config,
                                           local_settings_path))
                grant = db_type.grant_privileges(name, name)
                if grant:
                    return True
                else:
                    print 'Unable to grant DB privileges'
                    return False
            else:
                print ('Unable to complete DB/User creation.'
                       'Skipping DB settings update.')
                return False
    else:
        print 'No database selected, skipping DB settings update'
        return True
def setup_server(local=False):
    """ WARNING: under development """
    with settings(warn_only=True):
        sudo('apt-get update')
    add_os_package(' '.join(REQUIRED_SYSTEM_PACKAGES))
    server_setup_info = ['-'*80, 'Server setup for %s' % env.host]
    #if not local:
    #    password = add_user(PRODUCTION_USER, True)
    #    if password:
    #        server_setup_info.append('www user password: %s' % password)
    db_type_class = select_db_type()
    if db_type_class:
        db = db_type_class()
        db_password = db.install()
        if db_password:
            server_setup_info.append('Database Root Password: %s' % db_password)
    sudo('reboot') # FIX ME: add check for is reboot required
    print '\n'.join(server_setup_info)
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)