Skip to content

Commit 198ba06

Browse files
committed
Adding functions for issue #27
1 parent 434daea commit 198ba06

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

archinstall.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,12 +748,32 @@ def phone_home(url):
748748
request = urllib.request.Request(url, data=payload, headers={'content-type': 'application/json'})
749749
response = urllib.request.urlopen(request)
750750

751+
def get_external_ip(*positionals, **kwargs):
752+
result = urllib.request.urlopen("https://hvornum.se/ip/?f=json").read().decode('UTF-8')
753+
return json.loads(result)['ip']
754+
755+
def guess_country(*positionals, **kwargs):
756+
# python-pygeoip
757+
# geoip-database
758+
result = None
759+
GEOIP_DB = '/usr/share/GeoIP/GeoIP.dat'
760+
if os.path.isfile(GEOIP_DB):
761+
try:
762+
import pygeoip
763+
except:
764+
return result
765+
766+
gi = pygeoip.GeoIP(GEOIP_DB)
767+
result = gi.country_name_by_addr(ip)
768+
else:
769+
log(f'Missing GeoIP database: {GEOIP_DB}', origin='guess_country', level=LOG_LEVELS.ERROR)
770+
return result
771+
751772
def setup_args_defaults(args, interactive=True):
752773
if not 'size' in args: args['size'] = '100%'
753774
if not 'start' in args: args['start'] = '513MiB'
754775
if not 'pwfile' in args: args['pwfile'] = '/tmp/diskpw'
755776
if not 'hostname' in args: args['hostname'] = 'Archinstall'
756-
if not 'country' in args: args['country'] = 'SE' # 'all' if we don't want country specific mirrors.
757777
if not 'packages' in args: args['packages'] = '' # extra packages other than default
758778
if not 'post' in args: args['post'] = 'reboot'
759779
if not 'password' in args: args['password'] = '0000' # Default disk passord, can be <STDIN> or a fixed string
@@ -764,7 +784,6 @@ def setup_args_defaults(args, interactive=True):
764784
if not 'aur-keep' in args: args['aur-keep'] = False
765785
if not 'aur-support' in args: args['aur-support'] = True # Support adds yay (https://github.com/Jguer/yay) in installation steps.
766786
if not 'ignore-rerun' in args: args['ignore-rerun'] = False
767-
if not 'localtime' in args: args['localtime'] = 'Europe/Stockholm' if args['country'] == 'SE' else 'GMT+0' # TODO: Arbitrary for now
768787
if not 'phone-home' in args: args['phone-home'] = False
769788
if not 'drive' in args:
770789
if interactive and len(harddrives):
@@ -793,6 +812,15 @@ def setup_args_defaults(args, interactive=True):
793812
exit(1)
794813

795814
args['drive'] = drive
815+
816+
# Setup locales if we didn't get one.
817+
if not 'country' in args:
818+
country = None
819+
if get_default_gateway_linux():
820+
country = guess_country(get_external_ip())
821+
args['country'] = 'all' if not country else country
822+
if not 'localtime' in args: args['localtime'] = 'Europe/Stockholm' if args['country'] == 'SE' else 'GMT+0' # TODO: Arbitrary for now
823+
796824
return args
797825

798826
def load_automatic_instructions(*positionals, **kwargs):

0 commit comments

Comments
 (0)