Simplify bash conditionals

This commit is contained in:
Marc Aymerich 2015-09-20 10:57:13 +00:00
parent 912ee9744e
commit f368f5dbe8
13 changed files with 33 additions and 34 deletions

View File

@ -130,11 +130,12 @@ class MailmanBackend(MailmanVirtualDomainBackend):
self.append(textwrap.dedent("""\
# Create list alias for custom domain
aliases='%(aliases)s'
if [[ ! $(grep '\s\s*%(name)s\s*$' %(virtual_alias)s) ]]; then
if ( ! grep '\s\s*%(name)s\s*$' %(virtual_alias)s ); then
echo "${aliases}" >> %(virtual_alias)s
UPDATED_VIRTUAL_ALIAS=1
else
if [[ $(grep -E '^\s*(%(address_name)s|%(name)s)@%(address_domain)s\s\s*%(name)s\s*$' %(virtual_alias)s|wc -l) -ne %(num_entries)s ]]; then
existing=$(grep -E '^\s*(%(address_name)s|%(name)s)@%(address_domain)s\s\s*%(name)s\s*$' %(virtual_alias)s|wc -l)
if [[ $existing -ne %(num_entries)s ]]; then
sed -i -e '/^.*\s%(name)s\(%(suffixes_regex)s\)\s*$/d' \\
-e 'N; /^\s*\\n\s*$/d; P; D' %(virtual_alias)s
echo "${aliases}" >> %(virtual_alias)s
@ -149,7 +150,7 @@ class MailmanBackend(MailmanVirtualDomainBackend):
else:
self.append(textwrap.dedent("""\
# Cleanup possible ex-custom domain
if [[ ! $(grep '\s\s*%(name)s\s*$' %(virtual_alias)s) ]]; then
if ( ! grep '\s\s*%(name)s\s*$' %(virtual_alias)s ); then
sed -i "/^.*\s%(name)s\s*$/d" %(virtual_alias)s
fi""") % context
)

View File

@ -27,7 +27,7 @@ class SieveFilteringMixin(object):
su %(user)s --shell /bin/bash << 'EOF'
mkdir -p "%(maildir)s/.%(box)s"
EOF
if [[ ! $(grep '%(box)s' %(maildir)s/subscriptions) ]]; then
if ( ! grep '%(box)s' %(maildir)s/subscriptions ); then
echo '%(box)s' >> %(maildir)s/subscriptions
chown %(user)s:%(user)s %(maildir)s/subscriptions
fi
@ -68,7 +68,7 @@ class UNIXUserMaildirBackend(SieveFilteringMixin, ServiceController):
context = self.get_context(mailbox)
self.append(textwrap.dedent("""
# Update/create %(user)s user state
if [[ $( id %(user)s ) ]]; then
if ( id %(user)s ); then
old_password=$(getent shadow %(user)s | cut -d':' -f2)
usermod %(user)s \\
--shell %(initial_shell)s \\
@ -100,7 +100,7 @@ class UNIXUserMaildirBackend(SieveFilteringMixin, ServiceController):
su %(user)s --shell /bin/bash << 'EOF'
mkdir -p %(maildir)s
EOF
if [[ ! -f %(maildir)s/maildirsize ]]; then
if [ ! -f %(maildir)s/maildirsize ]; then
echo "%(quota)iS" > %(maildir)s/maildirsize
chown %(user)s:%(group)s %(maildir)s/maildirsize
else
@ -148,7 +148,7 @@ class DovecotPostfixPasswdVirtualUserBackend(SieveFilteringMixin, ServiceControl
def set_user(self, context):
self.append(textwrap.dedent("""
if [[ $( grep '^%(user)s:' %(passwd_path)s ) ]]; then
if ( grep '^%(user)s:' %(passwd_path)s ); then
sed -i 's#^%(user)s:.*#%(passwd)s#' %(passwd_path)s
else
echo '%(passwd)s' >> %(passwd_path)s
@ -159,7 +159,7 @@ class DovecotPostfixPasswdVirtualUserBackend(SieveFilteringMixin, ServiceControl
def set_mailbox(self, context):
self.append(textwrap.dedent("""
if [[ ! $(grep '^%(user)s@%(mailbox_domain)s\s' %(virtual_mailbox_maps)s) ]]; then
if ( ! grep '^%(user)s@%(mailbox_domain)s\s' %(virtual_mailbox_maps)s ); then
echo "%(user)s@%(mailbox_domain)s\tOK" >> %(virtual_mailbox_maps)s
UPDATED_VIRTUAL_MAILBOX_MAPS=1
fi""") % context
@ -252,7 +252,7 @@ class PostfixAddressVirtualDomainBackend(ServiceController):
if domain.name != context['local_domain'] and self.is_hosted_domain(domain):
self.append(textwrap.dedent("""
# %(domain)s is a virtual domain belonging to this server
if [[ ! $(grep '^\s*%(domain)s\s*$' %(virtual_alias_domains)s) ]]; then
if ( ! grep '^\s*%(domain)s\s*$' %(virtual_alias_domains)s) ); then
echo '%(domain)s' >> %(virtual_alias_domains)s
UPDATED_VIRTUAL_ALIAS_DOMAINS=1
fi""") % context
@ -266,7 +266,7 @@ class PostfixAddressVirtualDomainBackend(ServiceController):
if self.is_last_domain(domain):
self.append(textwrap.dedent("""
# Delete %(domain)s virtual domain
if [[ $(grep '^%(domain)s\s*$' %(virtual_alias_domains)s) ]]; then
if ( grep '^%(domain)s\s*$' %(virtual_alias_domains)s ); then
sed -i '/^%(domain)s\s*/d' %(virtual_alias_domains)s
UPDATED_VIRTUAL_ALIAS_DOMAINS=1
fi""") % context
@ -326,13 +326,13 @@ class PostfixAddressBackend(PostfixAddressVirtualDomainBackend):
self.append(textwrap.dedent("""
# Set virtual alias entry for %(email)s
LINE='%(email)s\t%(destination)s'
if [[ ! $(grep '^%(email)s\s' %(virtual_alias_maps)s) ]]; then
if ( ! grep '^%(email)s\s' %(virtual_alias_maps)s ); then
# Add new line
echo "${LINE}" >> %(virtual_alias_maps)s
UPDATED_VIRTUAL_ALIAS_MAPS=1
else
# Update existing line, if needed
if [[ ! $(grep "^${LINE}$" %(virtual_alias_maps)s) ]]; then
if ( ! grep "^${LINE}$" %(virtual_alias_maps)s ); then
sed -i "s/^%(email)s\s.*$/${LINE}/" %(virtual_alias_maps)s
UPDATED_VIRTUAL_ALIAS_MAPS=1
fi
@ -352,7 +352,7 @@ class PostfixAddressBackend(PostfixAddressVirtualDomainBackend):
def exclude_virtual_alias_maps(self, context):
self.append(textwrap.dedent("""
# Remove %(email)s virtual alias entry
if [[ $(grep '^%(email)s\s' %(virtual_alias_maps)s) ]]; then
if ( grep '^%(email)s\s' %(virtual_alias_maps)s ); then
sed -i '/^%(email)s\s/d' %(virtual_alias_maps)s
UPDATED_VIRTUAL_ALIAS_MAPS=1
fi""") % context

View File

@ -51,8 +51,8 @@ def get_instance_url(operation):
try:
url = change_url(operation.instance)
except NoReverseMatch:
return _("Deleted {0}").format(operation.instance_repr or '-'.join(
(escape(operation.content_type), escape(operation.object_id))))
alt_repr = '%s-%i' % (operation.content_type, operation.object_id)
return _("Deleted {0}").format(operation.instance_repr or alt_repr)
return orchestra_settings.ORCHESTRA_SITE_URL + url

View File

@ -3,6 +3,7 @@ import socket
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils.encoding import force_text
from django.utils.functional import cached_property
from django.utils.module_loading import autodiscover_modules
from django.utils.translation import ugettext_lazy as _
@ -107,11 +108,8 @@ class BackendLog(models.Model):
class BackendOperationQuerySet(models.QuerySet):
def create(self, **kwargs):
instance = kwargs.get('instance')
if instance and not instance.pk and 'instance_repr' not in kwargs:
try:
kwargs['instance_repr'] = str(instance)[:256]
except:
pass
if instance and 'instance_repr' not in kwargs:
kwargs['instance_repr'] = force_text(instance)[:256]
return super(BackendOperationQuerySet, self).create(**kwargs)

View File

@ -70,11 +70,11 @@ class SaaSWebTraffic(ServiceMonitor):
with open(access_log, 'r') as handler:
for line in handler.readlines():
line = line.split()
meta = line[:4]
host, __, __, date, tz = meta.split()
size, hostname = line[-2:]
host, __, __, date = line[:4]
if host in {ignore_hosts}:
continue
size, hostname = line[-2:]
hostname = hostname.replace('"', '')
try:
site = sites[hostname]
except KeyError:

View File

@ -19,10 +19,10 @@ class BSCWBackend(ServiceController):
def validate_creation(self, saas):
context = self.get_context(saas)
self.append(textwrap.dedent("""\
if [[ $(%(bsadmin)s register %(email)s) ]]; then
if ( %(bsadmin)s register %(email)s ); then
echo 'ValidationError: email-exists'
fi
if [[ $(%(bsadmin)s users -n %(username)s) ]]; then
if ( %(bsadmin)s users -n %(username)s ); then
echo 'ValidationError: user-exists'
fi""") % context
)
@ -31,7 +31,7 @@ class BSCWBackend(ServiceController):
context = self.get_context(saas)
if hasattr(saas, 'password'):
self.append(textwrap.dedent("""\
if [[ ! $(%(bsadmin)s register %(email)s) && ! $(%(bsadmin)s users -n %(username)s) ]]; then
if ( ! %(bsadmin)s register %(email)s && ! %(bsadmin)s users -n %(username)s ); then
# Create new user
%(bsadmin)s register -r %(email)s %(username)s '%(password)s'
else

View File

@ -37,7 +37,7 @@ class DokuWikiMuBackend(ServiceController):
)
if context['password']:
self.append(textwrap.dedent("""\
if [[ $(grep '^admin:' %(users_path)s) ]]; then
if ( grep '^admin:' %(users_path)s ); then
sed -i 's#^admin:.*$#admin:%(password)s:admin:%(email)s:admin,user#' %(users_path)s
else
echo 'admin:%(password)s:admin:%(email)s:admin,user' >> %(users_path)s

View File

@ -28,7 +28,7 @@ class DrupalMuBackend(ServiceController):
# the following assumes settings.php to be previously configured
REGEX='^\s*$databases\[.default.\]\[.default.\]\[.prefix.\]'
CONFIG='$databases[\'default\'][\'default\'][\'prefix\'] = \'%(app_name)s_\';'
if [[ ! $(grep $REGEX %(drupal_settings)s) ]]; then
if ( ! grep $REGEX %(drupal_settings)s ); then
echo $CONFIG >> %(drupal_settings)s
fi""") % context
)

View File

@ -83,7 +83,7 @@ class PhpListSaaSBackend(ServiceController):
context['escaped_crontab'] = context['crontab'].replace('$', '\\$')
self.append(textwrap.dedent("""\
# Configuring phpList crontabs
if [[ ! $(crontab -u %(user)s -l | grep 'phpList:"%(site_name)s"') ]]; then
if ( ! crontab -u %(user)s -l | grep 'phpList:"%(site_name)s"' ); then
cat << EOF | su %(user)s --shell /bin/bash -c 'crontab'
$(crontab -u %(user)s -l)

View File

@ -33,7 +33,7 @@ class UNIXUserBackend(ServiceController):
# TODO userd add will fail if %(user)s group already exists
self.append(textwrap.dedent("""
# Update/create user state for %(user)s
if [[ $( id %(user)s ) ]]; then
if ( id %(user)s ); then
usermod %(user)s --home %(home)s \\
--password '%(password)s' \\
--shell %(shell)s %(groups_arg)s
@ -60,7 +60,7 @@ class UNIXUserBackend(ServiceController):
if context['home'] != context['base_home']:
self.append(textwrap.dedent("""
# Set extra permissions: %(user)s home is inside %(mainuser)s home
if [[ $(mount | grep "^$(df %(home)s|grep '^/')\s" | grep acl) ]]; then
if ( mount | grep "^$(df %(home)s|grep '^/')\s" | grep acl ); then
# Accountn group as the owner
chown %(mainuser)s:%(mainuser)s %(home)s
chmod g+s %(home)s

View File

@ -32,7 +32,7 @@ class WebAppServiceMixin(object):
# Async wait 2 more seconds for other backends to lock app_path or cp under construction
nohup bash -c '
sleep 2
if [[ ! $(ls -A %(app_path)s) ]]; then
if ( ! ls -A %(app_path)s ); then
cp -r %(under_construction_path)s %(app_path)s
chown -R %(user)s:%(group)s %(app_path)s
fi' &> /dev/null &

View File

@ -168,7 +168,7 @@ class PHPBackend(WebAppServiceMixin, ServiceController):
}
if [[ $is_last -eq 1 ]]; then
if [[ $UPDATED_APACHE -eq 1 || "$state" =~ .*RESTART$ ]]; then
if [[ $(service apache2 status) ]]; then
if ( service apache2 status ); then
service apache2 reload
else
service apache2 start

View File

@ -158,7 +158,7 @@ class Apache2Backend(ServiceController):
}
if [[ $is_last -eq 1 ]]; then
if [[ $UPDATED_APACHE -eq 1 || "$state" =~ .*RESTART$ ]]; then
if [[ $(service apache2 status) ]]; then
if ( service apache2 status ); then
service apache2 reload
else
service apache2 start