Example #1
0
 def _configure_rsyslog():
     """Restart rsyslog service for reflecting supportbundle rsyslog config."""
     try:
         Log.info("Restarting rsyslog service")
         service_obj = Service("rsyslog.service")
         service_obj.restart()
     except Exception as e:
         Log.warn(f"Error in rsyslog service restart: {e}")
    def config(self):
        """
        Performs configurations.

        Raises exception on error
        """
        try:
            # Create backup of elasticsearch_config file.
            if not os.path.exists(
                    f'{self.elasticsearch_file_path}/elasticsearch.yml.bkp'):
                shutil.copyfile(
                    self.elasticsearch_config_path,
                    f'{self.elasticsearch_file_path}/elasticsearch.yml.bkp')
            else:
                shutil.copyfile(
                    f'{self.elasticsearch_file_path}/elasticsearch.yml.bkp',
                    self.elasticsearch_config_path)
            # Get config entries that needs to add in elasticsearch.yml
            config_to_add = self.get_config_entries()
            file_contents = Elasticsearch.read_file_contents(
                self.elasticsearch_config_path)
            with open(self.elasticsearch_config_path, "a+") as f:
                for line in config_to_add:
                    f.write(f'\n{line}')
                f.close()

            # load omelasticsearch module in rsyslog.
            file_contents = Elasticsearch.read_file_contents(self.rsyslog_conf)
            insert_line = 'module(load="omelasticsearch")'
            if not any(insert_line in i for i in file_contents):
                with open(self.rsyslog_conf, "w") as f:
                    for line in file_contents:
                        if line == '\n':
                            continue
                        f.write(f'\n{line}')
                        if line.strip('\n') == "#### MODULES ####":
                            f.write(f'\n{insert_line}\n')
                    f.close()

            try:
                service_obj = Service('rsyslog.service')
                service_obj.restart()
            except ServiceError as e:
                msg = f"Restarting rsyslog.service failed due to error, {e}."
                Log.error(msg)

        except (Exception, OSError) as e:
            msg = f"Failed in config stage due to error {e}."
            Log.error(msg)
            raise

        Log.info("Config done.")
        return 0
Example #3
0
    def reset(self):
        """ Performs reset. Raises exception on error """

        # Check service status
        service_obj = Service('elasticsearch.service')
        service_state = service_obj.get_state()
        if service_state._state == 'active':
            Log.warn("Elasticsearch service in active state. \n"
                     "Stopping Elasticsearch service now...")
            service_obj.stop()

        # Clear log files.
        Elasticsearch.truncate_log_files(self.log_path)
        Log.info("Reset done.")
        return 0
    def cleanup(self, pre_factory=False):
        try:
            Service("consul.service").stop()
        except ServiceError:
            pass

        content = ""
        with open("/usr/lib/systemd/system/consul.service", "r+") as f:
            content = f.read()
            content = re.sub("config-dir=.*", "config-dir=/etc/consul.d",
                             content)
            content = re.sub("ConditionFileNotEmpty=.*",
                             "ConditionFileNotEmpty=/etc/consul.d/consul.hcl",
                             content)
            content = re.sub(
                "User=.*", "User=consul",
                content
            )
            content = re.sub(
                "Group=.*", "Group=consul",
                content
            )
            f.seek(0)
            f.truncate()
            f.write(content)

        command = "systemctl daemon-reload"
        _, err, returncode = SimpleProcess(command).run()
        if returncode != 0:
            raise ConsulSetupError(
                returncode,
                "Consul Setup systemd daemon-reload failed with error: %s" %
                err)

        config_path = Conf.get(self.index, "cortx>software>consul>config_path",
                               "/etc/consul.d")
        shutil.rmtree(config_path, ignore_errors=True)
        os.makedirs("/etc/consul.d", exist_ok=True)
        shutil.copy("/opt/seagate/cortx/utils/conf/consul.hcf.default",
                    "/etc/consul.d/consul.hcl")
        command = "chown -R consul:consul /etc/consul.d"
        _, err, returncode = SimpleProcess(command).run()
        if returncode != 0:
            raise ConsulSetupError(
                returncode,
                "Consul Setup changing ownership failed for %s with error: %s",
                config_path, err)
        data_path = Conf.get(self.index, "cortx>software>consul>data_path",
                             "/opt/consul/")
        shutil.rmtree(data_path, ignore_errors=True)

        if pre_factory:
            pass
 def init(self):
     """Perform initialization. Raises exception on error."""
     max_retry = 3
     for i in range(max_retry):
         try:
             Service("consul.service").start()
             ServiceV().validate('isrunning', ["consul"])
             break
         except (VError, ServiceError):
             if i == (max_retry - 1):
                 raise
             time.sleep(0.5)
 def setUpClass(cls):
     cls.service_obj = Service(TestSystemHandler._service_name)
 def setUp(self):
     self.service_obj = Service(TestSystemHandler._service_name)
class TestSystemHandler(unittest.TestCase):
    _service_name = 'rsyslog.service'

    def setUp(self):
        self.service_obj = Service(TestSystemHandler._service_name)

    def test_start(self):
        self.service_obj.start()
        service_state = self.test_get_state()
        self.assertEqual(service_state.state, 'active')
        self.assertEqual(service_state.substate, 'running')

    def test_stop(self):
        self.service_obj.stop()
        service_state = self.test_get_state()
        self.assertEqual(service_state.state, 'inactive')
        self.assertEqual(service_state.substate, 'dead')

    def test_restart(self):
        self.service_obj.restart()
        service_state = self.test_get_state()
        self.assertEqual(service_state.state, 'active')
        self.assertEqual(service_state.substate, 'running')

    def test_enable(self):
        self.service_obj.enable()
        is_enable = self.test_is_enabled()
        self.assertTrue(is_enable)

    def test_disable(self):
        self.service_obj.disable()
        is_enable = self.test_is_enabled()
        self.assertFalse(is_enable)

    def test_get_state(self):
        return self.service_obj.get_state()

    def test_is_enabled(self):
        return self.service_obj.is_enabled()