Merge trunk
This commit is contained in:
@@ -27,6 +27,9 @@ The underlying driver is loaded as a :class:`LazyPluggable`.
|
||||
|
||||
:sql_connection: string specifying the sqlalchemy connection to use, like:
|
||||
`sqlite:///var/lib/nova/nova.sqlite`.
|
||||
|
||||
:enable_new_services: when adding a new service to the database, is it in the
|
||||
pool of available hardware (Default: True)
|
||||
"""
|
||||
|
||||
from nova import exception
|
||||
@@ -37,6 +40,8 @@ from nova import utils
|
||||
FLAGS = flags.FLAGS
|
||||
flags.DEFINE_string('db_backend', 'sqlalchemy',
|
||||
'The backend to use for db')
|
||||
flags.DEFINE_boolean('enable_new_services', True,
|
||||
'Services to be added to the available pool on create')
|
||||
|
||||
|
||||
IMPL = utils.LazyPluggable(FLAGS['db_backend'],
|
||||
@@ -348,9 +353,9 @@ def instance_get_project_vpn(context, project_id):
|
||||
return IMPL.instance_get_project_vpn(context, project_id)
|
||||
|
||||
|
||||
def instance_get_by_internal_id(context, internal_id):
|
||||
"""Get an instance by internal id."""
|
||||
return IMPL.instance_get_by_internal_id(context, internal_id)
|
||||
def instance_get_by_id(context, instance_id):
|
||||
"""Get an instance by id."""
|
||||
return IMPL.instance_get_by_id(context, instance_id)
|
||||
|
||||
|
||||
def instance_is_vpn(context, instance_id):
|
||||
@@ -714,7 +719,7 @@ def security_group_get_all(context):
|
||||
|
||||
|
||||
def security_group_get(context, security_group_id):
|
||||
"""Get security group by its internal id."""
|
||||
"""Get security group by its id."""
|
||||
return IMPL.security_group_get(context, security_group_id)
|
||||
|
||||
|
||||
|
||||
@@ -19,6 +19,25 @@
|
||||
"""
|
||||
SQLAlchemy database backend
|
||||
"""
|
||||
import logging
|
||||
import time
|
||||
|
||||
from sqlalchemy.exc import OperationalError
|
||||
|
||||
from nova import flags
|
||||
from nova.db.sqlalchemy import models
|
||||
|
||||
models.register_models()
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
|
||||
|
||||
for i in xrange(FLAGS.sql_max_retries):
|
||||
if i > 0:
|
||||
time.sleep(FLAGS.sql_retry_interval)
|
||||
|
||||
try:
|
||||
models.register_models()
|
||||
break
|
||||
except OperationalError:
|
||||
logging.exception(_("Data store is unreachable."
|
||||
" Trying again in %d seconds.") % FLAGS.sql_retry_interval)
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
Implementation of SQLAlchemy backend.
|
||||
"""
|
||||
|
||||
import random
|
||||
import warnings
|
||||
|
||||
from nova import db
|
||||
@@ -236,6 +235,8 @@ def service_get_by_args(context, host, binary):
|
||||
def service_create(context, values):
|
||||
service_ref = models.Service()
|
||||
service_ref.update(values)
|
||||
if not FLAGS.enable_new_services:
|
||||
service_ref.disabled = True
|
||||
service_ref.save()
|
||||
return service_ref
|
||||
|
||||
@@ -604,30 +605,18 @@ def fixed_ip_update(context, address, values):
|
||||
###################
|
||||
|
||||
|
||||
#TODO(gundlach): instance_create and volume_create are nearly identical
|
||||
#and should be refactored. I expect there are other copy-and-paste
|
||||
#functions between the two of them as well.
|
||||
|
||||
|
||||
@require_context
|
||||
def instance_create(context, values):
|
||||
"""Create a new Instance record in the database.
|
||||
|
||||
context - request context object
|
||||
values - dict containing column values.
|
||||
'internal_id' is auto-generated and should not be specified.
|
||||
"""
|
||||
instance_ref = models.Instance()
|
||||
instance_ref.update(values)
|
||||
|
||||
session = get_session()
|
||||
with session.begin():
|
||||
while instance_ref.internal_id == None:
|
||||
# Instances have integer internal ids.
|
||||
internal_id = random.randint(0, 2 ** 31 - 1)
|
||||
if not instance_internal_id_exists(context, internal_id,
|
||||
session=session):
|
||||
instance_ref.internal_id = internal_id
|
||||
instance_ref.save(session=session)
|
||||
return instance_ref
|
||||
|
||||
@@ -751,37 +740,28 @@ def instance_get_project_vpn(context, project_id):
|
||||
|
||||
|
||||
@require_context
|
||||
def instance_get_by_internal_id(context, internal_id):
|
||||
def instance_get_by_id(context, instance_id):
|
||||
session = get_session()
|
||||
|
||||
if is_admin_context(context):
|
||||
result = session.query(models.Instance).\
|
||||
options(joinedload('security_groups')).\
|
||||
filter_by(internal_id=internal_id).\
|
||||
filter_by(id=instance_id).\
|
||||
filter_by(deleted=can_read_deleted(context)).\
|
||||
first()
|
||||
elif is_user_context(context):
|
||||
result = session.query(models.Instance).\
|
||||
options(joinedload('security_groups')).\
|
||||
filter_by(project_id=context.project_id).\
|
||||
filter_by(internal_id=internal_id).\
|
||||
filter_by(id=instance_id).\
|
||||
filter_by(deleted=False).\
|
||||
first()
|
||||
if not result:
|
||||
raise exception.NotFound(_('Instance %s not found') % (internal_id))
|
||||
raise exception.NotFound(_('Instance %s not found') % (instance_id))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@require_context
|
||||
def instance_internal_id_exists(context, internal_id, session=None):
|
||||
if not session:
|
||||
session = get_session()
|
||||
return session.query(exists().\
|
||||
where(models.Instance.internal_id == internal_id)).\
|
||||
one()[0]
|
||||
|
||||
|
||||
@require_context
|
||||
def instance_get_fixed_address(context, instance_id):
|
||||
session = get_session()
|
||||
@@ -862,12 +842,9 @@ def instance_action_create(context, values):
|
||||
def instance_get_actions(context, instance_id):
|
||||
"""Return the actions associated to the given instance id"""
|
||||
session = get_session()
|
||||
actions = {}
|
||||
for action in session.query(models.InstanceActions).\
|
||||
return session.query(models.InstanceActions).\
|
||||
filter_by(instance_id=instance_id).\
|
||||
all():
|
||||
actions[action.action] = action.error
|
||||
return actions
|
||||
all()
|
||||
|
||||
|
||||
###################
|
||||
@@ -1317,10 +1294,6 @@ def volume_create(context, values):
|
||||
|
||||
session = get_session()
|
||||
with session.begin():
|
||||
while volume_ref.ec2_id == None:
|
||||
ec2_id = utils.generate_uid('vol')
|
||||
if not volume_ec2_id_exists(context, ec2_id, session=session):
|
||||
volume_ref.ec2_id = ec2_id
|
||||
volume_ref.save(session=session)
|
||||
return volume_ref
|
||||
|
||||
@@ -1418,41 +1391,6 @@ def volume_get_all_by_project(context, project_id):
|
||||
all()
|
||||
|
||||
|
||||
@require_context
|
||||
def volume_get_by_ec2_id(context, ec2_id):
|
||||
session = get_session()
|
||||
result = None
|
||||
|
||||
if is_admin_context(context):
|
||||
result = session.query(models.Volume).\
|
||||
filter_by(ec2_id=ec2_id).\
|
||||
filter_by(deleted=can_read_deleted(context)).\
|
||||
first()
|
||||
elif is_user_context(context):
|
||||
result = session.query(models.Volume).\
|
||||
filter_by(project_id=context.project_id).\
|
||||
filter_by(ec2_id=ec2_id).\
|
||||
filter_by(deleted=False).\
|
||||
first()
|
||||
else:
|
||||
raise exception.NotAuthorized()
|
||||
|
||||
if not result:
|
||||
raise exception.NotFound(_('Volume %s not found') % ec2_id)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@require_context
|
||||
def volume_ec2_id_exists(context, ec2_id, session=None):
|
||||
if not session:
|
||||
session = get_session()
|
||||
|
||||
return session.query(exists().\
|
||||
where(models.Volume.id == ec2_id)).\
|
||||
one()[0]
|
||||
|
||||
|
||||
@require_admin_context
|
||||
def volume_get_instance(context, volume_id):
|
||||
session = get_session()
|
||||
|
||||
Reference in New Issue
Block a user