~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/db/sqlalchemy/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-01-21 11:48:06 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110121114806-v8fvnnl6az4m4ohv
Tags: upstream-2011.1~bzr597
ImportĀ upstreamĀ versionĀ 2011.1~bzr597

Show diffs side-by-side

added added

removed removed

Lines of Context:
90
90
            setattr(self, k, v)
91
91
 
92
92
    def iteritems(self):
93
 
        """Make the model object behave like a dict"""
94
 
        return iter(self)
95
 
 
96
 
 
97
 
# TODO(vish): Store images in the database instead of file system
98
 
#class Image(BASE, NovaBase):
99
 
#    """Represents an image in the datastore"""
100
 
#    __tablename__ = 'images'
101
 
#    id = Column(Integer, primary_key=True)
102
 
#    ec2_id = Column(String(12), unique=True)
103
 
#    user_id = Column(String(255))
104
 
#    project_id = Column(String(255))
105
 
#    image_type = Column(String(255))
106
 
#    public = Column(Boolean, default=False)
107
 
#    state = Column(String(255))
108
 
#    location = Column(String(255))
109
 
#    arch = Column(String(255))
110
 
#    default_kernel_id = Column(String(255))
111
 
#    default_ramdisk_id = Column(String(255))
112
 
#
113
 
#    @validates('image_type')
114
 
#    def validate_image_type(self, key, image_type):
115
 
#        assert(image_type in ['machine', 'kernel', 'ramdisk', 'raw'])
116
 
#
117
 
#    @validates('state')
118
 
#    def validate_state(self, key, state):
119
 
#        assert(state in ['available', 'pending', 'disabled'])
120
 
#
121
 
#    @validates('default_kernel_id')
122
 
#    def validate_kernel_id(self, key, val):
123
 
#        if val != 'machine':
124
 
#            assert(val is None)
125
 
#
126
 
#    @validates('default_ramdisk_id')
127
 
#    def validate_ramdisk_id(self, key, val):
128
 
#        if val != 'machine':
129
 
#            assert(val is None)
130
 
#
131
 
#
132
 
# TODO(vish): To make this into its own table, we need a good place to
133
 
#             create the host entries. In config somwhere? Or the first
134
 
#             time any object sets host? This only becomes particularly
135
 
#             important if we need to store per-host data.
136
 
#class Host(BASE, NovaBase):
137
 
#    """Represents a host where services are running"""
138
 
#    __tablename__ = 'hosts'
139
 
#    id = Column(String(255), primary_key=True)
 
93
        """Make the model object behave like a dict.
 
94
 
 
95
        Includes attributes from joins."""
 
96
        local = dict(self)
 
97
        joined = dict([(k, v) for k, v in self.__dict__.iteritems()
 
98
                      if not k[0] == '_'])
 
99
        local.update(joined)
 
100
        return local.iteritems()
140
101
 
141
102
 
142
103
class Service(BASE, NovaBase):
149
110
    topic = Column(String(255))
150
111
    report_count = Column(Integer, nullable=False, default=0)
151
112
    disabled = Column(Boolean, default=False)
 
113
    availability_zone = Column(String(255), default='nova')
 
114
 
 
115
 
 
116
class Certificate(BASE, NovaBase):
 
117
    """Represents a an x509 certificate"""
 
118
    __tablename__ = 'certificates'
 
119
    id = Column(Integer, primary_key=True)
 
120
 
 
121
    user_id = Column(String(255))
 
122
    project_id = Column(String(255))
 
123
    file_name = Column(String(255))
152
124
 
153
125
 
154
126
class Instance(BASE, NovaBase):
155
127
    """Represents a guest vm."""
156
128
    __tablename__ = 'instances'
157
 
    id = Column(Integer, primary_key=True)
158
 
    internal_id = Column(Integer, unique=True)
 
129
    id = Column(Integer, primary_key=True, autoincrement=True)
 
130
 
 
131
    @property
 
132
    def name(self):
 
133
        return FLAGS.instance_name_template % self.id
159
134
 
160
135
    admin_pass = Column(String(255))
161
 
 
162
136
    user_id = Column(String(255))
163
137
    project_id = Column(String(255))
164
138
 
170
144
    def project(self):
171
145
        return auth.manager.AuthManager().get_project(self.project_id)
172
146
 
173
 
    @property
174
 
    def name(self):
175
 
        return "instance-%d" % self.internal_id
176
 
 
177
147
    image_id = Column(String(255))
178
148
    kernel_id = Column(String(255))
179
149
    ramdisk_id = Column(String(255))
210
180
    launched_at = Column(DateTime)
211
181
    terminated_at = Column(DateTime)
212
182
 
 
183
    availability_zone = Column(String(255))
 
184
 
213
185
    # User editable field for display in user-facing UIs
214
186
    display_name = Column(String(255))
215
187
    display_description = Column(String(255))
216
188
 
 
189
    locked = Column(Boolean)
 
190
 
217
191
    # TODO(vish): see Ewan's email about state improvements, probably
218
192
    #             should be in a driver base class or some such
219
193
    # vmstate_state = running, halted, suspended, paused
226
200
    #                     'shutdown', 'shutoff', 'crashed'])
227
201
 
228
202
 
 
203
class InstanceActions(BASE, NovaBase):
 
204
    """Represents a guest VM's actions and results"""
 
205
    __tablename__ = "instance_actions"
 
206
    id = Column(Integer, primary_key=True)
 
207
    instance_id = Column(Integer, ForeignKey('instances.id'))
 
208
 
 
209
    action = Column(String(255))
 
210
    error = Column(Text)
 
211
 
 
212
 
229
213
class Volume(BASE, NovaBase):
230
214
    """Represents a block storage device that can be attached to a vm."""
231
215
    __tablename__ = 'volumes'
232
 
    id = Column(Integer, primary_key=True)
233
 
    ec2_id = Column(String(12), unique=True)
 
216
    id = Column(Integer, primary_key=True, autoincrement=True)
 
217
 
 
218
    @property
 
219
    def name(self):
 
220
        return FLAGS.volume_name_template % self.id
234
221
 
235
222
    user_id = Column(String(255))
236
223
    project_id = Column(String(255))
256
243
    display_name = Column(String(255))
257
244
    display_description = Column(String(255))
258
245
 
259
 
    @property
260
 
    def name(self):
261
 
        return self.ec2_id
262
 
 
263
246
 
264
247
class Quota(BASE, NovaBase):
265
248
    """Represents quota overrides for a project."""
389
372
 
390
373
    injected = Column(Boolean, default=False)
391
374
    cidr = Column(String(255), unique=True)
 
375
    cidr_v6 = Column(String(255), unique=True)
 
376
 
 
377
    ra_server = Column(String(255))
 
378
 
392
379
    netmask = Column(String(255))
393
380
    bridge = Column(String(255))
394
381
    gateway = Column(String(255))
417
404
    """
418
405
    __tablename__ = 'auth_tokens'
419
406
    token_hash = Column(String(255), primary_key=True)
420
 
    user_id = Column(Integer)
 
407
    user_id = Column(String(255))
421
408
    server_manageent_url = Column(String(255))
422
409
    storage_url = Column(String(255))
423
410
    cdn_management_url = Column(String(255))
519
506
    host = Column(String(255))  # , ForeignKey('hosts.id'))
520
507
 
521
508
 
 
509
class ConsolePool(BASE, NovaBase):
 
510
    """Represents pool of consoles on the same physical node."""
 
511
    __tablename__ = 'console_pools'
 
512
    id = Column(Integer, primary_key=True)
 
513
    address = Column(String(255))
 
514
    username = Column(String(255))
 
515
    password = Column(String(255))
 
516
    console_type = Column(String(255))
 
517
    public_hostname = Column(String(255))
 
518
    host = Column(String(255))
 
519
    compute_host = Column(String(255))
 
520
 
 
521
 
 
522
class Console(BASE, NovaBase):
 
523
    """Represents a console session for an instance."""
 
524
    __tablename__ = 'consoles'
 
525
    id = Column(Integer, primary_key=True)
 
526
    instance_name = Column(String(255))
 
527
    instance_id = Column(Integer)
 
528
    password = Column(String(255))
 
529
    port = Column(Integer, nullable=True)
 
530
    pool_id = Column(Integer, ForeignKey('console_pools.id'))
 
531
    pool = relationship(ConsolePool, backref=backref('consoles'))
 
532
 
 
533
 
522
534
def register_models():
523
535
    """Register Models and create metadata.
524
536
 
525
537
    Called from nova.db.sqlalchemy.__init__ as part of loading the driver,
526
 
    it will never need to be called explicitly elsewhere.
 
538
    it will never need to be called explicitly elsewhere unless the
 
539
    connection is lost and needs to be reestablished.
527
540
    """
528
541
    from sqlalchemy import create_engine
529
 
    models = (Service, Instance, Volume, ExportDevice, IscsiTarget, FixedIp,
530
 
              FloatingIp, Network, SecurityGroup,
531
 
              SecurityGroupIngressRule, SecurityGroupInstanceAssociation,
532
 
              AuthToken, User, Project)  # , Image, Host
 
542
    models = (Service, Instance, InstanceActions,
 
543
              Volume, ExportDevice, IscsiTarget, FixedIp, FloatingIp,
 
544
              Network, SecurityGroup, SecurityGroupIngressRule,
 
545
              SecurityGroupInstanceAssociation, AuthToken, User,
 
546
              Project, Certificate, ConsolePool, Console)  # , Image, Host
533
547
    engine = create_engine(FLAGS.sql_connection, echo=False)
534
548
    for model in models:
535
549
        model.metadata.create_all(engine)