Пример #1
0
def setup_logging(
        default_path='logging.yml', default_level=logging.INFO, env_key='LOG_CFG'):
    config_location = get_config_location('logging.yml')
    path = os.path.join(config_location, default_path)
    value = os.getenv(env_key, None)
    if value:
        path = value
    if os.path.exists(path):
        with open(path, 'rt') as f:
            config = yaml.load(f.read())
            for (handler, value) in config['handlers'].iteritems():
                if handler == 'console':
                    pass
                else:
                    if hasattr(sys, 'real_prefix'):
                        value['filename'] = (os.path.join
                                             (os.path.expanduser("~"),
                                              '.jsnapy',
                                              'logs/jsnapy/jsnapy.log'))
                    elif 'win' in sys.platform:
                        value['filename'] = (os.path.join
                                             (os.path.expanduser('~'),
                                              'logs\jsnapy\jsnapy.log'))

                with open(path, "w") as f:
                    yaml.dump(config, f, default_flow_style=False)
        logging.config.dictConfig(config)
    else:
        logging.basicConfig(level=default_level)
Пример #2
0
def setup_logging(
        default_path='logging.yml', default_level=logging.INFO,
        env_key='LOG_CFG'):
    config_location = get_config_location('logging.yml')
    path = os.path.join(config_location, default_path)
    value = os.getenv(env_key, None)
    if value:
        path = value
    if os.path.exists(path):
        with open(path, 'rt') as f:
            config = yaml.load(f.read())
        logging.config.dictConfig(config)
    else:
        logging.basicConfig(level=default_level)

    ###################################
    # Creating Folder path for SNAPSHOT
    ###################################
    if 'win32' not in sys.platform and not hasattr(sys, 'real_prefix'):
        snapshots_path = get_path('DEFAULT', 'snapshot_path')
        snapshots_path = os.path.expanduser(snapshots_path)
        if not os.path.isdir(snapshots_path):
            os.makedirs(snapshots_path)

    HOME = os.path.expanduser("~")  # correct cross platform way to do it
    home_folder = os.path.join(HOME, '.jsnapy')
    if not os.path.isdir(home_folder):
        os.mkdir(home_folder)
Пример #3
0
def setup_logging(default_path='logging.yml',
                  default_level=logging.INFO,
                  env_key='LOG_CFG'):
    config_location = get_config_location('logging.yml')
    path = os.path.join(config_location, default_path)
    value = os.getenv(env_key, None)
    if value:
        path = value
    if os.path.exists(path):
        with open(path, 'rt') as f:
            config = yaml.load(f.read(), Loader=yaml.FullLoader)
        logging.config.dictConfig(config)
    else:
        logging.basicConfig(level=default_level)

    ###################################
    # Creating Folder path for SNAPSHOT
    ###################################
    # Modified by @gcasella to use the function created on lines 24-29 in the __init__.py.
    if 'win32' not in sys.platform and not venv_check:
        snapshots_path = get_path('DEFAULT', 'snapshot_path')
        snapshots_path = os.path.expanduser(snapshots_path)
        if not os.path.isdir(snapshots_path):
            os.makedirs(snapshots_path)

    HOME = os.path.expanduser("~")  # correct cross platform way to do it
    home_folder = os.path.join(HOME, '.jsnapy')
    if not os.path.isdir(home_folder):
        os.mkdir(home_folder)
Пример #4
0
 def test_config_location_etc(self, mock_is_file):
     if hasattr(sys, 'real_prefix'):
         mock_is_file.side_effect = lambda arg: arg in [os.path.join(sys.prefix, 'etc',
                                                                     'jsnapy',
                                                   'jsnapy.cfg')]
         loc = get_config_location()
         self.assertEqual(loc, os.path.join(sys.prefix, 'etc', 'jsnapy'))
     elif 'win32' in sys.platform:
         mock_is_file.side_effect = lambda arg: arg in [os.path.join(os.path.expanduser('~'),
                                                                     'jsnapy', 'jsnapy.cfg')]
         loc = get_config_location()
         self.assertEqual(loc, os.path.join(os.path.expanduser('~'),'jsnapy'))
     else:
         mock_is_file.side_effect = lambda arg: arg in ['/etc/jsnapy/jsnapy.cfg']
         loc = get_config_location()
         self.assertEqual(loc, '/etc/jsnapy')
Пример #5
0
    def get_hosts(self):
        """
        Called by main function, it extracts main config file and also check for database
        Reads the yaml config file given by user and pass the extracted data to login function to
        read device details and connect them. Also checks sqlite key to check if user wants to
        create database for snapshots
        """
        self.logger.debug(
            colorama.Fore.BLUE +
            "jsnapy.cfg file location used : %s" % get_config_location(),
            extra=self.log_detail)
        self.logger.debug(colorama.Fore.BLUE +
                          "Configuration file location used : %s" %
                          get_path('DEFAULT', 'config_file_path'),
                          extra=self.log_detail)

        if self.args.pre_snapfile is not None:
            output_file = self.args.pre_snapfile
        elif self.args.snapcheck is True and self.args.pre_snapfile is None:
            output_file = "snap_temp"
            self.snap_del = True
        else:
            output_file = ""
        conf_file = self.args.file
        check = self.args.check
        snap = self.args.snap
        if os.path.isfile(conf_file):
            config_file = open(conf_file, 'r')
            self.main_file = yaml.load(config_file)
        elif os.path.isfile(
                os.path.join(get_path('DEFAULT', 'config_file_path'),
                             conf_file)):
            fpath = get_path('DEFAULT', 'config_file_path')
            config_file = open(os.path.join(fpath, conf_file), 'r')
            self.main_file = yaml.load(config_file)
        else:
            self.logger.error(
                colorama.Fore.RED +
                "ERROR!! Config file '%s' is not present " % conf_file,
                extra=self.log_detail)
            sys.exit(1)

        #### if --check option is given for sqlite, then snap file name is not compulsory  ####
        #### else exit the function saying arguments not correct  ####
        if self.main_file.__contains__('sqlite') and self.main_file[
                'sqlite'] and self.main_file['sqlite'][0]:
            self.chk_database(self.main_file, self.args.pre_snapfile,
                              self.args.post_snapfile, check, snap)
        else:
            if (self.args.check is True and
                (self.args.file is None or self.args.pre_snapfile is None
                 or self.args.post_snapfile is None)):
                self.logger.error(
                    colorama.Fore.RED +
                    "Arguments not given correctly, Please refer help message",
                    extra=self.log_detail)
                self.parser.print_help()
                sys.exit(1)
        self.login(output_file)
Пример #6
0
 def test_config_location_etc(self, mock_is_file):
     # Modified by @gcasella to use the function created on lines in __init__.py.
     if venv_check():
         mock_is_file.side_effect = lambda arg: arg in [os.path.join(sys.prefix, 'etc',
                                                                     'jsnapy',
                                                   'jsnapy.cfg')]
         loc = get_config_location()
         self.assertEqual(loc, os.path.join(sys.prefix, 'etc', 'jsnapy'))
     elif 'win32' in sys.platform:
         mock_is_file.side_effect = lambda arg: arg in [os.path.join(os.path.expanduser('~'),
                                                                     'jsnapy', 'jsnapy.cfg')]
         loc = get_config_location()
         self.assertEqual(loc, os.path.join(os.path.expanduser('~'),'jsnapy'))
     else:
         mock_is_file.side_effect = lambda arg: arg in ['/etc/jsnapy/jsnapy.cfg']
         loc = get_config_location()
         self.assertEqual(loc, '/etc/jsnapy')
Пример #7
0
 def pre_process_information(self):
     """
     The function provides the information mentioned in the configuration files
     """
     self.logger.debug(
         colorama.Fore.BLUE +
         "jsnapy.cfg file location used : %s" % get_config_location(),
         extra=self.log_detail)
     self.logger.debug(colorama.Fore.BLUE +
                       "Configuration file location used : %s" %
                       get_path('DEFAULT', 'config_file_path'),
                       extra=self.log_detail)
Пример #8
0
def setup_logging(default_path='logging.yml',
                  default_level=logging.INFO,
                  env_key='LOG_CFG'):
    config_location = get_config_location('logging.yml')
    path = os.path.join(config_location, default_path)
    value = os.getenv(env_key, None)
    if value:
        path = value
    if os.path.exists(path):
        with open(path, 'rt') as f:
            config = yaml.load(f.read())
        logging.config.dictConfig(config)
    else:
        logging.basicConfig(level=default_level)
Пример #9
0
 def test_config_location_wrong_path(self, mock_is_file):
     mock_is_file.side_effect = lambda arg: arg == '/xyz'
     loc = get_config_location()
     self.assertEqual(loc, None)
Пример #10
0
 def test_config_location_home(self, mock_is_file):
     mock_is_file.side_effect = lambda arg: arg == os.path.join(
         os.path.expanduser('~'), '.jsnapy', 'jsnapy.cfg')
     loc = get_config_location()
     self.assertEqual(loc, os.path.join(os.path.expanduser('~'), '.jsnapy'))
Пример #11
0
 def test_config_location_env(self, mock_is_file):
     os.environ['JSNAPY_HOME'] = os.path.join('bogus', 'path')
     mock_is_file.side_effect = lambda arg: arg == os.path.join(
         'bogus', 'path', 'jsnapy.cfg')
     loc = get_config_location()
     self.assertEqual(loc, os.path.join('bogus', 'path'))
Пример #12
0
 def test_config_location_wrong_path(self, mock_is_file):
     with self.assertRaises(Exception):
         mock_is_file.side_effect = lambda arg: arg == '/xyz'
         loc = get_config_location()
Пример #13
0
 def test_config_location_etc(self, mock_is_file):
     mock_is_file.side_effect = lambda arg: arg in [
         '/etc/jsnapy/jsnapy.cfg'
     ]
     loc = get_config_location()
     self.assertEqual(loc, '/etc/jsnapy')
Пример #14
0
 def test_config_location_env(self, mock_is_file):
     os.environ['JSNAPY_HOME'] = '/bogus/path'
     mock_is_file.side_effect = lambda arg: arg == '/bogus/path/jsnapy.cfg'
     loc = get_config_location()
     self.assertEqual(loc, '/bogus/path')
Пример #15
0
    def get_hosts(self):
        """
        Called by main function, it extracts main config file and also check for database
        Reads the yaml config file given by user and pass the extracted data to login function to
        read device details and connect them. Also checks sqlite key to check if user wants to
        create database for snapshots
        """
        self.logger.debug(colorama.Fore.BLUE +
                "jsnapy.cfg file location used : %s" %
                get_config_location(), extra=self.log_detail)
        self.logger.debug(colorama.Fore.BLUE +
                "Configuration file location used : %s" %
                get_path('DEFAULT', 'config_file_path'), extra=self.log_detail)
                        
        if self.args.pre_snapfile is not None:
            output_file = self.args.pre_snapfile
        elif self.args.snapcheck is True and self.args.pre_snapfile is None:
            output_file = "snap_temp"
            self.snap_del = True
        else:
            output_file = ""
        conf_file = self.args.file
        check = self.args.check
        snap = self.args.snap
        if conf_file is not None:
            if os.path.isfile(conf_file):
                config_file = open(conf_file, 'r')
                self.main_file = yaml.load(config_file)
            elif os.path.isfile(os.path.join(get_path('DEFAULT', 'config_file_path'), conf_file)):
                fpath = get_path('DEFAULT', 'config_file_path')
                config_file = open(os.path.join(fpath, conf_file), 'r')
                self.main_file = yaml.load(config_file)
            else:
                self.logger.error(
                    colorama.Fore.RED +
                    "ERROR!! Config file '%s' is not present " %
                    conf_file, extra=self.log_detail)
                sys.exit(1)
        else:
            if self.args.hostname and self.args.testfiles:
                temp_dict = {'hosts':[{'device':'', 'username':'', 'passwd':''}], 'tests':[]}
                temp_dict['hosts'][0]['device'] = self.args.hostname
                temp_dict['hosts'][0]['username'] = self.args.login
                temp_dict['hosts'][0]['passwd'] = self.args.passwd
                for tfile in self.args.testfiles:
                    temp_dict['tests'].append(tfile)
                self.main_file = temp_dict


        #### if --check option is given for sqlite, then snap file name is not compulsory  ####
        #### else exit the function saying arguments not correct  ####
        if self.main_file.__contains__(
                'sqlite') and self.main_file['sqlite'] and self.main_file['sqlite'][0]:
            self.chk_database(
                self.main_file,
                self.args.pre_snapfile,
                self.args.post_snapfile,
                check,
                snap)
        else:
            if (self.args.check is True and (
                    self.args.file is None or self.args.pre_snapfile is None or self.args.post_snapfile is None)):
                self.logger.error(colorama.Fore.RED +
                                  "Arguments not given correctly, Please refer help message",
                                  extra=self.log_detail)
                self.parser.print_help()
                sys.exit(1)
        self.login(output_file)