pep8 cleanup

This commit is contained in:
Vishvananda Ishaya
2010年08月30日 17:53:59 -07:00
parent d1c7d29726
commit a64149a8b1

View File

@@ -1,309 +0,0 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Datastore Model objects for Compute Instances, with
InstanceDirectory manager.
# Create a new instance?
>>> InstDir = InstanceDirectory()
>>> inst = InstDir.new()
>>> inst.destroy()
True
>>> inst = InstDir['i-123']
>>> inst['ip'] = "192.168.0.3"
>>> inst['project_id'] = "projectA"
>>> inst.save()
True
>>> InstDir['i-123']
<Instance:i-123>
>>> InstDir.all.next()
<Instance:i-123>
>>> inst.destroy()
True
"""
import datetime
import uuid
from nova import datastore
from nova import exception
from nova import flags
from nova import utils
FLAGS = flags.FLAGS
# TODO(todd): Implement this at the class level for Instance
class InstanceDirectory(object):
"""an api for interacting with the global state of instances"""
def get(self, instance_id):
"""returns an instance object for a given id"""
return Instance(instance_id)
def __getitem__(self, item):
return self.get(item)
def by_project(self, project):
"""returns a list of instance objects for a project"""
for instance_id in datastore.Redis.instance().smembers('project:%s:instances' % project):
yield Instance(instance_id)
def by_node(self, node):
"""returns a list of instances for a node"""
for instance_id in datastore.Redis.instance().smembers('node:%s:instances' % node):
yield Instance(instance_id)
def by_ip(self, ip):
"""returns an instance object that is using the IP"""
# NOTE(vish): The ip association should be just a single value, but
# to maintain consistency it is using the standard
# association and the ugly method for retrieving
# the first item in the set below.
result = datastore.Redis.instance().smembers('ip:%s:instances' % ip)
if not result:
return None
return Instance(list(result)[0])
def by_volume(self, volume_id):
"""returns the instance a volume is attached to"""
pass
def exists(self, instance_id):
return datastore.Redis.instance().sismember('instances', instance_id)
@property
def all(self):
"""returns a list of all instances"""
for instance_id in datastore.Redis.instance().smembers('instances'):
yield Instance(instance_id)
def new(self):
"""returns an empty Instance object, with ID"""
instance_id = utils.generate_uid('i')
return self.get(instance_id)
class Instance():
"""Wrapper around stored properties of an instance"""
def __init__(self, instance_id):
"""loads an instance from the datastore if exists"""
# set instance data before super call since it uses default_state
self.instance_id = instance_id
super(Instance, self).__init__()
def default_state(self):
return {'state': 0,
'state_description': 'pending',
'instance_id': self.instance_id,
'node_name': 'unassigned',
'project_id': 'unassigned',
'user_id': 'unassigned',
'private_dns_name': 'unassigned'}
@property
def identifier(self):
return self.instance_id
@property
def project(self):
if self.state.get('project_id', None):
return self.state['project_id']
return self.state.get('owner_id', 'unassigned')
@property
def volumes(self):
"""returns a list of attached volumes"""
pass
@property
def reservation(self):
"""Returns a reservation object"""
pass
def save(self):
"""Call into superclass to save object, then save associations"""
# NOTE(todd): doesn't track migration between projects/nodes,
# it just adds the first one
is_new = self.is_new_record()
node_set = (self.state['node_name'] != 'unassigned' and
self.initial_state.get('node_name', 'unassigned')
== 'unassigned')
success = super(Instance, self).save()
if success and is_new:
self.associate_with("project", self.project)
self.associate_with("ip", self.state['private_dns_name'])
if success and node_set:
self.associate_with("node", self.state['node_name'])
return True
def destroy(self):
"""Destroy associations, then destroy the object"""
self.unassociate_with("project", self.project)
self.unassociate_with("node", self.state['node_name'])
self.unassociate_with("ip", self.state['private_dns_name'])
return super(Instance, self).destroy()
class Host():
"""A Host is the machine where a Daemon is running."""
def __init__(self, hostname):
"""loads an instance from the datastore if exists"""
# set instance data before super call since it uses default_state
self.hostname = hostname
super(Host, self).__init__()
def default_state(self):
return {"hostname": self.hostname}
@property
def identifier(self):
return self.hostname
class Daemon():
"""A Daemon is a job (compute, api, network, ...) that runs on a host."""
def __init__(self, host_or_combined, binpath=None):
"""loads an instance from the datastore if exists"""
# set instance data before super call since it uses default_state
# since loading from datastore expects a combined key that
# is equivilent to identifier, we need to expect that, while
# maintaining meaningful semantics (2 arguments) when creating
# from within other code like the bin/nova-* scripts
if binpath:
self.hostname = host_or_combined
self.binary = binpath
else:
self.hostname, self.binary = host_or_combined.split(":")
super(Daemon, self).__init__()
def default_state(self):
return {"hostname": self.hostname,
"binary": self.binary,
"updated_at": utils.isotime()
}
@property
def identifier(self):
return "%s:%s" % (self.hostname, self.binary)
def save(self):
"""Call into superclass to save object, then save associations"""
# NOTE(todd): this makes no attempt to destroy itsself,
# so after termination a record w/ old timestmap remains
success = super(Daemon, self).save()
if success:
self.associate_with("host", self.hostname)
return True
def destroy(self):
"""Destroy associations, then destroy the object"""
self.unassociate_with("host", self.hostname)
return super(Daemon, self).destroy()
def heartbeat(self):
self['updated_at'] = utils.isotime()
return self.save()
@classmethod
def by_host(cls, hostname):
for x in cls.associated_to("host", hostname):
yield x
class SessionToken():
"""This is a short-lived auth token that is passed through web requests"""
def __init__(self, session_token):
self.token = session_token
self.default_ttl = FLAGS.auth_token_ttl
super(SessionToken, self).__init__()
@property
def identifier(self):
return self.token
def default_state(self):
now = datetime.datetime.utcnow()
diff = datetime.timedelta(seconds=self.default_ttl)
expires = now + diff
return {'user': None, 'session_type': None, 'token': self.token,
'expiry': expires.strftime(utils.TIME_FORMAT)}
def save(self):
"""Call into superclass to save object, then save associations"""
if not self['user']:
raise exception.Invalid("SessionToken requires a User association")
success = super(SessionToken, self).save()
if success:
self.associate_with("user", self['user'])
return True
@classmethod
def lookup(cls, key):
token = super(SessionToken, cls).lookup(key)
if token:
expires_at = utils.parse_isotime(token['expiry'])
if datetime.datetime.utcnow() >= expires_at:
token.destroy()
return None
return token
@classmethod
def generate(cls, userid, session_type=None):
"""make a new token for the given user"""
token = str(uuid.uuid4())
while cls.lookup(token):
token = str(uuid.uuid4())
instance = cls(token)
instance['user'] = userid
instance['session_type'] = session_type
instance.save()
return instance
def update_expiry(self, **kwargs):
"""updates the expirty attribute, but doesn't save"""
if not kwargs:
kwargs['seconds'] = self.default_ttl
time = datetime.datetime.utcnow()
diff = datetime.timedelta(**kwargs)
expires = time + diff
self['expiry'] = expires.strftime(utils.TIME_FORMAT)
def is_expired(self):
now = datetime.datetime.utcnow()
expires = utils.parse_isotime(self['expiry'])
return expires <= now
def ttl(self):
"""number of seconds remaining before expiration"""
now = datetime.datetime.utcnow()
expires = utils.parse_isotime(self['expiry'])
delta = expires - now
return (delta.seconds + (delta.days * 24 * 3600))
if __name__ == "__main__":
import doctest
doctest.testmod()

View File

@@ -29,4 +29,3 @@ class ComputeService(service.Service):
Compute Service automatically passes commands on to the Compute Manager
"""
pass

View File

@@ -1,3 +1,3 @@
from models import register_models
register_models()
register_models()

View File

@@ -340,13 +340,14 @@ def network_destroy(context, network_id):
'(select id from fixed_ips '
'where network_id=:id)',
{'id': network_id})
session.execute('update network_indexes set network_id=NULL where network_id=:id',
session.execute('update network_indexes set network_id=NULL '
'where network_id=:id',
{'id': network_id})
session.commit()
def network_get(context, network_id, session=None):
return models.Network.find(network_id, session=session)
def network_get(context, network_id):
return models.Network.find(network_id)
def network_get_associated_fixed_ips(context, network_id):
@@ -357,7 +358,6 @@ def network_get_associated_fixed_ips(context, network_id):
.all()
def network_get_by_bridge(context, bridge):
with managed_session() as session:
rv = session.query(models.Network) \
@@ -383,7 +383,8 @@ def network_get_index(context, network_id):
.first()
if not network_index:
raise db.NoMoreNetworks()
network_index['network'] = network_get(context, network_id, session=session)
network_index['network'] = models.Network.find(network_id,
session=session)
session.add(network_index)
session.commit()
return network_index['index']
@@ -446,7 +447,8 @@ def project_get_network(context, project_id):
def queue_get_for(context, topic, physical_node_id):
return "%s.%s" % (topic, physical_node_id) # FIXME(ja): this should be servername?
# FIXME(ja): this should be servername?
return "%s.%s" % (topic, physical_node_id)
###################
@@ -505,7 +507,8 @@ def volume_destroy(context, volume_id):
# TODO(vish): do we have to use sql here?
session.execute('update volumes set deleted=1 where id=:id',
{'id': volume_id})
session.execute('update export_devices set volume_id=NULL where volume_id=:id',
session.execute('update export_devices set volume_id=NULL '
'where volume_id=:id',
{'id': volume_id})
session.commit()

View File

@@ -20,8 +20,6 @@
SQLAlchemy models for nova data
"""
import logging
from sqlalchemy.orm import relationship, backref, validates, exc
from sqlalchemy import Table, Column, Integer, String
from sqlalchemy import MetaData, ForeignKey, DateTime, Boolean, Text
@@ -32,12 +30,14 @@ from nova import auth
from nova import exception
from nova import flags
FLAGS=flags.FLAGS
FLAGS=flags.FLAGS
Base = declarative_base()
class NovaBase(object):
__table_args__ = {'mysql_engine':'InnoDB'}
__table_args__ = {'mysql_engine':'InnoDB'}
__table_initialized__ = False
__prefix__ = 'none'
created_at = Column(DateTime)
@@ -110,8 +110,8 @@ class Image(Base, NovaBase):
__tablename__ = 'images'
__prefix__ = 'ami'
id = Column(Integer, primary_key=True)
user_id = Column(String(255))#, ForeignKey('users.id'), nullable=False)
project_id = Column(String(255))#, ForeignKey('projects.id'), nullable=False)
user_id = Column(String(255))
project_id = Column(String(255))
image_type = Column(String(255))
public = Column(Boolean, default=False)
state = Column(String(255))
@@ -143,10 +143,11 @@ class PhysicalNode(Base, NovaBase):
__tablename__ = 'physical_nodes'
id = Column(String(255), primary_key=True)
class Daemon(Base, NovaBase):
__tablename__ = 'daemons'
id = Column(Integer, primary_key=True)
node_name = Column(String(255)) #, ForeignKey('physical_node.id'))
node_name = Column(String(255)) #, ForeignKey('physical_node.id'))
binary = Column(String(255))
report_count = Column(Integer, nullable=False, default=0)
@@ -172,8 +173,8 @@ class Instance(Base, NovaBase):
__prefix__ = 'i'
id = Column(Integer, primary_key=True)
user_id = Column(String(255)) #, ForeignKey('users.id'), nullable=False)
project_id = Column(String(255)) #, ForeignKey('projects.id'))
user_id = Column(String(255))
project_id = Column(String(255))
@property
def user(self):
@@ -183,12 +184,10 @@ class Instance(Base, NovaBase):
def project(self):
return auth.manager.AuthManager().get_project(self.project_id)
# TODO(vish): make this opaque somehow
@property
def name(self):
return self.str_id
image_id = Column(Integer, ForeignKey('images.id'), nullable=True)
kernel_id = Column(Integer, ForeignKey('images.id'), nullable=True)
ramdisk_id = Column(Integer, ForeignKey('images.id'), nullable=True)
@@ -202,7 +201,7 @@ class Instance(Base, NovaBase):
state_description = Column(String(255))
hostname = Column(String(255))
node_name = Column(String(255)) #, ForeignKey('physical_node.id'))
node_name = Column(String(255)) #, ForeignKey('physical_node.id'))
instance_type = Column(Integer)
@@ -219,11 +218,9 @@ class Instance(Base, NovaBase):
state_description = power_state.name(state_code)
self.state_description = state_description
self.save()
# ramdisk = relationship(Ramdisk, backref=backref('instances', order_by=id))
# kernel = relationship(Kernel, backref=backref('instances', order_by=id))
# project = relationship(Project, backref=backref('instances', order_by=id))
#TODO - see Ewan's email about state improvements
# vmstate_state = running, halted, suspended, paused
# power_state = what we have
@@ -231,24 +228,27 @@ class Instance(Base, NovaBase):
#@validates('state')
#def validate_state(self, key, state):
# assert(state in ['nostate', 'running', 'blocked', 'paused', 'shutdown', 'shutoff', 'crashed'])
# assert(state in ['nostate', 'running', 'blocked', 'paused',
# 'shutdown', 'shutoff', 'crashed'])
class Volume(Base, NovaBase):
__tablename__ = 'volumes'
__prefix__ = 'vol'
id = Column(Integer, primary_key=True)
user_id = Column(String(255)) #, ForeignKey('users.id'), nullable=False)
project_id = Column(String(255)) #, ForeignKey('projects.id'))
user_id = Column(String(255))
project_id = Column(String(255))
node_name = Column(String(255)) #, ForeignKey('physical_node.id'))
node_name = Column(String(255)) #, ForeignKey('physical_node.id'))
size = Column(Integer)
availability_zone = Column(String(255)) # TODO(vish) foreign key?
availability_zone = Column(String(255)) # TODO(vish): foreign key?
instance_id = Column(Integer, ForeignKey('instances.id'), nullable=True)
mountpoint = Column(String(255))
attach_time = Column(String(255)) # TODO(vish) datetime
status = Column(String(255)) # TODO(vish) enum?
attach_status = Column(String(255)) # TODO(vish) enum
attach_time = Column(String(255)) # TODO(vish): datetime
status = Column(String(255)) # TODO(vish): enum?
attach_status = Column(String(255)) # TODO(vish): enum
class ExportDevice(Base, NovaBase):
__tablename__ = 'export_devices'
@@ -299,8 +299,8 @@ class FloatingIp(Base, NovaBase):
fixed_ip_id = Column(Integer, ForeignKey('fixed_ips.id'), nullable=True)
fixed_ip = relationship(FixedIp, backref=backref('floating_ips'))
project_id = Column(String(255)) #, ForeignKey('projects.id'), nullable=False)
node_name = Column(String(255)) #, ForeignKey('physical_node.id'))
project_id = Column(String(255))
node_name = Column(String(255)) #, ForeignKey('physical_node.id'))
@property
def str_id(self):
@@ -339,8 +339,8 @@ class Network(Base, NovaBase):
vpn_private_ip_str = Column(String(255))
dhcp_start = Column(String(255))
project_id = Column(String(255)) #, ForeignKey('projects.id'), nullable=False)
node_name = Column(String(255)) #, ForeignKey('physical_node.id'))
project_id = Column(String(255))
node_name = Column(String(255)) #, ForeignKey('physical_node.id'))
fixed_ips = relationship(FixedIp,
single_parent=True,

View File

@@ -25,6 +25,7 @@ from nova import flags
FLAGS = flags.FLAGS
def managed_session(autocommit=True):
return SessionExecutionManager(autocommit=autocommit)
@@ -40,7 +41,6 @@ class SessionExecutionManager:
self._session = create_session(bind=cls._engine,
autocommit=autocommit)
def __enter__(self):
return self._session

View File

@@ -40,6 +40,7 @@ flags.DEFINE_string('public_interface', 'vlan1',
flags.DEFINE_string('bridge_dev', 'eth0',
'network device for bridges')
def bind_floating_ip(floating_ip):
"""Bind ip to public interface"""
_execute("sudo ip addr add %s dev %s" % (floating_ip,
@@ -59,8 +60,10 @@ def ensure_vlan_forward(public_ip, port, private_ip):
"PREROUTING -t nat -d %s -p udp --dport %s -j DNAT --to %s:1194"
% (public_ip, port, private_ip))
DEFAULT_PORTS = [("tcp", 80), ("tcp", 22), ("udp", 1194), ("tcp", 443)]
def ensure_floating_forward(floating_ip, fixed_ip):
"""Ensure floating ip forwarding rule"""
_confirm_rule("PREROUTING -t nat -d %s -j DNAT --to %s"
@@ -75,6 +78,7 @@ def ensure_floating_forward(floating_ip, fixed_ip):
"FORWARD -d %s -p %s --dport %s -j ACCEPT"
% (fixed_ip, protocol, port))
def remove_floating_forward(floating_ip, fixed_ip):
"""Remove forwarding for floating ip"""
_remove_rule("PREROUTING -t nat -d %s -j DNAT --to %s"
@@ -93,6 +97,7 @@ def ensure_vlan_bridge(vlan_num, bridge, net_attrs=None):
interface = ensure_vlan(vlan_num)
ensure_bridge(bridge, interface, net_attrs)
def ensure_vlan(vlan_num):
interface = "vlan%s" % vlan_num
if not _device_exists(interface):
@@ -162,6 +167,7 @@ def update_dhcp(context, network_id):
command = _dnsmasq_cmd(network_ref)
_execute(command, addl_env=env)
def _host_dhcp(address):
"""Return a host string for an address"""
instance_ref = db.fixed_ip_get_instance(None, address)

View File

@@ -110,4 +110,3 @@ class AOEDriver(object):
check_exit_code=False)
yield process.simple_execute("sudo vblade-persist start all",
check_exit_code=False)

View File

@@ -29,7 +29,6 @@ from nova import exception
from nova import flags
from nova import manager
from nova import utils
from nova.volume import driver
FLAGS = flags.FLAGS
@@ -53,9 +52,9 @@ class AOEManager(manager.Manager):
if not volume_driver:
# NOTE(vish): support the legacy fake storage flag
if FLAGS.fake_storage:
volume_driver='nova.volume.driver.FakeAOEDriver'
volume_driver='nova.volume.driver.FakeAOEDriver'
else:
volume_driver=FLAGS.volume_driver
volume_driver=FLAGS.volume_driver
self.driver = utils.import_object(volume_driver)
super(AOEManager, self).__init__(*args, **kwargs)
@@ -117,4 +116,3 @@ class AOEManager(manager.Manager):
yield self.driver.delete_volume(volume_id)
self.db.volume_destroy(context, volume_id)
defer.returnValue(True)
Reference in New Issue
openstack/nova
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.

The note is not visible to the blocked user.