def run_commands(name, **kwargs): from tsuru.stream import Stream config = load_config(**kwargs) watcher = kwargs.get("watcher") parts = name.split(":") cmds = config.get("hooks", {}) for part in parts: cmds = cmds.get(part, {}) if cmds: Stream(watcher_name=watcher.name)({ "data": " ---> Running {}".format(name) }) for command in cmds: try: commands = ["/bin/bash", "-c"] cmd = "source /home/application/apprc && {}" commands.append(cmd.format(command)) result = subprocess.check_output(commands, stderr=subprocess.STDOUT, cwd=watcher.working_dir, preexec_fn=set_uid(watcher)) Stream(watcher_name=watcher.name)({"data": result}) except subprocess.CalledProcessError as e: Stream(watcher_name=watcher.name)({"data": str(e)}) Stream(watcher_name=watcher.name)({"data": e.output}) return True
def setUp(self, gethostname): gethostname.return_value = "myhost" l_out = '2012-11-06 17:13:55 [12019] [INFO] Starting gunicorn 0.15.0\n' l_err = '2012-11-06 17:13:55 [12019] [ERROR] Error starting gunicorn\n' self.data = {} self.data['stderr'] = {'pid': 12018, 'data': l_err, 'name': 'stderr'} self.data['stdout'] = {'pid': 12018, 'data': l_out, 'name': 'stdout'} self.stream = Stream(watcher_name='mywatcher') self.stream.apprc = os.path.join(os.path.dirname(__file__), "testdata/apprc")
def test_timeout_is_configurable(self, post, gethostname): post.return_value = mock.Mock(status_code=200) gethostname.return_value = "myhost" stream = Stream(watcher_name="watcher", timeout=10) stream.apprc = os.path.join(os.path.dirname(__file__), "testdata/apprc") stream(self.data['stdout']) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = self.stream.load_envs() url = "{0}/apps/{1}/log?source=watcher&unit=myhost".format(host, appname) expected_msg = "Starting gunicorn 0.15.0\n" expected_data = json.dumps([expected_msg]) post.assert_called_with(url, data=expected_data, headers={"Authorization": "bearer " + token}, timeout=10)
class StreamTestCase(unittest.TestCase): def setUp(self): l = '2012-11-06 17:13:55 [12019] [INFO] Starting gunicorn 0.15.0\n' self.data = {'pid': 12018, 'data': l, 'name': 'stderr'} self.stream = Stream(watcher_name='mywatcher') self.stream.apprc = os.path.join(os.path.dirname(__file__), "testdata/apprc") def test_should_have_the_close_method(self): self.assertTrue(hasattr(Stream, "close")) @mock.patch("requests.post") def test_should_send_log_to_tsuru(self, post): post.return_value = mock.Mock(status_code=200) self.stream(self.data) appname, host, token = self.stream.load_envs() url = "{0}/apps/{1}/log?source=mywatcher".format(host, appname) expected_msg = "Starting gunicorn 0.15.0\n" expected_data = json.dumps([expected_msg]) post.assert_called_with(url, data=expected_data, headers={"Authorization": "bearer " + token}) @mock.patch("tsuru.common.load_envs") def test_should_slience_errors_when_envs_does_not_exist(lenvs, self): lenvs.return_value = {} try: stream = Stream() stream(self.data) except Exception as e: msg = "Should not fail when envs does not exist. " \ "Exception: {}".format(e) self.fail(msg)
def test_timeout_is_configurable(self, post, gethostname): post.return_value = mock.Mock(status_code=200) gethostname.return_value = "myhost" stream = Stream(watcher_name="watcher", timeout=10) stream.apprc = os.path.join(os.path.dirname(__file__), "testdata/apprc") stream(self.data['stdout']) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = self.stream.load_envs() url = "{0}/apps/{1}/log?source=watcher&unit=myhost".format( host, appname) expected_msg = "Starting gunicorn 0.15.0\n" expected_data = json.dumps([expected_msg]) post.assert_called_with(url, data=expected_data, headers={"Authorization": "bearer " + token}, timeout=10)
def test_should_slience_errors_when_envs_does_not_exist(lenvs, self): lenvs.return_value = {} try: stream = Stream() stream(self.data) except Exception as e: msg = "Should not fail when envs does not exist. " \ "Exception: {}".format(e) self.fail(msg)
def setUp(self): l = '2012-11-06 17:13:55 [12019] [INFO] Starting gunicorn 0.15.0\n' self.data = { 'pid': 12018, 'data': l, 'name': 'stderr' } self.stream = Stream(watcher_name='mywatcher') self.stream.apprc = os.path.join(os.path.dirname(__file__), "testdata/apprc")
def setUp(self, gethostname): gethostname.return_value = "myhost" l_out = '2012-11-06 17:13:55 [12019] [INFO] Starting gunicorn 0.15.0\n' l_err = '2012-11-06 17:13:55 [12019] [ERROR] Error starting gunicorn\n' self.data = {} self.data['stderr'] = { 'pid': 12018, 'data': l_err, 'name': 'stderr' } self.data['stdout'] = { 'pid': 12018, 'data': l_out, 'name': 'stdout' } self.stream = Stream(watcher_name='mywatcher') self.stream.apprc = os.path.join(os.path.dirname(__file__), "testdata/apprc")
class StreamTestCase(unittest.TestCase): def setUp(self): l = '2012-11-06 17:13:55 [12019] [INFO] Starting gunicorn 0.15.0\n' self.data = { 'pid': 12018, 'data': l, 'name': 'stderr' } self.stream = Stream(watcher_name='mywatcher') self.stream.apprc = os.path.join(os.path.dirname(__file__), "testdata/apprc") def test_should_have_the_close_method(self): self.assertTrue(hasattr(Stream, "close")) @mock.patch("requests.post") def test_should_send_log_to_tsuru(self, post): post.return_value = mock.Mock(status_code=200) self.stream(self.data) appname, host, token = self.stream.load_envs() url = "{0}/apps/{1}/log?source=mywatcher".format(host, appname) expected_msg = "Starting gunicorn 0.15.0\n" expected_data = json.dumps([expected_msg]) post.assert_called_with(url, data=expected_data, headers={"Authorization": "bearer " + token}) @mock.patch("tsuru.common.load_envs") def test_should_slience_errors_when_envs_does_not_exist(lenvs, self): lenvs.return_value = {} try: stream = Stream() stream(self.data) except Exception as e: msg = "Should not fail when envs does not exist. " \ "Exception: {}".format(e) self.fail(msg)
class StreamTestCase(unittest.TestCase): @mock.patch("tsuru.stream.gethostname") def setUp(self, gethostname): gethostname.return_value = "myhost" l_out = '2012-11-06 17:13:55 [12019] [INFO] Starting gunicorn 0.15.0\n' l_err = '2012-11-06 17:13:55 [12019] [ERROR] Error starting gunicorn\n' self.data = {} self.data['stderr'] = { 'pid': 12018, 'data': l_err, 'name': 'stderr' } self.data['stdout'] = { 'pid': 12018, 'data': l_out, 'name': 'stdout' } self.stream = Stream(watcher_name='mywatcher') self.stream.apprc = os.path.join(os.path.dirname(__file__), "testdata/apprc") def test_should_have_the_close_method(self): self.assertTrue(hasattr(Stream, "close")) @mock.patch("requests.post") def test_should_send_log_to_tsuru(self, post): post.return_value = mock.Mock(status_code=200) self.stream(self.data['stdout']) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = self.stream.load_envs() url = "{0}/apps/{1}/log?source=mywatcher&unit=myhost".format(host, appname) expected_msg = "Starting gunicorn 0.15.0\n" expected_data = json.dumps([expected_msg]) post.assert_called_with(url, data=expected_data, headers={"Authorization": "bearer " + token}, timeout=2) @mock.patch("logging.getLogger") @mock.patch('logging.handlers.SysLogHandler') def test_should_send_log_to_syslog_as_info(self, s_handler, logger): self.stream(self.data['stdout']) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = self.stream.load_envs() my_logger = logger(appname) log_handler = s_handler(address=(syslog_server, syslog_port), facility=syslog_facility, socktype=syslog_socket) expected_msg = "Starting gunicorn 0.15.0\n" my_logger.addHandler(log_handler) my_logger.info.assert_called_with(expected_msg) @mock.patch("logging.getLogger") @mock.patch('logging.handlers.SysLogHandler') def test_should_send_log_to_syslog_as_error(self, s_handler, logger): self.stream(self.data['stderr']) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = self.stream.load_envs() my_logger = logger(appname) log_handler = s_handler(address=(syslog_server, syslog_port), facility=syslog_facility, socktype=syslog_socket) expected_msg = "Error starting gunicorn\n" my_logger.addHandler(log_handler) my_logger.error.assert_called_with(expected_msg) def test_should_send_log_to_syslog_and_use_one_handler(self): self.stream(self.data['stderr']) self.stream(self.data['stderr']) self.stream(self.data['stderr']) self.stream(self.data['stderr']) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = self.stream.load_envs() my_logger = logging.getLogger(appname) self.assertEqual(len(my_logger.handlers), 1) @mock.patch("tsuru.stream.gethostname") @mock.patch("requests.post") def test_timeout_is_configurable(self, post, gethostname): post.return_value = mock.Mock(status_code=200) gethostname.return_value = "myhost" stream = Stream(watcher_name="watcher", timeout=10) stream.apprc = os.path.join(os.path.dirname(__file__), "testdata/apprc") stream(self.data['stdout']) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = self.stream.load_envs() url = "{0}/apps/{1}/log?source=watcher&unit=myhost".format(host, appname) expected_msg = "Starting gunicorn 0.15.0\n" expected_data = json.dumps([expected_msg]) post.assert_called_with(url, data=expected_data, headers={"Authorization": "bearer " + token}, timeout=10) @mock.patch("requests.post") def test_should_ignore_errors_in_post_call(self, post): post.side_effect = Exception() self.stream(self.data['stdout']) @mock.patch("tsuru.common.load_envs") def test_should_slience_errors_when_envs_does_not_exist(lenvs, self): lenvs.return_value = {} try: stream = Stream() stream(self.data['stdout']) except Exception as e: msg = "Should not fail when envs does not exist. " \ "Exception: {}".format(e) self.fail(msg)
def setUp(self): l = '2012-11-06 17:13:55 [12019] [INFO] Starting gunicorn 0.15.0\n' self.data = {'pid': 12018, 'data': l, 'name': 'stderr'} self.stream = Stream(watcher_name='mywatcher') self.stream.apprc = os.path.join(os.path.dirname(__file__), "testdata/apprc")
class StreamTestCase(unittest.TestCase): @mock.patch("tsuru.stream.gethostname") def setUp(self, gethostname): gethostname.return_value = "myhost" l_out = '2012-11-06 17:13:55 [12019] [INFO] Starting gunicorn 0.15.0\n' l_err = '2012-11-06 17:13:55 [12019] [ERROR] Error starting gunicorn\n' self.data = {} self.data['stderr'] = {'pid': 12018, 'data': l_err, 'name': 'stderr'} self.data['stdout'] = {'pid': 12018, 'data': l_out, 'name': 'stdout'} self.stream = Stream(watcher_name='mywatcher') self.stream.apprc = os.path.join(os.path.dirname(__file__), "testdata/apprc") def test_should_have_the_close_method(self): self.assertTrue(hasattr(Stream, "close")) @mock.patch("requests.post") def test_should_send_log_to_tsuru(self, post): post.return_value = mock.Mock(status_code=200) self.stream(self.data['stdout']) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = self.stream.load_envs() url = "{0}/apps/{1}/log?source=mywatcher&unit=myhost".format( host, appname) expected_msg = "Starting gunicorn 0.15.0\n" expected_data = json.dumps([expected_msg]) post.assert_called_with(url, data=expected_data, headers={"Authorization": "bearer " + token}, timeout=2) @mock.patch("logging.getLogger") @mock.patch('logging.handlers.SysLogHandler') def test_should_send_log_to_syslog_as_info(self, s_handler, logger): self.stream(self.data['stdout']) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = self.stream.load_envs() my_logger = logger(appname) log_handler = s_handler(address=(syslog_server, syslog_port), facility=syslog_facility, socktype=syslog_socket) expected_msg = "Starting gunicorn 0.15.0\n" my_logger.addHandler(log_handler) my_logger.info.assert_called_with(expected_msg) @mock.patch("logging.getLogger") @mock.patch('logging.handlers.SysLogHandler') def test_should_send_log_to_syslog_as_error(self, s_handler, logger): self.stream(self.data['stderr']) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = self.stream.load_envs() my_logger = logger(appname) log_handler = s_handler(address=(syslog_server, syslog_port), facility=syslog_facility, socktype=syslog_socket) expected_msg = "Error starting gunicorn\n" my_logger.addHandler(log_handler) my_logger.error.assert_called_with(expected_msg) def test_should_send_log_to_syslog_and_use_one_handler(self): self.stream(self.data['stderr']) self.stream(self.data['stderr']) self.stream(self.data['stderr']) self.stream(self.data['stderr']) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = self.stream.load_envs() my_logger = logging.getLogger(appname) self.assertEqual(len(my_logger.handlers), 1) @mock.patch("tsuru.stream.gethostname") @mock.patch("requests.post") def test_timeout_is_configurable(self, post, gethostname): post.return_value = mock.Mock(status_code=200) gethostname.return_value = "myhost" stream = Stream(watcher_name="watcher", timeout=10) stream.apprc = os.path.join(os.path.dirname(__file__), "testdata/apprc") stream(self.data['stdout']) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = self.stream.load_envs() url = "{0}/apps/{1}/log?source=watcher&unit=myhost".format( host, appname) expected_msg = "Starting gunicorn 0.15.0\n" expected_data = json.dumps([expected_msg]) post.assert_called_with(url, data=expected_data, headers={"Authorization": "bearer " + token}, timeout=10) @mock.patch("requests.post") def test_should_ignore_errors_in_post_call(self, post): post.side_effect = Exception() self.stream(self.data['stdout']) @mock.patch("tsuru.common.load_envs") def test_should_slience_errors_when_envs_does_not_exist(lenvs, self): lenvs.return_value = {} try: stream = Stream() stream(self.data['stdout']) except Exception as e: msg = "Should not fail when envs does not exist. " \ "Exception: {}".format(e) self.fail(msg)