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("""\ self.append(textwrap.dedent("""\
# Create list alias for custom domain # Create list alias for custom domain
aliases='%(aliases)s' 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 echo "${aliases}" >> %(virtual_alias)s
UPDATED_VIRTUAL_ALIAS=1 UPDATED_VIRTUAL_ALIAS=1
else else
@ -150,7 +150,7 @@ class MailmanBackend(MailmanVirtualDomainBackend):
else: else:
self.append(textwrap.dedent("""\ self.append(textwrap.dedent("""\
# Cleanup possible ex-custom domain # 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 sed -i "/^.*\s%(name)s\s*$/d" %(virtual_alias)s
fi""") % context fi""") % context
) )

View File

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

View File

@ -19,10 +19,10 @@ class BSCWBackend(ServiceController):
def validate_creation(self, saas): def validate_creation(self, saas):
context = self.get_context(saas) context = self.get_context(saas)
self.append(textwrap.dedent("""\ self.append(textwrap.dedent("""\
if ( %(bsadmin)s register %(email)s ); then if %(bsadmin)s register %(email)s > /dev/null; then
echo 'ValidationError: email-exists' echo 'ValidationError: email-exists'
fi fi
if ( %(bsadmin)s users -n %(username)s ); then if %(bsadmin)s users -n %(username)s > /dev/null; then
echo 'ValidationError: user-exists' echo 'ValidationError: user-exists'
fi""") % context fi""") % context
) )
@ -31,7 +31,7 @@ class BSCWBackend(ServiceController):
context = self.get_context(saas) context = self.get_context(saas)
if hasattr(saas, 'password'): if hasattr(saas, 'password'):
self.append(textwrap.dedent("""\ 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 # Create new user
%(bsadmin)s register -r %(email)s %(username)s '%(password)s' %(bsadmin)s register -r %(email)s %(username)s '%(password)s'
else else

View File

@ -37,7 +37,7 @@ class DokuWikiMuBackend(ServiceController):
) )
if context['password']: if context['password']:
self.append(textwrap.dedent("""\ 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 sed -i 's#^admin:.*$#admin:%(password)s:admin:%(email)s:admin,user#' %(users_path)s
else else
echo 'admin:%(password)s:admin:%(email)s:admin,user' >> %(users_path)s 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 # the following assumes settings.php to be previously configured
REGEX='^\s*$databases\[.default.\]\[.default.\]\[.prefix.\]' REGEX='^\s*$databases\[.default.\]\[.default.\]\[.prefix.\]'
CONFIG='$databases[\'default\'][\'default\'][\'prefix\'] = \'%(app_name)s_\';' 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 echo $CONFIG >> %(drupal_settings)s
fi""") % context fi""") % context
) )

View File

@ -83,7 +83,7 @@ class PhpListSaaSBackend(ServiceController):
context['escaped_crontab'] = context['crontab'].replace('$', '\\$') context['escaped_crontab'] = context['crontab'].replace('$', '\\$')
self.append(textwrap.dedent("""\ self.append(textwrap.dedent("""\
# Configuring phpList crontabs # 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' cat << EOF | su %(user)s --shell /bin/bash -c 'crontab'
$(crontab -u %(user)s -l) $(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 # TODO userd add will fail if %(user)s group already exists
self.append(textwrap.dedent(""" self.append(textwrap.dedent("""
# Update/create user state for %(user)s # Update/create user state for %(user)s
if ( id %(user)s ); then if id %(user)s ; then
usermod %(user)s --home %(home)s \\ usermod %(user)s --home %(home)s \\
--password '%(password)s' \\ --password '%(password)s' \\
--shell %(shell)s %(groups_arg)s --shell %(shell)s %(groups_arg)s
@ -60,7 +60,7 @@ class UNIXUserBackend(ServiceController):
if context['home'] != context['base_home']: if context['home'] != context['base_home']:
self.append(textwrap.dedent(""" self.append(textwrap.dedent("""
# Set extra permissions: %(user)s home is inside %(mainuser)s home # 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 # Accountn group as the owner
chown %(mainuser)s:%(mainuser)s %(home)s chown %(mainuser)s:%(mainuser)s %(home)s
chmod g+s %(home)s chmod g+s %(home)s

View File

@ -28,11 +28,11 @@ class WebAppServiceMixin(object):
if context['under_construction_path']: if context['under_construction_path']:
self.append(textwrap.dedent(""" self.append(textwrap.dedent("""
# Set under construction if needed # 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 # Async wait 2 more seconds for other backends to lock app_path or cp under construction
nohup bash -c ' nohup bash -c '
sleep 2 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 cp -r %(under_construction_path)s %(app_path)s
chown -R %(user)s:%(group)s %(app_path)s chown -R %(user)s:%(group)s %(app_path)s
fi' &> /dev/null & fi' &> /dev/null &

View File

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

View File

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