| peusterm | cbcd4c2 | 2015-12-28 11:33:42 +0100 | [diff] [blame] | 1 | """ |
| 2 | Distributed Cloud Emulator (dcemulator) |
| 3 | (c) 2015 by Manuel Peuster <manuel.peuster@upb.de> |
| 4 | """ |
| 5 | import logging |
| 6 | |
| 7 | |
| 8 | DCDPID_BASE = 1000 # start of switch dpid's used for data center switches |
| 9 | |
| 10 | |
| 11 | class Datacenter(object): |
| 12 | """ |
| 13 | Represents a logical data center to which compute resources |
| 14 | (Docker containers) can be added at runtime. |
| peusterm | e4e89d3 | 2016-01-07 09:14:54 +0100 | [diff] [blame] | 15 | |
| 16 | Will also implement resource bookkeeping in later versions. |
| peusterm | cbcd4c2 | 2015-12-28 11:33:42 +0100 | [diff] [blame] | 17 | """ |
| 18 | |
| 19 | def __init__(self, name): |
| 20 | self.net = None # DCNetwork to which we belong |
| 21 | self.name = name |
| 22 | self.switch = None # first prototype assumes one "bigswitch" per DC |
| peusterm | a2ad9ff | 2016-01-11 17:10:07 +0100 | [diff] [blame] | 23 | self.containers = {} # keep track of running containers |
| peusterm | cbcd4c2 | 2015-12-28 11:33:42 +0100 | [diff] [blame] | 24 | |
| 25 | def _get_next_dc_dpid(self): |
| 26 | global DCDPID_BASE |
| 27 | DCDPID_BASE += 1 |
| 28 | return DCDPID_BASE |
| 29 | |
| 30 | def create(self): |
| 31 | """ |
| 32 | Each data center is represented by a single switch to which |
| 33 | compute resources can be connected at run time. |
| 34 | |
| peusterm | 9c252b6 | 2016-01-06 16:59:53 +0100 | [diff] [blame] | 35 | TODO: This will be changed in the future to support multiple networks |
| peusterm | cbcd4c2 | 2015-12-28 11:33:42 +0100 | [diff] [blame] | 36 | per data center |
| 37 | """ |
| 38 | self.switch = self.net.mnet.addSwitch( |
| 39 | "%s.s1" % self.name, dpid=hex(self._get_next_dc_dpid())[2:]) |
| 40 | logging.debug("created data center switch: %s" % str(self.switch)) |
| 41 | |
| 42 | def start(self): |
| 43 | pass |
| 44 | |
| peusterm | 4e98b63 | 2016-01-12 14:08:07 +0100 | [diff] [blame^] | 45 | def addCompute(self, name, image=None, network=None): |
| peusterm | e4e89d3 | 2016-01-07 09:14:54 +0100 | [diff] [blame] | 46 | """ |
| 47 | Create a new container as compute resource and connect it to this |
| 48 | data center. |
| peusterm | 4e98b63 | 2016-01-12 14:08:07 +0100 | [diff] [blame^] | 49 | |
| 50 | TODO: This interface will change to support multiple networks to which |
| 51 | a single container can be connected. |
| peusterm | e4e89d3 | 2016-01-07 09:14:54 +0100 | [diff] [blame] | 52 | """ |
| peusterm | 4e98b63 | 2016-01-12 14:08:07 +0100 | [diff] [blame^] | 53 | assert name is not None |
| 54 | # set default parameter |
| 55 | if image is None: |
| 56 | image = "ubuntu" |
| 57 | if network is None: |
| 58 | network = {} # {"ip": "10.0.0.254/8"} |
| 59 | # create the container and connect it to the given network |
| 60 | d = self.net.addDocker("%s" % (name), dimage=image) |
| 61 | self.net.addLink(d, self.switch, params1=network) |
| peusterm | a2ad9ff | 2016-01-11 17:10:07 +0100 | [diff] [blame] | 62 | self.containers[name] = d |
| peusterm | 4e98b63 | 2016-01-12 14:08:07 +0100 | [diff] [blame^] | 63 | return name # we might use UUIDs for naming later on |
| peusterm | cbcd4c2 | 2015-12-28 11:33:42 +0100 | [diff] [blame] | 64 | |
| peusterm | 70baaf8 | 2016-01-06 17:14:40 +0100 | [diff] [blame] | 65 | def removeCompute(self, name): |
| peusterm | a2ad9ff | 2016-01-11 17:10:07 +0100 | [diff] [blame] | 66 | """ |
| 67 | Stop and remove a container from this data center. |
| 68 | """ |
| 69 | assert name in self.containers |
| 70 | self.net.removeLink( |
| 71 | link=None, node1=self.containers[name], node2=self.switch) |
| peusterm | c3b977e | 2016-01-12 10:09:35 +0100 | [diff] [blame] | 72 | self.net.removeDocker("%s" % (name)) |
| peusterm | a2ad9ff | 2016-01-11 17:10:07 +0100 | [diff] [blame] | 73 | del self.containers[name] |
| peusterm | 4e98b63 | 2016-01-12 14:08:07 +0100 | [diff] [blame^] | 74 | return True |
| 75 | |
| 76 | def listCompute(self): |
| 77 | """ |
| 78 | Return a list of all running containers assigned to this |
| 79 | data center. |
| 80 | """ |
| 81 | return self.containers.itervalues() |