From 198ba06c2749bae8f1e35a6e69a6ddf50708a17d Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 27 Nov 2019 22:16:06 +0000 Subject: Adding functions for issue #27 --- archinstall.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/archinstall.py b/archinstall.py index 38856973..e2d8f945 100644 --- a/archinstall.py +++ b/archinstall.py @@ -748,12 +748,32 @@ def phone_home(url): request = urllib.request.Request(url, data=payload, headers={'content-type': 'application/json'}) response = urllib.request.urlopen(request) +def get_external_ip(*positionals, **kwargs): + result = urllib.request.urlopen("https://hvornum.se/ip/?f=json").read().decode('UTF-8') + return json.loads(result)['ip'] + +def guess_country(*positionals, **kwargs): + # python-pygeoip + # geoip-database + result = None + GEOIP_DB = '/usr/share/GeoIP/GeoIP.dat' + if os.path.isfile(GEOIP_DB): + try: + import pygeoip + except: + return result + + gi = pygeoip.GeoIP(GEOIP_DB) + result = gi.country_name_by_addr(ip) + else: + log(f'Missing GeoIP database: {GEOIP_DB}', origin='guess_country', level=LOG_LEVELS.ERROR) + return result + def setup_args_defaults(args, interactive=True): if not 'size' in args: args['size'] = '100%' if not 'start' in args: args['start'] = '513MiB' if not 'pwfile' in args: args['pwfile'] = '/tmp/diskpw' if not 'hostname' in args: args['hostname'] = 'Archinstall' - if not 'country' in args: args['country'] = 'SE' # 'all' if we don't want country specific mirrors. if not 'packages' in args: args['packages'] = '' # extra packages other than default if not 'post' in args: args['post'] = 'reboot' if not 'password' in args: args['password'] = '0000' # Default disk passord, can be or a fixed string @@ -764,7 +784,6 @@ def setup_args_defaults(args, interactive=True): if not 'aur-keep' in args: args['aur-keep'] = False if not 'aur-support' in args: args['aur-support'] = True # Support adds yay (https://github.com/Jguer/yay) in installation steps. if not 'ignore-rerun' in args: args['ignore-rerun'] = False - if not 'localtime' in args: args['localtime'] = 'Europe/Stockholm' if args['country'] == 'SE' else 'GMT+0' # TODO: Arbitrary for now if not 'phone-home' in args: args['phone-home'] = False if not 'drive' in args: if interactive and len(harddrives): @@ -793,6 +812,15 @@ def setup_args_defaults(args, interactive=True): exit(1) args['drive'] = drive + + # Setup locales if we didn't get one. + if not 'country' in args: + country = None + if get_default_gateway_linux(): + country = guess_country(get_external_ip()) + args['country'] = 'all' if not country else country + if not 'localtime' in args: args['localtime'] = 'Europe/Stockholm' if args['country'] == 'SE' else 'GMT+0' # TODO: Arbitrary for now + return args def load_automatic_instructions(*positionals, **kwargs): -- cgit v1.2.3-70-g09d2 From d4c2fd61aad5010349767729eb913a75996de74c Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 27 Nov 2019 22:17:34 +0000 Subject: Adding debug --- archinstall.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/archinstall.py b/archinstall.py index e2d8f945..548f9355 100644 --- a/archinstall.py +++ b/archinstall.py @@ -817,7 +817,10 @@ def setup_args_defaults(args, interactive=True): if not 'country' in args: country = None if get_default_gateway_linux(): - country = guess_country(get_external_ip()) + ip = get_external_ip() + print('IP:', ip) + country = guess_country(ip) + print('Country:', country) args['country'] = 'all' if not country else country if not 'localtime' in args: args['localtime'] = 'Europe/Stockholm' if args['country'] == 'SE' else 'GMT+0' # TODO: Arbitrary for now -- cgit v1.2.3-70-g09d2 From 9d984957feb3810461312f5cdeaf4b235c180ea9 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 27 Nov 2019 22:25:59 +0000 Subject: Missing parameter in guess_country() --- archinstall.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/archinstall.py b/archinstall.py index 548f9355..33c39827 100644 --- a/archinstall.py +++ b/archinstall.py @@ -752,7 +752,7 @@ def get_external_ip(*positionals, **kwargs): result = urllib.request.urlopen("https://hvornum.se/ip/?f=json").read().decode('UTF-8') return json.loads(result)['ip'] -def guess_country(*positionals, **kwargs): +def guess_country(ip, *positionals, **kwargs): # python-pygeoip # geoip-database result = None @@ -761,6 +761,7 @@ def guess_country(*positionals, **kwargs): try: import pygeoip except: + ## TODO: Do a best-effort-guess based off the hostname given off the IP instead, if GoeIP doesn't exist. return result gi = pygeoip.GeoIP(GEOIP_DB) @@ -818,9 +819,7 @@ def setup_args_defaults(args, interactive=True): country = None if get_default_gateway_linux(): ip = get_external_ip() - print('IP:', ip) country = guess_country(ip) - print('Country:', country) args['country'] = 'all' if not country else country if not 'localtime' in args: args['localtime'] = 'Europe/Stockholm' if args['country'] == 'SE' else 'GMT+0' # TODO: Arbitrary for now -- cgit v1.2.3-70-g09d2 From bbcfd7a73da772dfeed3dd0d7a1265f4d6e5ac1d Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 27 Nov 2019 22:27:12 +0000 Subject: Country code instead of name --- archinstall.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/archinstall.py b/archinstall.py index 33c39827..50e59d04 100644 --- a/archinstall.py +++ b/archinstall.py @@ -765,7 +765,7 @@ def guess_country(ip, *positionals, **kwargs): return result gi = pygeoip.GeoIP(GEOIP_DB) - result = gi.country_name_by_addr(ip) + result = gi.country_code_by_addr(ip) else: log(f'Missing GeoIP database: {GEOIP_DB}', origin='guess_country', level=LOG_LEVELS.ERROR) return result -- cgit v1.2.3-70-g09d2 From c5d9be669dc6241f94c458bfd8b565b293549695 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 27 Nov 2019 23:22:01 +0000 Subject: Update: mirrors are now re-ordered by default if a gateway and country is found --- archinstall.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/archinstall.py b/archinstall.py index 50e59d04..8def7adb 100644 --- a/archinstall.py +++ b/archinstall.py @@ -772,6 +772,7 @@ def guess_country(ip, *positionals, **kwargs): def setup_args_defaults(args, interactive=True): if not 'size' in args: args['size'] = '100%' + if not 'mirrors' in args: args['mirrors'] = True if not 'start' in args: args['start'] = '513MiB' if not 'pwfile' in args: args['pwfile'] = '/tmp/diskpw' if not 'hostname' in args: args['hostname'] = 'Archinstall' @@ -1178,7 +1179,7 @@ if __name__ == '__main__': format_disk('drive', start='start', end='size') refresh_partition_list('drive') - print(f'Partitions: (Boot: {list(args["partitions"].keys())[0]})') + print(f'[N] Partitions: {len(args["partitions"])} (Boot: {list(args["partitions"].keys())[0]})') if len(args['partitions']) <= 0: print(f'[E] No partitions were created on {args["drive"]}', o) -- cgit v1.2.3-70-g09d2 From 59994c1ffdcc916f782d50e248316790fec70974 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 27 Nov 2019 23:28:25 +0000 Subject: Fixes issue on line 944. list has no attribute 'split' --- archinstall.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/archinstall.py b/archinstall.py index 8def7adb..725d05ee 100644 --- a/archinstall.py +++ b/archinstall.py @@ -1211,7 +1211,7 @@ if __name__ == '__main__': if 'mirrors' in args and args['mirrors'] and 'country' in args and get_default_gateway_linux(): print('[N] Reordering mirrors.') - filter_mirrors_by_country_list([args['country']]) + filter_mirrors_by_country_list(args['country']) pre_conf = {} if 'pre' in instructions: -- cgit v1.2.3-70-g09d2