Make feed item update optional
- SF_NOTIFIER_FEED_ENABLED env var
True by default for backward compatibility
- cleanup config setting and validation
- redirect uwsgi request logs to file to limit noise
- cleanup logging path setting
Change-Id: I1e40a83ce5aadb58f9908c586bc0b92e02a902a9
Related-PROD: PRODX-15636
diff --git a/entrypoint.sh b/entrypoint.sh
index fd12ae6..fc6325f 100755
--- a/entrypoint.sh
+++ b/entrypoint.sh
@@ -1,16 +1,15 @@
#!/bin/ash
export SIMPLE_SETTINGS=${SIMPLE_SETTINGS:-sf_notifier.settings.production}
-export SF_NOTIFIER_ALERT_ID_HASH_FUNC=${SF_NOTIFIER_ALERT_ID_HASH_FUNC:-sha256}
-export SF_NOTIFIER_ADD_LINKS=${SF_NOTIFIER_ADD_LINKS:-False}
+export LOGPATH=/var/log/sf-notifier/sfnotifier.log
WORKERS=${SF_NOTIFIER_WORKERS:-4}
BUFFER=${SF_NOTIFIER_BUFFER_SIZE:-32768}
PORT=${SF_NOTIFIER_APP_PORT:-5000}
-LOGPATH=/var/log/sf-notifier
-mkdir -p $LOGPATH
-chown -R 1000:1000 $LOGPATH
+mkdir -p `dirname $LOGPATH`
+touch $LOGPATH
+chown -R 1000:1000 `dirname $LOGPATH`
uwsgi -p ${WORKERS} \
--uid 1000 \
@@ -20,4 +19,5 @@
--callable app_dispatch \
--buffer-size=${BUFFER} \
--max-worker-lifetime 300 \
- --master
+ --master \
+ --req-logger=file:${LOGPATH}
diff --git a/sf_notifier/helpers.py b/sf_notifier/helpers.py
index 9715545..6965b70 100644
--- a/sf_notifier/helpers.py
+++ b/sf_notifier/helpers.py
@@ -66,3 +66,9 @@
fields.append(_format_description(alert, add_links))
fields.append(alert['labels'])
return fields, action
+
+
+def is_true(name):
+ if isinstance(name, str):
+ return name.lower() == 'true'
+ return name is True
diff --git a/sf_notifier/salesforce/client.py b/sf_notifier/salesforce/client.py
index 4403528..cf2f4c6 100644
--- a/sf_notifier/salesforce/client.py
+++ b/sf_notifier/salesforce/client.py
@@ -36,6 +36,8 @@
'organization_id': 'organizationId',
'environment_id': 'environment_id',
'sandbox_enabled': 'domain',
+ 'feed_enabled': 'feed_enabled',
+ 'hash_func': 'hash_func',
}
ALLOWED_HASHING = ('md5', 'sha256')
@@ -82,43 +84,47 @@
'sf_error_count': Counter('sf_error_count', 'sf-notifier'),
'sf_request_count': Counter('sf_request_count', 'sf-notifier')
}
- self.config = self._validate_config(config)
- self.hash_func = self._hash_func()
- self.environment = self.config.pop('environment_id')
self._registered_alerts = TTLCache(maxsize=2048, ttl=300)
+
+ self.config = self._validate_config(config)
+ self.hash_func = self._hash_func(self.config.pop('hash_func'))
+ self.feed_enabled = self.config.pop('feed_enabled')
+
+ self.environment = self.config.pop('environment_id')
self.sf = None
self.session = Session()
self.auth()
@staticmethod
- def _hash_func():
- name = os.environ.get('SF_NOTIFIER_ALERT_ID_HASH_FUNC', 'sha256')
+ def _hash_func(name):
if name in ALLOWED_HASHING:
return getattr(hashlib, name)
+ msg = ('Invalid hashing function "{}".'
+ 'Switching to default "sha256".').format(name)
+ logger.warn(msg)
return hashlib.sha256
@staticmethod
def _validate_config(config):
kwargs = {}
- for param, field in CONFIG_FIELD_MAP.items():
- setting_var = param.upper()
- env_var = 'SFDC_{}'.format(setting_var)
- kwargs[field] = os.environ.get(
- env_var, config.get(setting_var))
+ for param, value in config.items():
+ field = CONFIG_FIELD_MAP.get(param.lower())
+ if field is None:
+ env_var = 'SFDC_{}'.format(param)
+ msg = ('Invalid config: missing "{}" field or "{}" environment'
+ ' variable.').format(field, env_var)
+ logger.error(msg)
+ raise SfNotifierError(msg)
+
+ kwargs[field] = value
if field == 'domain':
- if kwargs[field] in ['true', 'True', True]:
+ if value:
kwargs[field] = 'test'
else:
del kwargs[field]
- continue
- if kwargs[field] is None:
- msg = ('Invalid config: missing "{}" field or "{}" environment'
- ' variable.').format(param, env_var)
- logger.error(msg)
- raise SfNotifierError(msg)
return kwargs
def _auth(self, config):
@@ -263,6 +269,7 @@
@sf_auth_retry
def _create_feed_item(self, subject, body, case_id):
feed_item = {'Title': subject, 'ParentId': case_id, 'Body': body}
+ logger.debug('Creating feed item: {}.'.format(feed_item))
return self.sf.FeedItem.create(feed_item)
@sf_auth_retry
@@ -285,14 +292,15 @@
error_code, case_id = self._create_case(subject, body,
labels, alert_id)
- self._create_feed_item(subject, body, case_id)
-
response = {'case_id': case_id, 'alert_id': alert_id}
if error_code == 1:
response['status'] = 'duplicate'
else:
response['status'] = 'created'
+
+ if self.feed_enabled:
+ self._create_feed_item(subject, body, case_id)
return response
def close_case(self, labels):
diff --git a/sf_notifier/settings/development.py b/sf_notifier/settings/development.py
index 876a671..a164977 100644
--- a/sf_notifier/settings/development.py
+++ b/sf_notifier/settings/development.py
@@ -1,5 +1,8 @@
import os
+from ..helpers import is_true
+
+
VERSION = 'development'
LOGGING = {
@@ -25,7 +28,20 @@
'CONFIGURE_LOGGING': True,
}
-ADD_LINKS = os.environ.get('SF_NOTIFIER_ADD_LINKS',
- False) in ['true', 'True', True]
+ADD_LINKS = is_true(os.environ.get('SF_NOTIFIER_ADD_LINKS'))
-SF_CONFIG = {}
+SF_CONFIG = {
+ # Salesforce login params
+ 'AUTH_URL': os.environ.get('SFDC_AUTH_URL', 'null'),
+ 'USERNAME': os.environ.get('SFDC_USERNAME', 'null'),
+ 'PASSWORD': os.environ.get('SFDC_PASSWORD', 'null'),
+ 'ORGANIZATION_ID': os.environ.get('SFDC_ORGANIZATION_ID', 'null'),
+ 'ENVIRONMENT_ID': os.environ.get('SFDC_ENVIRONMENT_ID', 'null'),
+ 'SANDBOX_ENABLED': is_true(os.environ.get(
+ 'SFDC_SANDBOX_ENABLED', 'False')),
+ # sf-notifier specific params
+ 'FEED_ENABLED': is_true(os.environ.get(
+ 'SF_NOTIFIER_FEED_ENABLED', 'True')),
+ 'HASH_FUNC': os.environ.get(
+ 'SF_NOTIFIER_ALERT_ID_HASH_FUNC', 'sha256'),
+}
diff --git a/sf_notifier/settings/production.py b/sf_notifier/settings/production.py
index e0450c8..6251daf 100644
--- a/sf_notifier/settings/production.py
+++ b/sf_notifier/settings/production.py
@@ -1,5 +1,8 @@
import os
+from ..helpers import is_true
+
+
VERSION = 'production'
LOGGING = {
@@ -16,7 +19,8 @@
'file': {
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'default',
- 'filename': '/var/log/sf-notifier/sfnotifier.log',
+ 'filename': os.getenv('LOGPATH',
+ '/var/log/sf-notifier/sfnotifier.log'),
'mode': 'a',
'maxBytes': 10485760,
'backupCount': 5
@@ -35,7 +39,20 @@
'CONFIGURE_LOGGING': True,
}
-ADD_LINKS = os.environ.get('SF_NOTIFIER_ADD_LINKS',
- False) in ['true', 'True', True]
+ADD_LINKS = is_true(os.environ.get('SF_NOTIFIER_ADD_LINKS'))
-SF_CONFIG = {}
+SF_CONFIG = {
+ # Salesforce login params
+ 'AUTH_URL': os.environ.get('SFDC_AUTH_URL', 'null'),
+ 'USERNAME': os.environ.get('SFDC_USERNAME', 'null'),
+ 'PASSWORD': os.environ.get('SFDC_PASSWORD', 'null'),
+ 'ORGANIZATION_ID': os.environ.get('SFDC_ORGANIZATION_ID', 'null'),
+ 'ENVIRONMENT_ID': os.environ.get('SFDC_ENVIRONMENT_ID', 'null'),
+ 'SANDBOX_ENABLED': is_true(os.environ.get(
+ 'SFDC_SANDBOX_ENABLED', 'False')),
+ # sf-notifier specific params
+ 'FEED_ENABLED': is_true(os.environ.get(
+ 'SF_NOTIFIER_FEED_ENABLED', 'True')),
+ 'HASH_FUNC': os.environ.get(
+ 'SF_NOTIFIER_ALERT_ID_HASH_FUNC', 'sha256'),
+}