本文整理汇总了Python中nailgun.db.db函数的典型用法代码示例。如果您正苦于以下问题:Python db函数的具体用法?Python db怎么用?Python db使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了db函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_verify_networks
def update_verify_networks(cls, uuid, status,
progress, msg, result):
#TODO(dshulyak) move network tests into ostf
task = db().query(Task).filter_by(uuid=uuid).first()
if not task:
logger.error("Can't set status='%s', message='%s': No task \
with UUID %s found!", status, msg, uuid)
return
previous_status = task.status
statuses = [sub.status for sub in task.subtasks]
messages = [sub.message for sub in task.subtasks]
messages.append(msg)
statuses.append(status)
if any(st == 'error' for st in statuses):
task.status = 'error'
else:
task.status = status or task.status
task.progress = progress or task.progress
task.result = result or task.result
# join messages if not None or ""
task.message = '\n'.join([m for m in messages if m])
db().commit()
if previous_status != task.status and task.cluster_id:
logger.debug("Updating cluster status: "
"cluster_id: %s status: %s",
task.cluster_id, status)
cls.update_cluster_status(uuid)
示例2: message
def message(cls, task, nodes):
logger.debug("%s.message(task=%s)", cls.__class__.__name__, task.uuid)
for n in nodes:
if n.pending_roles:
n.roles += n.pending_roles
n.pending_roles = []
n.status = 'provisioned'
n.progress = 0
# here we replace deployment data if user redefined them
serialized_cluster = deployment_serializers.serialize(
task.cluster, nodes)
# After serialization set pending_addition to False
for node in nodes:
node.pending_addition = False
rpc_message = make_astute_message(
task,
'deploy',
'deploy_resp',
{
'deployment_info': serialized_cluster
}
)
db().commit()
return rpc_message
示例3: update
def update(cls, role, data):
role.name = data['name']
cls._update_release(role, data)
db().flush()
return role
示例4: PUT
def PUT(self, cluster_id):
""":returns: JSONized Cluster attributes.
:http: * 200 (OK)
* 400 (wrong attributes data specified)
* 404 (cluster not found in db)
* 500 (cluster has no attributes)
"""
cluster = self.get_object_or_404(
Cluster,
cluster_id,
log_404=(
"warning",
"Error: there is no cluster "
"with id '{0}' in DB.".format(cluster_id)
)
)
if not cluster.attributes:
logger.error('ClusterAttributesDefaultsHandler: no attributes'
' found for cluster_id %s' % cluster_id)
raise web.internalerror("No attributes found!")
cluster.attributes.editable = cluster.release.attributes_metadata.get(
"editable"
)
db().commit()
cluster.add_pending_changes("attributes")
logger.debug('ClusterAttributesDefaultsHandler:'
' editable attributes for cluster_id %s were reset'
' to default' % cluster_id)
return {"editable": cluster.attributes.editable}
示例5: PUT
def PUT(self):
""":returns: node id.
:http: * 200 (node are successfully updated)
* 304 (node data not changed since last request)
* 400 (data validation failed)
* 404 (node not found)
"""
nd = self.checked_data(
self.validator.validate_update,
data=web.data())
node = self.collection.single.get_by_meta(nd)
if not node:
raise self.http(404, "Can't find node: {0}".format(nd))
node.timestamp = datetime.now()
if not node.online:
node.online = True
msg = u"Node '{0}' is back online".format(node.human_readable_name)
logger.info(msg)
notifier.notify("discover", msg, node_id=node.id)
db().flush()
if 'agent_checksum' in nd and (
node.agent_checksum == nd['agent_checksum']
):
return {'id': node.id, 'cached': True}
self.collection.single.update_by_agent(node, nd)
return {"id": node.id}
示例6: add_pending_changes
def add_pending_changes(cls, instance, changes_type, node_id=None):
"""Add pending changes for current Cluster.
If node_id is specified then links created changes with node.
:param instance: Cluster instance
:param changes_type: name of changes to add
:param node_id: node id for changes
:returns: None
"""
#TODO(enchantner): check if node belongs to cluster
ex_chs = db().query(models.ClusterChanges).filter_by(
cluster=instance,
name=changes_type
)
if not node_id:
ex_chs = ex_chs.first()
else:
ex_chs = ex_chs.filter_by(node_id=node_id).first()
# do nothing if changes with the same name already pending
if ex_chs:
return
ch = models.ClusterChanges(
cluster_id=instance.id,
name=changes_type
)
if node_id:
ch.node_id = node_id
db().add(ch)
db().flush()
示例7: get_interface_by_net_name
def get_interface_by_net_name(cls, node_id, netname):
"""Get interface with specified network assigned to it.
This method first checks for a NodeNICInterface with the specified
network assigned. If that fails it will look for a NodeBondInterface
with that network assigned.
:param instance_id: Node ID
:param netname: NetworkGroup name
:returns: either NodeNICInterface or NodeBondInterface
"""
iface = db().query(models.NodeNICInterface).join(
(models.NetworkGroup,
models.NodeNICInterface.assigned_networks_list)
).filter(
models.NetworkGroup.name == netname
).filter(
models.NodeNICInterface.node_id == node_id
).first()
if iface:
return iface
return db().query(models.NodeBondInterface).join(
(models.NetworkGroup,
models.NodeBondInterface.assigned_networks_list)
).filter(
models.NetworkGroup.name == netname
).filter(
models.NodeBondInterface.node_id == node_id
).first()
示例8: update_roles
def update_roles(cls, instance, new_roles):
"""Update roles for Node instance.
Logs an error if node doesn't belong to Cluster
:param instance: Node instance
:param new_roles: list of new role names
:returns: None
"""
if not instance.cluster_id:
logger.warning(
u"Attempting to assign roles to node "
u"'{0}' which isn't added to cluster".format(
instance.name or instance.id
)
)
return
if new_roles:
instance.role_list = db().query(models.Role).filter_by(
release_id=instance.cluster.release_id,
).filter(
models.Role.name.in_(new_roles)
).all()
else:
instance.role_list = []
db().flush()
db().refresh(instance)
示例9: update
def update(cls, cluster, network_configuration):
from nailgun.network.neutron import NeutronManager
network_manager = NeutronManager()
if 'networks' in network_configuration:
for ng in network_configuration['networks']:
if ng['id'] == network_manager.get_admin_network_group_id():
continue
ng_db = db().query(NetworkGroup).get(ng['id'])
for key, value in ng.iteritems():
if key == "ip_ranges":
cls._set_ip_ranges(ng['id'], value)
else:
if key == 'cidr' and \
not ng['name'] in ('private',):
network_manager.update_ranges_from_cidr(
ng_db, value)
setattr(ng_db, key, value)
if ng['name'] != 'private':
network_manager.create_networks(ng_db)
ng_db.cluster.add_pending_changes('networks')
if 'neutron_parameters' in network_configuration:
for key, value in network_configuration['neutron_parameters'] \
.items():
setattr(cluster.neutron_config, key, value)
db().add(cluster.neutron_config)
db().commit()
示例10: get_networks_to_interfaces_mapping_on_all_nodes
def get_networks_to_interfaces_mapping_on_all_nodes(cls, cluster):
"""Query networks to interfaces mapping on all nodes in cluster.
Returns combined results for NICs and bonds for every node.
Names are returned for node and interface (NIC or bond),
IDs are returned for networks. Results are sorted by node name then
interface name.
"""
nodes_nics_networks = db().query(
models.Node.hostname,
models.NodeNICInterface.name,
models.NetworkGroup.id,
).join(
models.Node.nic_interfaces,
models.NodeNICInterface.assigned_networks_list
).filter(
models.Node.cluster_id == cluster.id,
)
nodes_bonds_networks = db().query(
models.Node.hostname,
models.NodeBondInterface.name,
models.NetworkGroup.id,
).join(
models.Node.bond_interfaces,
models.NodeBondInterface.assigned_networks_list
).filter(
models.Node.cluster_id == cluster.id,
)
return nodes_nics_networks.union(
nodes_bonds_networks
).order_by(
# column 1 then 2 from the result. cannot call them by name as
# names for column 2 are different in this union
'1', '2'
)
示例11: test_undeployed_node_called
def test_undeployed_node_called(
self,
mremove_undeployed_nodes_from_db,
mmake_astute_message,
mrpc):
self.add_node(consts.NODE_STATUSES.discover)
nodes = DeletionTask.get_task_nodes_for_cluster(self.cluster_db)
self.assertEqual(len(nodes['nodes_to_delete']), 1)
self.assertEqual(len(nodes['nodes_to_restore']), 0)
task = models.Task(
name=consts.TASK_NAMES.cluster_deletion,
cluster=self.cluster_db
)
db().add(task)
db().commit()
mremove_undeployed_nodes_from_db.return_value = []
DeletionTask.execute(task, nodes=nodes)
mremove_undeployed_nodes_from_db.assert_called_once_with(
nodes['nodes_to_delete'])
self.assertEqual(mmake_astute_message.call_count, 1)
self.assertEqual(mrpc.cast.call_count, 1)
示例12: test_astute_message_creation
def test_astute_message_creation(self, mmake_astute_message, mrpc):
# 'discover' node is not deployed yet -- it will be removed
# immediately
n_discover = self.add_node(consts.NODE_STATUSES.discover)
# 'ready' node is deployed -- astute will take care of it
self.add_node(consts.NODE_STATUSES.ready)
# 'offline' node will also be passed to astute
self.add_node(consts.NODE_STATUSES.ready, online=False)
nodes = DeletionTask.get_task_nodes_for_cluster(self.cluster_db)
astute_nodes = [node for node in nodes['nodes_to_delete']
if node['id'] != n_discover.id]
self.assertEqual(len(nodes['nodes_to_delete']), 3)
self.assertEqual(len(nodes['nodes_to_restore']), 0)
task = models.Task(
name=consts.TASK_NAMES.cluster_deletion,
cluster=self.cluster_db
)
db().add(task)
db().commit()
DeletionTask.execute(task, nodes=nodes)
self.assertEqual(mmake_astute_message.call_count, 1)
message = mmake_astute_message.call_args[0][3]
self.assertIn('nodes', message)
self.assertItemsEqual(message['nodes'], astute_nodes)
self.assertEqual(mrpc.cast.call_count, 1)
示例13: expose_network_check_error_messages
def expose_network_check_error_messages(task, result, err_messages):
if err_messages:
task.result = result
db().add(task)
db().commit()
full_err_msg = u"\n".join(err_messages)
raise errors.NetworkCheckError(full_err_msg, add_client=False)
示例14: create
def create(cls, data):
"""Create DeploymentTask model.
:param data: task data
:type data: dict
:return: DeploymentGraphTask instance
:rtype: DeploymentGraphTask
"""
db_fields = set(c.name for c in cls.model.__table__.columns)
data_to_create = {}
custom_fields = {} # fields that is not in table
for field, value in six.iteritems(data):
# pack string roles to be [role]
if field in ('role', 'groups') and \
isinstance(value, six.string_types):
value = [value]
# remap fields
if field in cls._incoming_fields_map:
data_to_create[cls._incoming_fields_map[field]] = value
else:
if field in db_fields:
data_to_create[field] = value
else:
custom_fields[field] = value
# wrap custom fields
if custom_fields:
data_to_create['_custom'] = custom_fields
# todo(ikutukov): super for this create method is not called to avoid
# force flush in base method.
deployment_task_instance = models.DeploymentGraphTask(**data_to_create)
db().add(deployment_task_instance)
return deployment_task_instance
示例15: remove_from_cluster
def remove_from_cluster(cls, instance):
"""Remove Node from Cluster.
Also drops networks assignment for Node and clears both
roles and pending roles
:param instance: Node instance
:returns: None
"""
if instance.cluster:
Cluster.clear_pending_changes(
instance.cluster,
node_id=instance.id
)
netmanager = Cluster.get_network_manager(
instance.cluster
)
netmanager.clear_assigned_networks(instance)
netmanager.clear_bond_configuration(instance)
cls.update_roles(instance, [])
cls.update_pending_roles(instance, [])
cls.remove_replaced_params(instance)
instance.cluster_id = None
instance.group_id = None
instance.kernel_params = None
instance.reset_name_to_default()
db().flush()
db().refresh(instance)