Skip to content

Commit

Permalink
automatically install correct python-dev packages for environments.
Browse files Browse the repository at this point in the history
Grabs `python --version`, isolates the #, and deducts until match found
  • Loading branch information
ccurtin committed Oct 11, 2016
1 parent 6483d13 commit ef0328a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Vagrant-Python-Django VM

A Vagrantfile utilizing Ubuntu 14.04/Trusty to get you started with self-contained Python/Django projects quickly via VirtualEnv.
Create contained environments within the VM via `init_python_env`
Get started with self-contained Python/Django projects quickly. Create contained environments within the VM via `init_python_env` and administer and swap between environments, projects and DBMS quickly and with ease!

**CURRENTLY ONLY SUPPORTS POSTGRESQL for DMBS**

## Installation
#### Prerequisites
Expand All @@ -23,9 +24,10 @@ Create contained environments within the VM via `init_python_env`
`setup_phppgadmin`

#### Setting Up a New Database For A Django Project
`manage_django_db`. **Switch to a Django Project Folder before running**. This will create a new user, alter their role, create a new database, and assign them to a DB. If you just want to assign roles and not create new users/DBs, that works too. Running this command will also automatically update the django `settings.py` file for your project. ** CURRENTLY ONLY SUPPORTS POSTGRESQL ** More DBMS will be added in the future.
`manage_django_db`. **Switch to a Django Project Folder before running**. This will create a new user, alter their role, create a new database, and assign them to a DB. If you just want to assign roles and not create new users/DBs, that works too. Running this command will also automatically update the django `settings.py` file for your project. More DBMS will be added in the future.

## Notes to User:
- Feel free to change up the base-box OS, but note this has only been tested with Ubuntu 14.04/Trusty.
- The default settings will run Django on port 80
- To change the port Django runs on just run: `python manage.py runserver [::]:YOUR_NEW_PORT` in a Django project and that port will be accessible via the `hostname` entered in the `config.yml` file.

Expand All @@ -49,11 +51,10 @@ Create contained environments within the VM via `init_python_env`
##### Smaller To Dos
- Let user define PostgreSQL port when running `manage_django_db`. Edit `sed` `/etc/postgresql/9.3/main/postgresql.conf` port=5432. Might want to create a sepatate shell file or method, to scan `/etc/postgresql/` for versions and ask which to update... `sed` return change before confirm and restart service.
- reconfigure `configure_md5_login` in `manage_django_db_postgres` shouldn't be automatically run, only once or manually.
- in `manage_django_db_postgres` when installing psycopg2, need to first automatically install the _correct_ version of the `python-dev` package. Note to self: grab `which python` and loop through versions, popping off version number until match is met. Use method `check_package`
- when selecting a DBMS for project, also accept a DBMS version number for each. eg: `"Select which engine you'd like to use: 1,2,4,5,6"; user selects #1 postgresql and then prompt them for version to install; create warnings if DMBS already installed.`
- don't force any ports for a DBMS'. Let user configure any ports in `config.yml`
- update PostgreSQL

- Organize the Installation section in the README for each individual DBMS. Have a "General Installation" then sub-headings for each DMBS, explaining extensions, commands, configurations, etc.

##### Bigger To Dos
- create a utility that installs necessary dependencies to run gulp/grunt tasks for Django Projects.
- also create task-runner templates for projects... `init_taskrunner` >>> `gulp`...running. Maybe best to package it all up into one command so no separate install takes place; just run prior checks when `init_taskrunner` or similar. Setup BrowserSync, PostCSS, Autoprefixer, SourceMaps, Uglify, etc.
Expand All @@ -76,10 +77,6 @@ Create contained environments within the VM via `init_python_env`
- to install these packages within a different environment: `pip install -r requirements.txt`
- run `vagrant share` on the HOST machine to share your project(s) with the world. For development and Q/A only. Be careful with sensitive data before proceeding. You can even use your own domain to share projects: http://projectname.yourwebsite.com

## Issues
- Be sure that Python and Django versions are compatible together when installing both at the same time.
- when running `startserver`,or `python manage.py runserver [::]:80`, **if you receive and error related to psycopg2**, make sure you install the python-dev for your **active** version of python, e.g `pytho3.4-dev`. After running `sudo apt-get install pythonX.X-dev`, install the psycopg2 python module via pip by making sure you virtualenv is active and entering `pip install psycopg2`.

Please [open an issue](https://github.com/ccurtin/vagrant-python-django/issues/new) for support.

## Contributing
Expand Down
30 changes: 24 additions & 6 deletions bootstrap/manage_django_db_postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ function check_module(){

### CHECK LINUX PACKAGES ARE INSTALLED ###
function check_package(){
# find package status with -s flag
# Note about BUG: using `-l` flag may return false-positives!
# find if a package is installed via the status flag `-s` instead.
dpkg -s ${1} &> /dev/null
INSTALLED=$?
if [ $INSTALLED == 1 ]; then
if [ ${1} == 'python-dev-AUTO' ]; then
install_python_dev
elif [ $INSTALLED == 1 ] && [ ${1} != 'python-dev-AUTO' ]; then
echo -e ${BYELLOW}
echo -e ${1} not found ${BGREEN}
echo installing ${1}...
Expand All @@ -29,6 +32,23 @@ function check_package(){
fi
}

function install_python_dev(){
# return active env python version. # eg: Python 3.4.3
FIND_PYTHON_VERSION=$(python --version 2>&1)
# isolate just the version #.
CURRENT_PYTHON_VERSION=$(echo $FIND_PYTHON_VERSION | sed -e 's/\<Python\>//g')
# since `python-dev` has many possibilities, define all, ie: 3.4, 3, and then fall back to 'python-dev' if no match found.
POSSIBLE_MATCHES=("python${CURRENT_PYTHON_VERSION::-2}-dev" "python${CURRENT_PYTHON_VERSION::-4}-dev" "python-dev")
# for each possibility, check_package along with return value, and exit once proper version is installed.
for FIND_PY_DEV in "${POSSIBLE_MATCHES[@]}"
do
check_package ${FIND_PY_DEV}
dpkg -s ${FIND_PY_DEV} &> /dev/null
if [ $? == 0 ]; then
break;
fi
done
}

function change_your_dir(){

Expand Down Expand Up @@ -266,10 +286,8 @@ fi
check_package postgresql-client-common
check_package libpq-dev
# is needed to compile Python extension written in C ot C++, ie: psycopg2
check_package python-dev
# USER MAY NEED A DIFFERENT VERSION OF python-dev, ie: 'python3.4-dev'
# JUST INSTALL THIS TOO FOR NOW! EVENTUALLY NEED TO FIX THIS.
check_package python3-dev
# triggers auto-detection via deductions of `python --version`
check_package python-dev-AUTO
check_module psycopg2
check_package python-psycopg2
# Setup user and privs,
Expand Down

0 comments on commit ef0328a

Please sign in to comment.