def approve_rule(rule_id, approver=None, notify_approvers=True, session=None):
"""
Approve a specific replication rule.
:param rule_id: The rule_id to approve.
:param approver: The account which is approving the rule.
:param notify_approvers: Notify the other approvers.
:param session: The database session in use.
:raises: RuleNotFound if no Rule can be found.
"""
try:
rule = session.query(models.ReplicationRule).filter_by(id=rule_id).one()
if rule.state == RuleState.WAITING_APPROVAL:
rule.ignore_account_limit = True
rule.state = RuleState.INJECT
if approver:
approver_email = get_account(account=approver, session=session).email
if approver_email:
approver = '%s (%s)' % (approver, approver_email)
else:
approver = 'AUTOMATIC'
with open('%s/rule_approved_user.tmpl' % config_get('common', 'mailtemplatedir'), 'r') as templatefile:
template = Template(templatefile.read())
email = get_account(account=rule.account, session=session).email
if email:
text = template.safe_substitute({'rule_id': str(rule.id),
'expires_at': str(rule.expires_at),
'rse_expression': rule.rse_expression,
'comment': rule.comments,
'scope': rule.scope,
'name': rule.name,
'did_type': rule.did_type,
'approver': approver})
add_message(event_type='email',
payload={'body': text,
'to': [email],
'subject': '[RUCIO] Replication rule %s has been approved' % (str(rule.id))},
session=session)
# Also notify the other approvers
if notify_approvers:
with open('%s/rule_approved_admin.tmpl' % config_get('common', 'mailtemplatedir'), 'r') as templatefile:
template = Template(templatefile.read())
text = template.safe_substitute({'rule_id': str(rule.id),
'approver': approver})
recipents = __create_recipents_list(rse_expression=rule.rse_expression, session=session)
for recipent in recipents:
add_message(event_type='email',
payload={'body': text,
'to': [recipent[0]],
'subject': 'Re: [RUCIO] Request to approve replication rule %s' % (str(rule.id))},
session=session)
except NoResultFound:
raise RuleNotFound('No rule with the id %s found' % (rule_id))
except StatementError:
raise RucioException('Badly formatted rule id (%s)' % (rule_id))
评论列表
文章目录