def run(self): abort = self.get_abort() harc = DatasourceBuilder.build('harc-ds.json') logger = Logger(self) job_repository = JobRepository(harc) job = job_repository.job() job_name = job['job_name'] job_repository.close() settings = self.get_settings() threads = self.get_threads() while self.is_running(): try: watcher = self.get_watcher() watcher.watch(settings, threads, abort) # check if we need to abort, can be called from the main thread or other thread aborted = abort.is_aborted() self.running(not aborted) time.sleep(settings['watcher_idle_delay']) except: aborted = abort.is_aborted() self.running(not aborted) result = Traceback.build() logger.fatal(job_name, result['message'], result['backtrace']) time.sleep(settings['worker_exception_delay'])
def execute(arguments, settings, properties): job_name = properties['job_name'] logger = Logger(StartService()) try: s = arguments.s environment = arguments.e project = arguments.n if not s: raise PluginException("no service") if not environment: raise PluginException("no environment") if not project: raise PluginException("no project") details = settings[environment] for detail in details: if detail['name'] == project: services = detail['services'] for service in services: if service['name'] == s: service_password = service['password'] result = Systemctl.start(s, service_password) logger.info(job_name, str(result)) except: result = Traceback.build() logger.fatal(job_name, result['message'], result['backtrace'])
def post(self): logger = Logger(self) job_name = '' try: harc = DatasourceBuilder.build("harc-ds.json") content = request.get_json(silent=True) logger.info(job_name, json.dumps(content)) session = content['session'] session_repository = SessionRepository(harc) if not session_repository.valid(session): abort(403, message='failed', reason='session not found', job_name=job_name) job_name = content['job_name'] log_repository = LogRepository(harc) logs = log_repository.list_by_job_name(job_name) return logs except: result = Traceback.build() logger.fatal('', result['message'], result['backtrace']) abort(500, message='failed', reason=result['message'], backtrace=result['backtrace'], job_name=job_name)
def find_by_name(self, content): logger = Logger(self) connection = self.get_connection() cursor = connection.cursor() statement = "select * from commands where name=:name and environment=:environment" m = {} m['name'] = content['command'] m['environment'] = content['environment'] statement, parameters = self.statement(statement, m) cursor.execute(statement, parameters) result = Helper.cursor_to_json(cursor) if len(result) == 0: return None return result[0]
def execute(arguments, settings, properties): job_name = properties['job_name'] logger = Logger(GitDeploy()) try: version = arguments.v environment = arguments.e project_name = arguments.n if not version: raise PluginException("no version") if not project_name: raise PluginException("no project") # if no environment is given, dev is assumed. if not environment: environment = "dev" logger.info(job_name, "using environment : " + environment) project = Settings.find_project(settings, project_name) if not project: message = "project " + project_name + " not found" raise PluginException(message) details = settings[environment] username = project['username'] password = project['password'] repository = project['repository'].format(urllib.quote(username), urllib.quote(password)) for detail in details: if detail['name'] == project['name']: virtualenv = detail['virtualenv'] result = Pip.install(repository, version, project, virtualenv) logger.info(job_name, str(result)) except: result = Traceback.build() logger.fatal(job_name, result['message'], result['backtrace'])
def post(self): logger = Logger(self) try: content = request.get_json(silent=True) logger.trace("", json.dumps(content)) harc = DatasourceBuilder.build("harc-ds.json") user_repository = UserRepository(harc) result = user_repository.login(content['username'], content['password']) logger.trace("", str(result)) if not result: return {"message": "failed", "reason": "you do not have permission", "backtrace": ""}, 403 return {"message": "success", "session": result} except: result = Traceback.build() logger.fatal('', result['message'], result['backtrace']) abort(500, message='failed', reason=result['message'], backtrace=result['backtrace'])
def post(self): logger = Logger(self) job_name = '' try: harc = DatasourceBuilder.build("harc-ds.json") job_repository = JobRepository(harc) job = job_repository.job() job_name = job['job_name'] content = request.get_json(silent=True) logger.info(job_name, json.dumps(content)) session = content['session'] session_repository = SessionRepository(harc) if not session_repository.valid(session): abort(403, message='failed', reason='session not found', job_name=job_name) content['job_name'] = job_name Message.enqueue('messages', content, 'jobs', 'harc.consumers.ExecuteJob.ExecuteJob') # parser = HarcCliArguments("Harc, hit and release code") # args = parser.parse_content(content) # # os.path.abspath('.') # # data = open("harc.json") # settings = json.load(data) # # properties = {} # properties['harc_dir'] = os.path.abspath('.') # properties['job_name'] = job_name # plugin = PluginFactory.create_plugin(args.command) # # path, filename = os.path.split(inspect.getfile(plugin)) # properties['plugin_dir'] = path # # plugin.execute(args, settings, properties) return {"message": "success", "job_name": job_name} except: result = Traceback.build() logger.fatal('', result['message'], result['backtrace']) abort(500, message='failed', reason=result['message'], backtrace=result['backtrace'], job_name=job_name)
def run(self, queue): abort = self.get_abort() claim = self.get_claim() stop = self.get_stop() settings = self.get_settings() logger = Logger(self) job_name = '' message = None while self.is_running(): try: job_name = '' message = None logger = Logger(self) # retrieve the next message datasource = queue['datasource'] queue_ds = DatasourceBuilder.find(datasource) message_repository = MessageRepository(queue_ds) message = message_repository.dequeue(claim, queue) if message: consumer = ClassLoader.find(message['consumer']) consumer.action(message) message_repository.state(queue, message, 'PROCESSED') # no message to process if not message: time.sleep(settings['worker_idle_delay']) # check if we need to abort, can be called from the main thread or other thread aborted = abort.is_aborted() self.running(not aborted) # check if we need to stop, will be set by the agent's WatchWorker thread if not aborted: stopped = stop.is_stopped() self.running(not stopped) logger.trace(job_name, 'worker #' + str(self.get_id()) + " executed.") except: aborted = abort.is_aborted() self.running(not aborted) if not aborted: stopped = stop.is_stopped() self.running(not stopped) result = Traceback.build() if message: try: datasource = queue['datasource'] queue_ds = DatasourceBuilder.find(datasource) message_repository = MessageRepository(queue_ds) result['id'] = message['id'] message_repository.state(queue, result, 'FAILED') message_repository.close() except: logger.fatal(job_name, 'Failed to persist message failure') logger.fatal(job_name, result['message'], result['backtrace']) time.sleep(settings['worker_exception_delay'])