Adding functions for issue #27

This commit is contained in:
Anton Hvornum 2019-11-27 22:16:06 +00:00
parent 434daeae68
commit 198ba06c27
1 changed files with 30 additions and 2 deletions

View File

@ -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 <STDIN> 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):