Skip to content

Commit

Permalink
update permissions so user "vagrant" can run all commands. Fix usability
Browse files Browse the repository at this point in the history
  • Loading branch information
ccurtin committed Oct 10, 2016
1 parent 6bd24f7 commit db6a2f8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 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`
Create contained environments within the VM via `init_python_env`

## Installation
#### Prerequisites
Expand All @@ -13,17 +13,17 @@ Create contained environments within the VM via `. init_python_env`

#### Logging Into Your VM
- `vagrant up` to setup and initialize the VM. ( only the first time you run `vagrant up` will take long 5-10 mins )
- After the VM is initialized, run `vagrant ssh` and then `sudo su` to login as the root user.
- After the VM is initialized, run `vagrant ssh`
- change the current directory to your projects root by typing `cd $WORKON_HOME`

#### Create a Contained Python Environment
`. init_python_env` **(Notice the preceding period!)** Django install is optional.
`init_python_env`. Installing Django is optional.

#### Setup PgAdmin Web interface (phpPgAdmin)
`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. ** CURRENTLY ONLY SUPPORTS POSTGRESQL ** More DBMS will be added in the future.

## Notes to User:
- The default settings will run Django on port 80
Expand All @@ -33,13 +33,13 @@ Create contained environments within the VM via `. init_python_env`
- Defaults to latest stable version PostgreSQL
- Default settings run Apache on port 8080, needed for phpPgAdmin web interface.
- To change the port(s) Apache runs on edit the following:
- `vim /etc/apache2/ports.conf`
- `vim /etc/apache2/sites-available/000-default.conf`
- `sudo vim /etc/apache2/ports.conf`
- `sudo vim /etc/apache2/sites-available/000-default.conf`
- Then restart Apache `sudo /etc/init.d/apache2 restart`

## Usage
- VirtualEnv is _not_ a VM container, it is simply to create self-contained python environments. Think of it as a sandbox, not a full fledged VM. Plus, we already have the VM!
- `cd` into the synced_folder and run the command `. init_python_env` to create a new Python Environment so projects/packages are contained. **All python environments will be initialized in the synced_folder (`/vagrant/www/` by default). Notice the preceding period.**
- `cd` into the synced_folder and run the command `init_python_env` to create a new Python Environment so projects/packages are contained. **All python environments will be initialized in the synced_folder (`/vagrant/www/` by default).**
- Be aware that self-contained Python Environments does ***NOT*** mean self-contained Database Environments. Future releases may take this into account through porting.
- Run `python -V` and `django-admin --version` to make sure everything checked out.
- run `deactivate` to exit virtualenv environment or `workon [PROJECT_NAME]` to activate it. Alternatively, whenever you navigate into a project folder, the virtual environment will become activated.
Expand Down
3 changes: 2 additions & 1 deletion bootstrap/manage_django_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ function check_module() {
# for formatting the settings.py file.
check_module autopep8

PS3="Select A Database Engine to Use: "
echo -e "${BYELLOW}Select A Database Engine to Use: ${NIL}"
PS3="Enter a corresponding number: "

select db_engine in postgresql mysql sqlite3 oracle SKIP
do
Expand Down
36 changes: 24 additions & 12 deletions bootstrap/manage_django_db_postgres.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
source /bin/colors

source /etc/bash.bashrc
### CHECK PYTHON MODULES ARE INSTALLED ###
function check_module(){
python -c "import ${1}" 2> /dev/null
Expand Down Expand Up @@ -33,6 +33,7 @@ function check_package(){
function change_your_dir(){

if [ ! -f $PWD/settings.py ]; then
echo -e "${BYELLOW} Let's update the Django Settings File...${NIL}"
echo -e ${RED}
echo -e " Select a project's ${BRED}main app folder${RED} first!\n ${YELLOW}(where the ${BWHITE}settings.py${YELLOW} file lives.${NIL})\n"
echo -e ${NIL}
Expand All @@ -48,7 +49,14 @@ function change_your_dir(){
function make_user(){
echo "Enter a password for $NEW_DB_USER: "
read -s NEW_DB_PASS
sudo -u postgres psql -tAc "CREATE USER $NEW_DB_USER WITH PASSWORD '$NEW_DB_PASS';"
echo "Enter it again: "
read -s NEW_DB_PASS_CONFIRM
if [ $NEW_DB_PASS != $NEW_DB_PASS_CONFIRM ]; then
echo -e "${BRED}passwords do not match!${NIL}"
make_user
else
sudo -u postgres psql -tAc "CREATE USER $NEW_DB_USER WITH PASSWORD '$NEW_DB_PASS';"
fi
}

function assign_privs(){
Expand Down Expand Up @@ -80,13 +88,17 @@ function configure_md5_login(){
sudo -u postgres psql -tAc "\password postgres"
sudo sed -i "s/\s*local\s*all\s*all\s*peer/local all all md5/" /etc/postgresql/*/main/pg_hba.conf
sudo service postgresql restart
sudo sh -c ' echo "export POSTGRES_PASS=configured" >> /etc/bash.bashrc'
exec $SHELL
}

function update_app_settings(){

change_your_dir
continue_update_app_settings ${1}
configure_md5_login
if [ -z "$POSTGRES_PASS" ]; then
configure_md5_login
fi
}

### USER INPUT METHODS ###
Expand All @@ -102,31 +114,31 @@ function update_data(){
echo -e ${BGREEN}using ALIAS: $DB_ALIAS ${NIL}
;;
engine)
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'ENGINE':/{s/\s\+'ENGINE':.*/'ENGINE': 'django.db.backends.postgresql',/g}; t loop; /}/{s/\s\+}.*/'ENGINE': 'django.db.backends.postgresql',\\n },/}; t loop; b moop} ;b loop}" settings.py
sudo sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'ENGINE':/{s/\s\+'ENGINE':.*/'ENGINE': 'django.db.backends.postgresql',/g}; t loop; /}/{s/\s\+}.*/'ENGINE': 'django.db.backends.postgresql',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
name)
DB_NAME=${2}
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'NAME':/{s/\s\+'NAME':.*/'NAME': '${2}',/g}; t loop; /}/{s/\s\+}.*/'NAME': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
sudo sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'NAME':/{s/\s\+'NAME':.*/'NAME': '${2}',/g}; t loop; /}/{s/\s\+}.*/'NAME': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
user)
DB_USER=${2}
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'USER':/{s/\s\+'USER':.*/'USER': '${2}',/g}; t loop; /}/{s/\s\+}.*/'USER': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
sudo sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'USER':/{s/\s\+'USER':.*/'USER': '${2}',/g}; t loop; /}/{s/\s\+}.*/'USER': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
password)
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'PASSWORD':/{s/\s\+'PASSWORD':.*/'PASSWORD': '${2}',/g}; t loop; /}/{s/\s\+}.*/'PASSWORD': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
sudo sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'PASSWORD':/{s/\s\+'PASSWORD':.*/'PASSWORD': '${2}',/g}; t loop; /}/{s/\s\+}.*/'PASSWORD': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
host)
DB_HOST=${2}
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'HOST':/{s/\s\+'HOST':.*/'HOST': '${2}',/g}; t loop; /}/{s/\s\+}.*/'HOST': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
sudo sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'HOST':/{s/\s\+'HOST':.*/'HOST': '${2}',/g}; t loop; /}/{s/\s\+}.*/'HOST': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
port)
DB_PORT=${2}
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'PORT':/{s/\s\+'PORT':.*/'PORT': '${2}',/g}; t loop; /}/{s/\s\+}.*/'PORT': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
sudo sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'PORT':/{s/\s\+'PORT':.*/'PORT': '${2}',/g}; t loop; /}/{s/\s\+}.*/'PORT': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
esac
Expand All @@ -143,13 +155,13 @@ function continue_update_app_settings(){
read -e -i 'default' -p 'Enter the database ALIAS youd like to edit: ' THE_ALIAS

# output to a temp file
sed -n '/DATABASES*/,/# Password validation/p' settings.py >> tmp_alias_generator.txt
TEST_ALIAS=$(sudo sed -n '/DATABASES*/,/# Password validation/p' settings.py)
# check if exact match exists.
grep -Fxq " '$THE_ALIAS': {" tmp_alias_generator.txt
echo "$TEST_ALIAS" | grep -q " '$THE_ALIAS': {"
ALIAS_EXISTS=$?
# create if it doesnt exist
if [ $ALIAS_EXISTS == 1 ]; then
sed -i "/DATABASES = {/{s/.*/DATABASES = {\n'$THE_ALIAS': {\n},/}" settings.py
sudo sed -i "/DATABASES = {/{s/.*/DATABASES = {\n'$THE_ALIAS': {\n},/}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py

fi
Expand Down
8 changes: 4 additions & 4 deletions bootstrap/setup_phppgadmin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ source /bin/colors
# INSTALL PHPPGADMIN
sudo apt-get install phppgadmin -y
# configure Apache server to tell it where to find phppgadmin.
sudo echo 'Include /etc/apache2/conf.d/phppgadmin' >> /etc/apache2/apache2.conf
echo 'Include /etc/apache2/conf.d/phppgadmin'| sudo tee --append /etc/apache2/apache2.conf
# allow permission to access phppgadmin.
sed -i 's/^allow from 127.0.0.0\/255.0.0.0 ::1\/128/# allow from 127.0.0.0\/255.0.0.0 ::1\/128/' /etc/apache2/conf.d/phppgadmin
sed -i 's/^#allow from all/allow from all/' /etc/apache2/conf.d/phppgadmin
sed -i 's/^# allow from all/allow from all/' /etc/apache2/conf.d/phppgadmin
sudo sed -i 's/^allow from 127.0.0.0\/255.0.0.0 ::1\/128/# allow from 127.0.0.0\/255.0.0.0 ::1\/128/' /etc/apache2/conf.d/phppgadmin
sudo sed -i 's/^#allow from all/allow from all/' /etc/apache2/conf.d/phppgadmin
sudo sed -i 's/^# allow from all/allow from all/' /etc/apache2/conf.d/phppgadmin
sudo service apache2 reload
# enable user "postgres" to login
sudo sed -i "s/\s*\$conf\['extra_login_security'\] = true;/ \$conf\['extra_login_security'\] = false;/" /etc/phppgadmin/config.inc.php
Expand Down

0 comments on commit db6a2f8

Please sign in to comment.