def execute(self, args): client = get_client_from_server_conf(args.path) request = { 'return_internal': False, 'include': args.include, 'exclude': args.exclude, } response = client.invoke('zato.apispec.get-api-spec', request) data = response.data['response']['data'] now = fs_safe_now() out_dir = '{}.{}'.format('apispec', now) out_dir = os.path.abspath(out_dir) os.mkdir(out_dir) for file_path, contents in data.items(): full_file_path = os.path.join(out_dir, file_path) file_dir = os.path.abspath(os.path.dirname(full_file_path)) try: os.makedirs(file_dir) except OSError: pass # Must have been already created finally: if contents: f = open(full_file_path, 'w') f.write(contents) f.close() self.logger.info('Output saved to %s', out_dir)
def execute(self, args): client = get_client_from_server_conf(args.path) exclude = args.exclude.split(',') or [] exclude = [elem.strip() for elem in exclude] if args.with_internal: for item in internal_patterns: try: exclude.remove(item) except ValueError: pass request = { 'return_internal': args.with_internal, 'include': args.include, 'exclude': ','.join(exclude), 'needs_api_invoke': args.with_api_invoke, 'needs_rest_channels': args.with_rest_channels, } if args.with_api_invoke: request['api_invoke_path'] = args.api_invoke_path if args.api_invoke_path else '/zato/api/invoke/{service_name}' if not args.dir: now = fs_safe_now() out_dir = '{}.{}'.format('apispec', now) else: out_dir = args.dir out_dir = os.path.abspath(out_dir) if os.path.exists(out_dir): if args.delete_dir: self.logger.info('Deleting %s', out_dir) rmtree(out_dir) else: self.logger.warn('Output directory %s already exists and --delete-dir was not provided', out_dir) return os.mkdir(out_dir) response = client.invoke('zato.apispec.get-api-spec', request) data = response.data['response']['data'] for file_path, contents in data.items(): full_file_path = os.path.join(out_dir, file_path) file_dir = os.path.abspath(os.path.dirname(full_file_path)) try: os.makedirs(file_dir) except OSError: pass # Must have been already created finally: if contents: f = open(full_file_path, 'w') f.write(contents) f.close() self.logger.info('Output saved to %s', out_dir) self.logger.info('To build the documentation, run:\ncd %s\nmake html', out_dir)
def decompress(self, archive, work_dir): """ Decompresses an archive into a randomly named directory. """ # 6 characters will do, we won't deploy millions of services # in the very same (micro-)second after all rand = uuid4().hex[:6] dir_name = os.path.join(work_dir, '{}-{}'.format(fs_safe_now(), rand), os.path.split(archive)[1]) os.makedirs(dir_name) # .. unpack the archive into it .. decompress(archive, dir_name) # .. and return the name of the newly created directory so that the # rest of the machinery can pick the services up return dir_name
def backup_current_work_dir(self): # Save a few keystrokes last_backup_work_dir = self.server.hot_deploy_config.last_backup_work_dir current_work_dir = self.server.hot_deploy_config.current_work_dir backup_work_dir = self.server.hot_deploy_config.backup_work_dir backup_history = self.server.hot_deploy_config.backup_history backup_format = self.server.hot_deploy_config.backup_format # Safe to use as a directory name fs_now = fs_safe_now() # Store the last backup self._backup_last(fs_now, current_work_dir, backup_format, last_backup_work_dir) # Now store the same thing in the linear log of backups self._backup_linear_log(fs_now, current_work_dir, backup_format, backup_work_dir, backup_history)
def store_config(self, args): """ Stores the config options in a config file for a later use. """ now = util.fs_safe_now() # noqa file_name = "zato.{}.config".format(now) file_args = StringIO() for arg, value in args._get_kwargs(): if value: file_args.write("{}={}\n".format(arg, value)) body = "# {} - {}\n{}".format(now, self._get_user_host(), file_args.getvalue()) open(file_name, "w").write(body) file_args.close() self.logger.debug("Options saved in file {file_name}".format(file_name=os.path.abspath(file_name)))
def store_config(self, args): """ Stores the config options in a config file for a later use. """ now = util.fs_safe_now() # noqa file_name = 'zato.{}.config'.format(now) file_args = StringIO() for arg, value in args._get_kwargs(): if value: file_args.write('{}={}\n'.format(arg, value)) body = '# {} - {}\n{}'.format(now, self._get_user_host(), file_args.getvalue()) open(file_name, 'w').write(body) file_args.close() self.logger.debug('Options saved in file {file_name}'.format( file_name=os.path.abspath(file_name)))
def reset_logger(self, args, reload_=False): if reload_: logging.shutdown() # noqa reload(logging) # noqa self.logger = logging.getLogger(self.__class__.__name__) # noqa self.logger.setLevel(logging.DEBUG if self.verbose else logging.INFO) # noqa self.logger.handlers[:] = [] console_handler = logging.StreamHandler() # noqa console_formatter = logging.Formatter('%(message)s') # noqa console_handler.setFormatter(console_formatter) self.logger.addHandler(console_handler) if args.store_log: verbose_handler = logging.FileHandler('zato.{}.log'.format(util.fs_safe_now())) # noqa verbose_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # noqa verbose_handler.setFormatter(verbose_formatter) self.logger.addHandler(verbose_handler)
def reset_logger(self, args, reload_=False): if reload_: logging.shutdown() # noqa reload(logging) # noqa self.logger = logging.getLogger(self.__class__.__name__) # noqa self.logger.setLevel(logging.DEBUG if self.verbose else logging.INFO) # noqa self.logger.handlers[:] = [] console_handler = logging.StreamHandler(sys.stdout) # noqa console_formatter = logging.Formatter('%(message)s') # noqa console_handler.setFormatter(console_formatter) self.logger.addHandler(console_handler) if args.store_log: verbose_handler = logging.FileHandler('zato.{}.log'.format(util.fs_safe_now())) # noqa verbose_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # noqa verbose_handler.setFormatter(verbose_formatter) self.logger.addHandler(verbose_handler)