Skip to content

Commit

Permalink
Merge pull request #981 from fxdgear/develop
Browse files Browse the repository at this point in the history
Adding some support for running django management commands
  • Loading branch information
thatch45 committed Mar 21, 2012
2 parents 6d66a18 + 5162e39 commit 48c1e56
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 3 deletions.
132 changes: 132 additions & 0 deletions salt/modules/django.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import os

def _get_django_admin(bin_env):
if not bin_env:
da = 'django-admin.py'
else:
# try to get pip bin from env
if os.path.exists(os.path.join(bin_env, 'bin', 'django-admin.py')):
da = os.path.join(bin_env, 'bin', 'django-admin.py')
else:
da = bin_env
return da


def command(settings_module,
command,
bin_env=None,
pythonpath=None,
*args, **kwargs):
"""
run arbitrary django management command
"""
da = _get_django_admin(bin_env)
cmd = "{0} {1} --settings={2}".format(da, command, settings_module)

if pythonpath:
cmd = "{0} --pythonpath={1}".format(cmd, pythonpath)

for arg in args:
cmd = "{0} --{1}".format(cmd, arg)

for key, value in kwargs.iteritems():
cmd = '{0} --{1}={2}'.format(cmd, key, value)

return __salt__['cmd.run'](cmd)


def syncdb(settings_module,
bin_env=None,
migrate=False,
database=None,
pythonpath=None):
"""
run syncdb
if you have south installed, you can pass in the optional
``migrate`` kwarg and run the migrations after the syncdb
finishes.
"""
da = _get_django_admin(bin_env)
cmd = "{0} syncdb --settings={1}".format(da, settings_module)
if migrate:
cmd = "{0} --migrate".format(cmd)
if database:
cmd = "{0} --database={1}".format(cmd, database)
if pythonpath:
cmd = "{0} --pythonpath={1}".format(cmd, pythonpath)
return __salt__['cmd.run'](cmd)


def createsuperuser(settings_module,
username,
email,
bin_env=None,
database=None,
pythonpath=None):
"""
create a super user for the database.
this defaults to use the ``--noinput`` flag which will
not create a password for the superuser.
"""
da = _get_django_admin(bin_env)
cmd = "{0} createsuperuser --settings={1} --noinput --email='{2}' --username={3}".format(
da, settings_module, email, username)
if database:
cmd = "{0} --database={1}".format(cmd, database)
if pythonpath:
cmd = "{0} --pythonpath={1}".format(cmd, pythonpath)
return __salt__['cmd.run'](cmd)


def loaddata(settings_module,
fixtures,
bin_env=None,
database=None,
pythonpath=None):
"""
load fixture data
fixtures:
comma separated list of fixtures to load
"""
da = _get_django_admin(bin_env)
cmd = "{0} loaddata --settings={1} {2}".format(
da, settings_module, " ".join(fixtures.split(",")))
if database:
cmd = "{0} --database={1}".format(cmd, database)
if pythonpath:
cmd = "{0} --pythonpath={1}".format(cmd, pythonpath)
return __salt__['cmd.run'](cmd)


def collectstatic(settings_module,
bin_env=None,
no_post_process=False,
ignore=None,
dry_run=False,
clear=False,
link=False,
no_default_ignore=False,
pythonpath=None):

da = _get_django_admin(bin_env)
cmd = "{0} collectstatic --settings={1} --noinput".format(
da, settings_module)
if no_post_process:
cmd = "{0} --no-post-process".format(cmd)
if ignore:
cmd = "{0} --ignore=".format(cmd, ignore)
if dry_run:
cmd = "{0} --dry-run".format(cmd)
if clear:
cmd = "{0} --clear".format(cmd)
if link:
cmd = "{0} --link".format(cmd)
if no_default_ignore:
cmd = "{0} --no-default-ignore".format(cmd)
if pythonpath:
cmd = "{0} --pythonpath={1}".format(cmd, pythonpath)

return __salt__['cmd.run'](cmd)
5 changes: 2 additions & 3 deletions salt/modules/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,9 @@ def user_create(username,

if sub_cmd.endswith("WITH"):
sub_cmd = sub_cmd.replace(" WITH", "")

cmd = "psql -h {host} -U {user} -p {port} -c '{sub_cmd}'".format(
cmd = 'psql -h {host} -U {user} -p {port} -c "{sub_cmd}"'.format(
host=host, user=user, port=port, sub_cmd=sub_cmd)

return __salt__['cmd.run'](cmd)


Expand Down

0 comments on commit 48c1e56

Please sign in to comment.