+### 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, 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, 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,
+ 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) )
+
+
+