1
0
mirror of https://github.com/mbirth/tcl_ota_check.git synced 2024-09-19 22:33:25 +01:00

Move retry code for checks into tcllib.

This commit is contained in:
Markus Birth 2017-10-05 23:51:07 +02:00
parent 11370f618e
commit 8b7de97f84
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
3 changed files with 68 additions and 85 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# pylint: disable=C0111,C0326 # pylint: disable=C0111,C0326,C0103
import tcllib import tcllib
from requests.exceptions import RequestException, Timeout from requests.exceptions import RequestException, Timeout
@ -16,35 +16,23 @@ fc.mode = fc.MODE_FULL
# CLTP = 10 (only show actual updates or HTTP 206) / 2010 (always show latest version for MODE_FULL) # CLTP = 10 (only show actual updates or HTTP 206) / 2010 (always show latest version for MODE_FULL)
#fc.cltp = 10 #fc.cltp = 10
fc.cltp = 2010 fc.cltp = 2010
fc.timeout = 20
print("List of latest FULL firmware by PRD:") print("List of latest FULL firmware by PRD:")
with open("prds.txt", "r") as afile: with open("prds.txt", "rt") as f:
prdx = afile.read() for prdline in f:
prds = list(filter(None, prdx.split("\n"))) prdline = prdline.strip()
prd, lastver, model = prdline.split(" ", 2)
while len(prds) > 0: try:
prd, lastver, model = prds[0].split(" ", 2) fc.reset_session()
try: fc.curef = prd
fc.reset_session() check_xml = fc.do_check()
fc.curef = prd curef, fv, tv, fw_id, fileid, fn, fsize, fhash = fc.parse_check(check_xml)
check_xml = fc.do_check() txt_tv = tv
curef, fv, tv, fw_id, fileid, fn, fsize, fhash = fc.parse_check(check_xml) if tv != lastver:
txt_tv = tv txt_tv = tcllib.ANSI_CYAN + txt_tv + tcllib.ANSI_RESET
if tv != lastver: print("{}: {} {} ({})".format(prd, txt_tv, fhash, model))
txt_tv = tcllib.ANSI_CYAN + txt_tv + tcllib.ANSI_RESET except RequestException as e:
print("{}: {} {} ({})".format(prd, txt_tv, fhash, model)) print("{}: {}".format(prd, str(e)))
prds.pop(0) continue
except Timeout as e:
print("{} failed. (Connection timed out.)".format(prd))
print(tcllib.ANSI_UP_DEL, end="")
continue
except (SystemExit, RequestException) as e:
print("{} failed. ({})".format(prd, str(e)))
if e.response.status_code in [204, 404]:
# No update available or invalid request - remove from queue
prds.pop(0)
else:
print(tcllib.ANSI_UP_DEL, end="")
continue

View File

@ -1,11 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# pylint: disable=C0111,C0326 # pylint: disable=C0111,C0326,C0103
import sys import sys
import tcllib import tcllib
from requests.exceptions import RequestException, Timeout from requests.exceptions import RequestException
tcllib.make_escapes_work() tcllib.make_escapes_work()
@ -14,7 +14,6 @@ fc.serid = "3531510"
#fc.osvs = "7.1.1" #fc.osvs = "7.1.1"
fc.mode = fc.MODE_OTA fc.mode = fc.MODE_OTA
fc.cltp = 10 fc.cltp = 10
fc.timeout = 20
force_ver = False force_ver = False
force_ver_text = "" force_ver_text = ""
@ -24,31 +23,19 @@ if len(sys.argv) > 1:
print("List of latest OTA firmware{} by PRD:".format(force_ver_text)) print("List of latest OTA firmware{} by PRD:".format(force_ver_text))
with open("prds.txt", "r") as afile: with open("prds.txt", "r") as f:
prdx = afile.read() for prdline in f:
prds = list(filter(None, prdx.split("\n"))) prdline = prdline.strip()
prd, lastver, model = prdline.split(" ", 2)
while len(prds) > 0: if force_ver != False:
prd, lastver, model = prds[0].split(" ", 2) lastver = force_ver
if force_ver != False: try:
lastver = force_ver fc.reset_session()
try: fc.curef = prd
fc.reset_session() fc.fv = lastver
fc.curef = prd check_xml = fc.do_check()
fc.fv = lastver curef, fv, tv, fw_id, fileid, fn, fsize, fhash = fc.parse_check(check_xml)
check_xml = fc.do_check() print("{}: {}{} {} ({})".format(prd, fv, tv, fhash, model))
curef, fv, tv, fw_id, fileid, fn, fsize, fhash = fc.parse_check(check_xml) except RequestException as e:
print("{}: {}{} {} ({})".format(prd, fv, tv, fhash, model)) print("{} ({}): {}".format(prd, lastver, str(e)))
prds.pop(0) continue
except Timeout as e:
print("{} failed. (Connection timed out.)".format(prd))
print(tcllib.ANSI_UP_DEL, end="")
continue
except (SystemExit, RequestException) as e:
print("{} ({}) failed. ({})".format(prd, lastver, str(e)))
if e.response.status_code in [204, 404]:
# No update available or invalid request - remove from queue
prds.pop(0)
else:
print(tcllib.ANSI_UP_DEL, end="")
continue

View File

@ -59,7 +59,15 @@ class FotaCheck:
self.cktp = self.CKTP_CHECKMANUAL self.cktp = self.CKTP_CHECKMANUAL
self.rtd = self.RTD_UNROOTED self.rtd = self.RTD_UNROOTED
self.chnl = self.CHNL_WIFI self.chnl = self.CHNL_WIFI
self.timeout = 10 self.g2master = None
self.master_servers = [
"g2master-us-east.tclclouds.com",
"g2master-us-west.tclclouds.com",
"g2master-eu-west.tclclouds.com",
"g2master-ap-south.tclclouds.com",
"g2master-ap-north.tclclouds.com",
"g2master-sa-east.tclclouds.com",
]
self.reset_session() self.reset_session()
def reset_session(self): def reset_session(self):
@ -77,19 +85,10 @@ class FotaCheck:
tail = "{:06d}".format(random.randint(0, 999999)) tail = "{:06d}".format(random.randint(0, 999999))
return "{}{}".format(str(millis), tail) return "{}{}".format(str(millis), tail)
@staticmethod def get_master_server(self):
def get_master_server(): return random.choice(self.master_servers)
master_servers = [
"g2master-us-east.tclclouds.com",
"g2master-us-west.tclclouds.com",
"g2master-eu-west.tclclouds.com",
"g2master-ap-south.tclclouds.com",
"g2master-ap-north.tclclouds.com",
"g2master-sa-east.tclclouds.com",
]
return random.choice(master_servers)
def do_check(self, https=True): def do_check(self, https=True, timeout=10, max_tries=5):
protocol = "https://" if https else "http://" protocol = "https://" if https else "http://"
url = protocol + self.g2master + "/check.php" url = protocol + self.g2master + "/check.php"
params = OrderedDict() params = OrderedDict()
@ -104,18 +103,27 @@ class FotaCheck:
params["chnl"] = self.chnl params["chnl"] = self.chnl
#params["osvs"] = self.osvs #params["osvs"] = self.osvs
req = self.sess.get(url, params=params, timeout=self.timeout) last_response = None
if req.status_code == 200: for num_try in range(0, max_tries):
return req.text try:
elif req.status_code == 204: req = self.sess.get(url, params=params, timeout=timeout)
raise requests.exceptions.HTTPError("No update available.", response=req) last_response = req
elif req.status_code == 404: if req.status_code == 200:
raise requests.exceptions.HTTPError("No data for requested CUREF/FV combination.", response=req) return req.text
elif req.status_code in (500, 503): elif req.status_code == 204:
raise requests.exceptions.HTTPError("Server busy, try again later.", response=req) raise requests.exceptions.HTTPError("No update available.", response=req)
else: elif req.status_code == 404:
req.raise_for_status() raise requests.exceptions.HTTPError("No data for requested CUREF/FV combination.", response=req)
raise requests.exceptions.HTTPError("HTTP {}.".format(req.status_code), response=req) elif req.status_code not in [500, 503]:
req.raise_for_status()
raise requests.exceptions.HTTPError("HTTP {}.".format(req.status_code), response=req)
except requests.exceptions.Timeout:
pass
# Something went wrong, try a different server
self.g2master = self.get_master_server()
protocol = "https://" if https else "http://"
url = protocol + self.g2master + "/check.php"
raise requests.exceptions.RetryError("Max tries ({}) reached.".format(max_tries), response=last_response)
@staticmethod @staticmethod
def pretty_xml(xmlstr): def pretty_xml(xmlstr):