def checkPool(): ret = True try : pool = MscDatabase().db.pool if pool._max_overflow > -1 and pool._overflow >= pool._max_overflow : logging.getLogger().error('scheduler %s: CHECK: NOK: timeout then overflow (%d vs. %d) detected in SQL pool : check your network connectivity !' % (SchedulerConfig().name, pool._overflow, pool._max_overflow)) pool.dispose() pool = pool.recreate() ret = False except Exception, e: logging.getLogger().warn('scheduler %s: CHECK: NOK: got the following error : %s' % (SchedulerConfig().name, e)) ret = False
def get_commands_stats(scheduler_name, cmd_id=None): database = MscDatabase() session = create_session() now = time.strftime("%Y-%m-%d %H:%M:%S") query = session.query(CommandsOnHost.fk_commands, CommandsOnHost.current_state, func.count(CommandsOnHost.current_state)) query = query.select_from(database.commands_on_host.join( database.commands)) if cmd_id: query = query.filter(database.commands.c.id == cmd_id) else: query = query.filter(database.commands.c.end_date > now) query = query.filter(database.commands.c.start_date < now) query = query.filter( or_(database.commands_on_host.c.scheduler == '', database.commands_on_host.c.scheduler == scheduler_name, database.commands_on_host.c.scheduler == None)) query = query.group_by(database.commands_on_host.c.fk_commands, database.commands_on_host.c.current_state) ret = [q for q in query.all()] session.close() return ret
def get_all_phases(id): database = MscDatabase() session = create_session() phases = session.query(CommandsOnHostPhase) phases = phases.filter(database.commands_on_host_phase.c.fk_commands_on_host == id) session.close() return phases.all()
def get_phases(self): database = MscDatabase() session = create_session() phases = session.query(CommandsOnHostPhase) phases = phases.filter(database.commands_on_host_phase.c.fk_commands_on_host == self._id) phases = phases.filter(database.commands_on_host_phase.c.state != "done") session.close() return [q.name for q in phases.all()]
def get_pull_targets(scheduler_name, uuids): database = MscDatabase() session = create_session() query = session.query(PullTargets) query = query.select_from(database.pull_targets) query = query.filter(database.pull_targets.c.scheduler == scheduler_name) query = query.filter(database.pull_targets.c.target_uuid.in_(uuids)) session.close() return query.all()
def get_commands(cohs): database = MscDatabase() session = create_session() query = session.query(CommandsOnHost) query = query.filter(database.commands_on_host.c.id.in_(cohs)) session.close() cmd_ids = [] [cmd_ids.append(q.fk_commands) for q in query.all() if q.fk_commands not in cmd_ids] return cmd_ids
def get_cohs_with_failed_phase(id, phase_name): database = MscDatabase() session = create_session() query = session.query(CommandsOnHost).\ select_from(database.commands_on_host.join(database.commands).\ join(database.commands_on_host_phase) ).filter(database.commands.c.id == id ).filter(database.commands_on_host_phase.c.name==phase_name ).filter(database.commands_on_host_phase.c.state=="failed") return [q.id for q in query.all()] session.close()
def get_phase(self, name): database = MscDatabase() session = create_session() phase = session.query(CommandsOnHostPhase) phase = phase.filter(database.commands_on_host_phase.c.fk_commands_on_host == self._id) phase = phase.filter(database.commands_on_host_phase.c.name == name) phase = phase.first() session.close() if isinstance(phase, list): return phase[0] else : return phase
def get_starting_phase(self): database = MscDatabase() session = create_session() phases = session.query(CommandsOnHostPhase) phases = phases.filter(database.commands_on_host_phase.c.fk_commands_on_host == self._id) phases = phases.filter(database.commands_on_host_phase.c.state != "done") phases = phases.order_by(database.commands_on_host_phase.c.phase_order) session.close() for phase in phases.all(): if phase.state in ("ready","running", "failed"): return phase.name else : return None
def in_pull_targets(scheduler_name, uuid): database = MscDatabase() session = create_session() query = session.query(PullTargets) query = query.select_from(database.pull_targets) query = query.filter(database.pull_targets.c.scheduler == scheduler_name) query = query.filter(database.pull_targets.c.target_uuid == uuid) session.close() if query.first(): return True else: return False
def verify_target(coh_id, hostname, mac): database = MscDatabase() session = create_session() query = session.query(CommandsOnHost, Target) query = query.select_from(database.commands_on_host.join(database.commands).join(database.target)) query = query.filter(database.commands_on_host.c.id==coh_id) query = query.filter(database.target.c.target_name==hostname) query = query.filter(database.target.c.target_macaddr.like("%%%s%%" % mac)) session.close() if query.first() : return True else : return False
def get_history_stdout(coh_id, phase): database = MscDatabase() session = create_session() query = session.query(CommandsHistory) query = query.select_from(database.commands_history) query = query.filter(database.commands_history.c.phase == phase) query = query.filter(database.commands_history.c.fk_commands_on_host == coh_id) stdout = "" for q in query.all(): stdout += q.stdout session.close() return stdout
def get_ids_to_start(scheduler_name, ids_to_exclude=[], top=None): # Mutable list ids_to_exclude used as default argument to a method or function database = MscDatabase() session = create_session() now = time.strftime("%Y-%m-%d %H:%M:%S") soon = time.strftime("0000-00-00 00:00:00") commands_query = session.query(Commands, CommandsOnHost, Target, PullTargets).\ select_from(database.commands_on_host.join(database.commands).join(database.target). outerjoin(database.pull_targets, Target.target_uuid == database.pull_targets.c.target_uuid) ).filter(not_(database.commands_on_host.c.current_state.in_(("failed", "over_timed", "done", "stopped"))) ).filter(database.commands_on_host.c.next_launch_date <= now ).filter(or_(database.commands.c.start_date == soon, database.commands.c.start_date <= now) ).filter(or_(database.commands.c.end_date == soon, database.commands.c.end_date > now) ).filter(or_(database.commands_on_host.c.scheduler == '', database.commands_on_host.c.scheduler == scheduler_name, database.commands_on_host.c.scheduler == None) ).filter(database.pull_targets.c.target_uuid == None ).filter(database.commands.c.ready == True) if len(ids_to_exclude) > 0: commands_query = commands_query.filter( not_(database.commands_on_host.c.id.in_(ids_to_exclude))) commands_query = commands_query.order_by( database.commands.c.order_in_bundle.asc(), database.commands_on_host.c.current_state.desc()) if top: commands_query = commands_query.limit(top) # IMPORTANT NOTE : This ordering is not alphabetical! # Field 'current_state' is ENUM type, so decisive condition # is order of element in the declaration of field. # Because this order of elements is suitable on workflow, # using of descending order allows to favouring the commands # which state is approaching to end of worklow. commands = commands_query.all() commands_to_perform = [ coh.id for (cmd, coh, target, pt) in commands if cmd.inDeploymentInterval() ] session.close() return commands_to_perform
def getHealth(): # take basic informations health = basicHealth() try: # add data about the current database connections pool pool = MscDatabase().db.pool health['db'] = { 'poolsize' : str(pool.size()), 'checkedinconns' : str(pool.checkedin()), 'overflow' : str(pool.overflow()), 'checkedoutconns': str(pool.checkedout()), 'recycle' : str(pool._recycle) } except Exception, e: logging.getLogger().warn('scheduler %s: HEALTH: got the following error : %s' % (SchedulerConfig().name, e))
def get_cohs(cmd_id, scheduler): database = MscDatabase() session = create_session() cohs = session.query(CommandsOnHost, Target, PullTargets) cohs = cohs.select_from(database.commands_on_host.join(database.target). outerjoin(database.pull_targets, Target.target_uuid == database.pull_targets.c.target_uuid)) cohs = cohs.filter(database.commands_on_host.c.fk_commands == cmd_id) cohs = cohs.filter(database.commands_on_host.c.scheduler == scheduler) cohs = cohs.filter(database.pull_targets.c.target_uuid == None) cohs = cohs.filter(not_(database.commands_on_host.c.current_state.in_(("failed", "over_timed", "done", "stopped")))) commands_to_perform = [coh.id for (coh, target, pt) in cohs.all()] session.close() return commands_to_perform
def pull_target_update(scheduler_name, uuid): database = MscDatabase() session = create_session() now = time.strftime("%Y-%m-%d %H:%M:%S") query = session.query(PullTargets) query = query.select_from(database.pull_targets) query = query.filter(database.pull_targets.c.target_uuid == uuid) query = query.filter(database.pull_targets.c.scheduler == scheduler_name) pt = query.first() session.close() if not pt: pt = PullTargets() pt.target_uuid = uuid pt.scheduler = scheduler_name pt.last_seen_time = now pt.flush()
def is_command_finished(scheduler_name, id): database = MscDatabase() session = create_session() now = time.strftime("%Y-%m-%d %H:%M:%S") query = session.query(CommandsOnHost).\ select_from(database.commands_on_host.join(database.commands) ).filter(not_(database.commands_on_host.c.current_state.in_(("failed", "over_timed", "done"))) ).filter(database.commands.c.id == id ).filter(database.commands.c.end_date < now ).filter(or_(database.commands_on_host.c.scheduler == '', database.commands_on_host.c.scheduler == scheduler_name, database.commands_on_host.c.scheduler == None)) nbr = query.count() session.close() if nbr > 1: return False else: return True
def process_non_valid(scheduler_name, non_fatal_steps, ids_to_exclude=[]): # Mutable list ids_to_exclude used as default argument to a method or function database = MscDatabase() session = create_session() now = time.strftime("%Y-%m-%d %H:%M:%S") commands_query = session.query(CommandsOnHost).\ select_from(database.commands_on_host.join(database.commands) ).filter(not_(database.commands_on_host.c.current_state.in_(("failed", "over_timed", "done"))) ).filter(database.commands.c.end_date < now ).filter(or_(database.commands_on_host.c.scheduler == '', database.commands_on_host.c.scheduler == scheduler_name, database.commands_on_host.c.scheduler == None)) #commands_query = commands_query.limit(top) if len(ids_to_exclude) > 0: commands_query = commands_query.filter( not_(database.commands_on_host.c.id.in_(ids_to_exclude))) fls = [] otd = [] for q in commands_query.all(): if any_failed(q.id, non_fatal_steps) or q.attempts_failed > 0: fls.append(q.id) else: otd.append(q.id) #yield q.id session.close() if len(otd) > 0: logging.getLogger().info("Switching %d circuits to overtimed" % len(otd)) CoHManager.setCoHsStateOverTimed(otd) if len(fls) > 0: logging.getLogger().info("Switching %d circuits to failed" % len(fls)) CoHManager.setCoHsStateFailed(fls)
def __available_downloads_query(scheduler_name, uuid): database = MscDatabase() session = create_session() now = time.strftime("%Y-%m-%d %H:%M:%S") soon = time.strftime("0000-00-00 00:00:00") commands_query = session.query(CommandsOnHost, Commands, Target, PullTargets).\ select_from(database.commands_on_host.join(database.commands).join(database.target). \ outerjoin(database.pull_targets, Target.target_uuid == database.pull_targets.c.target_uuid) ).filter(not_(database.commands_on_host.c.current_state.in_(("failed", "over_timed", "done", "stopped"))) ).filter(database.commands_on_host.c.next_launch_date <= now ).filter(or_(database.commands.c.start_date == soon, database.commands.c.start_date <= now) ).filter(or_(database.commands.c.end_date == soon, database.commands.c.end_date > now) ).filter(or_(database.commands_on_host.c.scheduler == '', database.commands_on_host.c.scheduler == scheduler_name, database.commands_on_host.c.scheduler == None) ).filter(database.target.c.target_uuid == uuid ).filter(database.pull_targets.c.target_uuid == uuid) session.close() return commands_query
def deploypackage(data, message, xmppobject): machine = XmppMasterDatabase().getMachinefromjid(message['from']) print json.dumps(machine, indent=4) nameuser = "******" % (machine['lastuser'], machine['hostname']) command = MscDatabase().createcommanddirectxmpp( data['uuid'], '', '', 'malistetodolistfiles', 'enable', 'enable', datetime.datetime.now(), datetime.datetime.now() + datetime.timedelta(hours=1), nameuser, nameuser, 'titlepackage', 60, 4, 0, '', None, None, None, 'none', 'active', '1', cmd_type=0) commandid = command.id commandstart = command.start_date commandstop = command.end_date jidrelay = machine['groupdeploy'] uuidmachine = machine['uuid_inventorymachine'] jidmachine = machine['jid'] # try: #target = MscDatabase().xmpp_create_Target(uuidmachine, machine['hostname']) # except Exception as e: # print str(e) # traceback.print_exc(file=sys.stdout) XmppMasterDatabase().addlogincommand(nameuser, commandid, "", "", "", "", "", 0, 0, 0, {}) sessionid = name_random(5, "deploykiosk_") name = managepackage.getnamepackagefromuuidpackage(data['uuid']) path = managepackage.getpathpackagename(name) descript = managepackage.loadjsonfile(os.path.join(path, 'xmppdeploy.json')) parsexmppjsonfile(os.path.join(path, 'xmppdeploy.json')) if descript is None: logger.error("deploy %s on %s error : xmppdeploy.json missing" % (data['uuid'], machine['hostname'])) return None objdeployadvanced = XmppMasterDatabase().datacmddeploy(commandid) datasend = { "name": name, "login": nameuser, "idcmd": commandid, "advanced": objdeployadvanced, 'methodetransfert': 'pushrsync', "path": path, "packagefile": os.listdir(path), "jidrelay": jidrelay, "jidmachine": jidmachine, "jidmaster": xmppobject.boundjid.bare, "iprelay": XmppMasterDatabase().ipserverARS(jidrelay)[0], "ippackageserver": XmppMasterDatabase().ippackageserver(jidrelay)[0], "portpackageserver": XmppMasterDatabase().portpackageserver(jidrelay)[0], "ipmachine": XmppMasterDatabase().ipfromjid(jidmachine)[0], "ipmaster": xmppobject.config.Server, "Dtypequery": "TQ", "Devent": "DEPLOYMENT START", "uuid": uuidmachine, "descriptor": descript, "transfert": True } # run deploy sessionid = xmppobject.send_session_command(jidrelay, "applicationdeploymentjson", datasend, datasession=None, encodebase64=False) # add deploy in table. XmppMasterDatabase().adddeploy(commandid, machine['jid'], # jidmachine machine['groupdeploy'], # jidrelay, machine['hostname'], # host, machine['uuid_inventorymachine'], # inventoryuuid, data['uuid'], # uuidpackage, 'DEPLOYMENT START', # state, sessionid, # id session, nameuser, # user nameuser, # login name + " " + \ commandstart.strftime("%Y/%m/%d/ %H:%M:%S"), # title, "", # group_uuid commandstart, # startcmd commandstop, # endcmd machine['macaddress']) xmppobject.xmpplog("Start deploy on machine %s" % jidmachine, type='deploy', sessionname=sessionid, priority=-1, action="", who=nameuser, how="", why=xmppobject.boundjid.bare, module="Deployment | Start | Creation", date=None, fromuser=nameuser, touser="")