Scott Baker | 48ce352 | 2014-02-05 16:17:47 -0800 | [diff] [blame] | 1 | import datetime |
| 2 | import os |
| 3 | import operator |
| 4 | import socket |
| 5 | import pytz |
| 6 | import json |
| 7 | import random |
| 8 | import sys |
| 9 | import time |
| 10 | |
| 11 | #sys.path.append("/opt/planetstack") |
| 12 | sys.path.append("/home/smbaker/projects/vicci/plstackapi/planetstack") |
| 13 | |
| 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
| 15 | from core.models import Slice, Sliver, ServiceClass, Reservation, Tag, Network, User, Node, Image, Deployment, Site, NetworkTemplate, NetworkSlice, Service |
| 16 | from hpc.models import HpcService, ServiceProvider, ContentProvider, OriginServer, CDNPrefix, HpcService |
| 17 | |
| 18 | def format_float(x): |
| 19 | try: |
| 20 | return "%10.5f" % x |
| 21 | except: |
| 22 | return str(x) |
| 23 | |
| 24 | class HpcWizard: |
| 25 | def __init__(self): |
| 26 | self.hpcService = HpcService.objects.get() |
| 27 | |
| 28 | def get_hpc_slices(self): |
| 29 | slices = self.hpcService.slices.all() |
| 30 | return slices |
| 31 | |
| 32 | def get_hpc_slivers(self): |
| 33 | slivers = [] |
| 34 | for slice in self.get_hpc_slices(): |
| 35 | for sliver in slice.slivers.all(): |
| 36 | slivers.append(sliver) |
| 37 | return slivers |
| 38 | |
| 39 | def fill_site_nodes(self, site, hpc_slivers=None): |
| 40 | if hpc_slivers is None: |
| 41 | hpc_slivers = self.get_hpc_slivers() |
| 42 | |
| 43 | site.availNodes = [] |
| 44 | site.hpcNodes = [] |
| 45 | for node in site.nodes.all(): |
| 46 | has_hpc = False |
| 47 | for sliver in node.slivers.all(): |
| 48 | if sliver in hpc_slivers: |
| 49 | has_hpc = True |
| 50 | if has_hpc: |
| 51 | site.hpcNodes.append(node) |
| 52 | else: |
| 53 | site.availNodes.append(node) |
| 54 | |
| 55 | def get_sites(self): |
| 56 | sites = list(Site.objects.all()) |
| 57 | |
| 58 | for site in sites: |
| 59 | self.fill_site_nodes(site, self.get_hpc_slivers()) |
| 60 | |
| 61 | return sites |
| 62 | |
| 63 | def get_site(self, site_name): |
| 64 | site = Site.objects.get(name=site_name) |
| 65 | self.fill_site_nodes(site) |
| 66 | return site |
| 67 | |
| 68 | def increase_slivers(self, site_name, count): |
| 69 | site = self.get_site(site_name) |
| 70 | hpc_slice = self.get_hpc_slices()[0] |
| 71 | while (len(site.availNodes) > 0) and (count > 0): |
| 72 | node = site.availNodes.pop() |
| 73 | hostname = node.name |
| 74 | sliver = Sliver(name=node.name, |
| 75 | slice=hpc_slice, |
| 76 | node=node, |
| 77 | image = Image.objects.all()[0], |
| 78 | creator = User.objects.get(email="scott@onlab.us"), |
| 79 | deploymentNetwork=node.deployment, |
| 80 | numberCores = 1, |
| 81 | ip=socket.gethostbyname(hostname)) |
| 82 | sliver.save() |
| 83 | |
| 84 | print "created sliver", sliver |
| 85 | |
| 86 | site.hpcNodes.append(node) |
| 87 | |
| 88 | count = count - 1 |
| 89 | |
| 90 | def decrease_slivers(self, site_name, count): |
| 91 | site = self.get_site(site_name) |
| 92 | hpc_slices = self.get_hpc_slices() |
| 93 | while (len(site.hpcNodes) > 0) and (count > 0): |
| 94 | node = site.hpcNodes.pop() |
| 95 | for sliver in node.slivers.all(): |
| 96 | if sliver.slice in hpc_slices: |
| 97 | print "deleting sliver", sliver |
| 98 | sliver.delete() |
| 99 | |
| 100 | site.availNodes.append(node) |
| 101 | count = count - 1 |
| 102 | |
| 103 | def dump(self): |
| 104 | print "slices:" |
| 105 | for slice in self.get_hpc_slices(): |
| 106 | print " ", slice |
| 107 | |
| 108 | print "sites:" |
| 109 | print "%20s %10s %10s %10s %10s" % ("name", "avail", "hpc", "lat", "long") |
| 110 | for site in self.get_sites(): |
| 111 | print "%20s %10d %10d %10s %10s" % (site.name, len(site.availNodes), len(site.hpcNodes), format_float(site.location.latitude), format_float(site.location.longitude)) |
| 112 | |
| 113 | #print "slivers:" |
| 114 | #for sliver in self.get_hpc_slivers(): |
| 115 | # print " ", sliver |
| 116 | |
| 117 | if __name__=="__main__": |
| 118 | x = HpcWizard() |
| 119 | x.dump() |
| 120 | |
| 121 | x.increase_slivers("Princeton", 1) |
| 122 | x.decrease_slivers("Princeton", 1) |