reguire gnupg1 on f>=31; sense the system to use gpg1 when installed
[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={}".format(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         if ipv6addr:
71             logger.log("update_ipv6addr_slivertag: slice={} getSliceIPv6Address={}"
72                        .format(slice['name'], ipv6addr))
73         # if the value to set is null...
74         if value is None:
75             if ipv6addr is not None:
76                 # then, let's remove the slice tag
77                 if slivertag_id:
78                     try:
79                         plc.DeleteSliceTag(slivertag_id)
80                         logger.log("update_ipv6addr_slivertag: slice tag deleted for slice={}"
81                                    .format(slice['name']))
82                     except:
83                         logger.log("update_ipv6addr_slivertag: slice tag not deleted for slice={}"
84                                    .format(slice['name']))
85             result = tools.search_ipv6addr_hosts(slice['name'], value)
86             if result:
87                 # if there's any ipv6 address, then remove everything from the /etc/hosts
88                 tools.remove_all_ipv6addr_hosts(slice['name'], data['hostname'])
89         else:
90             # if the ipv6 addr set on the slice does not exist yet, so, let's add it
91             if (ipv6addr is None) and len(value)>0:
92                 try:
93                     logger.log("update_ipv6addr_slivertag: slice name={}".format(slice['name']))
94                     slivertag_id=plc.AddSliceTag(slice['name'], tagname, value, node_id)
95                     logger.log("update_ipv6addr_slivertag: slice tag added to slice {}"
96                                .format(slice['name']))
97                 except:
98                     logger.log("update_ipv6addr_slivertag: could not set ipv6 addr tag to sliver. "
99                                "slice={} tag={} node_id={}".format(slice['name'], tagname, node_id))
100             # if the ipv6 addr set on the slice is different on the value provided, let's update it
101             if (ipv6addr is not None) and (len(value) > 0) and (ipv6addr != value):
102                 plc.UpdateSliceTag(slivertag_id, value)
103             # ipv6 entry on /etc/hosts of each slice
104             result = tools.search_ipv6addr_hosts(slice['name'], value)
105             if not result:
106                 tools.remove_all_ipv6addr_hosts(slice['name'], data['hostname'])
107                 tools.add_ipv6addr_hosts_line(slice['name'], data['hostname'], value)
108             #logger.log("update_ipv6addr_slivertag: finishing the update process for "
109             #   "slice={}".format(slice['name']))
110
111 def GetSlivers(data, config, plc):
112     SetSliverTag(plc, data, ipv6addrtag)