def update_avail(node, new_state, monit_state, timestamp, reason, logger, update=True, auth=None): sv_point = transaction.savepoint() try: strt = p_task_timing_start(logger, "UpdateAvailability", node.id, log_level="DEBUG") #there is a status change, update and send event #update current availability, #we only update avail-state, monit_state is updated #only by user actions node.current_state.avail_state = new_state node.current_state.timestamp = timestamp node.current_state.description = reason avh=DBSession.query(AvailHistory).filter(AvailHistory.entity_id==node.id).\ order_by(AvailHistory.timestamp.desc()).first() if avh is not None: avh.endtime=timestamp time_diff=timestamp-avh.timestamp avh.period=time_diff.days*24*60+time_diff.seconds/60 DBSession.add(avh) #insert availability history ah = AvailHistory(node.id, new_state, monit_state, timestamp, reason) DBSession.add(ah) if update==True: ent = DBSession.query(Entity).filter(Entity.entity_id==node.id).first() from convirt.model.ManagedNode import ManagedNode if ent.type.name == constants.MANAGED_NODE: if new_state == ManagedNode.DOWN: notify_node_down(ent.name, reason) else: node_up_action(auth, node.id) except Exception, e: #defer to next time import traceback traceback.print_exc() logger.error(e) sv_point.rollback()
def add_group_defn(self, group_id, def_id, def_type, status, oos_count): #Check whether the record is already present... row = DBSession.query(SPDefLink).filter_by(group_id = group_id, def_id = def_id).first() if not row: SPDL = SPDefLink() SPDL.group_id = group_id SPDL.def_type = def_type SPDL.def_id = def_id SPDL.status = status SPDL.oos_count = oos_count SPDL.dt_time = datetime.utcnow() DBSession.add(SPDL)
def setup(self): try: new_attrs = {} new_attrs.update(self.attrs) new_attrs.update(self.do_get_dependencies()) self.obj = self.klass(**new_attrs) DBSession.add(self.obj) DBSession.flush() return self.obj except: DBSession.rollback() raise
def add_site_defn(self, site_id, def_id, def_type, status, oos_count): #Check whether the record is already present... row = DBSession.query(DCDefLink).filter_by(site_id = site_id, def_id = def_id).first() if not row: DCDL = DCDefLink() DCDL.site_id = site_id DCDL.def_type = def_type DCDL.def_id = def_id DCDL.status = to_unicode(status) DCDL.oos_count = oos_count DCDL.dt_time = datetime.utcnow() DBSession.add(DCDL)
def add_node_defn(self, node_id, def_id, def_type, status, details): #Check whether the record is already present... row = DBSession.query(ServerDefLink).filter_by(server_id = node_id, def_id = def_id).first() if not row: node_defn = ServerDefLink() node_defn.server_id = to_unicode(node_id) node_defn.def_type = to_unicode(def_type) node_defn.def_id = def_id node_defn.status = to_unicode(status) node_defn.details = to_unicode(details) node_defn.dt_time = datetime.utcnow() DBSession.add(node_defn)
def add_node_defn(self, node_id, def_id, def_type, status, details): #Check whether the record is already present... row = DBSession.query(ServerDefLink).filter_by(server_id=node_id, def_id=def_id).first() if not row: node_defn = ServerDefLink() node_defn.server_id = to_unicode(node_id) node_defn.def_type = to_unicode(def_type) node_defn.def_id = def_id node_defn.status = to_unicode(status) node_defn.details = to_unicode(details) node_defn.dt_time = datetime.utcnow() DBSession.add(node_defn)
def add_site_defn(self, site_id, def_id, def_type, status, oos_count): #Check whether the record is already present... row = DBSession.query(DCDefLink).filter_by(site_id=site_id, def_id=def_id).first() if not row: DCDL = DCDefLink() DCDL.site_id = site_id DCDL.def_type = def_type DCDL.def_id = def_id DCDL.status = to_unicode(status) DCDL.oos_count = oos_count DCDL.dt_time = datetime.utcnow() DBSession.add(DCDL)
def update_execution_context(self): """ storing context in task for resume process """ tid = TaskUtil.get_task_context() WRK_LOGGER.debug("in update_execution_context Parent task : "+str(tid)+" : child tasks :"+str(self.worker_ids)) task=Task.get_task(tid) if task is not None: self.execution_context["start_time"]=self.start_time self.execution_context["worker_ids"]=self.worker_ids task.context["execution_context"]=self.execution_context DBSession.add(task) WRK_LOGGER.debug("in update_execution_context updated Parent task : "+str(tid))
def get_lock(self,sub_system,entity_id,operation,table_name): # select query with lock lock_m=DBSession.query(CMS_Locks).with_lockmode("update").\ filter(CMS_Locks.sub_system==sub_system).\ filter(CMS_Locks.entity_id==entity_id).\ filter(CMS_Locks.operation==operation).\ filter(CMS_Locks.table_name==table_name).all() # lock_m=DBSession.query(CMS_Locks).with_lockmode("update").\ # filter(CMS_Locks.table_name==table_name).first() if len(lock_m) == 0: lm=CMS_Locks(sub_system,entity_id,operation,table_name) DBSession.add(lm)
def get_lock(self, sub_system, entity_id, operation, table_name): # select query with lock lock_m=DBSession.query(CMS_Locks).with_lockmode("update").\ filter(CMS_Locks.sub_system==sub_system).\ filter(CMS_Locks.entity_id==entity_id).\ filter(CMS_Locks.operation==operation).\ filter(CMS_Locks.table_name==table_name).all() # lock_m=DBSession.query(CMS_Locks).with_lockmode("update").\ # filter(CMS_Locks.table_name==table_name).first() if len(lock_m) == 0: lm = CMS_Locks(sub_system, entity_id, operation, table_name) DBSession.add(lm)
def add_group_defn(self, group_id, def_id, def_type, status, oos_count): #Check whether the record is already present... row = DBSession.query(SPDefLink).filter_by(group_id=group_id, def_id=def_id).first() if not row: SPDL = SPDefLink() SPDL.group_id = group_id SPDL.def_type = def_type SPDL.def_id = def_id SPDL.status = status SPDL.oos_count = oos_count SPDL.dt_time = datetime.utcnow() DBSession.add(SPDL)
def send_deployment_stats(self): #task_service = self.svc_central.get_service(self.task_service_id) t = SendDeploymentStatsTask(u'Send Deployment Stats', {'quiet':True}, [],\ dict(), None, u'admin') dc_ent = DBSession.query(Entity).filter(Entity.type_id==1).first() t.set_entity_info(dc_ent) t.set_interval(TaskInterval(interval=None, next_execution=datetime.utcnow())) DBSession.add(t) import transaction transaction.commit() logger.debug("SendDeploymentStatsTask Submitted") return t.task_id
def send_deployment_stats(self): #task_service = self.svc_central.get_service(self.task_service_id) t = SendDeploymentStatsTask(u'Send Deployment Stats', {'quiet':True}, [],\ dict(), None, u'admin') dc_ent = DBSession.query(Entity).filter(Entity.type_id == 1).first() t.set_entity_info(dc_ent) t.set_interval( TaskInterval(interval=None, next_execution=datetime.utcnow())) DBSession.add(t) import transaction transaction.commit() logger.debug("SendDeploymentStatsTask Submitted") return t.task_id
def update_execution_context(self): """ storing context in task for resume process """ tid = TaskUtil.get_task_context() WRK_LOGGER.debug("in update_execution_context Parent task : " + str(tid) + " : child tasks :" + str(self.worker_ids)) task = Task.get_task(tid) if task is not None: self.execution_context["start_time"] = self.start_time self.execution_context["worker_ids"] = self.worker_ids task.context["execution_context"] = self.execution_context DBSession.add(task) WRK_LOGGER.debug( "in update_execution_context updated Parent task : " + str(tid))
def save_email_setup_details(self, desc, servername, port, useremail, password, secure): #SiteRecord = DBSession.query(Site).filter(Site.name == 'Data Center').first() SiteRecord = DBSession.query(Site).first() if SiteRecord: site_id = SiteRecord.id # check if record for same server name avoid duplicate records of same name EmailRecord = DBSession.query(EmailSetup).filter(EmailSetup.site_id == site_id).filter(EmailSetup.mail_server == servername).first() if EmailRecord: return dict(success=True,msg="Duplicaate Record found in list") else: # Add record in EmailSetup table for site id queried email_setup_obj = EmailSetup(servername, desc, port, secure, site_id, useremail, password ) DBSession.add(email_setup_obj) emailsetupid = email_setup_obj.getEmailSetupId() EmailManager().add_entity(to_unicode(servername), emailsetupid, to_unicode(constants.EMAIL), None) return dict(success=True, msg="New Record Added Successfully")
def exec_task(self, auth, ctx): manager = Basic.getGridManager() groups = manager.getGroupList(auth) for group in groups: nodes = manager.getNodeList(auth, group.id) for n in nodes: try: transaction.begin() n=DBSession.query(ManagedNode).filter(ManagedNode.id==n.id).one() n.refresh_environ() n.get_running_vms() n.socket=n.get_socket_info() DBSession.add(n) transaction.commit() except Exception, e: LOGGER.error(to_str(e)) DBSession.rollback()
def update_avail(node, new_state, monit_state, timestamp, reason, logger, update=True, auth=None): sv_point = transaction.savepoint() try: strt = p_task_timing_start(logger, "UpdateAvailability", node.id, log_level="DEBUG") #there is a status change, update and send event #update current availability, #we only update avail-state, monit_state is updated #only by user actions node.current_state.avail_state = new_state node.current_state.timestamp = timestamp node.current_state.description = reason avh=DBSession.query(AvailHistory).filter(AvailHistory.entity_id==node.id).\ order_by(AvailHistory.timestamp.desc()).first() if avh is not None: avh.endtime = timestamp time_diff = timestamp - avh.timestamp avh.period = time_diff.days * 24 * 60 + time_diff.seconds / 60 DBSession.add(avh) #insert availability history ah = AvailHistory(node.id, new_state, monit_state, timestamp, reason) DBSession.add(ah) if update == True: ent = DBSession.query(Entity).filter( Entity.entity_id == node.id).first() from convirt.model.ManagedNode import ManagedNode if ent.type.name == constants.MANAGED_NODE: if new_state == ManagedNode.DOWN: notify_node_down(ent.name, reason) else: node_up_action(auth, node.id) except Exception, e: #defer to next time import traceback traceback.print_exc() logger.error(e) sv_point.rollback()
def exec_task(self, auth, ctx): manager = Basic.getGridManager() groups = manager.getGroupList(auth) for group in groups: nodes = manager.getNodeList(auth, group.id) for n in nodes: try: transaction.begin() n=DBSession.query(ManagedNode).filter(ManagedNode.id==n.id).one() n.refresh_environ() n.get_running_vms() n.socket=n.get_socket_info() if n.is_up(): n.isHVM = n.is_HVM() DBSession.add(n) transaction.commit() except Exception, e: LOGGER.error(to_str(e)) DBSession.rollback()
def save_email_setup_details(self, desc, servername, port, useremail, password, secure): SiteRecord = DBSession.query(Site).filter(Site.name == "Data Center").first() if SiteRecord: site_id = SiteRecord.id # check if record for same server name avoid duplicate records of same name EmailRecord = ( DBSession.query(EmailSetup) .filter(EmailSetup.site_id == site_id) .filter(EmailSetup.mail_server == servername) .first() ) if EmailRecord: return dict(success=True, msg="Duplicaate Record found in list") else: # Add record in EmailSetup table for site id queried email_setup_obj = EmailSetup(servername, desc, port, secure, site_id, useremail, password) DBSession.add(email_setup_obj) emailsetupid = email_setup_obj.getEmailSetupId() EmailManager().add_entity(to_unicode(servername), emailsetupid, to_unicode(constants.EMAIL), None) return dict(success=True, msg="New Record Added Sucessfully")
class AppUpdateManager: def __init__(self): #self.update_url = "http://www.convirture.com/updates/updates.xml" self.update_url = "http://127.0.0.1:8081/updates/updates.xml" self.updates_file = "/var/cache/convirt/updates.xml" def check_for_updates(self, send_mail=False): update_items = [] dep = None try: from convirt.model import Deployment deps = DBSession.query(Deployment).all() if len(deps) > 0: dep = deps[0] #update_items=retrieve_updates(dep.deployment_id) edition = get_product_edition() (new_updates, max_dt) = self.get_new_updates(dep.deployment_id, dep.update_checked_date, edition) if send_mail and new_updates: self.send_update_mails(new_updates) else: LOGGER.error( "Deployment table is not set. Update can not proceed.") return update_items except Exception, ex: traceback.print_exc() LOGGER.error("Error fetching updates:" + to_str(ex)) return update_items #update deployment table try: dep.update_checked_date = datetime.utcnow() DBSession.add(dep) except: pass return update_items
class RefreshNodeInfoTask(Task): def get_descriptions(self): short_desc = m_("Refresh Task for All Nodes") return (short_desc, (), short_desc, ()) def exec_task(self, auth, ctx): manager = Basic.getGridManager() groups = manager.getGroupList(auth) for group in groups: nodes = manager.getNodeList(auth, group.id) for n in nodes: try: transaction.begin() n=DBSession.query(ManagedNode).filter(ManagedNode.id==n.id).one() n.refresh_environ() n.get_running_vms() n.socket=n.get_socket_info() if n.is_up(): n.isHVM = n.is_HVM() DBSession.add(n) transaction.commit() except Exception, e: LOGGER.error(to_str(e)) DBSession.rollback() ungrouped_nodes = manager.getNodeList(auth) for n in ungrouped_nodes: try: transaction.begin() n=DBSession.query(ManagedNode).filter(ManagedNode.id==n.id).one() n.refresh_environ() n.get_running_vms() n.socket=n.get_socket_info() DBSession.add(n) transaction.commit() except Exception, e: LOGGER.error(to_str(e)) DBSession.rollback()
def add_defn(self, defn, site, group, node, auth, defType, op, action_op, def_manager, grid_manager, op_level, sp_ids=None, scan_result=None): #Get definition scope here scope = op_level #Check privileges if scope == constants.SCOPE_S: entityId = node.id elif scope == constants.SCOPE_SP: entityId = group.id elif scope == constants.SCOPE_DC: entityId = site.id ent = auth.get_entity(entityId) if not auth.has_privilege(action_op, ent): raise Exception(constants.NO_PRIVILEGE) #Save definition DBSession.add(defn) #update storage stats and storage disks table with the disks here #The change is for following issue. #Exception: The transaction is inactive due to a rollback in a subtransaction. Issue rollback() to cancel the transaction. def_manager.SaveScanResult(defn.id, grid_manager, scan_result, site.id) #remove scan result from session def_manager.RemoveScanResult(scan_result) errs = [] details = None status = to_unicode(constants.OUT_OF_SYNC) #Save server and network definition link details if scope == constants.SCOPE_DC: #data center level #Add definition to dcdeflinks table since the definition scope is DC. #there is no server linked with this definition so the oos_count is zero. #since oos_count is zero, we need to keep status as IN_SYNC oos_count = 0 status = to_unicode(constants.IN_SYNC) self.add_site_defn(site.id, defn.id, defType, status, oos_count) #associate definition to selected server pools. def_manager.manage_defn_to_groups(site, group, sp_ids, defn, defType, op, def_manager, auth, errs, grid_manager) elif scope == constants.SCOPE_SP: #server pool level node_entities = auth.get_entities(to_unicode(constants.MANAGED_NODE), ent) for eachnode in node_entities: self.add_node_defn(eachnode.entity_id, defn.id, defType, status, details) #Add definition to deflinks table only when the definition scope is SP. oos_count = len(node_entities) status = to_unicode(constants.OUT_OF_SYNC) self.add_group_defn(group.id, defn.id, defType, status, oos_count) elif scope == constants.SCOPE_S: self.add_node_defn(node.id, defn.id, defType, status, details) op = constants.ATTACH update_status=True errs=[] def_manager.sync_node_defn(node, group.id, site.id, defn, defType, op, def_manager, update_status, errs) #compute storage stats def_manager.Recompute(defn) if errs: if len(errs) > 0: LOGGER.error("Error:" + to_str(errs)) return errs
def update(self,obj): DBSession.add(obj)
def add(self,obj): DBSession.add(obj)
def add_defn(self, defn, site, group, node, auth, defType, op, action_op, def_manager, grid_manager, op_level, sp_ids=None, scan_result=None): #Get definition scope here scope = op_level #Check privileges if scope == constants.SCOPE_S: entityId = node.id elif scope == constants.SCOPE_SP: entityId = group.id elif scope == constants.SCOPE_DC: entityId = site.id ent = auth.get_entity(entityId) if not auth.has_privilege(action_op, ent): raise Exception(constants.NO_PRIVILEGE) #Save definition DBSession.add(defn) #update storage stats and storage disks table with the disks here #The change is for following issue. #Exception: The transaction is inactive due to a rollback in a subtransaction. Issue rollback() to cancel the transaction. def_manager.SaveScanResult(defn.id, grid_manager, scan_result, site.id) #remove scan result from session def_manager.RemoveScanResult(scan_result) errs = [] details = None status = to_unicode(constants.OUT_OF_SYNC) #Save server and network definition link details if scope == constants.SCOPE_DC: #data center level #Add definition to dcdeflinks table since the definition scope is DC. #there is no server linked with this definition so the oos_count is zero. #since oos_count is zero, we need to keep status as IN_SYNC oos_count = 0 status = to_unicode(constants.IN_SYNC) self.add_site_defn(site.id, defn.id, defType, status, oos_count) #associate definition to selected server pools. def_manager.manage_defn_to_groups(site, group, sp_ids, defn, defType, op, def_manager, auth, errs, grid_manager) elif scope == constants.SCOPE_SP: #server pool level node_entities = auth.get_entities( to_unicode(constants.MANAGED_NODE), ent) for eachnode in node_entities: self.add_node_defn(eachnode.entity_id, defn.id, defType, status, details) #Add definition to deflinks table only when the definition scope is SP. oos_count = len(node_entities) status = to_unicode(constants.OUT_OF_SYNC) self.add_group_defn(group.id, defn.id, defType, status, oos_count) elif scope == constants.SCOPE_S: self.add_node_defn(node.id, defn.id, defType, status, details) op = constants.ATTACH update_status = True errs = [] def_manager.sync_node_defn(node, group.id, site.id, defn, defType, op, def_manager, update_status, errs) #compute storage stats def_manager.Recompute(defn) if errs: if len(errs) > 0: LOGGER.error("Error:" + to_str(errs)) return errs