Skip to content

Commit

Permalink
Add log rotation and allow to run without sudo if the user is indy
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Bailey <[email protected]>
  • Loading branch information
Mike Bailey committed Sep 24, 2018
1 parent 54b25b1 commit c249967
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
69 changes: 46 additions & 23 deletions scripts/current_validators
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@ import string
import json
import csv
import logging
from logging.handlers import RotatingFileHandler
import argparse
import subprocess
import sys
import collections
import getpass

logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
filename='current_validators.log',
datefmt='%Y-%m-%d %H:%M:%S')
log_formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')

logFile = 'current_validators.log'

my_handler = RotatingFileHandler(logFile, mode='a', maxBytes=1024*1024,
backupCount=1, encoding=None, delay=0)
my_handler.setFormatter(log_formatter)
my_handler.setLevel(logging.INFO)

log = logging.getLogger('root')
log.setLevel(logging.INFO)

log.addHandler(my_handler)

def parseTransactions(ledger):

logging.info("Parsing ledger transactions into dictionary")
log.info("Parsing ledger transactions into dictionary")

validators = collections.OrderedDict()
# validators is dictionary of dictionaries that maps dest to the current values of the attritubes for that dest
Expand Down Expand Up @@ -80,34 +89,48 @@ def parseTransactions(ledger):

def getLedger():
try:
completed = subprocess.run(
['sudo', '/usr/local/bin/read_ledger', '--type', 'pool', '--count'],
check=True,
stdout=subprocess.PIPE,
)
if (getpass.getuser() == 'indy'):
completed = subprocess.run(
['/usr/local/bin/read_ledger', '--type', 'pool', '--count'],
check=True,
stdout=subprocess.PIPE,
)
else:
completed = subprocess.run(
['sudo', '/usr/local/bin/read_ledger', '--type', 'pool', '--count'],
check=True,
stdout=subprocess.PIPE,
)
except subprocess.CalledProcessError as err:
logger.error('ERROR attempting to run read_ledger --count subprocess: {}'.format(err))
log.error('ERROR attempting to run read_ledger --count subprocess: {}'.format(err))
raise
transCount = completed.stdout.decode('utf-8').strip()
logging.info("{} entries in ledger.".format(transCount))
log.info("{} entries in ledger.".format(transCount))
try:
completed = subprocess.run(
['sudo', '/usr/local/bin/read_ledger', '--type', 'pool', '--frm', '1', '--to', completed.stdout],
check=True,
stdout=subprocess.PIPE,
)
if (getpass.getuser() == 'indy'):
completed = subprocess.run(
['/usr/local/bin/read_ledger', '--type', 'pool', '--frm', '1', '--to', completed.stdout],
check=True,
stdout=subprocess.PIPE,
)
else:
completed = subprocess.run(
['sudo', '/usr/local/bin/read_ledger', '--type', 'pool', '--frm', '1', '--to', completed.stdout],
check=True,
stdout=subprocess.PIPE,
)
except subprocess.CalledProcessError as err:
logger.error('ERROR attempting to run read_ledger subprocess to capture ledger data: {}'.format(err))
log.error('ERROR attempting to run read_ledger subprocess to capture ledger data: {}'.format(err))
raise

ledgerLines = completed.stdout.decode('utf-8').splitlines()
logging.info("{} ledger entries received.".format(len(ledgerLines)))
log.info("{} ledger entries received.".format(len(ledgerLines)))
return ledgerLines

def writeResult(jsonOut):

if jsonOut:
logging.info("Serializing info on validators to json")
log.info("Serializing info on validators to json")
count = len(validators)
print('[', end='')
for i in validators.keys():
Expand All @@ -117,7 +140,7 @@ def writeResult(jsonOut):
count -= 1
print(']')
else:
logging.info("Serializing info on validators to csv")
log.info("Serializing info on validators to csv")
fieldnames = ['alias', 'blskey', 'client_ip', 'client_port', 'node_ip', 'node_port', 'services', 'dest', 'identifier']
writer = csv.DictWriter(sys.stdout, fieldnames=fieldnames)
writer.writeheader()
Expand All @@ -135,4 +158,4 @@ if __name__ == '__main__':
writeJson = parseInputs()
ledger = getLedger()
validators = parseTransactions(ledger)
writeResult = writeResult(writeJson)
writeResult = writeResult(writeJson)
27 changes: 17 additions & 10 deletions scripts/node_address_list
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@
import string
import json
import logging
from logging.handlers import RotatingFileHandler
import argparse
import sys
import fileinput
import csv
import collections

logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
filename='node_address_list.log',
datefmt='%Y-%m-%d %H:%M:%S')
log_formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')

logFile = 'node_address_list.log'

my_handler = RotatingFileHandler(logFile, mode='a', maxBytes=1024*1024,
backupCount=1, encoding=None, delay=0)
my_handler.setFormatter(log_formatter)
my_handler.setLevel(logging.INFO)

log = logging.getLogger('root')
log.setLevel(logging.INFO)

log.addHandler(my_handler)

def getCurrentValidatorsAddressPort():
logging.info("extracting addresses and ports of current validators")
log.info("extracting addresses and ports of current validators")
validatorsAddressPort = collections.OrderedDict()
jsonStr = input()
logging.info(jsonStr)
log.info(jsonStr)
validators = json.loads(jsonStr)
for validator in validators:
if 'VALIDATOR' in validator['services']:
Expand All @@ -30,17 +37,17 @@ def getCurrentValidatorsAddressPort():

def writeResult(validatorsAddressPort, outputFormat):
if outputFormat == 'json':
logging.info("Serializing validators' address and port to json")
log.info("Serializing validators' address and port to json")
print(json.dumps(validatorsAddressPort))
elif outputFormat == 'aws':
logging.info("Writing a string suitable for a source string for an AWS security group")
log.info("Writing a string suitable for a source string for an AWS security group")
addressOnly = []
for key in validatorsAddressPort.keys():
addressOnly.append(validatorsAddressPort[key]['address'])
conjunction = '/32,'
print(conjunction.join(addressOnly) + '/32')
else:
logging.info("Serializing validators' address and port to csv")
log.info("Serializing validators' address and port to csv")
fieldnames = ['alias', 'address', 'port']
writer = csv.DictWriter(sys.stdout, fieldnames=fieldnames)
writer.writeheader()
Expand Down

0 comments on commit c249967

Please sign in to comment.