Skip to content

Commit

Permalink
Pull Request review and better log formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan Douglas R. de Oliveira committed Mar 13, 2017
1 parent 75d130c commit f656118
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
6 changes: 2 additions & 4 deletions flintrock/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import time
import urllib.request
import base64
import os
import logging
from collections import namedtuple
from datetime import datetime
Expand Down Expand Up @@ -127,9 +126,8 @@ def wait_for_state(self, state: str):
while any([i.state['Name'] != state for i in self.instances]):
if logger.isEnabledFor(logging.DEBUG):
waiting_instances = [i for i in self.instances if i.state['Name'] != state]
sample_size = 3
sample = [i.id for i in waiting_instances][:sample_size]
logger.debug('{size} instances not in state {state}, like: {sample} ...'.format(size=len(waiting_instances), state=state, sample=sample))
sample = ', '.join(["'{}'".format(i.id) for i in waiting_instances][:3])
logger.debug("{size} instances not in state '{state}': {sample}, ...".format(size=len(waiting_instances), state=state, sample=sample))
time.sleep(3)
# Update metadata for all instances in one shot. We don't want
# to make a call to AWS for each of potentially hundreds of
Expand Down
14 changes: 8 additions & 6 deletions flintrock/flintrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,17 @@ def get_config_file() -> str:


def configure_log(debug: bool):
handler = logging.StreamHandler()
root_logger = logging.getLogger('flintrock')
import sys
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
if debug:
logger.setLevel(logging.DEBUG)
handler.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
root_logger.setLevel(logging.DEBUG)
handler.setFormatter(logging.Formatter('%(asctime)s - flintrock.%(module)-9s - %(levelname)-5s - %(message)s'))
else:
logger.setLevel(logging.INFO)
root_logger.setLevel(logging.INFO)
handler.setFormatter(logging.Formatter('%(message)s'))
logger.addHandler(handler)
root_logger.addHandler(handler)


@click.group()
Expand All @@ -180,7 +182,7 @@ def configure_log(debug: bool):
@click.option('--provider', default='ec2', type=click.Choice(['ec2']))
@click.version_option(version=__version__)
# TODO: implement some solution like in https://github.com/pallets/click/issues/108
@click.option('--debug/--no-debug', default=False, help="Whether to show debug messages")
@click.option('--debug/--no-debug', default=False, help="Show debug information.")
@click.pass_context
def cli(cli_context, config, provider, debug):
"""
Expand Down
6 changes: 3 additions & 3 deletions flintrock/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ def get_ssh_client(
logger.info("[{h}] SSH online.".format(h=host))
break
except socket.timeout as e:
logger.debug("[{h}] SSH got a timeout...".format(h=host))
logger.debug("[{h}] SSH timeout.".format(h=host))
time.sleep(5)
except paramiko.ssh_exception.NoValidConnectionsError as e:
if any(error.errno != errno.ECONNREFUSED for error in e.errors.values()):
raise
logger.debug("[{h}] SSH got an exception: {e}".format(h=host, e=e))
logger.debug("[{h}] SSH exception: {e}".format(h=host, e=e))
time.sleep(5)
# We get this exception during startup with CentOS but not Amazon Linux,
# for some reason.
except paramiko.ssh_exception.AuthenticationException as e:
logger.debug("[{h}] SSH got an AuthenticationException".format(h=host))
logger.debug("[{h}] SSH AuthenticationException.".format(h=host))
time.sleep(5)
else:
raise SSHError(
Expand Down

0 comments on commit f656118

Please sign in to comment.