Skip to content

Commit fbbeae2

Browse files
committed
Made an easy way to run the example project using vagrant
To run you need * virtualbox * vagrant * fabric * fabtool Then type vagrant up ./provision_vagrant.sh
1 parent c461f23 commit fbbeae2

File tree

6 files changed

+110
-3
lines changed

6 files changed

+110
-3
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ VERSION
66
_build/
77
dist/
88
django_tenants.egg-info
9-
.idea/
9+
.idea/
10+
.vagrant

Vagrantfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
Vagrant.configure("2") do |config|
5+
config.vm.box = "django_tenants"
6+
config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-i386-vagrant-disk1.box"
7+
config.vm.network :forwarded_port, guest: 8080, host: 8080
8+
9+
end

examples/tenant_tutorial/tenant_tutorial/settings.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
'default': {
2121
'ENGINE': 'django_tenants.postgresql_backend', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
2222
'NAME': 'tenant_tutorial', # Or path to database file if using sqlite3.
23-
'USER': 'postgres',
24-
'PASSWORD': 'root',
23+
'USER': 'tenant_tutorial',
24+
'PASSWORD': 'qwerty',
2525
'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
2626
'PORT': '', # Set to empty string for default.
2727
}

fabfile.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from fabric.decorators import task, hosts
2+
from fabric.api import env, run, sudo
3+
from fabtools.postgres import drop_database
4+
from fabtools.vagrant import vagrant
5+
from fabtools.deb import update_index
6+
import fabtools
7+
from fabric.context_managers import cd
8+
9+
10+
@task
11+
def vagrant():
12+
env.user = 'vagrant'
13+
env.hosts = ['127.0.0.1:2222']
14+
env.passwords = {'[email protected]:2222': 'vagrant'}
15+
env.psql_db = 'tenant_tutorial'
16+
env.psql_user = 'tenant_tutorial'
17+
env.psql_password = 'qwerty'
18+
env.backup_path = '/vagrant/database_backup/'
19+
env.user = 'vagrant'
20+
env.deploy_user = 'vagrant'
21+
env.passwords = {'[email protected]:2222': 'vagrant'}
22+
env.vagrant = True
23+
return env.hosts
24+
25+
26+
@task
27+
@hosts(['[email protected]:2222'])
28+
def provision_vagrant():
29+
vagrant()
30+
update_index()
31+
fabtools.require.postfix.server('example.com')
32+
create_pg_database()
33+
update_requirements()
34+
django_manage("migrate")
35+
django_migrate()
36+
37+
38+
@task
39+
@hosts(['[email protected]:2222'])
40+
def create_superuser():
41+
vagrant()
42+
django_manage("createsuperuser [email protected]", True)
43+
44+
45+
@task
46+
def django_manage(command):
47+
with cd("/vagrant/examples/tenant_tutorial/"):
48+
run("python manage.py %s" % command)
49+
50+
51+
52+
def update_requirements():
53+
fabtools.require.deb.packages(['python2.7',
54+
'python-virtualenv',
55+
'python-dev',
56+
'python-pip',
57+
'pkg-config',
58+
'postgresql-server-dev-9.3'])
59+
60+
61+
sudo("pip install psycopg2==2.6.1 django==1.9")
62+
63+
64+
65+
@task
66+
def create_pg_database():
67+
fabtools.require.postgres.server()
68+
fabtools.require.postgres.user(env.psql_user, env.psql_password, createdb=True)
69+
fabtools.require.postgres.database(env.psql_db, env.psql_user)
70+
71+
sudo("sed -i 's/all peer/"
72+
"all md5/g' /etc/postgresql/9.3/main/pg_hba.conf")
73+
sudo('service postgresql restart')
74+
75+
76+
@task
77+
def reset_database():
78+
sudo('service postgresql restart')
79+
try:
80+
drop_database(env.psql_db)
81+
except:
82+
pass
83+
create_pg_database()
84+
django_migrate()
85+
86+
87+
def django_migrate():
88+
django_manage("migrate_schemas")
89+
90+
91+
@task
92+
def create_tenant():
93+
django_manage("create_tenant")

provision_vagrant.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
fab provision_vagrant

vagrant_create_tenant.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
fab vagrant create_tenant

0 commit comments

Comments
 (0)