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