+### 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:
+ domain.destroy()
+ logger.log("tools: DESTROYED %s" % (domain.name()) )
+ domain.create()
+ logger.log("tools: CREATED %s" % (domain.name()) )
+ except:
+ logger.log("tools: FAILED to reboot %s" % (domain.name()) )
+
+###################################################
+# 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
+###################################################
+def search_ipv6addr_hosts(slicename, ipv6addr):
+ hostsFilePath = get_hosts_file_path(slicename)
+ found=False
+ try:
+ for line in fileinput.input(r'%s' % (hostsFilePath)):
+ if re.search(r'%s' % (ipv6addr), line):
+ found=True
+ fileinput.close()
+ return found
+ except:
+ logger.log("tools: FAILED to search %s in /etc/hosts file of slice=%s" % \
+ (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'%s' % (hostsFilePath), inplace=True):
+ search = re.search(r'^(.*)\s+(%s|%s)$' % (node,'localhost'), line)
+ if search:
+ ipv6candidate = search.group(1)
+ ipv6candidatestrip = ipv6candidate.strip()
+ valid = is_valid_ipv6(ipv6candidatestrip)
+ if not valid:
+ print line,
+ fileinput.close()
+ logger.log("tools: REMOVED IPv6 address from /etc/hosts file of slice=%s" % \
+ (slicename) )
+ except:
+ logger.log("tools: FAILED to remove the IPv6 address from /etc/hosts file of slice=%s" % \
+ (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: %s" % (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=%s" % \
+ (slicename) )
+ except:
+ logger.log("tools: FAILED to add the IPv6 address to /etc/hosts file of slice=%s" % \
+ (slicename) )
+
+
+