/
ec2.py
232 lines (187 loc) · 7.07 KB
/
ec2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Copyright 2014 Red Hat Inc.
#
# 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.
import functools
from oslo_utils import uuidutils
from nova import cache_utils
from nova.db import api as db
from nova import exception
from nova.objects import base
from nova.objects import fields
# NOTE(vish): cache mapping for one week
_CACHE_TIME = 7 * 24 * 60 * 60
_CACHE = None
def memoize(func):
@functools.wraps(func)
def memoizer(context, reqid):
global _CACHE
if not _CACHE:
_CACHE = cache_utils.get_client(expiration_time=_CACHE_TIME)
key = "%s:%s" % (func.__name__, reqid)
key = str(key)
value = _CACHE.get(key)
if value is None:
value = func(context, reqid)
_CACHE.set(key, value)
return value
return memoizer
def id_to_ec2_id(instance_id, template='i-%08x'):
"""Convert an instance ID (int) to an ec2 ID (i-[base 16 number])."""
return template % int(instance_id)
def id_to_ec2_inst_id(context, instance_id):
"""Get or create an ec2 instance ID (i-[base 16 number]) from uuid."""
if instance_id is None:
return None
elif uuidutils.is_uuid_like(instance_id):
int_id = get_int_id_from_instance_uuid(context, instance_id)
return id_to_ec2_id(int_id)
else:
return id_to_ec2_id(instance_id)
@memoize
def get_int_id_from_instance_uuid(context, instance_uuid):
if instance_uuid is None:
return
try:
imap = EC2InstanceMapping.get_by_uuid(context, instance_uuid)
return imap.id
except exception.NotFound:
imap = EC2InstanceMapping(context)
imap.uuid = instance_uuid
imap.create()
return imap.id
def glance_id_to_ec2_id(context, glance_id, image_type='ami'):
image_id = glance_id_to_id(context, glance_id)
if image_id is None:
return
template = image_type + '-%08x'
return id_to_ec2_id(image_id, template=template)
@memoize
def glance_id_to_id(context, glance_id):
"""Convert a glance id to an internal (db) id."""
if not glance_id:
return
try:
return S3ImageMapping.get_by_uuid(context, glance_id).id
except exception.NotFound:
s3imap = S3ImageMapping(context, uuid=glance_id)
s3imap.create()
return s3imap.id
def glance_type_to_ec2_type(image_type):
"""Converts to a three letter image type.
aki, kernel => aki
ari, ramdisk => ari
anything else => ami
"""
if image_type == 'kernel':
return 'aki'
if image_type == 'ramdisk':
return 'ari'
if image_type not in ['aki', 'ari']:
return 'ami'
return image_type
@base.NovaObjectRegistry.register
class EC2InstanceMapping(base.NovaPersistentObject, base.NovaObject):
# Version 1.0: Initial version
VERSION = '1.0'
fields = {
'id': fields.IntegerField(),
'uuid': fields.UUIDField(),
}
@staticmethod
def _from_db_object(context, imap, db_imap):
for field in imap.fields:
setattr(imap, field, db_imap[field])
imap._context = context
imap.obj_reset_changes()
return imap
@base.remotable
def create(self):
if self.obj_attr_is_set('id'):
raise exception.ObjectActionError(action='create',
reason='already created')
db_imap = db.ec2_instance_create(self._context, self.uuid)
self._from_db_object(self._context, self, db_imap)
@base.remotable_classmethod
def get_by_uuid(cls, context, instance_uuid):
db_imap = db.ec2_instance_get_by_uuid(context, instance_uuid)
if db_imap:
return cls._from_db_object(context, cls(), db_imap)
@base.remotable_classmethod
def get_by_id(cls, context, ec2_id):
db_imap = db.ec2_instance_get_by_id(context, ec2_id)
if db_imap:
return cls._from_db_object(context, cls(), db_imap)
@base.NovaObjectRegistry.register
class S3ImageMapping(base.NovaPersistentObject, base.NovaObject):
# Version 1.0: Initial version
VERSION = '1.0'
fields = {
'id': fields.IntegerField(read_only=True),
'uuid': fields.UUIDField(),
}
@staticmethod
def _from_db_object(context, s3imap, db_s3imap):
for field in s3imap.fields:
setattr(s3imap, field, db_s3imap[field])
s3imap._context = context
s3imap.obj_reset_changes()
return s3imap
@base.remotable
def create(self):
if self.obj_attr_is_set('id'):
raise exception.ObjectActionError(action='create',
reason='already created')
db_s3imap = db.s3_image_create(self._context, self.uuid)
self._from_db_object(self._context, self, db_s3imap)
@base.remotable_classmethod
def get_by_uuid(cls, context, s3_image_uuid):
db_s3imap = db.s3_image_get_by_uuid(context, s3_image_uuid)
if db_s3imap:
return cls._from_db_object(context, cls(context), db_s3imap)
@base.remotable_classmethod
def get_by_id(cls, context, s3_id):
db_s3imap = db.s3_image_get(context, s3_id)
if db_s3imap:
return cls._from_db_object(context, cls(context), db_s3imap)
@base.NovaObjectRegistry.register
class EC2Ids(base.NovaObject):
# Version 1.0: Initial version
VERSION = '1.0'
fields = {
'instance_id': fields.StringField(read_only=True),
'ami_id': fields.StringField(nullable=True, read_only=True),
'kernel_id': fields.StringField(nullable=True, read_only=True),
'ramdisk_id': fields.StringField(nullable=True, read_only=True),
}
@staticmethod
def _from_dict(ec2ids, dict_ec2ids):
for field in ec2ids.fields:
setattr(ec2ids, field, dict_ec2ids[field])
return ec2ids
@staticmethod
def _get_ec2_ids(context, instance):
ec2_ids = {}
ec2_ids['instance_id'] = id_to_ec2_inst_id(context, instance.uuid)
ec2_ids['ami_id'] = glance_id_to_ec2_id(context, instance.image_ref)
for image_type in ['kernel', 'ramdisk']:
image_id = getattr(instance, '%s_id' % image_type)
ec2_id = None
if image_id is not None:
ec2_image_type = glance_type_to_ec2_type(image_type)
ec2_id = glance_id_to_ec2_id(context, image_id, ec2_image_type)
ec2_ids['%s_id' % image_type] = ec2_id
return ec2_ids
@base.remotable_classmethod
def get_by_instance(cls, context, instance):
ec2_ids = cls._get_ec2_ids(context, instance)
return cls._from_dict(cls(context), ec2_ids)