def exec_with_envs(commands, with_shell=False, working_dir="/home/application/current", pipe_output=False, envs=None): global running_pipe if not envs: envs = {} app_envs = {} app_envs.update(os.environ) app_envs.update(envs) if not os.path.exists(working_dir): working_dir = "/" for command in commands: popen_output = None if pipe_output: popen_output = subprocess.PIPE pipe = subprocess.Popen(command, shell=with_shell, cwd=working_dir, env=app_envs, stdout=popen_output, stderr=popen_output) running_pipe = pipe if pipe_output: stdout = Stream(echo_output=sys.stdout, default_stream_name='stdout', watcher_name='unit-agent', envs=app_envs) stderr = Stream(echo_output=sys.stderr, default_stream_name='stderr', watcher_name='unit-agent', envs=app_envs) stdout_thread = Thread(target=process_output, args=(pipe.stdout, stdout)) stdout_thread.start() stderr_thread = Thread(target=process_output, args=(pipe.stderr, stderr)) stderr_thread.start() status = pipe.wait() running_pipe = None if pipe_output: stdout_thread.join() stderr_thread.join() if status != 0: sys.exit(status)
def test_should_slience_errors_when_envs_does_not_exist( self, TsuruLogWriter): 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 test_envs_with_rate_limit(self, TsuruLogWriter, gethostname): TsuruLogWriter.return_value = mock.Mock() gethostname.return_value = "myhost" stream = Stream(watcher_name="watcher", envs={ "LOG_RATE_LIMIT_WINDOW": "60", "LOG_RATE_LIMIT_COUNT": "1000", }) TsuruLogWriter.assert_called_with(mock.ANY, stream.queue, "60", "1000", 'host2', '514', 'LOCAL0', 'udp', 'appname1')
def setUp(self, TsuruLogWriter, gethostname): TsuruLogWriter.return_value = log_writer = mock.Mock() 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") TsuruLogWriter.assert_called_with(mock.ANY, self.stream.queue, None, None, 'host2', '514', 'LOCAL0', 'udp', 'appname1') log_writer.start.assert_called_once()
def test_timeout_is_configurable(self, TsuruLogWriter, gethostname): TsuruLogWriter.return_value = mock.Mock() gethostname.return_value = "myhost" stream = Stream(watcher_name="watcher", timeout=10) 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" entry = stream.queue.get(timeout=2) self.assertEqual(url, entry.url) self.assertEqual(10, entry.timeout) self.assertEqual([expected_msg], entry.messages)
def test_envs_may_be_overriden(self, TsuruLogWriter, gethostname): TsuruLogWriter.return_value = mock.Mock() gethostname.return_value = "myhost" stream = Stream(watcher_name="watcher", envs={"TSURU_APPNAME": "mynewappname"}) stream(self.data["stdout"]) (appname, host, token, syslog_server, syslog_port, syslog_facility, syslog_socket) = stream._load_envs() self.assertEqual(appname, "mynewappname") self.assertEqual(host, "host1") self.assertEqual(token, "secret123") self.assertEqual(syslog_server, "host2") self.assertEqual(syslog_port, "514") self.assertEqual(syslog_facility, "LOCAL0") self.assertEqual(syslog_socket, "udp")
def test_max_buffer_size_is_configurable(self, TsuruLogWriter): stream = Stream(max_buffer_size=500) self.assertEqual(500, stream._max_buffer_size)