Simplify bash conditionals

This commit is contained in:
Marc Aymerich 2015-09-20 11:35:22 +00:00
parent f368f5dbe8
commit 03e78ca593
10 changed files with 23 additions and 23 deletions

View file

@ -130,7 +130,7 @@ 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 > /dev/null; then
echo "${aliases}" >> %(virtual_alias)s
UPDATED_VIRTUAL_ALIAS=1
else
@ -150,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 > /dev/null; 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 > /dev/null; 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 \\
@ -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 > /dev/null ; 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 > /dev/null; 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 > /dev/null; 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 > /dev/null; 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 > /dev/null; 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 > /dev/null; 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 > /dev/null; then
sed -i '/^%(email)s\s/d' %(virtual_alias_maps)s
UPDATED_VIRTUAL_ALIAS_MAPS=1
fi""") % context

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 > /dev/null; then
echo 'ValidationError: email-exists'
fi
if ( %(bsadmin)s users -n %(username)s ); then
if %(bsadmin)s users -n %(username)s > /dev/null; 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 > /dev/null; 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 > /dev/null; 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"' > /dev/null; 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 > /dev/null; then
# Accountn group as the owner
chown %(mainuser)s:%(mainuser)s %(home)s
chmod g+s %(home)s

View file

@ -28,11 +28,11 @@ class WebAppServiceMixin(object):
if context['under_construction_path']:
self.append(textwrap.dedent("""
# Set under construction if needed
if [[ $CREATED == 1 && ! $(ls -A %(app_path)s) ]]; then
if [[ $CREATED == 1 ]] && ! ls -A %(app_path)s > /dev/null; then
# 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 > /dev/null; 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 > /dev/null; 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 > /dev/null; then
service apache2 reload
else
service apache2 start