def get_apps_list(self): result = list() self.logger.debug('Everithing is working fine') session = cherrypy.session.get("sessionKey") try: service = self.create_splunk_service(session) for app in service.apps: if app.name in self.black_list: continue result.append({ 'name': app.content['label'], 'id': app.name, 'author': app.content['author'] if app.content.get('author') else '', 'version': app.content['version'] if app.content.get('version') else '', 'visible': True if 'visible' in app.content else False }) return self.render_json(result) except Exception as e: self.logger.error( "HTTPError is caught when list all apps. %s, %s\n", e, Stacktrace.get()) return self.render_json([{'err_code': 20}])
def custom_deploy(self, **params): self.logger.debug('Method begin %s \n', params) archive_path = None apps_folder = params['dist_folder'] app_name = params['app_name'] arch_name = params['archive_name'] # имя новой папки try: new_folder = apps_folder + '/' + arch_name.split('.')[0] self.logger.debug('new_folder: ', new_folder) os.makedirs(new_folder) zip_ref = zipfile.ZipFile(params['arch_path'], 'r') app_folder = zip_ref.namelist()[0] res = zip_ref.extractall(new_folder) zip_ref.close() deep_folder_path = new_folder + '/' + app_folder # Копируем все файлы на уровень выше for file in glob.glob(deep_folder_path + '/*'): self.logger.debug('file to move: %s \n', file) shutil.move(file, new_folder) # Удаляем пустую старую папку с приложением shutil.rmtree(deep_folder_path) return None, '' except Exception as e: self.logger.error("Error_custom_deploy_0 %s, %s, \n", e, Stacktrace.get()) return e, False
def common_deploy(self, **params): archive_path = None apps_folder = params['dist_folder'] app_name = params['app_name'] app_path = apps_folder + '/' + app_name try: # Checking if application folder is already existed if os.path.exists(app_path): download_dir = AppsHandlerController.make_archive_path( app_name, '/static/old_apps/') archive_path = shutil.make_archive(download_dir, 'zip', apps_folder, app_name) self.logger.debug('old application path: %s \n', archive_path) shutil.rmtree(app_path) # Extracting archive to applications folder zip_ref = zipfile.ZipFile(params['arch_path'], 'r') res = zip_ref.extractall(apps_folder) zip_ref.close() # Removing the archive os.remove(params['arch_path']) # Возвращаем название архива, если приложение уже было установлено ранее if archive_path: return None, archive_path.split('/').pop() else: return None, '' except Exception as e: self.logger.error("Error_common_deploy_0 %s, %s, \n", e, Stacktrace.get()) return e, False
def get_zipped_app(self, appname): app_name = appname self.logger.debug('_____we are here 1') self.logger.debug("app_name: %s\n", app_name) # app_name = params["app_name"] self.logger.debug('_____we are here 2') etc_path = os.environ['SPLUNK_ETC'] self.logger.debug("app_name: %s\n", app_name) download_dir = AppsHandlerController.make_archive_path(app_name) self.logger.debug("download_dir: %s\n", download_dir) archive_path = shutil.make_archive(download_dir, 'zip', self.current_app_path, app_name) self.logger.debug("archive_path: %s\n", archive_path) try: return serve_file(archive_path, "application/x-download", "attachment") except Exception as e: self.logger.error( "HTTPError is caught when list all apps. %s, %s, \n", e, Stacktrace.get()) return self.render_json([{'err_code': 20}])
def create_splunk_service(self, session_key): self.logger.debug('splunkd_uri create_splunk_service %s ', session_key) try: cfg = cli.getConfStanza('web', 'settings') managmentIP = cfg.get('mgmtHostPort') port = managmentIP.split(':')[1] self.logger.debug('managment port: %s', port) s = client.connect(token=session_key, port=port) return s except Exception as e: self.logger.error('\n' + e + ' ' + Stacktrace.get()) return self.render_json("Read request failed")
def load_old(self, archive_name): self.logger.debug('load app params: archive_name %s \n', archive_name) arch_path = AppsHandlerController.etc_path + "/apps/api_app/appserver/static/old_apps/" + archive_name if not os.path.exists(arch_path): self.logger.error('Error_load_old_0: Wrong arch path: %s \n', arch_path) return self.render_json({'res': 'err', 'descr': 'wrong_link'}) try: return serve_file(arch_path, "application/x-download", "attachment") except Exception as e: self.logger.error("Error_load_old_1. %s, %s, \n", e, Stacktrace.get()) return self.render_json({'res': 'err', 'descr': 'INTERNAL_ERROR'})
def get_value(conf_name, stanza_name, param_name, default_value, logger=None): try: stanza = cli_common.getConfStanza(conf_name, stanza_name) try: v = stanza[param_name] except: if logger is not None: logger.warn( '%s.conf->[%s]->%s not found, using default value (%s=%s)' % (conf_name, stanza_name, param_name, param_name, default_value)) v = default_value except: if logger is not None: logger.warn(Stacktrace.getn()) v = default_value return v
def upload_app_archive(self, params): try: appHandler = params['app'] cl = cherrypy.request.headers['Content-Length'] self.logger.debug('cl: %s, \n', cl) archive_name = appHandler.filename app_name = appHandler.filename.split('.')[0] download_path = self.get_archive_path(archive_name) self.logger.debug('download_path: %s \n', download_path) fileHandler = open(download_path, 'w') fileHandler.write(appHandler.file.read(int(cl))) return None, { 'archive_name': archive_name, 'download_path': download_path, 'app_name': app_name } except Exception as e: self.logger.error("Error_load_app_archive_0. %s, %s, \n", e, Stacktrace.get()) return 'INTERNAL_ERROR', {}