diff --git a/config/main.py b/config/main.py index 95730cd9e3..0655af246b 100755 --- a/config/main.py +++ b/config/main.py @@ -8,6 +8,10 @@ import netaddr import re import syslog +import logging +import swsssdk +import getpass +import uuid import sonic_device_util import ipaddress @@ -17,6 +21,12 @@ import aaa import mlnx +import traceback +import base64 +from Crypto.Cipher import AES +from Crypto.Hash import SHA256 +from Crypto import Random +import time SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' SYSLOG_IDENTIFIER = "config" @@ -1501,5 +1511,134 @@ def del_ntp_server(ctx, ntp_ip_address): except SystemExit as e: ctx.fail("Restart service ntp-config failed with error {}".format(e)) +g_ext_cfg = {} + +@config.group('export', invoke_without_command=False) +@click.pass_context +def export(ctx): + pass + +@export.command('enable') +@click.pass_context +def export_enable(ctx): + """Enable the tech-support export service""" + config_db = ConfigDBConnector() + config_db.connect() + cfg_entry = config_db.get_entry('EXPORT', 'export') + + if 'servername' in cfg_entry: + cfg_entry['config'] = 'enable' + config_db.set_entry('EXPORT', 'export',cfg_entry) + else: + print "export is not configured\n" + +@export.command('disable') +@click.pass_context +def export_disable(ctx): + """Disable the tech-support export service""" + config_db = ConfigDBConnector() + config_db.connect() + cfg_entry = config_db.get_entry('EXPORT', 'export') + + if 'servername' in cfg_entry: + cfg_entry['config'] = 'disable' + config_db.set_entry('EXPORT', 'export',cfg_entry) + else: + print "export is not configured\n" + + + +class BasedIntParamType(click.ParamType): + + def convert(self, value, param, ctx): + minval=30 + maxval=1440 + try: + if int (value) == 0: + return int(value) + + if ( int(value) >= minval and int(value) <= maxval) : + return int(value) + else: + print "Invalid interval range, valid values[ 0(disable), {}-{} minutes]".format(minval, maxval) + except Exception as e: + print str(e) + +BASED_INT = BasedIntParamType() + +@export.command('interval') +#@click.argument('interval', metavar='', nargs=1, required=True, type=click.IntRange(0,1800,86400)) +@click.argument('interval', metavar='', nargs=1, required=True, type=BASED_INT) +@click.pass_context +def export_interval(ctx, interval): + """Configure the tech-support export interval """ + config_db = ConfigDBConnector() + config_db.connect() + cfg_entry = config_db.get_entry('EXPORT', 'export') + + if 'servername' in cfg_entry: + cfg_entry['interval'] = interval + config_db.set_entry('EXPORT', 'export',cfg_entry) + else: + print "export is not configured\n" + + +def encrypt(key, source, encode=True): + key = key.ljust(16,'F') + obj = AES.new(key, AES.MODE_CFB, 'A287AJSHJKHJS562') + data = obj.encrypt(source) + return base64.b64encode(data).decode("latin-1") if encode else data + + +@export.group('server') +@click.pass_context +def export_server(ctx): + """Configure the remote server name to connect""" + pass + +@export_server.group('username', invoke_without_command=False) +@click.pass_context +def export_user(ctx): + """Configure the remote server user name to upload""" + pass + +@export_user.group('destdir', invoke_without_command=False) +@click.pass_context +def export_destdir(ctx): + """Configure the remote server directory to save the tech-support data""" + pass + + +@export_destdir.command('protocol') +@click.argument('servername', metavar='', nargs=1, required=True) +@click.argument('username', metavar='', required=True) +@click.argument('destdir', metavar='', required=True) +@click.argument('protocol', metavar='', required=True, type=click.Choice(["scp", "sftp"])) +@click.pass_context +def export_protocol(ctx, servername, username, destdir, protocol ): + """ + Configures the export service to upload the tech-support data to a remote server \n + - Configure the remote server name to connect \n + - Configure the remote server user name to upload \n + - Configure the remote server directory to save + the tech-support data \n + - Configure the protocol type[scp/sftp] to upload + the tech-support data \n + """ + g_ext_cfg['servername'] = servername + g_ext_cfg['username'] = username + g_ext_cfg['destdir'] = destdir + g_ext_cfg['protocol'] = protocol + + try: + g_ext_cfg['password'] = getpass.getpass('Password:') + g_ext_cfg['password'] = encrypt(str(uuid.getnode()), g_ext_cfg['password']) + except: + print "Input reading error!" + traceback.print_exc(file=sys.stdout) + + config_db = ConfigDBConnector() + config_db.connect() + config_db.set_entry('EXPORT', 'export', g_ext_cfg) if __name__ == '__main__': config() diff --git a/scripts/generate_dump b/scripts/generate_dump index 14d03c0b59..8696949b74 100755 --- a/scripts/generate_dump +++ b/scripts/generate_dump @@ -25,11 +25,13 @@ DO_COMPRESS=true CMD_PREFIX= SINCE_DATE="@0" # default is set to January 1, 1970 at 00:00:00 GMT REFERENCE_FILE=/tmp/reference -BASE=sonic_dump_`hostname`_`date +%Y%m%d_%H%M%S` +BASE=${BASE:-sonic_dump_`hostname`_`date +%Y%m%d_%H%M%S`} DUMPDIR=/var/dump TARDIR=$DUMPDIR/$BASE TARFILE=$DUMPDIR/$BASE.tar LOGDIR=$DUMPDIR/$BASE/dump +HOME=${HOME:-/root} +USER=${USER:-root} ############################################################################### # Runs a comamnd and saves its output to the incrementally built tar. diff --git a/show/main.py b/show/main.py index 5a4ba17cde..90e9534b5b 100755 --- a/show/main.py +++ b/show/main.py @@ -2040,5 +2040,19 @@ def tablelize(keys, data, enable_table_keys, prefix): click.echo(tabulate(tablelize(keys, data, enable_table_keys, prefix), header)) state_db.close(state_db.STATE_DB) +@cli.command('export') +def export(): + """ show tech-support export configurations """ + config_db = ConfigDBConnector() + config_db.connect() + cfg_entry = config_db.get_entry('EXPORT', 'export') + + if 'servername' in cfg_entry: + del cfg_entry['password'] + print "Export Info : {}".format(cfg_entry) + else: + print "Export info is not configured/enabled" + + if __name__ == '__main__': cli()