Project

General

Profile

root / branches / 1.1 / src / haizea / core / enact / simulated.py @ 844

1
# -------------------------------------------------------------------------- #
2
# Copyright 2006-2009, University of Chicago                                 #
3
# Copyright 2008-2009, Distributed Systems Architecture Group, Universidad   #
4
# Complutense de Madrid (dsa-research.org)                                   #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
# -------------------------------------------------------------------------- #
18

    
19
from haizea.core.leases import Capacity
20
from haizea.core.scheduler.resourcepool import ResourcePoolNode
21
from haizea.core.enact import ResourcePoolInfo, VMEnactment, DeploymentEnactment
22
from haizea.common.utils import get_config
23
import haizea.common.constants as constants
24
import logging
25

    
26
class SimulatedResourcePoolInfo(ResourcePoolInfo):
27
    def __init__(self, site):
28
        ResourcePoolInfo.__init__(self)
29
        self.logger = logging.getLogger("ENACT.SIMUL.INFO")
30
                
31
        if not ("CPU" in site.resource_types and "Memory" in site.resource_types):
32
            # CPU and Memory must be specified
33
            # TODO: raise something more meaningful
34
            raise
35
        
36
        # Disk and network should be specified but, if not, we can
37
        # just add arbitrarily large values.
38
        if not "Disk" in site.resource_types:
39
            site.add_resource("Disk", [1000000])
40

    
41
        if not "Net-in" in site.resource_types:
42
            site.add_resource("Net-in", [1000000])
43

    
44
        if not "Net-out" in site.resource_types:
45
            site.add_resource("Net-out", [1000000])
46
        
47
        self.resource_types = site.get_resource_types()        
48
        
49
        nodes = site.nodes.get_all_nodes()
50
        
51
        self.nodes = dict([(id, ResourcePoolNode(id, "simul-%i" % id, capacity)) for (id, capacity) in nodes.items()])
52
        for node in self.nodes.values():
53
            node.enactment_info = node.id      
54
        
55
    def get_nodes(self):
56
        return self.nodes
57

    
58
    def refresh(self): 
59
        return []  
60
    
61
    def get_resource_types(self):
62
        return self.resource_types
63

    
64
    def get_migration_bandwidth(self):
65
        return get_config().get("imagetransfer-bandwidth")
66

    
67
class SimulatedVMEnactment(VMEnactment):
68
    def __init__(self):
69
        VMEnactment.__init__(self)
70
        self.logger = logging.getLogger("ENACT.SIMUL.VM")
71
        
72
    def start(self, action):
73
        for vnode in action.vnodes:
74
            # Unpack action
75
            pnode = action.vnodes[vnode].pnode
76
            image = action.vnodes[vnode].diskimage
77
            cpu = 100 #action.vnodes[vnode].resources.get_by_type(constants.RES_CPU)
78
            memory = 1024 #action.vnodes[vnode].resources.get_by_type(constants.RES_MEM)
79
            self.logger.debug("Received request to start VM for L%iV%i on host %i, image=%s, cpu=%i, mem=%i"
80
                         % (action.lease_haizea_id, vnode, pnode, image, cpu, memory))
81
    
82
    def stop(self, action):
83
        for vnode in action.vnodes:
84
            self.logger.debug("Received request to stop VM for L%iV%i"
85
                         % (action.lease_haizea_id, vnode))
86

    
87
    def suspend(self, action):
88
        for vnode in action.vnodes:
89
            self.logger.debug("Received request to suspend VM for L%iV%i"
90
                         % (action.lease_haizea_id, vnode))
91

    
92
    def resume(self, action):
93
        for vnode in action.vnodes:
94
            self.logger.debug("Received request to resume VM for L%iV%i"
95
                         % (action.lease_haizea_id, vnode))
96

    
97
    def verify_suspend(self, action):
98
        return 0
99
    
100
    def verify_resume(self, action):
101
        return 0
102
    
103
class SimulatedDeploymentEnactment(DeploymentEnactment):    
104
    def __init__(self, bandwidth):
105
        DeploymentEnactment.__init__(self)
106
        self.logger = logging.getLogger("ENACT.SIMUL.INFO")
107
                
108
        self.bandwidth = bandwidth
109
        
110
        imgcapacity = Capacity([constants.RES_NETOUT])
111
        imgcapacity.set_quantity(constants.RES_NETOUT, self.bandwidth)
112

    
113
        # TODO: Determine node number based on site
114
        self.imagenode = ResourcePoolNode(1000, "image_node", imgcapacity)
115
        
116
    def get_imagenode(self):
117
        return self.imagenode
118
        
119
    def get_aux_nodes(self):
120
        return [self.imagenode] 
121
    
122
    def get_bandwidth(self):
123
        return self.bandwidth
124
        
125
    def resolve_to_file(self, lease, vnode, diskimage_id):
126
        return "/var/haizea/images/%s-L%iV%i" % (diskimage_id, lease.id, vnode)