Last active
October 26, 2018 13:39
-
-
Save vsmelov/4ace436c2d712c960fd53248d7bd268b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import logging | |
from contextlib import closing | |
import datetime | |
from sshtunnel import SSHTunnelForwarder | |
from cmreslogging.handlers import CMRESHandler | |
logger = logging.getLogger(__name__) | |
def find_free_port(): | |
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: | |
s.bind(('', 0)) | |
return s.getsockname()[1] | |
LOCAL_BIND_PORT = find_free_port() | |
SSH_HOST = '1.2.3.4' | |
SSH_PORT = 10800 | |
print(f'LOCAL_BIND_PORT = {LOCAL_BIND_PORT}') | |
print(f'be sure that you executed command (once) "ssh-keyscan -p {SSH_PORT} {SSH_HOST} >> ~/.ssh/known_hosts"') | |
ssh_server = SSHTunnelForwarder( | |
(SSH_HOST, SSH_PORT), | |
ssh_username="dockerfeed", | |
ssh_pkey="dockerfeed.pem", | |
remote_bind_address=('localhost', 9200), | |
local_bind_address=('localhost', LOCAL_BIND_PORT), | |
) | |
# port-forwarding | |
def init_log(): | |
class ElasticSearchHandler(CMRESHandler): | |
@staticmethod | |
def _get_daily_index_name(es_index_name): | |
""" Returns elasticearch index name | |
:param: index_name the prefix to be used in the index | |
:return: A srting containing the elasticsearch indexname used which should include the date. | |
""" | |
return "{0!s}-{1!s}".format(es_index_name, datetime.datetime.now().strftime('%Y%m%d')) | |
logging.root.setLevel(logging.DEBUG) | |
handler = ElasticSearchHandler( | |
hosts=[{'host': 'localhost', 'port': LOCAL_BIND_PORT}], | |
auth_type=CMRESHandler.AuthType.NO_AUTH, | |
es_index_name="fluentd", | |
index_name_frequency=CMRESHandler.IndexNameFrequency.DAILY, | |
es_additional_fields={'App': 'test python logger', 'Environment': 'Dev'}) | |
logging.root.addHandler(handler) | |
fmt = logging.Formatter("%(asctime)s - %(name)s - %(filename)s:%(lineno)s - %(levelname)s - %(message)s") | |
sh = logging.StreamHandler() | |
sh.setFormatter(fmt) | |
logging.root.addHandler(sh) | |
def _main(): | |
logger.info(f'answer=42') | |
def main(): | |
with ssh_server: | |
init_log() | |
_main() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment