def create_app(): # Create application app = Flask(__name__) app.json_encoder = MongoJSONEncoder app.url_map.converters['objectid'] = ObjectIdConverter app.config['custom'] = Config().config SECRET_KEY = os.urandom(32) app.config['SECRET_KEY'] = SECRET_KEY # csrf = CSRFProtect() # csrf.init_app(app) login_manager = LoginManager(app) login_manager.login_view = 'login' app.config['login_manager'] = login_manager @login_manager.user_loader def load_user(user): return User.find_one(user) Database.initialize() register_blueprints(app) return app
def run_agent(args=None): """Creates and runs the agent.""" setup_logging(args.debug) app_conf = Config() Database.initialize() jenkins_agent = JenkinsAgent(app_conf.config['jenkins']['user'], app_conf.config['jenkins']['password'], app_conf.config['jenkins']['url']) DFG_db_lib.insert_DFG_data_to_db(DFGs) jenkins_agent.run()
def find_one(cls, name): """Returns one query result.""" query = {} if name: query['name'] = name DFGs = Database.find_one(collection="DFGs", query=query) return DFGs
def find(cls, name=None, exact_match=False, last_build_result=None, projection=None, build_number=None, query_str=None, **kwargs): """Returns find query.""" query = {} if name and not exact_match: regex = re.compile(name, re.IGNORECASE) query['name'] = regex elif name: query['name'] = name if build_number: query['builds.number'] = build_number if last_build_result: query['last_build.result'] = last_build_result for k, v in kwargs.items(): query[k] = v if query_str: query = query_str jobs = list( Database.find(collection="jobs", query=query, projection=projection)) return jobs
def find_one(cls, username): """Returns one query result.""" query = {} if username: query['username'] = username user = Database.find_one(collection="users", query=query) return user
def count(cls, name=None, last_build_res=None, DFG=None, release=None, squad=None, component=None, query_str=None): """Returns number of jobs based on passed arguments.""" query = {} if name: regex = re.compile(name, re.IGNORECASE) query['name'] = regex if last_build_res: query['last_build.status'] = last_build_res if DFG: query['DFG'] = DFG if squad: query['squad'] = squad if component: query['component'] = component if release: query['release'] = release jobs = Database.find(collection='jobs', query=query) return jobs.count()
def count(cls, squads=False): """Returns the count of DFGs documents.""" query = {} if squads: return len(cls.get_all_squads()) else: DFGs = Database.find(collection='DFGs', query=query) return DFGs.count()
def get_all_DFGs_based_on_jobs(cls): """Returns a list of all DFGs based on job model where it cuts the DFG name from the job name and makes sure the set is unique. """ DFGs = [] regex = re.compile('DFG-', re.IGNORECASE) DFG_jobs = Database.find(collection='jobs', query={'name': regex}) for job in DFG_jobs: jname = job['name'] DFG_name = jname.split('-')[1] if '-' in jname else jname if DFG_name not in DFGs: DFGs.append(DFG_name) return DFGs
def find(cls, name=None, exact_match=False, last_build_result=None, projection=None, build_number=None, query_str=None, **kwargs): """Returns find query.""" query = {} if build_number: query['builds.number'] = build_number if last_build_result: query['last_build.result'] = last_build_result for k, v in kwargs.items(): query[k] = v if name: regex = re.compile(name, re.IGNORECASE) query['name'] = regex if query_str and "last_added" not in query_str: if "name" in query_str: regex = re.compile(query_str['name'], re.IGNORECASE) query_str['name'] = regex query = query_str if query_str and "last_added" in query_str: jobs = list( Database.find(collection="jobs", query={ 'last_build.status': 'None', 'release': "15" }, projection=projection).limit(10)) else: jobs = list( Database.find(collection="jobs", query=query, projection=projection)) return jobs
def insert(self): job = Database.find_one("jobs", {"name": self.name}) if not job: Database.insert(collection='jobs', data=self.json()) else: # Check if last build is in job builds. If not, add it. if not Database.DATABASE['jobs'].find_one( { "builds.number": self.last_build['number'], "name": self.name }): Database.DATABASE['jobs'].update( {"name": self.name}, {"$addToSet": { "builds": self.last_build }}) # Update job # Database.DATABASE['jobs'].find_one_and_update( # {"name": self.name}, # {"$set": self.properties}) Database.DATABASE['jobs'].find_one_and_update( {"name": self.name}, {"$set": { 'lol': 'lol2' }})
def insert(self): """Inserts object to the database.""" if not Database.find_one("users", {"username": self.username}): Database.insert(collection='users', data=self.json())
def find(cls): """Returns find query.""" query = {} builds = Database.find(collection="builds", query=query) return builds
def count(cls): """Returns the count of builds documents.""" query = {} builds = Database.find(collection='builds', query=query) return builds.count()
def find(cls): """Returns find query.""" query = {} tests = Database.find(collection=cls.COLLECTION, query=query) return tests
def insert(self): if not Database.find_one(self.COLLECTION, { "build_number": self.build_number, "job_name": self.job_name }): Database.insert(collection='builds', data=self.json())
def delete_one(cls, name): """Deletes one job document from the database.""" query = {"name": name} Database.delete_one("jobs", query)
def find(cls): """Returns find query.""" query = {} DFGs = Database.find(collection="DFGs", query=query) return DFGs