|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import logging |
| 3 | +from util import full_stack |
| 4 | +from util import exec_remote_command |
| 5 | +from dbaas_cloudstack.models import HostAttr as CS_HostAttr |
| 6 | +from workflow.steps.util.base import BaseStep |
| 7 | +from workflow.exceptions.error_codes import DBAAS_0020 |
| 8 | + |
| 9 | +LOG = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +class ConfigLog(BaseStep): |
| 13 | + |
| 14 | + def __unicode__(self): |
| 15 | + return "Configuring rsyslog..." |
| 16 | + |
| 17 | + def do(self, workflow_dict): |
| 18 | + try: |
| 19 | + for source_host in workflow_dict['source_hosts']: |
| 20 | + future_host = source_host.future_host |
| 21 | + cs_host_attr = CS_HostAttr.objects.get(host=future_host) |
| 22 | + |
| 23 | + LOG.info("Configuring rsyslog {}".format(future_host)) |
| 24 | + |
| 25 | + script = self.rsyslog_create_config(workflow_dict['database']) |
| 26 | + LOG.info(script) |
| 27 | + |
| 28 | + output = {} |
| 29 | + return_code = exec_remote_command( |
| 30 | + server=future_host.address, |
| 31 | + username=cs_host_attr.vm_user, |
| 32 | + password=cs_host_attr.vm_password, |
| 33 | + command=script, |
| 34 | + output=output |
| 35 | + ) |
| 36 | + LOG.info(output) |
| 37 | + if return_code != 0: |
| 38 | + error_msg = "Error configuring rsyslog: {}".format(str(output)) |
| 39 | + LOG.error(error_msg) |
| 40 | + raise EnvironmentError(error_msg) |
| 41 | + |
| 42 | + return True |
| 43 | + except Exception: |
| 44 | + traceback = full_stack() |
| 45 | + |
| 46 | + workflow_dict['exceptions']['error_codes'].append(DBAAS_0020) |
| 47 | + workflow_dict['exceptions']['traceback'].append(traceback) |
| 48 | + |
| 49 | + return False |
| 50 | + |
| 51 | + def undo(self, workflow_dict): |
| 52 | + LOG.info("Running undo...") |
| 53 | + try: |
| 54 | + for source_host in workflow_dict['source_hosts']: |
| 55 | + future_host = source_host.future_host |
| 56 | + cs_host_attr = CS_HostAttr.objects.get(host=future_host) |
| 57 | + |
| 58 | + LOG.info("Removing rsyslog config in {}".format(future_host)) |
| 59 | + |
| 60 | + script = self.rsyslog_remove_config() |
| 61 | + LOG.info(script) |
| 62 | + |
| 63 | + output = {} |
| 64 | + return_code = exec_remote_command( |
| 65 | + server=future_host.address, |
| 66 | + username=cs_host_attr.vm_user, |
| 67 | + password=cs_host_attr.vm_password, |
| 68 | + command=script, |
| 69 | + output=output |
| 70 | + ) |
| 71 | + LOG.info(output) |
| 72 | + if return_code != 0: |
| 73 | + error_msg = "Error removing configuring rsyslog: {}".format(str(output)) |
| 74 | + LOG.error(error_msg) |
| 75 | + raise EnvironmentError(error_msg) |
| 76 | + |
| 77 | + return True |
| 78 | + except Exception: |
| 79 | + traceback = full_stack() |
| 80 | + |
| 81 | + workflow_dict['exceptions']['error_codes'].append(DBAAS_0020) |
| 82 | + workflow_dict['exceptions']['traceback'].append(traceback) |
| 83 | + |
| 84 | + return False |
| 85 | + |
| 86 | + def rsyslog_create_config(self, database): |
| 87 | + return \ |
| 88 | + ' echo "\$EscapeControlCharactersOnReceive off" >> /etc/rsyslog.d/globologging.conf &&' \ |
| 89 | + ' sed -i "\$a \$template db-log, \\\"<%PRI%>%TIMESTAMP% %HOSTNAME% %syslogtag%%msg% tags: dbaas,{}\\\"" /etc/rsyslog.d/globologging.conf &&' \ |
| 90 | + ' sed -i "\$a*.* @logging.udp.globoi.com:5140; db-log" /etc/rsyslog.d/globologging.conf &&' \ |
| 91 | + ' /etc/init.d/rsyslog restart'.format(database.name) |
| 92 | + |
| 93 | + def rsyslog_remove_config(self): |
| 94 | + return 'rm -f /etc/rsyslog.d/globologging.conf' |
0 commit comments