From 03e78ca59390536a5cdbbba904aca616ca8267f5 Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Sun, 20 Sep 2015 11:35:22 +0000 Subject: [PATCH] Simplify bash conditionals --- orchestra/contrib/lists/backends.py | 4 ++-- orchestra/contrib/mailboxes/backends.py | 18 +++++++++--------- orchestra/contrib/saas/backends/bscw.py | 6 +++--- orchestra/contrib/saas/backends/dokuwikimu.py | 2 +- orchestra/contrib/saas/backends/drupalmu.py | 2 +- orchestra/contrib/saas/backends/phplist.py | 2 +- orchestra/contrib/systemusers/backends.py | 4 ++-- orchestra/contrib/webapps/backends/__init__.py | 4 ++-- orchestra/contrib/webapps/backends/php.py | 2 +- orchestra/contrib/websites/backends/apache.py | 2 +- 10 files changed, 23 insertions(+), 23 deletions(-) diff --git a/orchestra/contrib/lists/backends.py b/orchestra/contrib/lists/backends.py index 6a6bd7b4..d32ec796 100644 --- a/orchestra/contrib/lists/backends.py +++ b/orchestra/contrib/lists/backends.py @@ -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 ) diff --git a/orchestra/contrib/mailboxes/backends.py b/orchestra/contrib/mailboxes/backends.py index 7d6a93aa..232997bb 100644 --- a/orchestra/contrib/mailboxes/backends.py +++ b/orchestra/contrib/mailboxes/backends.py @@ -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 diff --git a/orchestra/contrib/saas/backends/bscw.py b/orchestra/contrib/saas/backends/bscw.py index 4a67c914..351f5276 100644 --- a/orchestra/contrib/saas/backends/bscw.py +++ b/orchestra/contrib/saas/backends/bscw.py @@ -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 diff --git a/orchestra/contrib/saas/backends/dokuwikimu.py b/orchestra/contrib/saas/backends/dokuwikimu.py index c172cd73..aa81c855 100644 --- a/orchestra/contrib/saas/backends/dokuwikimu.py +++ b/orchestra/contrib/saas/backends/dokuwikimu.py @@ -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 diff --git a/orchestra/contrib/saas/backends/drupalmu.py b/orchestra/contrib/saas/backends/drupalmu.py index 9d51aac9..d97132e4 100644 --- a/orchestra/contrib/saas/backends/drupalmu.py +++ b/orchestra/contrib/saas/backends/drupalmu.py @@ -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 ) diff --git a/orchestra/contrib/saas/backends/phplist.py b/orchestra/contrib/saas/backends/phplist.py index e8281556..d35cc2b0 100644 --- a/orchestra/contrib/saas/backends/phplist.py +++ b/orchestra/contrib/saas/backends/phplist.py @@ -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) diff --git a/orchestra/contrib/systemusers/backends.py b/orchestra/contrib/systemusers/backends.py index 0d109d23..7e11897a 100644 --- a/orchestra/contrib/systemusers/backends.py +++ b/orchestra/contrib/systemusers/backends.py @@ -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 diff --git a/orchestra/contrib/webapps/backends/__init__.py b/orchestra/contrib/webapps/backends/__init__.py index 4bdfba61..dfd60f34 100644 --- a/orchestra/contrib/webapps/backends/__init__.py +++ b/orchestra/contrib/webapps/backends/__init__.py @@ -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 & diff --git a/orchestra/contrib/webapps/backends/php.py b/orchestra/contrib/webapps/backends/php.py index 65b18454..849b7c8c 100644 --- a/orchestra/contrib/webapps/backends/php.py +++ b/orchestra/contrib/webapps/backends/php.py @@ -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 diff --git a/orchestra/contrib/websites/backends/apache.py b/orchestra/contrib/websites/backends/apache.py index 0e033535..e019a203 100644 --- a/orchestra/contrib/websites/backends/apache.py +++ b/orchestra/contrib/websites/backends/apache.py @@ -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