This is an automated email from the ASF dual-hosted git repository. gabriel pushed a commit to branch python3-vr in repository https://gitbox.apache.org/repos/asf/cloudstack.git
commit ab0118064494a57d58845a3289ec326778372c53 Author: Gabriel Brascher <gabr...@apache.org> AuthorDate: Fri Jul 24 09:02:58 2020 -0300 Make Virtual Router '.py' scripts Python3 compatible --- systemvm/debian/opt/cloud/bin/configure.py | 69 +++++++++++----------- systemvm/debian/opt/cloud/bin/cs/CsAddress.py | 32 +++++----- systemvm/debian/opt/cloud/bin/cs/CsApp.py | 5 +- systemvm/debian/opt/cloud/bin/cs/CsConfig.py | 4 +- systemvm/debian/opt/cloud/bin/cs/CsDatabag.py | 2 +- systemvm/debian/opt/cloud/bin/cs/CsDhcp.py | 8 +-- systemvm/debian/opt/cloud/bin/cs/CsFile.py | 4 +- systemvm/debian/opt/cloud/bin/cs/CsGuestNetwork.py | 4 +- systemvm/debian/opt/cloud/bin/cs/CsHelper.py | 6 +- systemvm/debian/opt/cloud/bin/cs/CsLoadBalancer.py | 10 ++-- systemvm/debian/opt/cloud/bin/cs/CsMonitor.py | 8 +-- systemvm/debian/opt/cloud/bin/cs/CsNetfilter.py | 20 +++---- systemvm/debian/opt/cloud/bin/cs/CsProcess.py | 11 ++-- systemvm/debian/opt/cloud/bin/cs/CsRedundant.py | 22 +++---- systemvm/debian/opt/cloud/bin/cs/CsRoute.py | 6 +- systemvm/debian/opt/cloud/bin/cs/CsRule.py | 4 +- systemvm/debian/opt/cloud/bin/cs/CsStaticRoutes.py | 6 +- systemvm/debian/opt/cloud/bin/cs_dhcp.py | 4 +- systemvm/debian/opt/cloud/bin/cs_firewallrules.py | 4 +- .../debian/opt/cloud/bin/cs_forwardingrules.py | 8 +-- systemvm/debian/opt/cloud/bin/cs_guestnetwork.py | 4 +- systemvm/debian/opt/cloud/bin/cs_ip.py | 12 ++-- systemvm/debian/opt/cloud/bin/cs_monitorservice.py | 2 +- systemvm/debian/opt/cloud/bin/cs_network_acl.py | 2 +- .../debian/opt/cloud/bin/cs_remoteaccessvpn.py | 2 +- systemvm/debian/opt/cloud/bin/cs_site2sitevpn.py | 2 +- systemvm/debian/opt/cloud/bin/cs_vmp.py | 2 +- systemvm/debian/opt/cloud/bin/cs_vpnusers.py | 10 ++-- systemvm/debian/opt/cloud/bin/diagnostics.py | 2 +- .../debian/opt/cloud/bin/get_diagnostics_files.py | 4 +- systemvm/debian/opt/cloud/bin/merge.py | 6 +- systemvm/debian/opt/cloud/bin/passwd_server_ip.py | 16 ++--- systemvm/debian/opt/cloud/bin/update_config.py | 4 +- systemvm/debian/opt/cloud/bin/vmdata.py | 22 +++---- .../debian/root/health_checks/cpu_usage_check.py | 16 ++--- systemvm/debian/root/health_checks/dhcp_check.py | 10 ++-- .../debian/root/health_checks/disk_space_check.py | 10 ++-- systemvm/debian/root/health_checks/dns_check.py | 10 ++-- .../debian/root/health_checks/gateways_check.py | 12 ++-- .../debian/root/health_checks/haproxy_check.py | 26 ++++---- .../debian/root/health_checks/iptables_check.py | 10 ++-- .../root/health_checks/memory_usage_check.py | 18 +++--- .../root/health_checks/router_version_check.py | 25 ++++---- .../debian/root/health_checks/utility/__init__.py | 4 +- systemvm/debian/root/monitorServices.py | 12 ++-- systemvm/test/runtests.sh | 7 +++ 46 files changed, 250 insertions(+), 237 deletions(-) diff --git a/systemvm/debian/opt/cloud/bin/configure.py b/systemvm/debian/opt/cloud/bin/configure.py index be67f40..5378180 100755 --- a/systemvm/debian/opt/cloud/bin/configure.py +++ b/systemvm/debian/opt/cloud/bin/configure.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # -- coding: utf-8 -- # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file @@ -21,8 +21,9 @@ import logging import os import re import sys -import urllib -import urllib2 +from urllib.request import * +from urllib.parse import * +from urllib.error import * import time from collections import OrderedDict @@ -77,10 +78,10 @@ class CsPassword(CsDataBag): if proc.find(): url = "http://%s:8080/" % server_ip payload = {"ip": vm_ip, "password": password, "token": token} - data = urllib.urlencode(payload) - request = urllib2.Request(url, data=data, headers={"DomU_Request": "save_password"}) + data = urllib.parse.urlencode(payload) + request = urllib.request.Request(url, data=data, headers={"DomU_Request": "save_password"}) try: - resp = urllib2.urlopen(request, data) + resp = urlopen(request, data) logging.debug("Update password server result: http:%s, content:%s" % (resp.code, resp.read())) except Exception as e: logging.error("Failed to update password server due to: %s" % e) @@ -135,15 +136,15 @@ class CsAcl(CsDataBag): icmp_type = '' rule = self.rule icmp_type = "any" - if "icmp_type" in self.rule.keys() and self.rule['icmp_type'] != -1: + if "icmp_type" in list(self.rule.keys()) and self.rule['icmp_type'] != -1: icmp_type = self.rule['icmp_type'] - if "icmp_code" in self.rule.keys() and rule['icmp_code'] != -1: + if "icmp_code" in list(self.rule.keys()) and rule['icmp_code'] != -1: icmp_type = "%s/%s" % (self.rule['icmp_type'], self.rule['icmp_code']) rnge = '' - if "first_port" in self.rule.keys() and \ + if "first_port" in list(self.rule.keys()) and \ self.rule['first_port'] == self.rule['last_port']: rnge = " --dport %s " % self.rule['first_port'] - if "first_port" in self.rule.keys() and \ + if "first_port" in list(self.rule.keys()) and \ self.rule['first_port'] != self.rule['last_port']: rnge = " --dport %s:%s" % (rule['first_port'], rule['last_port']) @@ -246,9 +247,9 @@ class CsAcl(CsDataBag): self.netmask = obj['nic_netmask'] self.config = config self.cidr = "%s/%s" % (self.ip, self.netmask) - if "ingress_rules" in obj.keys(): + if "ingress_rules" in list(obj.keys()): self.ingress = obj['ingress_rules'] - if "egress_rules" in obj.keys(): + if "egress_rules" in list(obj.keys()): self.egress = obj['egress_rules'] self.fw = config.get_fw() @@ -286,9 +287,9 @@ class CsAcl(CsDataBag): self.type = rule['type'] self.icmp_type = "any" self.protocol = self.type - if "icmp_type" in rule.keys() and rule['icmp_type'] != -1: + if "icmp_type" in list(rule.keys()) and rule['icmp_type'] != -1: self.icmp_type = rule['icmp_type'] - if "icmp_code" in rule.keys() and rule['icmp_code'] != -1: + if "icmp_code" in list(rule.keys()) and rule['icmp_code'] != -1: self.icmp_type = "%s/%s" % (self.icmp_type, rule['icmp_code']) if self.type == "protocol": if rule['protocol'] == 41: @@ -296,11 +297,11 @@ class CsAcl(CsDataBag): self.protocol = rule['protocol'] self.action = "DROP" self.dport = "" - if 'allowed' in rule.keys() and rule['allowed']: + if 'allowed' in list(rule.keys()) and rule['allowed']: self.action = "ACCEPT" - if 'first_port' in rule.keys(): + if 'first_port' in list(rule.keys()): self.dport = "-m %s --dport %s" % (self.protocol, rule['first_port']) - if 'last_port' in rule.keys() and self.dport and \ + if 'last_port' in list(rule.keys()) and self.dport and \ rule['last_port'] != rule['first_port']: self.dport = "%s:%s" % (self.dport, rule['last_port']) @@ -385,18 +386,18 @@ class CsVmMetadata(CsDataBag): fh.write("") self.__unflock(fh) fh.close() - os.chmod(dest, 0644) + os.chmod(dest, 0o644) if folder == "metadata" or folder == "meta-data": try: - os.makedirs(metamanifestdir, 0755) + os.makedirs(metamanifestdir, 0o755) except OSError as e: # error 17 is already exists, we do it this way for concurrency if e.errno != 17: - print "failed to make directories " + metamanifestdir + " due to :" + e.strerror + print("failed to make directories " + metamanifestdir + " due to :" + e.strerror) sys.exit(1) if os.path.exists(metamanifest): - fh = open(metamanifest, "r+a") + fh = open(metamanifest, "r+") self.__exflock(fh) if file not in fh.read(): fh.write(file + '\n') @@ -410,17 +411,17 @@ class CsVmMetadata(CsDataBag): fh.close() if os.path.exists(metamanifest): - os.chmod(metamanifest, 0644) + os.chmod(metamanifest, 0o644) def __htaccess(self, ip, folder, file): entry = "RewriteRule ^" + file + "$ ../" + folder + "/%{REMOTE_ADDR}/" + file + " [L,NC,QSA]" htaccessFolder = "/var/www/html/latest" htaccessFile = htaccessFolder + "/.htaccess" - CsHelper.mkdir(htaccessFolder, 0755, True) + CsHelper.mkdir(htaccessFolder, 0o755, True) if os.path.exists(htaccessFile): - fh = open(htaccessFile, "r+a") + fh = open(htaccessFile, "r+") self.__exflock(fh) if entry not in fh.read(): fh.write(entry + '\n') @@ -439,11 +440,11 @@ class CsVmMetadata(CsDataBag): htaccessFile = htaccessFolder+"/.htaccess" try: - os.makedirs(htaccessFolder, 0755) + os.makedirs(htaccessFolder, 0o755) except OSError as e: # error 17 is already exists, we do it this way for sake of concurrency if e.errno != 17: - print "failed to make directories " + htaccessFolder + " due to :" + e.strerror + print("failed to make directories " + htaccessFolder + " due to :" + e.strerror) sys.exit(1) fh = open(htaccessFile, "w") @@ -457,7 +458,7 @@ class CsVmMetadata(CsDataBag): htaccessFolder = "/var/www/html/latest" htaccessFile = htaccessFolder + "/.htaccess" - fh = open(htaccessFile, "r+a") + fh = open(htaccessFile, "r+") self.__exflock(fh) if entry not in fh.read(): fh.write(entry + '\n') @@ -474,7 +475,7 @@ class CsVmMetadata(CsDataBag): try: flock(file, LOCK_EX) except IOError as e: - print "failed to lock file" + file.name + " due to : " + e.strerror + print("failed to lock file" + file.name + " due to : " + e.strerror) sys.exit(1) # FIXME return True @@ -482,7 +483,7 @@ class CsVmMetadata(CsDataBag): try: flock(file, LOCK_UN) except IOError as e: - print "failed to unlock file" + file.name + " due to : " + e.strerror + print("failed to unlock file" + file.name + " due to : " + e.strerror) sys.exit(1) # FIXME return True @@ -592,9 +593,9 @@ class CsSite2SiteVpn(CsDataBag): # This will load the new config CsHelper.execute("ipsec reload") - os.chmod(vpnsecretsfile, 0400) + os.chmod(vpnsecretsfile, 0o400) - for i in xrange(3): + for i in range(3): result = CsHelper.execute('ipsec status vpn-%s | grep "%s"' % (rightpeer, peerlist.split(",", 1)[0])) if len(result) > 0: break @@ -1060,7 +1061,7 @@ def main(argv): ]) def execDatabag(key, db): - if key not in db.keys() or 'executor' not in db[key]: + if key not in list(db.keys()) or 'executor' not in db[key]: logging.warn("Unable to find config or executor(s) for the databag type %s" % key) return for executor in db[key]['executor']: @@ -1074,10 +1075,10 @@ def main(argv): if json_type == "cmd_line": logging.debug("cmd_line.json changed. All other files will be processed as well.") - for key in databag_map.keys(): + for key in list(databag_map.keys()): execDatabag(key, databag_map) execIptables(config) - elif json_type in databag_map.keys(): + elif json_type in list(databag_map.keys()): execDatabag(json_type, databag_map) if databag_map[json_type]['process_iptables']: execIptables(config) diff --git a/systemvm/debian/opt/cloud/bin/cs/CsAddress.py b/systemvm/debian/opt/cloud/bin/cs/CsAddress.py index 44b6950..8f688d7 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsAddress.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsAddress.py @@ -16,14 +16,14 @@ # specific language governing permissions and limitations # under the License. import logging -from netaddr import IPAddress, IPNetwork +from ipaddress import * import subprocess import time -import CsHelper -from CsDatabag import CsDataBag -from CsApp import CsApache, CsDnsmasq, CsPasswdSvc -from CsRoute import CsRoute -from CsRule import CsRule +from . import CsHelper +from .CsDatabag import CsDataBag +from .CsApp import CsApache, CsDnsmasq, CsPasswdSvc +from .CsRoute import CsRoute +from .CsRule import CsRule VRRP_TYPES = ['guest'] @@ -148,8 +148,8 @@ class CsInterface: return self.config.cmdline().get_guest_gw() def ip_in_subnet(self, ip): - ipo = IPAddress(ip) - net = IPNetwork("%s/%s" % (self.get_ip(), self.get_size())) + ipo = ip_address(ip) + net = ip_network("%s/%s" % (self.get_ip(), self.get_size())) return ipo in net def get_gateway_cidr(self): @@ -496,7 +496,7 @@ class CsIP: "-A POSTROUTING -o %s -j SNAT --to-source %s" % (self.dev, self.address['public_ip'])]) if self.get_gateway() == self.get_ip_address(): - for inf, addresses in self.config.address().dbag.iteritems(): + for inf, addresses in list(self.config.address().dbag.items()): if not inf.startswith("eth"): continue for address in addresses: @@ -562,7 +562,7 @@ class CsIP: if self.config.is_vpc(): if self.get_type() in ["public"] and "gateway" in self.address and self.address["gateway"] and self.address["gateway"] != "None": route.add_route(self.dev, self.address["gateway"]) - for inf, addresses in self.config.address().dbag.iteritems(): + for inf, addresses in list(self.config.address().dbag.items()): if not inf.startswith("eth"): continue for address in addresses: @@ -636,7 +636,7 @@ class CsIP: self.iplist[cidr] = self.dev def configured(self): - if self.address['cidr'] in self.iplist.keys(): + if self.address['cidr'] in list(self.iplist.keys()): return True return False @@ -665,7 +665,7 @@ class CsIP: return self.dev def hasIP(self, ip): - return ip in self.address.values() + return ip in list(self.address.values()) def arpPing(self): cmd = "arping -c 1 -I %s -A -U -s %s %s" % ( @@ -676,7 +676,7 @@ class CsIP: # Delete any ips that are configured but not in the bag def compare(self, bag): - if len(self.iplist) > 0 and (self.dev not in bag.keys() or len(bag[self.dev]) == 0): + if len(self.iplist) > 0 and (self.dev not in list(bag.keys()) or len(bag[self.dev]) == 0): # Remove all IPs on this device logging.info( "Will remove all configured addresses on device %s", self.dev) @@ -687,13 +687,13 @@ class CsIP: # This condition should not really happen but did :) # It means an apache file got orphaned after a guest network address # was deleted - if len(self.iplist) == 0 and (self.dev not in bag.keys() or len(bag[self.dev]) == 0): + if len(self.iplist) == 0 and (self.dev not in list(bag.keys()) or len(bag[self.dev]) == 0): app = CsApache(self) app.remove() for ip in self.iplist: found = False - if self.dev in bag.keys(): + if self.dev in list(bag.keys()): for address in bag[self.dev]: self.setAddress(address) if (self.hasIP(ip) or self.is_guest_gateway(address, ip)) and address["add"]: @@ -726,7 +726,7 @@ class CsIP: remove = [] if ip == "all": logging.info("Removing addresses from device %s", self.dev) - remove = self.iplist.keys() + remove = list(self.iplist.keys()) else: remove.append(ip) for ip in remove: diff --git a/systemvm/debian/opt/cloud/bin/cs/CsApp.py b/systemvm/debian/opt/cloud/bin/cs/CsApp.py index a2292ae..412ae3a 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsApp.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsApp.py @@ -16,8 +16,9 @@ # specific language governing permissions and limitations # under the License. import os -from CsFile import CsFile -import CsHelper +from .CsFile import CsFile +from .CsProcess import CsProcess +from . import CsHelper class CsApp: diff --git a/systemvm/debian/opt/cloud/bin/cs/CsConfig.py b/systemvm/debian/opt/cloud/bin/cs/CsConfig.py index 390f563..4975f55 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsConfig.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsConfig.py @@ -16,8 +16,8 @@ # specific language governing permissions and limitations # under the License. -from CsDatabag import CsCmdLine -from CsAddress import CsAddress +from .CsDatabag import CsCmdLine +from .CsAddress import CsAddress import logging diff --git a/systemvm/debian/opt/cloud/bin/cs/CsDatabag.py b/systemvm/debian/opt/cloud/bin/cs/CsDatabag.py index adb9a1a..8154e63 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsDatabag.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsDatabag.py @@ -33,7 +33,7 @@ class CsDataBag(object): self.config = config def dump(self): - print self.dbag + print(self.dbag) def get_bag(self): return self.dbag diff --git a/systemvm/debian/opt/cloud/bin/cs/CsDhcp.py b/systemvm/debian/opt/cloud/bin/cs/CsDhcp.py index 2c0deea..7769024 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsDhcp.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsDhcp.py @@ -14,12 +14,12 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -import CsHelper +from . import CsHelper import logging import os -from netaddr import * +from ipaddress import ip_address from random import randint -from CsGuestNetwork import CsGuestNetwork +from .CsGuestNetwork import CsGuestNetwork from cs.CsDatabag import CsDataBag from cs.CsFile import CsFile @@ -207,7 +207,7 @@ class CsDhcp(CsDataBag): entry['ipv4_address'], entry['host_name'])) - i = IPAddress(entry['ipv4_address']) + i = ip_address(entry['ipv4_address']) # Calculate the device for v in self.devinfo: if i > v['network'].network and i < v['network'].broadcast: diff --git a/systemvm/debian/opt/cloud/bin/cs/CsFile.py b/systemvm/debian/opt/cloud/bin/cs/CsFile.py index 2ee631a..bad9cd9 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsFile.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsFile.py @@ -70,7 +70,7 @@ class CsFile: def dump(self): for line in self.new_config: - print line + print(line) def addeq(self, string): """ Update a line in a file of the form token=something @@ -153,7 +153,7 @@ class CsFile: logging.debug("Searching for %s string " % search) for index, line in enumerate(self.new_config): - print ' line = ' + line + print(' line = ' + line) if line.lstrip().startswith(ignoreLinesStartWith): continue if search in line: diff --git a/systemvm/debian/opt/cloud/bin/cs/CsGuestNetwork.py b/systemvm/debian/opt/cloud/bin/cs/CsGuestNetwork.py index 9a94dc6..a934862 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsGuestNetwork.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsGuestNetwork.py @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. from merge import DataBag -import CsHelper +from . import CsHelper class CsGuestNetwork: @@ -27,7 +27,7 @@ class CsGuestNetwork: db.load() dbag = db.getDataBag() self.config = config - if device in dbag.keys() and len(dbag[device]) != 0: + if device in list(dbag.keys()) and len(dbag[device]) != 0: self.data = dbag[device][0] else: self.guest = False diff --git a/systemvm/debian/opt/cloud/bin/cs/CsHelper.py b/systemvm/debian/opt/cloud/bin/cs/CsHelper.py index 00aa4cb..c818c39 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsHelper.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsHelper.py @@ -25,7 +25,7 @@ import sys import os.path import re import shutil -from netaddr import * +from ipaddress import * PUBLIC_INTERFACES = {"router": "eth2", "vpcrouter": "eth1"} @@ -86,7 +86,7 @@ def mkdir(name, mode, fatal): os.makedirs(name, mode) except OSError as e: if e.errno != 17: - print "failed to make directories " + name + " due to :" + e.strerror + print("failed to make directories " + name + " due to :" + e.strerror) if(fatal): sys.exit(1) @@ -119,7 +119,7 @@ def get_device_info(): to = {} to['ip'] = vals[1] to['dev'] = vals[-1] - to['network'] = IPNetwork(to['ip']) + to['network'] = ip_network(to['ip']) to['dnsmasq'] = False list.append(to) return list diff --git a/systemvm/debian/opt/cloud/bin/cs/CsLoadBalancer.py b/systemvm/debian/opt/cloud/bin/cs/CsLoadBalancer.py index a45d57e..a92f06b 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsLoadBalancer.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsLoadBalancer.py @@ -18,9 +18,9 @@ import logging import os.path import re from cs.CsDatabag import CsDataBag -from CsProcess import CsProcess -from CsFile import CsFile -import CsHelper +from .CsProcess import CsProcess +from .CsFile import CsFile +from . import CsHelper HAPROXY_CONF_T = "/etc/haproxy/haproxy.cfg.new" HAPROXY_CONF_P = "/etc/haproxy/haproxy.cfg" @@ -30,9 +30,9 @@ class CsLoadBalancer(CsDataBag): """ Manage Load Balancer entries """ def process(self): - if "config" not in self.dbag.keys(): + if "config" not in list(self.dbag.keys()): return - if 'configuration' not in self.dbag['config'][0].keys(): + if 'configuration' not in list(self.dbag['config'][0].keys()): return config = self.dbag['config'][0]['configuration'] file1 = CsFile(HAPROXY_CONF_T) diff --git a/systemvm/debian/opt/cloud/bin/cs/CsMonitor.py b/systemvm/debian/opt/cloud/bin/cs/CsMonitor.py index 5a0ff5b..3be158a 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsMonitor.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsMonitor.py @@ -16,7 +16,7 @@ # under the License. import logging from cs.CsDatabag import CsDataBag -from CsFile import CsFile +from .CsFile import CsFile import json MON_CONFIG = "/etc/monitor.conf" @@ -48,13 +48,13 @@ class CsMonitor(CsDataBag): cron_rep_basic = self.get_basic_check_interval() cron_rep_advanced = self.get_advanced_check_interval() cron = CsFile("/etc/cron.d/process") - cron.deleteLine("root /usr/bin/python /root/monitorServices.py") + cron.deleteLine("root /usr/bin/python3 /root/monitorServices.py") cron.add("SHELL=/bin/bash", 0) cron.add("PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin", 1) if cron_rep_basic > 0: - cron.add("*/" + str(cron_rep_basic) + " * * * * root /usr/bin/python /root/monitorServices.py basic", -1) + cron.add("*/" + str(cron_rep_basic) + " * * * * root /usr/bin/python3 /root/monitorServices.py basic", -1) if cron_rep_advanced > 0: - cron.add("*/" + str(cron_rep_advanced) + " * * * * root /usr/bin/python /root/monitorServices.py advanced", -1) + cron.add("*/" + str(cron_rep_advanced) + " * * * * root /usr/bin/python3 /root/monitorServices.py advanced", -1) cron.commit() def setupHealthChecksConfigFile(self): diff --git a/systemvm/debian/opt/cloud/bin/cs/CsNetfilter.py b/systemvm/debian/opt/cloud/bin/cs/CsNetfilter.py index 01dfa7c..e39595d 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsNetfilter.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsNetfilter.py @@ -15,8 +15,8 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -import CsHelper -from CsDatabag import CsCmdLine +from . import CsHelper +from .CsDatabag import CsCmdLine import logging @@ -28,7 +28,7 @@ class CsChain(object): self.count = {} def add(self, table, chain): - if table not in self.chain.keys(): + if table not in list(self.chain.keys()): self.chain.setdefault(table, []).append(chain) else: self.chain[table].append(chain) @@ -40,7 +40,7 @@ class CsChain(object): self.count[chain] += 1 def get(self, table): - if table not in self.chain.keys(): + if table not in list(self.chain.keys()): return {} return self.chain[table] @@ -51,7 +51,7 @@ class CsChain(object): return self.last_added def has_chain(self, table, chain): - if table not in self.chain.keys(): + if table not in list(self.chain.keys()): return False if chain not in self.chain[table]: return False @@ -242,7 +242,7 @@ class CsNetfilter(object): self.seen = True def __convert_to_dict(self, rule): - rule = unicode(rule.lstrip()) + rule = str(rule.lstrip()) rule = rule.replace('! -', '!_-') rule = rule.replace('-p all', '') rule = rule.replace(' ', ' ') @@ -253,8 +253,8 @@ class CsNetfilter(object): rule = rule.replace('-m state', '-m2 state') rule = rule.replace('ESTABLISHED,RELATED', 'RELATED,ESTABLISHED') bits = rule.split(' ') - rule = dict(zip(bits[0::2], bits[1::2])) - if "-A" in rule.keys(): + rule = dict(list(zip(bits[0::2], bits[1::2]))) + if "-A" in list(rule.keys()): self.chain = rule["-A"] return rule @@ -289,7 +289,7 @@ class CsNetfilter(object): '--to-source', '--to-destination', '--mark'] str = '' for k in order: - if k in self.rule.keys(): + if k in list(self.rule.keys()): printable = k.replace('-m2', '-m') printable = printable.replace('!_-', '! -') if delete: @@ -306,7 +306,7 @@ class CsNetfilter(object): return False if rule.get_chain() != self.get_chain(): return False - if len(rule.get_rule().items()) != len(self.get_rule().items()): + if len(list(rule.get_rule().items())) != len(list(self.get_rule().items())): return False common = set(rule.get_rule().items()) & set(self.get_rule().items()) if len(common) != len(rule.get_rule()): diff --git a/systemvm/debian/opt/cloud/bin/cs/CsProcess.py b/systemvm/debian/opt/cloud/bin/cs/CsProcess.py index 4a64807..f768c26f 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsProcess.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsProcess.py @@ -17,7 +17,7 @@ # under the License. import os import re -import CsHelper +from . import CsHelper import logging @@ -42,12 +42,15 @@ class CsProcess(object): self.pid = [] items = len(self.search) for i in CsHelper.execute("ps aux"): - proc = re.split(r"\s+", i)[10:] + items = len(self.search) + decodedItem = i.decode() + proc = re.split(r"\s+", decodedItem)[10:] matches = len([m for m in proc if m in self.search]) if matches == items: - self.pid.append(re.split(r"\s+", i)[1]) + self.pid.append(re.split(r"\s+", decodedItem)[1]) - logging.debug("CsProcess:: Searching for process ==> %s and found PIDs ==> %s", self.search, self.pid) + log_str = "CsProcess:: Searching for process ==> %s and found PIDs ==> %s" % ("self.search", "self.pid") + logging.debug(log_str) return self.pid def find(self): diff --git a/systemvm/debian/opt/cloud/bin/cs/CsRedundant.py b/systemvm/debian/opt/cloud/bin/cs/CsRedundant.py index 190de1a..7caf9ee 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsRedundant.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsRedundant.py @@ -32,13 +32,13 @@ # -------------------------------------------------------------------- # import os import logging -import CsHelper -from CsFile import CsFile -from CsProcess import CsProcess -from CsApp import CsPasswdSvc -from CsAddress import CsDevice -from CsRoute import CsRoute -from CsStaticRoutes import CsStaticRoutes +from . import CsHelper +from .CsFile import CsFile +from .CsProcess import CsProcess +from .CsApp import CsPasswdSvc +from .CsAddress import CsDevice +from .CsRoute import CsRoute +from .CsStaticRoutes import CsStaticRoutes import socket from time import sleep @@ -111,9 +111,9 @@ class CsRedundant(object): CsHelper.service("keepalived", "stop") return - CsHelper.mkdir(self.CS_RAMDISK_DIR, 0755, False) + CsHelper.mkdir(self.CS_RAMDISK_DIR, 0o755, False) CsHelper.mount_tmpfs(self.CS_RAMDISK_DIR) - CsHelper.mkdir(self.CS_ROUTER_DIR, 0755, False) + CsHelper.mkdir(self.CS_ROUTER_DIR, 0o755, False) for s in self.CS_TEMPLATES: d = s if s.endswith(".templ"): @@ -222,10 +222,10 @@ class CsRedundant(object): s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.bind('/tmp/master_lock') return s - except socket.error, e: + except socket.error as e: error_code = e.args[0] error_string = e.args[1] - print "Process already running (%d:%s). Exiting" % (error_code, error_string) + print("Process already running (%d:%s). Exiting" % (error_code, error_string)) logging.info("Master is already running, waiting") sleep(time_between) diff --git a/systemvm/debian/opt/cloud/bin/cs/CsRoute.py b/systemvm/debian/opt/cloud/bin/cs/CsRoute.py index a77a625..5ec5587 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsRoute.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsRoute.py @@ -15,7 +15,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -import CsHelper +from . import CsHelper import logging @@ -116,7 +116,7 @@ class CsRoute: route_found = CsHelper.execute("ip -4 route list 0/0") if len(route_found) > 0: - logging.info("Default route found: " + route_found[0]) + logging.info("Default route found: " + route_found[0].decode()) return True else: logging.warn("No default route found!") @@ -124,6 +124,6 @@ class CsRoute: def findRule(self, rule): for i in CsHelper.execute("ip rule show"): - if rule in i.strip(): + if rule in i.decode().strip(): return True return False diff --git a/systemvm/debian/opt/cloud/bin/cs/CsRule.py b/systemvm/debian/opt/cloud/bin/cs/CsRule.py index f1caa29..140bef7 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsRule.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsRule.py @@ -15,7 +15,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -import CsHelper +from . import CsHelper import logging @@ -57,6 +57,6 @@ class CsRule: def findMark(self): srch = "from all fwmark %s lookup %s" % (hex(self.tableNo), self.table) for i in CsHelper.execute("ip rule show"): - if srch in i.strip(): + if srch in i.decode().strip(): return True return False diff --git a/systemvm/debian/opt/cloud/bin/cs/CsStaticRoutes.py b/systemvm/debian/opt/cloud/bin/cs/CsStaticRoutes.py index df98b2e..3dd5fdf 100755 --- a/systemvm/debian/opt/cloud/bin/cs/CsStaticRoutes.py +++ b/systemvm/debian/opt/cloud/bin/cs/CsStaticRoutes.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # -- coding: utf-8 -- # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file @@ -18,8 +18,8 @@ # under the License. import logging -import CsHelper -from CsDatabag import CsDataBag +from . import CsHelper +from .CsDatabag import CsDataBag class CsStaticRoutes(CsDataBag): diff --git a/systemvm/debian/opt/cloud/bin/cs_dhcp.py b/systemvm/debian/opt/cloud/bin/cs_dhcp.py index d949981..9404e5b 100755 --- a/systemvm/debian/opt/cloud/bin/cs_dhcp.py +++ b/systemvm/debian/opt/cloud/bin/cs_dhcp.py @@ -16,7 +16,7 @@ # under the License. import logging -from netaddr import * +from ipaddress import * def merge(dbag, data): @@ -27,7 +27,7 @@ def merge(dbag, data): del(dbag[data['ipv4_address']]) else: remove_keys = set() - for key, entry in dbag.iteritems(): + for key, entry in list(dbag.items()): if key != 'id' and entry['mac_address'] == data['mac_address'] and data['remove']: remove_keys.add(key) break diff --git a/systemvm/debian/opt/cloud/bin/cs_firewallrules.py b/systemvm/debian/opt/cloud/bin/cs_firewallrules.py index 1357c6c..3c5e875 100755 --- a/systemvm/debian/opt/cloud/bin/cs_firewallrules.py +++ b/systemvm/debian/opt/cloud/bin/cs_firewallrules.py @@ -25,8 +25,8 @@ def merge(dbag, data): for rule in data['rules']: id = str(rule['id']) if rule['revoked']: - if id in dbagc.keys(): + if id in list(dbagc.keys()): del(dbagc[id]) - elif id not in dbagc.keys(): + elif id not in list(dbagc.keys()): dbagc[id] = rule return dbagc diff --git a/systemvm/debian/opt/cloud/bin/cs_forwardingrules.py b/systemvm/debian/opt/cloud/bin/cs_forwardingrules.py index 974c468..ec66979 100755 --- a/systemvm/debian/opt/cloud/bin/cs_forwardingrules.py +++ b/systemvm/debian/opt/cloud/bin/cs_forwardingrules.py @@ -39,7 +39,7 @@ def merge(dbag, rules): dbag[source_ip] = [newrule] elif rules["type"] == "forwardrules": index = -1 - if source_ip in dbag.keys(): + if source_ip in list(dbag.keys()): for forward in dbag[source_ip]: if ruleCompare(forward, newrule): index = dbag[source_ip].index(forward) @@ -51,15 +51,15 @@ def merge(dbag, rules): dbag[source_ip] = [newrule] else: if rules["type"] == "staticnatrules": - if source_ip in dbag.keys(): + if source_ip in list(dbag.keys()): del dbag[source_ip] elif rules["type"] == "forwardrules": - if source_ip in dbag.keys(): + if source_ip in list(dbag.keys()): index = -1 for forward in dbag[source_ip]: if ruleCompare(forward, newrule): index = dbag[source_ip].index(forward) - print "removing index %s" % str(index) + print("removing index %s" % str(index)) if not index == -1: del dbag[source_ip][index] diff --git a/systemvm/debian/opt/cloud/bin/cs_guestnetwork.py b/systemvm/debian/opt/cloud/bin/cs_guestnetwork.py index 9543469..c93fee5 100755 --- a/systemvm/debian/opt/cloud/bin/cs_guestnetwork.py +++ b/systemvm/debian/opt/cloud/bin/cs_guestnetwork.py @@ -28,8 +28,8 @@ def merge(dbag, gn): device_to_die = dbag[device][0] try: dbag[device].remove(device_to_die) - except ValueError, e: - print "[WARN] cs_guestnetwork.py :: Error occurred removing item from databag. => %s" % device_to_die + except ValueError as e: + print("[WARN] cs_guestnetwork.py :: Error occurred removing item from databag. => %s" % device_to_die) del(dbag[device]) else: del(dbag[device]) diff --git a/systemvm/debian/opt/cloud/bin/cs_ip.py b/systemvm/debian/opt/cloud/bin/cs_ip.py index a4e0c33..fe78c03 100755 --- a/systemvm/debian/opt/cloud/bin/cs_ip.py +++ b/systemvm/debian/opt/cloud/bin/cs_ip.py @@ -17,7 +17,7 @@ # under the License. import os -from netaddr import * +from ipaddress import * def macdevice_map(): @@ -42,7 +42,7 @@ def merge(dbag, ip): nic_dev_id = address['nic_dev_id'] dbag[dev].remove(address) - ipo = IPNetwork(ip['public_ip'] + '/' + ip['netmask']) + ipo = ip_network(ip['public_ip'] + '/' + ip['netmask']) if 'nic_dev_id' in ip: nic_dev_id = ip['nic_dev_id'] if 'vif_mac_address' in ip: @@ -51,11 +51,11 @@ def merge(dbag, ip): if mac_address in device_map: nic_dev_id = device_map[mac_address] ip['device'] = 'eth' + str(nic_dev_id) - ip['broadcast'] = str(ipo.broadcast) - ip['cidr'] = str(ipo.ip) + '/' + str(ipo.prefixlen) + ip['broadcast'] = str(ipo.broadcast_address) + ip['cidr'] = str(ipo.network_address) + '/' + str(ipo.prefixlen) ip['size'] = str(ipo.prefixlen) - ip['network'] = str(ipo.network) + '/' + str(ipo.prefixlen) - if 'nw_type' not in ip.keys(): + ip['network'] = str(ipo.ip_network) + if 'nw_type' not in list(ip.keys()): ip['nw_type'] = 'public' else: ip['nw_type'] = ip['nw_type'].lower() diff --git a/systemvm/debian/opt/cloud/bin/cs_monitorservice.py b/systemvm/debian/opt/cloud/bin/cs_monitorservice.py index 55c89df..db823f0 100755 --- a/systemvm/debian/opt/cloud/bin/cs_monitorservice.py +++ b/systemvm/debian/opt/cloud/bin/cs_monitorservice.py @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. -from netaddr import * +from ipaddress import * def merge(dbag, data): diff --git a/systemvm/debian/opt/cloud/bin/cs_network_acl.py b/systemvm/debian/opt/cloud/bin/cs_network_acl.py index 7d947d4..f499048 100755 --- a/systemvm/debian/opt/cloud/bin/cs_network_acl.py +++ b/systemvm/debian/opt/cloud/bin/cs_network_acl.py @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. -from netaddr import * +from ipaddress import * def merge(dbag, data): diff --git a/systemvm/debian/opt/cloud/bin/cs_remoteaccessvpn.py b/systemvm/debian/opt/cloud/bin/cs_remoteaccessvpn.py index dff05bd..a13bac1 100755 --- a/systemvm/debian/opt/cloud/bin/cs_remoteaccessvpn.py +++ b/systemvm/debian/opt/cloud/bin/cs_remoteaccessvpn.py @@ -20,7 +20,7 @@ def merge(dbag, vpn): key = vpn['vpn_server_ip'] op = vpn['create'] - if key in dbag.keys() and not op: + if key in list(dbag.keys()) and not op: del(dbag[key]) else: dbag[key] = vpn diff --git a/systemvm/debian/opt/cloud/bin/cs_site2sitevpn.py b/systemvm/debian/opt/cloud/bin/cs_site2sitevpn.py index 3fa8414..ebb5735 100755 --- a/systemvm/debian/opt/cloud/bin/cs_site2sitevpn.py +++ b/systemvm/debian/opt/cloud/bin/cs_site2sitevpn.py @@ -20,7 +20,7 @@ def merge(dbag, vpn): key = vpn['peer_gateway_ip'] op = vpn['create'] - if key in dbag.keys() and not op: + if key in list(dbag.keys()) and not op: del(dbag[key]) else: dbag[key] = vpn diff --git a/systemvm/debian/opt/cloud/bin/cs_vmp.py b/systemvm/debian/opt/cloud/bin/cs_vmp.py index beeadfc..10e0bb5 100755 --- a/systemvm/debian/opt/cloud/bin/cs_vmp.py +++ b/systemvm/debian/opt/cloud/bin/cs_vmp.py @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. -from netaddr import * +from ipaddress import * def merge(dbag, data): diff --git a/systemvm/debian/opt/cloud/bin/cs_vpnusers.py b/systemvm/debian/opt/cloud/bin/cs_vpnusers.py index 3bef1fe..77486bd 100755 --- a/systemvm/debian/opt/cloud/bin/cs_vpnusers.py +++ b/systemvm/debian/opt/cloud/bin/cs_vpnusers.py @@ -22,13 +22,13 @@ import copy def merge(dbag, data): dbagc = copy.deepcopy(dbag) - print dbag - print data + print(dbag) + print(data) if "vpn_users" not in data: return dbagc # remove previously deleted user from the dict - for user in dbagc.keys(): + for user in list(dbagc.keys()): if user == 'id': continue userrec = dbagc[user] @@ -39,9 +39,9 @@ def merge(dbag, data): for user in data['vpn_users']: username = user['user'] add = user['add'] - if username not in dbagc.keys(): + if username not in list(dbagc.keys()): dbagc[username] = user - elif username in dbagc.keys() and not add: + elif username in list(dbagc.keys()) and not add: dbagc[username] = user return dbagc diff --git a/systemvm/debian/opt/cloud/bin/diagnostics.py b/systemvm/debian/opt/cloud/bin/diagnostics.py index 477f99d..867d3e4 100755 --- a/systemvm/debian/opt/cloud/bin/diagnostics.py +++ b/systemvm/debian/opt/cloud/bin/diagnostics.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information diff --git a/systemvm/debian/opt/cloud/bin/get_diagnostics_files.py b/systemvm/debian/opt/cloud/bin/get_diagnostics_files.py index b95dfb5..3b3e9d1 100755 --- a/systemvm/debian/opt/cloud/bin/get_diagnostics_files.py +++ b/systemvm/debian/opt/cloud/bin/get_diagnostics_files.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -65,7 +65,7 @@ def zip_files(files): cleanup(files_from_shell_commands) generate_retrieved_files_txt(zf, files_found_list, files_not_found_list) zf.close() - print zf_name + print(zf_name) def get_cmd(script): diff --git a/systemvm/debian/opt/cloud/bin/merge.py b/systemvm/debian/opt/cloud/bin/merge.py index 4ab99118..607dca7 100755 --- a/systemvm/debian/opt/cloud/bin/merge.py +++ b/systemvm/debian/opt/cloud/bin/merge.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -155,7 +155,7 @@ class updateDataBag: dp['nw_type'] = 'guest' qf = QueueFile() qf.load({'ip_address': [dp], 'type': 'ips'}) - if 'domain_name' not in d.keys() or d['domain_name'] == '': + if 'domain_name' not in list(d.keys()) or d['domain_name'] == '': d['domain_name'] = "cloudnine.internal" return cs_guestnetwork.merge(dbag, d) @@ -246,7 +246,7 @@ class updateDataBag: def process_ipaliases(self, dbag): nic_dev = None # Should be a way to deal with this better - for intf, data in dbag.items(): + for intf, data in list(dbag.items()): if intf == 'id': continue elif any([net['nw_type'] == 'guest' for net in data]): diff --git a/systemvm/debian/opt/cloud/bin/passwd_server_ip.py b/systemvm/debian/opt/cloud/bin/passwd_server_ip.py index 85d76f6..b753859 100755 --- a/systemvm/debian/opt/cloud/bin/passwd_server_ip.py +++ b/systemvm/debian/opt/cloud/bin/passwd_server_ip.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -31,10 +31,10 @@ import os import sys import syslog import threading -import urlparse +from urllib.parse import * -from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer -from SocketServer import ThreadingMixIn #, ForkingMixIn +from http.server import BaseHTTPRequestHandler, HTTPServer +from socketserver import ThreadingMixIn #, ForkingMixIn passMap = {} @@ -64,7 +64,7 @@ def checkToken(token): def loadPasswordFile(): try: - with file(getPasswordFile()) as f: + with open(getPasswordFile()) as f: for line in f: if '=' not in line: continue key, value = line.strip().split('=', 1) @@ -75,11 +75,11 @@ def loadPasswordFile(): def savePasswordFile(): with lock: try: - with file(getPasswordFile(), 'w') as f: + with open(getPasswordFile(), 'w') as f: for ip in passMap: f.write('%s=%s\n' % (ip, passMap[ip])) f.close() - except IOError, e: + except IOError as e: syslog.syslog('serve_password: Unable to save to password file %s' % e) def getPassword(ip): @@ -192,7 +192,7 @@ def serve(HandlerClass = PasswordRequestHandler, except KeyboardInterrupt: syslog.syslog('serve_password shutting down') passwordServer.socket.close() - except Exception, e: + except Exception as e: syslog.syslog('serve_password hit exception %s -- died' % e) passwordServer.socket.close() diff --git a/systemvm/debian/opt/cloud/bin/update_config.py b/systemvm/debian/opt/cloud/bin/update_config.py index 518a31c..7f7f15f 100755 --- a/systemvm/debian/opt/cloud/bin/update_config.py +++ b/systemvm/debian/opt/cloud/bin/update_config.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -64,7 +64,7 @@ def is_guestnet_configured(guestnet_dict, keys): existing_keys = [] new_eth_key = None - for k1, v1 in guestnet_dict.iteritems(): + for k1, v1 in list(guestnet_dict.items()): if k1 in keys and len(v1) > 0: existing_keys.append(k1) diff --git a/systemvm/debian/opt/cloud/bin/vmdata.py b/systemvm/debian/opt/cloud/bin/vmdata.py index 5cf22eb..b7f7869 100755 --- a/systemvm/debian/opt/cloud/bin/vmdata.py +++ b/systemvm/debian/opt/cloud/bin/vmdata.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -31,7 +31,7 @@ def main(argv): try: opts, args = getopt.getopt(argv, "f:d:") except getopt.GetoptError: - print 'params: -f <filename> -d <b64jsondata>' + print('params: -f <filename> -d <b64jsondata>') sys.exit(2) for opt, arg in opts: if opt == '-f': @@ -46,7 +46,7 @@ def main(argv): elif b64data != '': json_data = json.loads(base64.b64decode(b64data)) else: - print '-f <filename> or -d <b64jsondata> required' + print('-f <filename> or -d <b64jsondata> required') sys.exit(2) for ip in json_data: @@ -99,15 +99,15 @@ def createfile(ip, folder, file, data): fh.write("") unflock(fh) fh.close() - os.chmod(dest, 0644) + os.chmod(dest, 0o644) if folder == "metadata" or folder == "meta-data": try: - os.makedirs(metamanifestdir, 0755) + os.makedirs(metamanifestdir, 0o755) except OSError as e: # error 17 is already exists, we do it this way for concurrency if e.errno != 17: - print "failed to make directories " + metamanifestdir + " due to :" + e.strerror + print("failed to make directories " + metamanifestdir + " due to :" + e.strerror) sys.exit(1) if os.path.exists(metamanifest): fh = open(metamanifest, "r+a") @@ -124,7 +124,7 @@ def createfile(ip, folder, file, data): fh.close() if os.path.exists(metamanifest): - os.chmod(metamanifest, 0644) + os.chmod(metamanifest, 0o644) def htaccess(ip, folder, file): @@ -133,11 +133,11 @@ def htaccess(ip, folder, file): htaccessFile = htaccessFolder+"/.htaccess" try: - os.makedirs(htaccessFolder, 0755) + os.makedirs(htaccessFolder, 0o755) except OSError as e: # error 17 is already exists, we do it this way for sake of concurrency if e.errno != 17: - print "failed to make directories " + htaccessFolder + " due to :" + e.strerror + print("failed to make directories " + htaccessFolder + " due to :" + e.strerror) sys.exit(1) fh = open(htaccessFile, "w") @@ -151,7 +151,7 @@ def exflock(file): try: flock(file, LOCK_EX) except IOError as e: - print "failed to lock file" + file.name + " due to : " + e.strerror + print("failed to lock file" + file.name + " due to : " + e.strerror) sys.exit(1) return True @@ -160,7 +160,7 @@ def unflock(file): try: flock(file, LOCK_UN) except IOError as e: - print "failed to unlock file" + file.name + " due to : " + e.strerror + print("failed to unlock file" + file.name + " due to : " + e.strerror) sys.exit(1) return True diff --git a/systemvm/debian/root/health_checks/cpu_usage_check.py b/systemvm/debian/root/health_checks/cpu_usage_check.py index 5e6a2fe..a027ae3 100644 --- a/systemvm/debian/root/health_checks/cpu_usage_check.py +++ b/systemvm/debian/root/health_checks/cpu_usage_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -18,7 +18,7 @@ from os import sys, path, statvfs from subprocess import * -from utility import getHealthChecksData +from .utility import getHealthChecksData def main(): @@ -28,7 +28,7 @@ def main(): data = entries[0] if "maxCpuUsage" not in data: - print "Missing maxCpuUsage in health_checks_data systemThresholds, skipping" + print("Missing maxCpuUsage in health_checks_data systemThresholds, skipping") exit(0) maxCpuUsage = float(data["maxCpuUsage"]) @@ -40,14 +40,14 @@ def main(): if pout.wait() == 0: currentUsage = float(pout.communicate()[0].strip()) if currentUsage > maxCpuUsage: - print "CPU Usage " + str(currentUsage) + \ - "% has crossed threshold of " + str(maxCpuUsage) + "%" + print("CPU Usage " + str(currentUsage) + + "% has crossed threshold of " + str(maxCpuUsage) + "%") exit(1) - print "CPU Usage within limits with current at " \ - + str(currentUsage) + "%" + print("CPU Usage within limits with current at " + + str(currentUsage) + "%") exit(0) else: - print "Failed to retrieve cpu usage using " + cmd + print("Failed to retrieve cpu usage using " + cmd) exit(1) diff --git a/systemvm/debian/root/health_checks/dhcp_check.py b/systemvm/debian/root/health_checks/dhcp_check.py index be7a840..8610f69 100755 --- a/systemvm/debian/root/health_checks/dhcp_check.py +++ b/systemvm/debian/root/health_checks/dhcp_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -17,14 +17,14 @@ # under the License. from os import sys, path -from utility import getHealthChecksData +from .utility import getHealthChecksData def main(): vMs = getHealthChecksData("virtualMachines") if vMs is None or len(vMs) == 0: - print "No VMs running data available, skipping" + print("No VMs running data available, skipping") exit(0) with open('/etc/dhcphosts.txt', 'r') as hostsFile: @@ -57,10 +57,10 @@ def main(): failureMessage = failureMessage + entry + ", " if failedCheck: - print failureMessage[:-2] + print(failureMessage[:-2]) exit(1) else: - print "All " + str(len(vMs)) + " VMs are present in dhcphosts.txt" + print("All " + str(len(vMs)) + " VMs are present in dhcphosts.txt") exit(0) diff --git a/systemvm/debian/root/health_checks/disk_space_check.py b/systemvm/debian/root/health_checks/disk_space_check.py index af8cb3d..a1af658 100644 --- a/systemvm/debian/root/health_checks/disk_space_check.py +++ b/systemvm/debian/root/health_checks/disk_space_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -17,7 +17,7 @@ # under the License. from os import sys, path, statvfs -from utility import getHealthChecksData +from .utility import getHealthChecksData def main(): @@ -27,7 +27,7 @@ def main(): data = entries[0] if "minDiskNeeded" not in data: - print "Missing minDiskNeeded in health_checks_data systemThresholds, skipping" + print("Missing minDiskNeeded in health_checks_data systemThresholds, skipping") exit(0) minDiskNeeded = float(data["minDiskNeeded"]) * 1024 @@ -35,10 +35,10 @@ def main(): freeSpace = (s.f_bavail * s.f_frsize) / 1024 if (freeSpace < minDiskNeeded): - print "Insufficient free space is " + str(freeSpace/1024) + " MB" + print("Insufficient free space is " + str(freeSpace/1024) + " MB") exit(1) else: - print "Sufficient free space is " + str(freeSpace/1024) + " MB" + print("Sufficient free space is " + str(freeSpace/1024) + " MB") exit(0) diff --git a/systemvm/debian/root/health_checks/dns_check.py b/systemvm/debian/root/health_checks/dns_check.py index c177888..faefd5b 100644 --- a/systemvm/debian/root/health_checks/dns_check.py +++ b/systemvm/debian/root/health_checks/dns_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -17,14 +17,14 @@ # under the License. from os import sys, path -from utility import getHealthChecksData +from .utility import getHealthChecksData def main(): vMs = getHealthChecksData("virtualMachines") if vMs is None or len(vMs) == 0: - print "No VMs running data available, skipping" + print("No VMs running data available, skipping") exit(0) with open('/etc/hosts', 'r') as hostsFile: @@ -47,10 +47,10 @@ def main(): failureMessage = failureMessage + vM["ip"] + " " + vM["vmName"] + ", " if failedCheck: - print failureMessage[:-2] + print(failureMessage[:-2]) exit(1) else: - print "All " + str(len(vMs)) + " VMs are present in /etc/hosts" + print("All " + str(len(vMs)) + " VMs are present in /etc/hosts") exit(0) diff --git a/systemvm/debian/root/health_checks/gateways_check.py b/systemvm/debian/root/health_checks/gateways_check.py index 29ce884..cdb275b 100644 --- a/systemvm/debian/root/health_checks/gateways_check.py +++ b/systemvm/debian/root/health_checks/gateways_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -18,13 +18,13 @@ from os import sys, path from subprocess import * -from utility import getHealthChecksData +from .utility import getHealthChecksData def main(): gws = getHealthChecksData("gateways") if gws is None and len(gws) == 0: - print "No gateways data available, skipping" + print("No gateways data available, skipping") exit(0) unreachableGateWays = [] @@ -44,11 +44,11 @@ def main(): unreachableGateWays.append(gw) if len(unreachableGateWays) == 0: - print "All " + str(len(gws)) + " gateways are reachable via ping" + print("All " + str(len(gws)) + " gateways are reachable via ping") exit(0) else: - print "Unreachable gateways found-" - print unreachableGateWays + print("Unreachable gateways found-") + print(unreachableGateWays) exit(1) diff --git a/systemvm/debian/root/health_checks/haproxy_check.py b/systemvm/debian/root/health_checks/haproxy_check.py index 56e0ce7..bb28894 100644 --- a/systemvm/debian/root/health_checks/haproxy_check.py +++ b/systemvm/debian/root/health_checks/haproxy_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -17,13 +17,13 @@ # under the License. from os import sys, path -from utility import getHealthChecksData, formatPort +from .utility import getHealthChecksData, formatPort def checkMaxconn(haproxyData, haCfgSections): if "maxconn" in haproxyData and "maxconn" in haCfgSections["global"]: if haproxyData["maxconn"] != haCfgSections["global"]["maxconn"][0].strip(): - print "global maxconn mismatch occured" + print("global maxconn mismatch occured") return False return True @@ -38,26 +38,26 @@ def checkLoadBalance(haproxyData, haCfgSections): secName = "listen " + srcServer if secName not in haCfgSections: - print "Missing section for load balancing " + secName + "\n" + print("Missing section for load balancing " + secName + "\n") correct = False else: cfgSection = haCfgSections[secName] if "server" in cfgSection: if lbSec["algorithm"] != cfgSection["balance"][0]: - print "Incorrect balance method for " + secName + \ - "Expected : " + lbSec["algorithm"] + \ - " but found " + cfgSection["balance"][0] + "\n" + print("Incorrect balance method for " + secName + + "Expected : " + lbSec["algorithm"] + + " but found " + cfgSection["balance"][0] + "\n") correct = False bindStr = lbSec["sourceIp"] + ":" + formatPort(lbSec["sourcePortStart"], lbSec["sourcePortEnd"]) if cfgSection["bind"][0] != bindStr: - print "Incorrect bind string found. Expected " + bindStr + " but found " + cfgSection["bind"][0] + "." + print("Incorrect bind string found. Expected " + bindStr + " but found " + cfgSection["bind"][0] + ".") correct = False if (lbSec["sourcePortStart"] == "80" and lbSec["sourcePortEnd"] == "80" and lbSec["keepAliveEnabled"] == "false") \ or (lbSec["stickiness"].find("AppCookie") != -1 or lbSec["stickiness"].find("LbCookie") != -1): if not ("mode" in cfgSection and cfgSection["mode"][0] == "http"): - print "Expected HTTP mode but not found" + print("Expected HTTP mode but not found") correct = False expectedServerIps = lbSec["vmIps"].split(" ") @@ -74,7 +74,7 @@ def checkLoadBalance(haproxyData, haCfgSections): if not foundPattern: correct = False - print "Missing load balancing for " + pattern + ". " + print("Missing load balancing for " + pattern + ". ") return correct @@ -86,7 +86,7 @@ def main(): ''' haproxyData = getHealthChecksData("haproxyData") if haproxyData is None or len(haproxyData) == 0: - print "No data provided to check, skipping" + print("No data provided to check, skipping") exit(0) with open("/etc/haproxy/haproxy.cfg", 'r') as haCfgFile: @@ -94,7 +94,7 @@ def main(): haCfgFile.close() if len(haCfgLines) == 0: - print "Unable to read config file /etc/haproxy/haproxy.cfg" + print("Unable to read config file /etc/haproxy/haproxy.cfg") exit(1) haCfgSections = {} @@ -123,7 +123,7 @@ def main(): checkLbRules = checkLoadBalance(haproxyData, haCfgSections) if checkMaxConn and checkLbRules: - print "All checks pass" + print("All checks pass") exit(0) else: exit(1) diff --git a/systemvm/debian/root/health_checks/iptables_check.py b/systemvm/debian/root/health_checks/iptables_check.py index d80f05b..aa9547b 100644 --- a/systemvm/debian/root/health_checks/iptables_check.py +++ b/systemvm/debian/root/health_checks/iptables_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -18,13 +18,13 @@ from os import sys, path from subprocess import * -from utility import getHealthChecksData, formatPort +from .utility import getHealthChecksData, formatPort def main(): portForwards = getHealthChecksData("portForwarding") if portForwards is None or len(portForwards) == 0: - print "No portforwarding rules provided to check, skipping" + print("No port forwarding rules provided to check, skipping") exit(0) failedCheck = False @@ -68,10 +68,10 @@ def main(): failureMessage = failureMessage + str(pfEntryListExpected) + "\n" if failedCheck: - print failureMessage + print(failureMessage) exit(1) else: - print "Found all entries (count " + str(len(portForwards)) + ") in iptables" + print("Found all entries (count " + str(len(portForwards)) + ") in iptables") exit(0) diff --git a/systemvm/debian/root/health_checks/memory_usage_check.py b/systemvm/debian/root/health_checks/memory_usage_check.py index 97ca0c5..20b5653 100644 --- a/systemvm/debian/root/health_checks/memory_usage_check.py +++ b/systemvm/debian/root/health_checks/memory_usage_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -18,7 +18,7 @@ from os import sys, path, statvfs from subprocess import * -from utility import getHealthChecksData +from .utility import getHealthChecksData def main(): @@ -28,8 +28,8 @@ def main(): data = entries[0] if "maxMemoryUsage" not in data: - print "Missing maxMemoryUsage in health_checks_data " + \ - "systemThresholds, skipping" + print("Missing maxMemoryUsage in health_checks_data " + \ + "systemThresholds, skipping") exit(0) maxMemoryUsage = float(data["maxMemoryUsage"]) @@ -39,14 +39,14 @@ def main(): if pout.wait() == 0: currentUsage = float(pout.communicate()[0].strip()) if currentUsage > maxMemoryUsage: - print "Memory Usage " + str(currentUsage) + \ - "% has crossed threshold of " + str(maxMemoryUsage) + "%" + print("Memory Usage " + str(currentUsage) + \ + "% has crossed threshold of " + str(maxMemoryUsage) + "%") exit(1) - print "Memory Usage within limits with current at " + \ - str(currentUsage) + "%" + print("Memory Usage within limits with current at " + \ + str(currentUsage) + "%") exit(0) else: - print "Failed to retrieve memory usage using " + cmd + print("Failed to retrieve memory usage using " + cmd) exit(1) diff --git a/systemvm/debian/root/health_checks/router_version_check.py b/systemvm/debian/root/health_checks/router_version_check.py index 2173e09..b65aca4 100644 --- a/systemvm/debian/root/health_checks/router_version_check.py +++ b/systemvm/debian/root/health_checks/router_version_check.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -17,7 +17,7 @@ # under the License. from os import sys, path, statvfs -from utility import getHealthChecksData +from .utility import getHealthChecksData def getFirstLine(file=None): @@ -41,7 +41,8 @@ def main(): data = entries[0] if len(data) == 0: - print "Missing routerVersion in health_checks_data, skipping" + print("Missing routerVersion in health_checks_data, skipping") + exit(0) templateVersionMatches = True @@ -52,11 +53,11 @@ def main(): releaseFile = "/etc/cloudstack-release" found = getFirstLine(releaseFile) if found is None: - print "Release version not yet setup at " + releaseFile +\ - ", skipping." + print("Release version not yet setup at " + releaseFile +\ + ", skipping.") elif expected != found: - print "Template Version mismatch. Expected: " + \ - expected + ", found: " + found + print("Template Version mismatch. Expected: " + \ + expected + ", found: " + found) templateVersionMatches = False if "scriptsVersion" in data: @@ -64,15 +65,15 @@ def main(): sigFile = "/var/cache/cloud/cloud-scripts-signature" found = getFirstLine(sigFile) if found is None: - print "Scripts signature is not yet setup at " + sigFile +\ - ", skipping" + print("Scripts signature is not yet setup at " + sigFile +\ + ", skipping") if expected != found: - print "Scripts Version mismatch. Expected: " + \ - expected + ", found: " + found + print("Scripts Version mismatch. Expected: " + \ + expected + ", found: " + found) scriptVersionMatches = False if templateVersionMatches and scriptVersionMatches: - print "Template and scripts version match successful" + print("Template and scripts version match successful") exit(0) else: exit(1) diff --git a/systemvm/debian/root/health_checks/utility/__init__.py b/systemvm/debian/root/health_checks/utility/__init__.py index 22ac3ff..65d98dd 100644 --- a/systemvm/debian/root/health_checks/utility/__init__.py +++ b/systemvm/debian/root/health_checks/utility/__init__.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -16,4 +16,4 @@ # specific language governing permissions and limitations # under the License. -from sharedFunctions import getHealthChecksData, formatPort +from .sharedFunctions import getHealthChecksData, formatPort diff --git a/systemvm/debian/root/monitorServices.py b/systemvm/debian/root/monitorServices.py index 909e419..398d404 100755 --- a/systemvm/debian/root/monitorServices.py +++ b/systemvm/debian/root/monitorServices.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -16,7 +16,7 @@ # specific language governing permissions and limitations # under the License. -from ConfigParser import SafeConfigParser +from configparser import SafeConfigParser from subprocess import * from datetime import datetime import time @@ -81,7 +81,7 @@ def printd (msg): f.seek(0, 2) f.write(str(msg)+"\n") f.close() - print str(msg) + print(str(msg)) def raisealert(severity, msg, process_name=None): """ Writes the alert message""" @@ -96,7 +96,7 @@ def raisealert(severity, msg, process_name=None): logging.info(log) msg = 'logger -t monit '+ log pout = Popen(msg, shell=True, stdout=PIPE) - print "[Alert] " + msg + print("[Alert] " + msg) def isPidMatchPidFile(pidfile, pids): @@ -258,12 +258,12 @@ def monitProcess( processes_info ): printd("No config items provided - means a redundant VR or a VPC Router") return service_status, failing_services - print "[Process Info] " + json.dumps(processes_info) + print("[Process Info] " + json.dumps(processes_info)) #time for noting process down time csec = repr(time.time()).split('.')[0] - for process,properties in processes_info.items(): + for process,properties in list(processes_info.items()): printd ("---------------------------\nchecking the service %s\n---------------------------- " %process) serviceName = process + ".service" processStatus, wasRestarted = checkProcessStatus(properties) diff --git a/systemvm/test/runtests.sh b/systemvm/test/runtests.sh index c6a58bd..b4fb5a1 100644 --- a/systemvm/test/runtests.sh +++ b/systemvm/test/runtests.sh @@ -31,8 +31,15 @@ then fi echo "Running pylint to check systemvm/python code for errors" + pylint --disable=R,C,W *.py pylint --disable=R,C,W `find ../debian -name \*.py` + +python --version +pyenv versions +pylint3 --disable=R,C,W *.py +pylint3 --disable=R,C,W `find ../debian -name \*.py` + if [ $? -gt 0 ] then echo "pylint failed, please check your code"