Skip to content

Instantly share code, notes, and snippets.

@xtornasol512
Created May 24, 2018 20:15
Show Gist options
  • Save xtornasol512/b5ddcae466d7db040d5ca27fd0cfb9b8 to your computer and use it in GitHub Desktop.
Save xtornasol512/b5ddcae466d7db040d5ca27fd0cfb9b8 to your computer and use it in GitHub Desktop.
New Vagrant Project Files
#!/usr/bin/env bash
echo "=> Start config box..."
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y build-essential libssl-dev wget python-pip python-dev libffi-dev
sudo pip install -U pip
# Install PostgreSQL
echo "Installing postgresql"
sudo apt-get install -y libpq-dev postgresql postgresql-contrib libxml2-dev libxslt1-dev zlib1g-dev build-essential libssl-dev libffi-dev
# Create database if not exist
sudo -i -u postgres psql -tc "SELECT 1 FROM pg_database WHERE datname = 'mydb'" | grep -q 1 || sudo -i -u postgres psql -c "CREATE DATABASE mydb"
# Create user
echo "Creating user and granting privileges"
sudo -i -u postgres psql -c "CREATE USER vagrant WITH PASSWORD 'vagrant';"
sudo -i -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE mydb TO vagrant;"
echo "Install Python 3"
sudo apt-get install -y python3
echo "Install Python 3 dev"
sudo apt-get install -y python3-dev
echo "Install pip3"
sudo apt-get install -y python3-pip
echo "setuptools"
sudo pip3 install -vU setuptools
echo "Update pip3 lib"
sudo python3 -m pip install -U pip
echo "Install pip3 requirements"
sudo pip3 install -r /vagrant/requirements.txt
echo "=> End config box..."
# -*- mode: django -*-
# vi: set ft=python :
# Module to know the host platform
# Based on BernardoSilva's answer in http://stackoverflow.com/questions/26811089/vagrant-how-to-have-host-platform-specific-provisioning-steps
module OS
def OS.mac?
(/darwin/ =~ RUBY_PLATFORM) != nil
end
def OS.linux?
not OS.mac?
end
end
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = '2'
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = 'debian/stretch64'
# Open ports:
# 5432 - Postgres
# 6379 - Redis
# 8000 - Django
[5432, 6379, 8000].each do |p|
config.vm.network :forwarded_port, guest: p, host: p
end
# NFS ---- NFS improves speed of VM if supported by your OS
# It does not work with encrypted volumes
# Linux Need a plugin for Virtualbox to shared files `vagrant plugin install vagrant-vbguest`
if OS.linux?
puts "Vagrant launched from linux."
config.vm.synced_folder '.', '/vagrant', type: 'virtualbox'
elsif OS.mac?
puts "Vagrant launched from mac."
config.vm.network 'private_network', ip: '192.168.50.4'
config.vm.synced_folder '.', '/vagrant', type: 'nfs'
end
# Provider-specific configuration so you can fine-tune various
# backing providers for rant. These expose provider-specific options.
config.vm.provider 'virtualbox' do |vb|
vb.customize ['modifyvm', :id, '--memory', '2048']
end
# Provision application
config.vm.provision "shell", privileged: false, run: "always", path: "config/vagrantvars.sh"
config.vm.provision "shell", privileged: false, run: "always", path: "bin/config_box.sh"
end
#!/bin/bash
echo "-------- Setting environmental variables into ~/.profile..."
source ~/.profile
# DEBUG_STATE
if [ -z "$DEBUG_STATE" ] || [ "$DEBUG_STATE" != True ]; then
echo "export DEBUG_STATE=True" >> ~/.profile
fi
# ALLOWED_HOSTS
if [ -z "$ALLOWED_HOSTS" ] || [ "$ALLOWED_HOSTS" != '*' ]; then
echo "export ALLOWED_HOSTS='*'" >> ~/.profile
fi
# PRODUCTION
if [ -z "$PRODUCTION" ] || [ "$PRODUCTION" != False ]; then
echo "export PRODUCTION=False" >> ~/.profile
fi
# SECRET_KEY
if [ -z "$SECRET_KEY" ] || [ "$SECRET_KEY" != "di9%t#g-wfmf-mghbwz@$-4x3fy)30v!=vbwynpc@iid#-*qcf" ]; then
echo "export SECRET_KEY='di9%t#g-wfmf-mghbwz@$-4x3fy)30v!=vbwynpc@iid#-*qcf'" >> ~/.profile
fi
# DATABASE_URL
if [ -z "$DATABASE_URL" ] || [ "$DATABASE_URL" != 'postgres://vagrant:vagrant@localhost/mydb' ]; then
echo "export DATABASE_URL='postgres://vagrant:vagrant@localhost/mydb'" >> ~/.profile
fi
# DJANGO_SETTINGS_MODULE
if [ -z "$DJANGO_SETTINGS_MODULE" ] || [ "$DJANGO_SETTINGS_MODULE" != 'django_project_name.settings' ]; then
echo "export DJANGO_SETTINGS_MODULE='django_project_name.settings'" >> ~/.profile
fi
echo "DONE"
@xtornasol512
Copy link
Author

Can use a requirements.txt
Can use db mydb or remove that lines

@xtornasol512
Copy link
Author

xtornasol512 commented May 24, 2018

Example to use it
mkdir new_project/
cd new_project
copy your files here
new_project/VagrantFile
new_project/config/vagrantvars.sh
new_project/bin/config_box.sh

And run with
vagrant up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment