Skip to content

Commit

Permalink
Use Logger class from sonic-py-common
Browse files Browse the repository at this point in the history
  • Loading branch information
jleveque committed Nov 3, 2020
1 parent 1623d5c commit 0376eea
Showing 1 changed file with 22 additions and 34 deletions.
56 changes: 22 additions & 34 deletions files/image_config/corefile_uploader/core_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import syslog
import tarfile
import time
import yaml

import yaml
from azure.storage.file import FileService
from sonic_py_common.logger import Logger
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

this_file = os.path.basename(__file__)
SYSLOG_IDENTIFIER = os.path.basename(__file__)

cfg = ""

Expand All @@ -36,47 +37,32 @@
MAX_RETRIES = 5
UPLOAD_PREFIX = "UPLOADED_"

log_level = syslog.LOG_DEBUG

def log_msg(lvl, fname, m):
if (lvl <= log_level):
syslog.syslog(lvl, "{}: {}".format(fname, m))

if log_level == syslog.LOG_DEBUG:
print("{}: {}".format(fname, m))

def log_err(m):
log_msg(syslog.LOG_ERR, this_file, m)

def log_info(m):
log_msg(syslog.LOG_INFO, this_file, m)

def log_warn(m):
log_msg(syslog.LOG_WARNING, this_file, m)

def log_debug(m):
log_msg(syslog.LOG_DEBUG, this_file, m)
# Global logger instance
logger = Logger(SYSLOG_IDENTIFIER)
logger.set_min_log_priority_info()


def make_new_dir(p):
os.system("rm -rf " + p)
os.system("mkdir -p " + p)


def parse_a_json(data, prefix, val):
for i in data:
if type(data[i]) == dict:
parse_a_json(data[i], prefix + (i,), val)
else:
val[prefix + (i,)] = data[i]


class config:
parsed_data = {}
cfg_data = {}

def __init__(self):
while not os.path.exists(RC_FILE):
# Wait here until service restart
log_err("Unable to retrieve Azure storage credentials")
logger.log_error("Unable to retrieve Azure storage credentials")
time.sleep (HOURS_4)

with open(RC_FILE, 'r') as f:
Expand Down Expand Up @@ -117,15 +103,17 @@ def run(self):
time.sleep(POLL_SLEEP)
except:
self.observer.stop()
log_err("Error in watcher")
logger.log_error("Error in watcher")

self.observer.join()


def set_env(lst):
for k in lst:
if lst[k]:
os.environ[k] = lst[k]
log_debug("set env {} = {}".format(k, lst[k]))
logger.log_debug("set env {} = {}".format(k, lst[k]))


class Handler(FileSystemEventHandler):

Expand All @@ -149,7 +137,7 @@ def init():
if not acctname or not acctkey or not sharename:
while True:
# Wait here until service restart
log_err("Unable to retrieve Azure storage credentials")
logger.log_error("Unable to retrieve Azure storage credentials")
time.sleep (HOURS_4)

with open("/etc/sonic/sonic_version.yml", 'r') as stream:
Expand All @@ -176,7 +164,7 @@ def on_any_event(event):

elif event.event_type == 'created':
# Take any action here when a file is first created.
log_debug("Received create event - " + event.src_path)
logger.log_debug("Received create event - " + event.src_path)
Handler.wait_for_file_write_complete(event.src_path)
Handler.handle_file(event.src_path)

Expand All @@ -199,7 +187,7 @@ def wait_for_file_write_complete(path):
raise Exception("Dump file creation is too slow: " + path)
# Give up as something is terribly wrong with this file.

log_debug("File write complete - " + path)
logger.log_debug("File write complete - " + path)


@staticmethod
Expand All @@ -221,11 +209,11 @@ def handle_file(path):
tar.add(metafiles[e])
tar.add(path)
tar.close()
log_debug("Tar file for upload created: " + tarf_name)
logger.log_debug("Tar file for upload created: " + tarf_name)

Handler.upload_file(tarf_name, tarf_name, path)

log_debug("File uploaded - " + path)
logger.log_debug("File uploaded - " + path)
os.chdir(INIT_CWD)

@staticmethod
Expand All @@ -244,16 +232,16 @@ def upload_file(fname, fpath, coref):
e.append(l[len(e)])
svc.create_directory(sharename, "/".join(e))

log_debug("Remote dir created: " + "/".join(e))
logger.log_debug("Remote dir created: " + "/".join(e))

svc.create_file_from_path(sharename, "/".join(l), fname, fpath)
log_debug("Remote file created: name{} path{}".format(fname, fpath))
logger.log_debug("Remote file created: name{} path{}".format(fname, fpath))
newcoref = os.path.dirname(coref) + "/" + UPLOAD_PREFIX + os.path.basename(coref)
os.rename(coref, newcoref)
break

except Exception as ex:
log_err("core uploader failed: Failed during upload (" + coref + ") err: ("+ str(ex) +") retry:" + str(i))
logger.log_error("core uploader failed: Failed during upload (" + coref + ") err: ("+ str(ex) +") retry:" + str(i))
if not os.path.exists(fpath):
break
i += 1
Expand All @@ -275,5 +263,5 @@ def scan():
Handler.scan()
w.run()
except Exception as e:
log_err("core uploader failed: " + str(e) + " Exiting ...")
logger.log_err("core uploader failed: " + str(e) + " Exiting ...")

0 comments on commit 0376eea

Please sign in to comment.