本文整理汇总了Python中stackone.model.DBSession.query方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.query方法的具体用法?Python DBSession.query怎么用?Python DBSession.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stackone.model.DBSession
的用法示例。
在下文中一共展示了DBSession.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_vm_states
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def add_vm_states(self, vm_ids):
try:
transaction.begin()
avail_states = DBSession.query(AvailState).filter(AvailState.entity_id.in_(vm_ids)).all()
from stackone.model.VM import VM
(vshs, avails) = ([], [])
for avail in avail_states:
vsh = DBSession.query(VMStateHistory).filter(VMStateHistory.node_id == self.node_id).filter(VMStateHistory.vm_id == avail.entity_id).first()
if vsh is None:
vsh = VMStateHistory(self.node_id, avail.entity_id, avail.avail_state, avail.monit_state, avail.transient_state, avail.transient_state_time, avail.owner)
else:
vsh.avail_state = avail.avail_state
vsh.monit_state = avail.monit_state
vsh.transient_state = avail.transient_state
vsh.transient_state_time = avail.transient_state_time
vsh.owner = avail.owner
vsh.timestamp = datetime.now()
vshs.append(vsh)
avails.append(avail)
DBSession.add_all(vshs)
DBSession.add_all(avails)
transaction.commit()
except Exception as e:
LOGGER.error(to_str(e))
DBSession.rollback()
transaction.begin()
traceback.print_exc()
raise e
示例2: set_transient_state
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def set_transient_state(self, defn, transient_state, op, site_id=None, group_id=None, node_id=None):
scope = defn.scope
if op==constants.SCOPE_LEVEL:
if scope == constants.SCOPE_S:
def_link = DBSession.query(ServerDefLink).filter_by(server_id=node_id, def_id=defn.id).first()
else:
if scope == constants.SCOPE_SP:
def_link = DBSession.query(SPDefLink).filter_by(group_id=group_id, def_id=defn.id).first()
else:
if scope == constants.SCOPE_DC:
def_link = DBSession.query(DCDefLink).filter_by(site_id=site_id, def_id=defn.id).first()
if def_link:
if transient_state:
def_link.transient_state = to_unicode(transient_state)
else:
def_link.transient_state = None
LOGGER.info('Transient state of ' + to_str(defn.name) + ' is changed to ' + to_str(transient_state) + ' at definition scope level')
if op==constants.NODE_LEVEL:
def_link = DBSession.query(ServerDefLink).filter_by(server_id=node_id, def_id=defn.id).first()
if def_link:
if transient_state:
def_link.transient_state = to_unicode(transient_state)
else:
def_link.transient_state = None
LOGGER.info('Transient state of ' + to_str(defn.name) + ' is changed to ' + to_str(transient_state) + ' at node level')
示例3: get_vm_restore_task_result
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def get_vm_restore_task_result(self, auth, vm_id):
task_backup_info = {}
tasklist_info = []
msg = " Number of VM's backed up= 2 \n Number of VM backup suceeded = 2\n Number of VM backup failed = 0"
vmrestore_result_obj_list = DBSession.query(VMRestoreResult, VMBackupResult).join((VMBackupResult, VMRestoreResult.backup_result_id == VMBackupResult.id)).filter(VMRestoreResult.vm_id == vm_id).order_by(VMRestoreResult.start_time).all()
if vmrestore_result_obj_list:
for vm_result_obj in vmrestore_result_obj_list:
vmrestore_result_obj = vm_result_obj[0]
vmbackup_result_obj = vm_result_obj[1]
sp_bkp_conf = DBSession.query(SPBackupConfig).filter_by(id=vmrestore_result_obj.backup_id).first()
if sp_bkp_conf:
detailResult_obj_list = DBSession.query(VMRestoreDetailResult).filter_by(result_id=vmrestore_result_obj.id).order_by(VMRestoreDetailResult.details).all()
msg2 = ''
if detailResult_obj_list:
for detailResult_obj in detailResult_obj_list:
if detailResult_obj:
msg2 += detailResult_obj.details + '\n'
task_info = {}
task_info['taskid'] = vmrestore_result_obj.id
task_info['status'] = vmrestore_result_obj.status
task_info['name'] = sp_bkp_conf.name
location = ''
exec_context = vmbackup_result_obj.execution_context
location = exec_context['SERVER'] + ':' + exec_context['BACKUP_DESTINATION']
task_info['location'] = location
task_info['backup_size'] = 0
task_info['starttime'] = vmrestore_result_obj.start_time
task_info['endtime'] = vmrestore_result_obj.end_time
task_info['errmsg'] = msg2
task_info['restore'] = vmrestore_result_obj.id
tasklist_info.append(task_info)
task_backup_info['rows'] = tasklist_info
return task_backup_info
示例4: on_add_node
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def on_add_node(self, nodeId, groupId, site_id, auth, def_manager):
op = constants.ATTACH
if not(nodeId or groupId):
return None
defn_list = []
errs = []
sync_manager = SyncDef()
defType = def_manager.getType()
sp_defns = DBSession.query(SPDefLink).filter_by(group_id=to_unicode(groupId))
if sp_defns:
for eachdefn in sp_defns:
defn = def_manager.get_defn(eachdefn.def_id)
if defn:
defn_list.append(defn)
status = to_unicode(constants.OUT_OF_SYNC)
details = None
sync_manager.add_node_defn(nodeId, defn.id, defType, status, details)
node = DBSession.query(ManagedNode).filter_by(id=nodeId).first()
if node:
update_status = True
try:
sync_manager.sync_node(defn_list, node, groupId, site_id, auth, defType, op, def_manager, update_status, errs)
except Exception as ex:
if errs:
if len(errs)>0:
LOGGER.error('Error in syncing definition while adding node: ' + to_str(errs))
示例5: get_storage_usage
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def get_storage_usage(self, auth, site_id, group_id, scope, defn):
usage = 0
vm_id_list = []
if scope==constants.SCOPE_DC:
site_entity = auth.get_entity(site_id)
groups = auth.get_entities(constants.SERVER_POOL, parent=site_entity)
for eachgroup in groups:
nodes = auth.get_entities(constants.MANAGED_NODE, parent=eachgroup)
vm_id_list = self.get_vm_id_list(auth, nodes, vm_id_list)
if scope == constants.SCOPE_SP:
group_entity = auth.get_entity(group_id)
nodes = auth.get_entities(constants.MANAGED_NODE, parent=group_entity)
vm_id_list = self.get_vm_id_list(auth, nodes, vm_id_list)
storage_disks = DBSession.query(StorageDisks).filter_by(storage_id=defn.id)
if storage_disks:
for each_disk in storage_disks:
vm_storage_link = DBSession.query(VMStorageLinks).filter_by(storage_disk_id=each_disk.id).first()
if vm_storage_link:
vm_id = None
vm_disk = DBSession.query(VMDisks).filter_by(id=vm_storage_link.vm_disk_id).first()
if vm_disk:
vm_id = vm_disk.vm_id
for each_vm_id in vm_id_list:
if each_vm_id == vm_id:
usage += vm_disk.disk_size
return usage
示例6: disassociate_defn
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def disassociate_defn(self, site, group, auth, defn, defType, add_mode, grid_manager):
LOGGER.info('Disassociating definition...')
allows_delete = False
if add_mode==False:
dc_defn = DBSession.query(DCDefLink).filter_by(site_id=site.id, def_id=defn.id, def_type=defType).first()
if dc_defn:
if dc_defn.oos_count == 0:
allows_delete = True
if dc_defn.oos_count>0:
LOGGER.info('Definition is OUT_OF_SYNC at data center level')
else:
if add_mode == True:
allows_delete = True
if allows_delete==True:
LOGGER.info('Allowing to delete definition...')
for node in group.getNodeList(auth).itervalues():
if node:
node_defn = DBSession.query(ServerDefLink).filter_by(server_id=node.id, def_id=defn.id, def_type=defType).first()
if node_defn:
DBSession.delete(node_defn)
group_defn = DBSession.query(SPDefLink).filter_by(group_id=group.id, def_id=defn.id, def_type=defType).first()
if group_defn:
DBSession.delete(group_defn)
vm_id_list = []
for node in group.getNodeList(auth).itervalues():
if node:
for vm in grid_manager.get_node_doms(auth, node.id):
if vm:
vm_id_list.append(vm.id)
grid_manager.remove_vm_links_to_storage(defn.id, vm_id_list)
transaction.commit()
示例7: sync_defn
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def sync_defn(self, auth, server_ids, def_id, site_id, group_id):
server_id_list = server_ids.split(',')
for server_id in server_id_list:
node = DBSession.query(ManagedNode).filter_by(id=server_id).first()
defn = DBSession.query(StorageDef).filter_by(id=def_id).first()
self.sync_manager.sync_node_defn(auth, node, group_id, site_id, defn, constants.STORAGE, constants.ATTACH, self.storage_manager)
return dict(success='true')
示例8: delete_vlan_network_info
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def delete_vlan_network_info(self, vlan_id_pool, vlan_ids):
if not isinstance(vlan_ids, list):
vlan_ids = [vlan_ids]
DBSession.query(VLANNetworkInfo).filter(VLANNetworkInfo.vlan_id.in_(vlan_ids)).filter(
VLANNetworkInfo.vlan_pool_id == vlan_id_pool.id
).delete()
LOGGER.info("deleted vlan_network_info for vlan_id_pool:%s" % vlan_id_pool.name)
示例9: get_next_id
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def get_next_id(self, pool_tag, context):
available_id = None
interface = None
if pool_tag == constants.VLAN_ID_POOL:
LOGGER.info("Get next vlan id from pool...")
pool_id = context.get("pool_id")
nw_def_id = context.get("nw_def_id")
null_value = None
LOGGER.info("Getting CMS Lock...")
LockManager().get_lock(
constants.VLAN_ID_POOL, pool_id, constants.NEXT_VLAN_ID, constants.Table_vlan_id_pools
)
interface = VLANManager().get_interface(pool_id)
rs = (
DBSession.query(func.min(VLANID.vlan_id).label("vlan_id"))
.join((VLANIDPool, VLANIDPool.id == VLANID.vlan_id_pool_id))
.filter(VLANID.used_by == null_value)
.filter(VLANIDPool.id == pool_id)
.first()
)
is_tuple = isinstance(rs, tuple)
if is_tuple and rs[0]:
available_id = rs.vlan_id
LOGGER.info("Available vlan id is " + to_str(available_id))
context["vlan_id"] = available_id
self.mark_used(pool_tag, context)
vlan = DBSession.query(VLANIDPool).filter(VLANIDPool.id == pool_id).first()
if vlan:
vlan.used = int(vlan.used) + 1
LOGGER.info("Releasing CMS Lock...")
LockManager().release_lock()
return (available_id, interface)
示例10: add_firewall_for_entity
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def add_firewall_for_entity(self, entity_id):
LOGGER.info("Add firewall for CSEP/ Data Center...")
if entity_id:
csep = DBSession.query(CSEP).filter_by(id=entity_id).first()
if csep:
LOGGER.info("Got CSEP")
nw_service_host = csep.get_nw_service_host()
if nw_service_host:
fw = IptablesManager(csep.name)
self.fw_map[entity_id] = fw
LOGGER.info("Firewall is added to CSEP")
self.set_nw_service_host(fw, nw_service_host)
LOGGER.info("Network service host is added to firewall")
return fw
dc = DBSession.query(Site).filter_by(id=entity_id).first()
if dc:
LOGGER.info("Got DC")
nw_service_host = get_cms_network_service_node()
if nw_service_host:
fw = IptablesManager(dc.name)
self.fw_map[entity_id] = fw
LOGGER.info("Firewall is added to DC")
self.set_nw_service_host(fw, nw_service_host)
LOGGER.info("Network service host is added to firewall")
return fw
示例11: get_fw_info
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def get_fw_info(self):
fw_main_data = []
LOGGER.info("Getting firewall rules info for all csep and Data Center...")
print "Getting firewall rules info for all csep and Data Center..."
sites = DBSession.query(Site)
if sites[0]:
LOGGER.info("Got the site. Site name is " + to_str(sites[0L].name))
site_id = sites[0L].id
site_name = sites[0L].name
nw_service_host = get_cms_network_service_node()
if nw_service_host:
fw = self.get_firewall(site_id)
fw.set_chain_name(site_name)
fw_data = self.dump(fw)
fw_main_data.append(fw_data)
csep_list = DBSession.query(CSEP)
for each_csep in csep_list:
LOGGER.info("Got the CSEP. CSEP name is " + to_str(each_csep.name))
nw_service_host = each_csep.get_nw_service_host()
if nw_service_host:
fw = self.get_firewall(each_csep.id)
fw.set_chain_name(each_csep.name)
fw_data = self.dump(fw)
fw_main_data.append(fw_data)
return fw_main_data
示例12: remove_context
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def remove_context(cls, context_id):
try:
DBSession.query(cls).filter(cls.id == context_id).delete()
except Exception as e:
LOGGER.error(e)
traceback.print_exc()
raise e
示例13: update_avail
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
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")
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)
DBSession.add(node)
if update==True:
487
ev_contents = {'state':new_state}
ev = AvailabilityEvent(node.id, ev_contents, timestamp)
DBSession.add(ev)
ent = DBSession.query(Entity).filter(Entity.entity_id==node.id).first()
from stackone.model.ManagedNode import ManagedNode
if ent.type.name == constants.MANAGED_NODE and new_state == ManagedNode.DOWN:
if node.is_maintenance():
logger.info('Node:%s is in Maintenance mode' %node.hostname)
else:
notify_node_down(ent,reason)
except Exception, e:
import traceback
traceback.print_exc()
logger.error(e)
sv_point.rollback()
示例14: delete_restore_single_result
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def delete_restore_single_result(self, result_id):
try:
DBSession.query(VMRestoreResult).filter(VMRestoreResult.id == result_id).delete()
LOGGER.info('VM restore single result is deleted.')
transaction.commit()
except Exception as ex:
LOGGER.error(to_str(ex).replace("'", ''))
示例15: manage_public_ip
# 需要导入模块: from stackone.model import DBSession [as 别名]
# 或者: from stackone.model.DBSession import query [as 别名]
def manage_public_ip(self, vm_id, public_ip_id, public_ip, csep_id, add_flag):
LOGGER.info('Calling network service...')
if vm_id:
nw_vm_rel = DBSession.query(NetworkVMRelation).filter_by(vm_id=vm_id).first()
elif public_ip:
nw_vm_rel = DBSession.query(NetworkVMRelation).filter_by(public_ip_id=public_ip_id).first()
if nw_vm_rel:
LOGGER.info('Got network VM relation')
nw_def_id = nw_vm_rel.nw_def_id
private_ip_id = nw_vm_rel.private_ip_id
nw_defn = NwManager().get_defn(nw_def_id)
public_interface = ''
if nw_defn:
bridge_name = nw_defn.bridge_info.get('name')
private_ip = ''
ip_rec = self.get_ip_by_id(private_ip_id)
if ip_rec:
private_ip = self.remove_cidr_format_from_ip(ip_rec.ip)
dom_id = None
nw_service_host = NwManager().get_nw_service_host(csep_id)
if nw_service_host:
LOGGER.info('Got network service host')
public_interface = self.get_public_interface(csep_id)
ctx = {}
ctx['public_ip'] = public_ip
ctx['private_ip'] = private_ip
ctx['public_interface'] = public_interface
ctx['bridge_name'] = bridge_name
ctx['add_flag'] = add_flag
ctx['csep_id'] = csep_id
LOGGER.info('context=' + to_str(ctx))
NwManager().manage_public_ip(nw_service_host, ctx)