d9e8094f474b13ff3c2f0681f90e798d6aa34cbe
[nodemanager.git] / plugins / update_ipv6addr_slivertag.py
1 """
2 Description: Update the IPv6 Address sliver tag accordingly to the IPv6 address set
3 update_ipv6addr_slivertag nodemanager plugin
4 Version: 0.6
5 Author: Guilherme Sperb Machado <gsm@machados.org>
6 """
7
8 import logger
9 import os
10 import socket
11 import re
12
13 import tools
14 import uuid
15 from xml.dom.minidom import parseString
16
17 # TODO: is there anything better to do if the "libvirt", "sliver_libvirt",
18 # and are not in place in the VS distro?
19 try:
20     import libvirt
21     from sliver_libvirt import Sliver_Libvirt
22 except:
23     logger.log("Could not import 'sliver_lxc' or 'libvirt'.")
24
25 priority=150
26
27 ipv6addrtag = 'ipv6_address'
28
29 def start():
30     logger.log("update_ipv6addr_slivertag: plugin starting up...")
31
32
33 def get_sliver_tag_id_value(slivertags):
34     for slivertag in slivertags:
35         if slivertag['tagname']==ipv6addrtag:
36             return slivertag['slice_tag_id'], slivertag['value']
37
38 def SetSliverTag(plc, data, tagname):
39
40     virt=tools.get_node_virt()
41     if virt!='lxc':
42         return
43
44     for slice in data['slivers']:
45         #logger.log("update_ipv6addr_slivertag: starting with slice=%s" % (slice['name']) )
46
47         # Check if the slice to be processed in a "system" slice
48         # If so, just loop to the next slice
49         system_slice = False
50         for attribute in slice['attributes']:
51             if attribute['tagname']=='system' and attribute['value']=='1':
52                 system_slice = True
53                 break
54         if system_slice: continue
55
56         # TODO: what about the prefixlen? Should we also inform the prefixlen?
57         # here, I'm just taking the ipv6addr (value)
58         value,prefixlen = tools.get_sliver_ipv6(slice['name'])
59
60         node_id = tools.node_id()
61         slivertags = plc.GetSliceTags({"name":slice['name'],"node_id":node_id,"tagname":tagname})
62         #logger.log(repr(str(slivertags)))
63         #for tag in slivertags:
64         #    logger.log(repr(str(tag)))
65
66         try:
67             slivertag_id,ipv6addr = get_sliver_tag_id_value(slivertags)
68         except:
69             slivertag_id,ipv6addr = None,None
70         logger.log("update_ipv6addr_slivertag: slice=%s getSliceIPv6Address=%s" % \
71                (slice['name'],ipv6addr) )
72         # if the value to set is null...
73         if value is None:
74             if ipv6addr is not None:
75                 # then, let's remove the slice tag
76                 if slivertag_id:
77                     try:
78                         plc.DeleteSliceTag(slivertag_id)
79                         logger.log("update_ipv6addr_slivertag: slice tag deleted for slice=%s" % \
80                                (slice['name']) )
81                     except:
82                         logger.log("update_ipv6addr_slivertag: slice tag not deleted for slice=%s" % \
83                                (slice['name']) )
84             result = tools.search_ipv6addr_hosts(slice['name'], value)
85             if result:
86                 # if there's any ipv6 address, then remove everything from the /etc/hosts
87                 tools.remove_all_ipv6addr_hosts(slice['name'], data['hostname'])
88         else:
89             # if the ipv6 addr set on the slice does not exist yet, so, let's add it
90             if (ipv6addr is None) and len(value)>0:
91                 try:
92                     logger.log("update_ipv6addr_slivertag: slice name=%s" % (slice['name']) )
93                     slivertag_id=plc.AddSliceTag(slice['name'],tagname,value,node_id)
94                     logger.log("update_ipv6addr_slivertag: slice tag added to slice %s" % \
95                            (slice['name']) )
96                 except:
97                     logger.log("update_ipv6addr_slivertag: could not set ipv6 addr tag to sliver. "+
98                            "slice=%s tag=%s node_id=%d" % (slice['name'],tagname,node_id) )
99             # if the ipv6 addr set on the slice is different on the value provided, let's update it
100             if (ipv6addr is not None) and (len(value)>0) and (ipv6addr!=value):
101                 plc.UpdateSliceTag(slivertag_id,value)
102             # ipv6 entry on /etc/hosts of each slice
103             result = tools.search_ipv6addr_hosts(slice['name'], value)
104             if not result:
105                 tools.remove_all_ipv6addr_hosts(slice['name'], data['hostname'])
106                 tools.add_ipv6addr_hosts_line(slice['name'], data['hostname'], value)
107             #logger.log("update_ipv6addr_slivertag: finishing the update process for " +
108             #   "slice=%s" % (slice['name']) )
109
110 def GetSlivers(data, config, plc):
111     SetSliverTag(plc, data, ipv6addrtag)