+###################################################
+# Author: Guilherme Sperb Machado <gsm@machados.org>
+###################################################
+# Get the slice ipv6 address
+# Only for LXC!
+###################################################
+def get_sliver_ipv6(slice_name):
+ ifconfig = get_sliver_ifconfig(slice_name)
+ if not ifconfig:
+ return None, None
+
+ # example: 'inet6 2001:67c:16dc:1302:5054:ff:fea7:7882 prefixlen 64 scopeid 0x0<global>'
+ prog = re.compile(r'inet6\s+(.*)\s+prefixlen\s+(\d+)\s+scopeid\s+(.+)<global>')
+ for line in ifconfig.split("\n"):
+ search = prog.search(line)
+ if search:
+ ipv6addr = search.group(1)
+ prefixlen = search.group(2)
+ return (ipv6addr, prefixlen)
+ return None, None
+
+###################################################
+# Author: Guilherme Sperb Machado <gsm@machados.org>
+###################################################
+# Check if the address is a AF_INET6 family address
+###################################################
+def is_valid_ipv6(ipv6addr):
+ try:
+ socket.inet_pton(socket.AF_INET6, ipv6addr)
+ except socket.error:
+ return False
+ return True
+
+### this returns the kind of virtualization on the node
+# either 'vs' or 'lxc'
+# also caches it in /etc/planetlab/virt for next calls
+# could be promoted to core nm if need be
+virt_stamp = "/etc/planetlab/virt"
+def get_node_virt ():
+ try:
+ with open(virt_stamp) as f:
+ return f.read().strip()
+ except:
+ pass
+ logger.log("Computing virt..")
+ try:
+ virt = 'vs' if subprocess.call ([ 'vserver', '--help' ]) == 0 else 'lxc'
+ except:
+ virt='lxc'
+ with file(virt_stamp, "w") as f:
+ f.write(virt)
+ return virt
+
+### this return True or False to indicate that systemctl is present on that box
+# cache result in memory as _has_systemctl
+_has_systemctl = None
+def has_systemctl ():
+ global _has_systemctl
+ if _has_systemctl is None:
+ _has_systemctl = (subprocess.call([ 'systemctl', '--help' ]) == 0)
+ return _has_systemctl
+
+###################################################
+# Author: Guilherme Sperb Machado <gsm@machados.org>
+###################################################
+# This method was developed to support the ipv6 plugin
+# Only for LXC!
+###################################################
+def reboot_slivers():
+ type = 'sliver.LXC'
+ # connecting to the libvirtd
+ connLibvirt = Sliver_Libvirt.getConnection(type)
+ domains = connLibvirt.listAllDomains()
+ for domain in domains:
+ try:
+ # set the flag VIR_DOMAIN_REBOOT_INITCTL, which uses "initctl"
+ result = domain.reboot(0x04)
+ if result == 0:
+ logger.log("tools: REBOOT {}".format(domain.name()) )
+ else:
+ raise Exception()
+ except Exception as e:
+ logger.log("tools: FAILED to reboot {} ({})".format(domain.name(), e) )
+ logger.log("tools: Trying to DESTROY/CREATE {} instead...".format(domain.name()) )
+ try:
+ result = domain.destroy()
+ if result==0:
+ logger.log("tools: DESTROYED {}".format(domain.name()) )
+ else: logger.log("tools: FAILED in the DESTROY call of {}".format(domain.name()) )
+ result = domain.create()
+ if result==0:
+ logger.log("tools: CREATED {}".format(domain.name()) )
+ else: logger.log("tools: FAILED in the CREATE call of {}".format(domain.name()) )
+ except Exception as e:
+ logger.log("tools: FAILED to DESTROY/CREATE {} ({})".format(domain.name(), e) )
+
+###################################################
+# Author: Guilherme Sperb Machado <gsm@machados.org>
+###################################################
+# Get the /etc/hosts file path
+###################################################
+def get_hosts_file_path(slicename):
+ containerDir = os.path.join(sliver_lxc.Sliver_LXC.CON_BASE_DIR, slicename)
+ return os.path.join(containerDir, 'etc', 'hosts')
+
+###################################################
+# Author: Guilherme Sperb Machado <gsm@machados.org>
+###################################################
+# Search if there is a specific ipv6 address in the
+# /etc/hosts file of a given slice
+# If the parameter 'ipv6addr' is None, then search
+# for any ipv6 address
+###################################################
+def search_ipv6addr_hosts(slicename, ipv6addr):
+ hostsFilePath = get_hosts_file_path(slicename)
+ found = False
+ try:
+ for line in fileinput.input(r'{}'.format(hostsFilePath)):
+ if ipv6addr is not None:
+ if re.search(r'{}'.format(ipv6addr), line):
+ found = True
+ else:
+ search = re.search(r'^(.*)\s+.*$', line)
+ if search:
+ ipv6candidate = search.group(1)
+ ipv6candidatestrip = ipv6candidate.strip()
+ valid = is_valid_ipv6(ipv6candidatestrip)
+ if valid:
+ found = True
+ fileinput.close()
+ return found
+ except:
+ logger.log("tools: FAILED to search {} in /etc/hosts file of slice={}"
+ .format(ipv6addr, slicename))
+
+###################################################
+# Author: Guilherme Sperb Machado <gsm@machados.org>
+###################################################
+# Removes all ipv6 addresses from the /etc/hosts
+# file of a given slice
+###################################################
+def remove_all_ipv6addr_hosts(slicename, node):
+ hostsFilePath = get_hosts_file_path(slicename)
+ try:
+ for line in fileinput.input(r'{}'.format(hostsFilePath), inplace=True):
+ search = re.search(r'^(.*)\s+({}|{})$'.format(node, 'localhost'), line)
+ if search:
+ ipv6candidate = search.group(1)
+ ipv6candidatestrip = ipv6candidate.strip()
+ valid = is_valid_ipv6(ipv6candidatestrip)
+ if not valid:
+ print(line, end=' ')
+ fileinput.close()
+ logger.log("tools: REMOVED IPv6 address from /etc/hosts file of slice={}"
+ .format(slicename) )
+ except:
+ logger.log("tools: FAILED to remove the IPv6 address from /etc/hosts file of slice={}"
+ .format(slicename) )
+
+###################################################
+# Author: Guilherme Sperb Machado <gsm@machados.org>
+###################################################
+# Adds an ipv6 address to the /etc/hosts file within a slice
+###################################################
+def add_ipv6addr_hosts_line(slicename, node, ipv6addr):
+ hostsFilePath = get_hosts_file_path(slicename)
+ logger.log("tools: {}".format(hostsFilePath) )
+ # debugging purposes:
+ #string = "127.0.0.1\tlocalhost\n192.168.100.179\tmyplc-node1-vm.mgmt.local\n"
+ #string = "127.0.0.1\tlocalhost\n"
+ try:
+ with open(hostsFilePath, "a") as file:
+ file.write(ipv6addr + " " + node + "\n")
+ file.close()
+ logger.log("tools: ADDED IPv6 address to /etc/hosts file of slice={}"
+ .format(slicename) )
+ except:
+ logger.log("tools: FAILED to add the IPv6 address to /etc/hosts file of slice={}"
+ .format(slicename) )
+
+
+
+# how to run a command in a slice
+# now this is a painful matter
+# the problem is with capsh that forces a bash command to be injected in its exec'ed command
+# so because lxcsu uses capsh, you cannot exec anything else than bash
+# bottom line is, what actually needs to be called is
+# vs: vserver exec slicename command and its arguments
+# lxc: lxcsu slicename "command and its arguments"
+# which, OK, is no big deal as long as the command is simple enough,
+# but do not stretch it with arguments that have spaces or need quoting as that will become a nightmare
+def command_in_slice (slicename, argv):
+ virt = get_node_virt()
+ if virt == 'vs':
+ return [ 'vserver', slicename, 'exec', ] + argv
+ elif virt == 'lxc':
+ # wrap up argv in a single string for -c
+ return [ 'lxcsu', slicename, ] + [ " ".join(argv) ]
+ logger.log("command_in_slice: WARNING: could not find a valid virt")
+ return argv
+
+####################
+def init_signals ():
+ def handler (signum, frame):
+ logger.log("Received signal {} - exiting".format(signum))
+ os._exit(1)
+ signal.signal(signal.SIGHUP, handler)
+ signal.signal(signal.SIGQUIT, handler)
+ signal.signal(signal.SIGINT, handler)
+ signal.signal(signal.SIGTERM, handler)