0001-re-usable-tenant-deploy-command.patch
entrouvert/djommon/multitenant/management/commands/__init__.py | ||
---|---|---|
3 | 3 |
# Email: carneiro.be@gmail.com |
4 | 4 |
# License: MIT license |
5 | 5 |
# Home-page: http://github.com/bcarneiro/django-tenant-schemas |
6 | ||
7 |
import json |
|
8 |
import sys |
|
9 |
import urllib2 |
|
10 | ||
6 | 11 |
from optparse import make_option |
7 | 12 |
from django.conf import settings |
8 | 13 |
from django.core.management import call_command, get_commands, load_command_class |
... | ... | |
12 | 17 |
from django.utils.six.moves import input |
13 | 18 |
except ImportError: |
14 | 19 |
input = raw_input |
15 |
from tenant_schemas.utils import get_public_schema_name |
|
20 |
from tenant_schemas.utils import get_public_schema_name, tenant_context
|
|
16 | 21 |
from entrouvert.djommon.multitenant.middleware import TenantMiddleware |
17 | 22 | |
18 | 23 | |
... | ... | |
169 | 174 | |
170 | 175 |
def _notice(self, output): |
171 | 176 |
self.stdout.write(self.style.NOTICE(output)) |
177 | ||
178 |
class DeployCommand(BaseCommand): |
|
179 |
help = 'Deploy a tenant from hobo' |
|
180 | ||
181 |
def handle(self, base_url, **options): |
|
182 |
environment = json.load(sys.stdin) |
|
183 |
for service in environment['services']: |
|
184 |
if service['base_url'] == base_url: |
|
185 |
break |
|
186 |
else: |
|
187 |
raise CommandError('Service %s not found' % base_url) |
|
188 |
hostname = urllib2.urlparse.urlsplit(base_url).netloc |
|
189 |
print "HOSTNAME: %s" % hostname |
|
190 | ||
191 |
call_command('create_tenant', hostname) |
|
192 | ||
193 |
tenant = TenantMiddleware.get_tenant_by_hostname(hostname) |
|
194 |
with tenant_context(tenant): |
|
195 |
self.deploy_tenant(environment, service, options) |
|
196 | ||
197 |
def deploy_tenant(self, environment, service, options): |
|
198 |
pass |
entrouvert/djommon/multitenant/management/commands/deploy.py | ||
---|---|---|
1 |
import urllib2 |
|
2 |
import json |
|
3 |
import sys |
|
4 | ||
5 |
from django.core.management import call_command |
|
6 |
from django.core.management.base import BaseCommand, CommandError |
|
7 | ||
8 |
from tenant_schemas.utils import tenant_context |
|
9 |
from entrouvert.djommon.multitenant.middleware import TenantMiddleware |
|
10 | ||
11 |
class Command(BaseCommand): |
|
12 |
help = 'Deploy a tenant from hobo' |
|
13 | ||
14 |
def handle(self, base_url, **options): |
|
15 |
environment = json.load(sys.stdin) |
|
16 |
for service in environment['services']: |
|
17 |
if service['base_url'] == base_url: |
|
18 |
break |
|
19 |
else: |
|
20 |
raise CommandError('Service %s not found' % base_url) |
|
21 |
hostname = urllib2.urlparse.urlsplit(base_url).netloc |
|
22 | ||
23 |
call_command('create_tenant', hostname) |
|
24 | ||
25 |
tenant = TenantMiddleware.get_tenant_by_hostname(hostname) |
|
26 |
with tenant_context(tenant): |
|
27 |
self.deploy_tenant(environment, service, options) |
|
28 | ||
29 |
def deploy_tenant(self, environment, service, options): |
|
30 |
pass |
|
31 |
- |