Tony Mack | 89f70f1 | 2013-05-10 20:20:03 -0400 | [diff] [blame] | 1 | import os |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 2 | #os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
Tony Mack | c261bd2 | 2013-05-10 21:04:42 -0400 | [diff] [blame] | 3 | import string |
| 4 | import random |
Tony Mack | 89f70f1 | 2013-05-10 20:20:03 -0400 | [diff] [blame] | 5 | |
Tony Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 6 | from netaddr import IPAddress, IPNetwork |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 7 | from planetstack import settings |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 8 | from django.core import management |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 9 | from core.models import * |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 10 | from planetstack.config import Config |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 11 | try: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 12 | from openstack.client import OpenStackClient |
| 13 | from openstack.driver import OpenStackDriver |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 14 | has_openstack = True |
| 15 | except: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 16 | has_openstack = False |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 17 | |
Tony Mack | 89f70f1 | 2013-05-10 20:20:03 -0400 | [diff] [blame] | 18 | manager_enabled = Config().api_nova_enabled |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 19 | |
Tony Mack | c261bd2 | 2013-05-10 21:04:42 -0400 | [diff] [blame] | 20 | |
| 21 | def random_string(size=6): |
| 22 | return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(size)) |
| 23 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 24 | def require_enabled(callable): |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 25 | def wrapper(*args, **kwds): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 26 | if manager_enabled and has_openstack: |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 27 | return callable(*args, **kwds) |
| 28 | else: |
| 29 | return None |
| 30 | return wrapper |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 31 | |
| 32 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 33 | class OpenStackManager: |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 34 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 35 | def __init__(self, auth={}, caller=None): |
Tony Mack | 620f0f3 | 2013-05-03 14:18:31 -0400 | [diff] [blame] | 36 | self.client = None |
| 37 | self.driver = None |
| 38 | self.caller = None |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 39 | self.has_openstack = has_openstack |
Tony Mack | 620f0f3 | 2013-05-03 14:18:31 -0400 | [diff] [blame] | 40 | self.enabled = manager_enabled |
| 41 | |
| 42 | if has_openstack and manager_enabled: |
| 43 | if auth: |
Tony Mack | 41945e0 | 2013-05-09 19:25:10 -0400 | [diff] [blame] | 44 | try: |
| 45 | self.init_user(auth, caller) |
| 46 | except: |
| 47 | # if this fails then it meanse the caller doesn't have a |
| 48 | # role at the slice's tenant. if the caller is an admin |
| 49 | # just use the admin client/manager. |
| 50 | if caller and caller.is_admin: |
| 51 | self.init_admin() |
| 52 | else: raise |
Tony Mack | 620f0f3 | 2013-05-03 14:18:31 -0400 | [diff] [blame] | 53 | else: |
Tony Mack | 41945e0 | 2013-05-09 19:25:10 -0400 | [diff] [blame] | 54 | self.init_admin() |
| 55 | |
| 56 | @require_enabled |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 57 | def init_caller(self, caller, tenant): |
| 58 | auth = {'username': caller.email, |
| 59 | 'password': '', |
| 60 | 'tenant': tenant} |
Tony Mack | 41945e0 | 2013-05-09 19:25:10 -0400 | [diff] [blame] | 61 | self.client = OpenStackClient(**auth) |
| 62 | self.driver = OpenStackDriver(client=self.client) |
| 63 | self.caller = caller |
| 64 | |
| 65 | @require_enabled |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 66 | def init_admin(self, tenant=None): |
Tony Mack | 41945e0 | 2013-05-09 19:25:10 -0400 | [diff] [blame] | 67 | # use the admin credentials |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 68 | self.client = OpenStackClient(tenant=tenant) |
Tony Mack | 41945e0 | 2013-05-09 19:25:10 -0400 | [diff] [blame] | 69 | self.driver = OpenStackDriver(client=self.client) |
| 70 | self.caller = self.driver.admin_user |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 71 | self.caller.kuser_id = self.caller.id |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 72 | |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 73 | @require_enabled |
| 74 | def save_role(self, role): |
Siobhan Tully | 47ae1b5 | 2013-05-10 15:53:14 -0400 | [diff] [blame] | 75 | if not role.role: |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 76 | keystone_role = self.driver.create_role(role.role_type) |
Siobhan Tully | 47ae1b5 | 2013-05-10 15:53:14 -0400 | [diff] [blame] | 77 | role.role = keystone_role.id |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 78 | |
| 79 | @require_enabled |
| 80 | def delete_role(self, role): |
Siobhan Tully | 47ae1b5 | 2013-05-10 15:53:14 -0400 | [diff] [blame] | 81 | if role.role: |
| 82 | self.driver.delete_role({'id': role.role}) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 83 | |
| 84 | @require_enabled |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 85 | def save_key(self, key, name): |
| 86 | key_fields = {'name': name, |
| 87 | 'public_key': key} |
| 88 | nova_key = self.driver.create_keypair(**key_fields) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 89 | |
| 90 | @require_enabled |
| 91 | def delete_key(self, key): |
Tony Mack | 71c685f | 2013-05-10 20:34:18 -0400 | [diff] [blame] | 92 | if key.nkey_id: |
| 93 | self.driver.delete_keypair(key.nkey_id) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 94 | |
| 95 | @require_enabled |
| 96 | def save_user(self, user): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 97 | if not user.kuser_id: |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 98 | name = user.email[:user.email.find('@')] |
| 99 | user_fields = {'name': name, |
| 100 | 'email': user.email, |
| 101 | 'password': user.password, |
| 102 | 'enabled': True} |
| 103 | keystone_user = self.driver.create_user(**user_fields) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 104 | user.kuser_id = keystone_user.id |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 105 | |
| 106 | if user.public_key: |
| 107 | self.save_key(user.public_key, user.keyname) |
| 108 | |
Tony Mack | 386419d | 2013-05-05 11:48:43 -0400 | [diff] [blame] | 109 | if user.site: |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 110 | self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'user') |
Tony Mack | 386419d | 2013-05-05 11:48:43 -0400 | [diff] [blame] | 111 | if user.is_admin: |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 112 | self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'admin') |
Tony Mack | 386419d | 2013-05-05 11:48:43 -0400 | [diff] [blame] | 113 | else: |
Tony Mack | 85d1883 | 2013-05-09 17:02:31 -0400 | [diff] [blame] | 114 | # may have admin role so attempt to remove it |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 115 | self.driver.delete_user_role(user.kuser_id, user.site.tenant_id, 'admin') |
Tony Mack | 386419d | 2013-05-05 11:48:43 -0400 | [diff] [blame] | 116 | |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 117 | @require_enabled |
| 118 | def delete_user(self, user): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 119 | if user.kuser_id: |
| 120 | self.driver.delete_user(user.kuser_id) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 121 | |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 122 | @require_enabled |
Tony Mack | ed163d7 | 2013-05-02 20:05:42 -0400 | [diff] [blame] | 123 | def save_site(self, site, add_role=True): |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 124 | if not site.tenant_id: |
| 125 | tenant = self.driver.create_tenant(tenant_name=site.login_base, |
| 126 | description=site.name, |
| 127 | enabled=site.enabled) |
| 128 | site.tenant_id = tenant.id |
| 129 | # give caller an admin role at the tenant they've created |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 130 | self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin') |
Tony Mack | 6072206 | 2013-05-02 10:57:04 -0400 | [diff] [blame] | 131 | |
| 132 | # update the record |
| 133 | if site.id and site.tenant_id: |
| 134 | self.driver.update_tenant(site.tenant_id, |
| 135 | description=site.name, |
| 136 | enabled=site.enabled) |
| 137 | |
| 138 | @require_enabled |
| 139 | def delete_site(self, site): |
| 140 | if site.tenant_id: |
| 141 | self.driver.delete_tenant(site.tenant_id) |
Tony Mack | d685bfa | 2013-05-02 10:09:51 -0400 | [diff] [blame] | 142 | |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 143 | @require_enabled |
| 144 | def save_slice(self, slice): |
| 145 | if not slice.tenant_id: |
| 146 | nova_fields = {'tenant_name': slice.name, |
| 147 | 'description': slice.description, |
| 148 | 'enabled': slice.enabled} |
| 149 | tenant = self.driver.create_tenant(**nova_fields) |
| 150 | slice.tenant_id = tenant.id |
| 151 | |
| 152 | # give caller an admin role at the tenant they've created |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 153 | self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin') |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 154 | |
| 155 | # refresh credentials using this tenant |
| 156 | self.driver.shell.connect(username=self.driver.shell.keystone.username, |
| 157 | password=self.driver.shell.keystone.password, |
| 158 | tenant=tenant.name) |
| 159 | |
| 160 | # create network |
| 161 | network = self.driver.create_network(slice.name) |
| 162 | slice.network_id = network['id'] |
| 163 | |
| 164 | # create router |
| 165 | router = self.driver.create_router(slice.name) |
| 166 | slice.router_id = router['id'] |
| 167 | |
Tony Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 168 | # create subnet |
| 169 | next_subnet = self.get_next_subnet() |
| 170 | cidr = str(next_subnet.cidr) |
| 171 | ip_version = next_subnet.version |
| 172 | start = str(next_subnet[2]) |
| 173 | end = str(next_subnet[-2]) |
| 174 | subnet = self.driver.create_subnet(name=slice.name, |
| 175 | network_id = network['id'], |
| 176 | cidr_ip = cidr, |
| 177 | ip_version = ip_version, |
| 178 | start = start, |
| 179 | end = end) |
| 180 | slice.subnet_id = subnet['id'] |
| 181 | # add subnet as interface to slice's router |
| 182 | self.driver.add_router_interface(router['id'], subnet['id']) |
Tony Mack | f180f21 | 2013-05-28 09:19:01 -0400 | [diff] [blame] | 183 | # add external route |
| 184 | self.driver.add_external_route(subnet) |
Tony Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 185 | |
| 186 | |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 187 | if slice.id and slice.tenant_id: |
| 188 | self.driver.update_tenant(slice.tenant_id, |
| 189 | description=slice.description, |
| 190 | enabled=slice.enabled) |
| 191 | |
| 192 | @require_enabled |
| 193 | def delete_slice(self, slice): |
| 194 | if slice.tenant_id: |
Tony Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 195 | self.driver.delete_router_interface(slice.router_id, slice.subnet_id) |
| 196 | self.driver.delete_subnet(slice.subnet_id) |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 197 | self.driver.delete_router(slice.router_id) |
| 198 | self.driver.delete_network(slice.network_id) |
| 199 | self.driver.delete_tenant(slice.tenant_id) |
Tony Mack | f180f21 | 2013-05-28 09:19:01 -0400 | [diff] [blame] | 200 | # delete external route |
| 201 | subnet = None |
| 202 | subnets = self.driver.shell.quantum.list_subnets()['subnets'] |
| 203 | for snet in subnets: |
| 204 | if snet['id'] == slice.subnet_id: |
| 205 | subnet = snet |
| 206 | if subnet: |
| 207 | self.driver.delete_external_route(subnet) |
Tony Mack | 93048c2 | 2013-05-02 11:20:26 -0400 | [diff] [blame] | 208 | |
Tony Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 209 | |
Tony Mack | c59fcaf | 2013-06-10 11:14:04 -0400 | [diff] [blame] | 210 | @require_enabled |
Tony Mack | 585160e | 2013-05-07 11:15:59 -0400 | [diff] [blame] | 211 | def get_next_subnet(self): |
| 212 | # limit ourself to 10.0.x.x for now |
| 213 | valid_subnet = lambda net: net.startswith('10.0') |
| 214 | subnets = self.driver.shell.quantum.list_subnets()['subnets'] |
| 215 | ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \ |
| 216 | if valid_subnet(subnet['cidr'])] |
| 217 | ints.sort() |
| 218 | last_ip = IPAddress(ints[-1]) |
| 219 | last_network = IPNetwork(str(last_ip) + "/24") |
| 220 | next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24") |
| 221 | return next_network |
| 222 | |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 223 | @require_enabled |
| 224 | def save_subnet(self, subnet): |
| 225 | if not subnet.subnet_id: |
| 226 | quantum_subnet = self.driver.create_subnet(name= subnet.slice.name, |
| 227 | network_id=subnet.slice.network_id, |
| 228 | cidr_ip = subnet.cidr, |
| 229 | ip_version=subnet.ip_version, |
| 230 | start = subnet.start, |
| 231 | end = subnet.end) |
| 232 | subnet.subnet_id = quantum_subnet['id'] |
| 233 | # add subnet as interface to slice's router |
| 234 | self.driver.add_router_interface(subnet.slice.router_id, subnet.subnet_id) |
| 235 | #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr |
| 236 | #commands.getstatusoutput(add_route) |
| 237 | |
| 238 | |
| 239 | @require_enabled |
| 240 | def delete_subnet(self, subnet): |
| 241 | if subnet.subnet_id: |
| 242 | self.driver.delete_router_interface(subnet.slice.router_id, subnet.subnet_id) |
| 243 | self.driver.delete_subnet(subnet.subnet_id) |
| 244 | #del_route = 'route del -net %s' % self.cidr |
| 245 | #commands.getstatusoutput(del_route) |
smbaker | 8f5cf5f | 2013-05-05 13:58:16 -0700 | [diff] [blame] | 246 | |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 247 | @require_enabled |
| 248 | def save_sliver(self, sliver): |
| 249 | if not sliver.instance_id: |
| 250 | instance = self.driver.spawn_instance(name=sliver.name, |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 251 | key_name = sliver.creator.keyname, |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 252 | image_id = sliver.image.image_id, |
| 253 | hostname = sliver.node.name ) |
| 254 | sliver.instance_id = instance.id |
| 255 | sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name') |
| 256 | |
Scott Baker | 13acdd6 | 2013-05-08 17:42:56 -0700 | [diff] [blame] | 257 | if sliver.instance_id and ("numberCores" in sliver.changed_fields): |
smbaker | 8f5cf5f | 2013-05-05 13:58:16 -0700 | [diff] [blame] | 258 | self.driver.update_instance_metadata(sliver.instance_id, {"cpu_cores": str(sliver.numberCores)}) |
| 259 | |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 260 | @require_enabled |
| 261 | def delete_sliver(self, sliver): |
| 262 | if sliver.instance_id: |
| 263 | self.driver.destroy_instance(sliver.instance_id) |
| 264 | |
| 265 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 266 | def refresh_nodes(self): |
| 267 | # collect local nodes |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 268 | nodes = Node.objects.all() |
| 269 | nodes_dict = {} |
| 270 | for node in nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 271 | if 'viccidev10' not in node.name: |
| 272 | nodes_dict[node.name] = node |
| 273 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 274 | deployment = Deployment.objects.filter(name='VICCI')[0] |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 275 | login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws'] |
| 276 | sites = Site.objects.filter(login_base__in=login_bases) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 277 | # collect nova nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 278 | compute_nodes = self.client.nova.hypervisors.list() |
| 279 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 280 | compute_nodes_dict = {} |
| 281 | for compute_node in compute_nodes: |
| 282 | compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node |
| 283 | |
| 284 | # add new nodes: |
| 285 | new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys()) |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 286 | i = 0 |
| 287 | max = len(sites) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 288 | for name in new_node_names: |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 289 | if i == max: |
| 290 | i = 0 |
| 291 | site = sites[i] |
| 292 | node = Node(name=compute_nodes_dict[name].hypervisor_hostname, |
| 293 | site=site, |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 294 | deployment=deployment) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 295 | node.save() |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 296 | i+=1 |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 297 | |
| 298 | # remove old nodes |
| 299 | old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys()) |
| 300 | Node.objects.filter(name__in=old_node_names).delete() |
| 301 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 302 | def refresh_images(self): |
Tony Mack | 89f70f1 | 2013-05-10 20:20:03 -0400 | [diff] [blame] | 303 | from core.models.image import Image |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 304 | # collect local images |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 305 | images = Image.objects.all() |
| 306 | images_dict = {} |
| 307 | for image in images: |
| 308 | images_dict[image.name] = image |
| 309 | |
| 310 | # collect glance images |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 311 | glance_images = self.client.glance.get_images() |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 312 | glance_images_dict = {} |
| 313 | for glance_image in glance_images: |
| 314 | glance_images_dict[glance_image['name']] = glance_image |
| 315 | |
| 316 | # add new images |
| 317 | new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys()) |
| 318 | for name in new_image_names: |
| 319 | image = Image(image_id=glance_images_dict[name]['id'], |
| 320 | name=glance_images_dict[name]['name'], |
| 321 | disk_format=glance_images_dict[name]['disk_format'], |
| 322 | container_format=glance_images_dict[name]['container_format']) |
| 323 | image.save() |
| 324 | |
| 325 | # remove old images |
| 326 | old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) |
| 327 | Image.objects.filter(name__in=old_image_names).delete() |
Tony Mack | 02755d4 | 2013-05-02 00:00:10 -0400 | [diff] [blame] | 328 | |
| 329 | |