2019-03-07 22:24:47 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
|
|
from pprint import pprint
|
|
|
|
import daemon
|
|
|
|
import time
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import json
|
2019-03-08 13:40:24 +00:00
|
|
|
from pyf import download, prt, sha256
|
2019-03-07 22:24:47 +00:00
|
|
|
|
|
|
|
|
2019-03-08 11:53:21 +00:00
|
|
|
WORKDIR = os.getcwd()
|
2019-03-07 22:24:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def init_parser():
|
|
|
|
''' initialize the argument parser
|
|
|
|
@return agrs: the parsed arguments
|
|
|
|
'''
|
|
|
|
parser = argparse.ArgumentParser(description='flash linux operating systems on the fly')
|
|
|
|
parser.add_argument('os',
|
|
|
|
default='arch',
|
|
|
|
metavar='os',
|
|
|
|
help='the operating system that should be flashed')
|
|
|
|
parser.add_argument('storage',
|
|
|
|
metavar='storage',
|
|
|
|
help='the storage where the os shall be flashed')
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2019-03-08 21:11:20 +00:00
|
|
|
def check_update(distributions):
|
|
|
|
''' check if distributions are up to date '''
|
|
|
|
|
2019-03-07 22:24:47 +00:00
|
|
|
from os import path
|
2019-03-08 13:40:24 +00:00
|
|
|
from datetime import datetime
|
2019-03-07 22:24:47 +00:00
|
|
|
|
2019-03-08 21:11:20 +00:00
|
|
|
# go through distributions
|
|
|
|
for name, dist in distributions.items():
|
|
|
|
prt('checking operating system {} for updates ...'.format(name))
|
2019-03-08 13:40:24 +00:00
|
|
|
|
2019-03-08 21:11:20 +00:00
|
|
|
if not dist['file_url'] or not dist['interval']:
|
|
|
|
prt('ERROR in distributions.json: *file_url* and *interval* \
|
|
|
|
must be specified, skipping {} ...'.format(name))
|
2019-03-08 11:53:21 +00:00
|
|
|
continue
|
|
|
|
|
2019-03-08 21:11:20 +00:00
|
|
|
# get absolute file name
|
|
|
|
if not dist['file_name']:
|
|
|
|
dist['file_name'] = dist['file_url'].split('/')[-1]
|
2019-03-08 11:53:21 +00:00
|
|
|
file_name = '{}/dist/{}'.format(WORKDIR, dist['file_name'])
|
2019-03-08 13:40:24 +00:00
|
|
|
|
2019-03-08 21:11:20 +00:00
|
|
|
if not path.isfile(file_name):
|
|
|
|
prt('{} does not yet exist, downloading ...'.format(file_url))
|
2019-03-08 13:40:24 +00:00
|
|
|
download(dist['file_url'], file_name)
|
|
|
|
continue
|
|
|
|
|
2019-03-08 21:11:20 +00:00
|
|
|
# is the file younger than interval?
|
2019-03-08 13:40:24 +00:00
|
|
|
file_mod = datetime.fromtimestamp(path.getmtime(file_name))
|
|
|
|
file_age = (datetime.now() - file_mod).days
|
|
|
|
if file_age < int(dist['interval']):
|
|
|
|
prt('{} is younger than {} days, skipping ...'.format(
|
|
|
|
name, dist['interval']))
|
|
|
|
continue
|
2019-03-08 21:11:20 +00:00
|
|
|
prt('{} is older than {} days, checking test file ...'.format(name, file_age))
|
2019-03-08 13:40:24 +00:00
|
|
|
|
2019-03-08 21:11:20 +00:00
|
|
|
if not dist['test_url']:
|
|
|
|
prt('distributions.json: `test_url` not specified, downloading {} ...'.format(file_url))
|
2019-03-08 11:53:21 +00:00
|
|
|
download(dist['file_url'], file_name)
|
2019-03-07 22:24:47 +00:00
|
|
|
continue
|
|
|
|
|
2019-03-08 21:11:20 +00:00
|
|
|
# get absolute test name
|
|
|
|
if not dist['test_name']:
|
|
|
|
dist['test_name'] = dist['test_url'].split('/')[-1]
|
|
|
|
test_name = '{}/dist/{}'.format(WORKDIR, dist['test_name'])
|
2019-03-08 13:40:24 +00:00
|
|
|
|
|
|
|
if not path.isfile(test_name):
|
2019-03-08 21:11:20 +00:00
|
|
|
prt('{} does not yet exist, downloading ...'.format(dist['test_url']))
|
2019-03-08 13:40:24 +00:00
|
|
|
download(dist['test_url'], test_name)
|
|
|
|
download(dist['file_url'], file_name)
|
|
|
|
continue
|
|
|
|
|
2019-03-08 21:11:20 +00:00
|
|
|
# did sha256 of test files change?
|
2019-03-08 13:40:24 +00:00
|
|
|
test_sha256_old = sha256(test_name)
|
|
|
|
download(dist['test_url'], test_name)
|
|
|
|
test_sha256_new = sha256(test_name)
|
|
|
|
if not test_sha256_old == test_sha256_new:
|
2019-03-08 21:11:20 +00:00
|
|
|
prt('`test_file` {} has changed, downloading {}'.format(test_name, dist['test_url']))
|
2019-03-08 13:40:24 +00:00
|
|
|
download(dist['file_url'], file_name)
|
|
|
|
continue
|
|
|
|
|
|
|
|
prt('{} is still up to date, skipping ...'.format(name))
|
2019-03-07 22:24:47 +00:00
|
|
|
|
|
|
|
|
2019-03-08 21:11:20 +00:00
|
|
|
def get_distributions():
|
|
|
|
''' read distributions.json and @return it as json object '''
|
|
|
|
with open('{}/distributions.json'.format(WORKDIR), 'r') as f:
|
|
|
|
#TODO catch json format errors and stuff like that
|
2019-03-07 22:24:47 +00:00
|
|
|
return json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
# https://stackoverflow.com/questions/4637420/efficient-python-daemon#8375012
|
|
|
|
def run_daemon():
|
|
|
|
''' run the daemon '''
|
|
|
|
with daemon.DaemonContext():
|
|
|
|
check_update()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
args = init_parser()
|
2019-03-08 21:11:20 +00:00
|
|
|
distributions = get_distributions()
|
|
|
|
check_update(distributions)
|
2019-03-07 22:24:47 +00:00
|
|
|
# run_daemon()
|