lookyloo/bin/update.py

118 lines
3.9 KiB
Python
Raw Normal View History

2020-09-28 13:32:19 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
2021-09-07 12:59:31 +02:00
import hashlib
2020-09-28 13:32:19 +02:00
import logging
2021-09-07 12:59:31 +02:00
import platform
2020-09-28 13:32:19 +02:00
import shlex
2021-09-07 12:59:31 +02:00
import subprocess
2020-09-28 13:32:19 +02:00
import sys
from pathlib import Path
2020-09-28 13:32:19 +02:00
from lookyloo.helpers import get_homedir
logging.basicConfig(format='%(asctime)s %(name)s %(levelname)s:%(message)s',
level=logging.INFO)
2020-09-28 13:32:19 +02:00
2020-10-03 21:32:30 +02:00
def compute_hash_self():
m = hashlib.sha256()
with (get_homedir() / 'bin' / 'update.py').open('rb') as f:
2020-10-13 16:18:01 +02:00
m.update(f.read())
return m.digest()
2020-10-03 21:32:30 +02:00
2020-10-13 16:38:56 +02:00
2020-09-28 13:32:19 +02:00
def keep_going(ignore=False):
if ignore:
return
keep_going = input('Continue? (y/N) ')
if keep_going.lower() != 'y':
print('Okay, quitting.')
sys.exit()
2020-10-22 16:41:00 +02:00
def run_command(command, expect_fail: bool=False, capture_output: bool=True):
2020-09-28 13:32:19 +02:00
args = shlex.split(command)
2020-09-28 13:57:21 +02:00
homedir = get_homedir()
2020-10-22 16:41:00 +02:00
process = subprocess.run(args, cwd=homedir, capture_output=capture_output)
if capture_output:
print(process.stdout.decode())
if process.returncode and not expect_fail:
2020-09-28 13:32:19 +02:00
print(process.stderr.decode())
sys.exit()
def check_poetry_version():
args = shlex.split("poetry self -V")
homedir = get_homedir()
process = subprocess.run(args, cwd=homedir, capture_output=True)
poetry_version_str = process.stdout.decode()
version = poetry_version_str.split()[2]
version_details = tuple(int(i) for i in version.split('.'))
if version_details < (1, 1, 0):
print('Lookyloo requires poetry >= 1.1.0, please update.')
print('If you installed with "pip install --user poetry", run "pip install --user -U poetry"')
print('If you installed via the recommended method, use "poetry self update"')
print('More details: https://github.com/python-poetry/poetry#updating-poetry')
sys.exit()
def main():
2020-09-28 13:32:19 +02:00
parser = argparse.ArgumentParser(description='Pull latest release, update dependencies, update and validate the config files, update 3rd deps for the website.')
parser.add_argument('--yes', default=False, action='store_true', help='Run all commands without asking.')
args = parser.parse_args()
2020-10-03 21:32:30 +02:00
old_hash = compute_hash_self()
2020-09-28 13:32:19 +02:00
print('* Update repository.')
keep_going(args.yes)
run_command('git pull')
2020-10-03 21:32:30 +02:00
new_hash = compute_hash_self()
if old_hash != new_hash:
print('Update script changed, please do "poetry run update"')
sys.exit()
check_poetry_version()
2020-09-28 13:32:19 +02:00
print('* Install/update dependencies.')
keep_going(args.yes)
run_command('poetry install')
print('* Validate configuration files.')
keep_going(args.yes)
2021-04-06 17:43:45 +02:00
run_command(f'poetry run {(Path("tools") / "validate_config_files.py").as_posix()} --check')
2020-09-28 13:32:19 +02:00
print('* Update configuration files.')
keep_going(args.yes)
2021-04-06 17:43:45 +02:00
run_command(f'poetry run {(Path("tools") / "validate_config_files.py").as_posix()} --update')
2020-09-28 13:32:19 +02:00
print('* Update third party dependencies for the website.')
keep_going(args.yes)
2021-04-06 17:43:45 +02:00
run_command(f'poetry run {(Path("tools") / "3rdparty.py").as_posix()}')
2020-10-13 16:38:56 +02:00
2020-10-13 15:05:36 +02:00
print('* Restarting Lookyloo.')
keep_going(args.yes)
if platform.system() == 'Windows':
2020-10-22 16:41:00 +02:00
print('Restarting Lookyloo with poetry...')
run_command('poetry run stop', expect_fail=True)
run_command('poetry run start', capture_output=False)
print('Lookyloo started.')
else:
service = "lookyloo"
p = subprocess.run(["systemctl", "is-active", "--quiet", service])
try:
p.check_returncode()
print('Restarting Lookyloo with systemd...')
run_command('sudo service lookyloo restart')
print('done.')
except subprocess.CalledProcessError:
print('Restarting Lookyloo with poetry...')
run_command('poetry run stop', expect_fail=True)
run_command('poetry run start', capture_output=False)
print('Lookyloo started.')
2020-10-13 16:38:56 +02:00
if __name__ == '__main__':
main()