Added scheduler and task processing thread to ec. Completed deploy and release methods.
[nepi.git] / src / neco / resources / omf / omf_node.py
1 #!/usr/bin/env python
2 from neco.execution.resource import ResourceManager, clsinit
3 from neco.execution.attribute import Attribute
4
5 from neco.resources.omf.omf_api import OMFAPIFactory
6
7 import neco
8 import logging
9
10 @clsinit
11 class OMFNode(ResourceManager):
12     _rtype = "OMFNode"
13     _authorized_connections = ["OMFApplication" , "OMFWifiInterface"]
14
15     @classmethod
16     def _register_attributes(cls):
17         hostname = Attribute("hostname", "Hostname of the machine")
18         cpu = Attribute("cpu", "CPU of the node")
19         ram = Attribute("ram", "RAM of the node")
20         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = "0x02")
21         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = "0x02")
22         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = "0x02")
23         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = "0x02")
24         cls._register_attribute(hostname)
25         cls._register_attribute(ram)
26         cls._register_attribute(cpu)
27         cls._register_attribute(xmppSlice)
28         cls._register_attribute(xmppHost)
29         cls._register_attribute(xmppPort)
30         cls._register_attribute(xmppPassword)
31
32     @classmethod
33     def _register_filters(cls):
34         hostname = Attribute("hostname", "Hostname of the machine")
35         gateway = Attribute("gateway", "Gateway")
36         granularity = Attribute("granularity", "Granularity of the reservation time")
37         hardware_type = Attribute("hardware_type", "Hardware type of the machine")
38         cls._register_filter(hostname)
39         cls._register_filter(gateway)
40         cls._register_filter(granularity)
41         cls._register_filter(hardware_type)
42
43     def __init__(self, ec, guid, creds):
44         super(OMFNode, self).__init__(ec, guid)
45         self.set('xmppSlice', creds['xmppSlice'])
46         self.set('xmppHost', creds['xmppHost'])
47         self.set('xmppPort', creds['xmppPort'])
48         self.set('xmppPassword', creds['xmppPassword'])
49
50         self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
51
52         self._logger = logging.getLogger("neco.omf.omfNode   ")
53         self._logger.setLevel(neco.LOGLEVEL)
54
55     def _validate_connection(self, guid):
56         rm = self.ec.resource(guid)
57         if rm.rtype() in self._authorized_connections:
58             self._logger.debug("Connection between %s %s and %s %s accepted" % (self.rtype(), self._guid, rm.rtype(), guid))
59             return True
60         self._logger.debug("Connection between %s %s and %s %s refused" % (self.rtype(), self._guid, rm.rtype(), guid))
61         return False
62
63     def discover(self):
64         pass
65      
66     def provision(self, credential):
67         pass
68
69     def start(self):
70         self._omf_api.enroll_host(self.get('hostname'))
71
72     def stop(self):
73         self._omf_api.disconnect()
74
75     def configure(self):
76         #routes = self.tc._add_route.get(self.guid, [])
77         #iface_guids = self.tc.get_connected(self.guid, "devs", "node")
78        
79         for route in routes:
80             (destination, netprefix, nexthop, metric, device) = route
81             netmask = ipaddr2.ipv4_mask2dot(netprefix)
82
83             # Validate that the interface is associated to the node
84             for iface_guid in iface_guids:
85                 iface = self.tc.elements.get(iface_guid)
86                 if iface.devname == device:
87                     self._omf_api.execute(self.get('hostname'), 
88                         "Id#%s" % str(random.getrandbits(128)), 
89                         "add -net %s netmask %s dev %s" % (destination, netmask, iface.devname), 
90                         "/sbin/route", # path
91                         None, # env
92                      )
93                     break