From 9106c258a8e3a7069fb63a70226c9ccfe7d68927 Mon Sep 17 00:00:00 2001 From: green <41323182+greeeen-dev@users.noreply.github.com> Date: Tue, 24 Sep 2024 14:15:20 +0200 Subject: [PATCH] bootloader update --- boot/bootloader.py | 97 +++++++++++++++++++++++++++++----------------- boot_config.json | 4 ++ 2 files changed, 65 insertions(+), 36 deletions(-) create mode 100644 boot_config.json diff --git a/boot/bootloader.py b/boot/bootloader.py index 42eb0e1..630b749 100644 --- a/boot/bootloader.py +++ b/boot/bootloader.py @@ -5,6 +5,7 @@ import time reinstall = '--reinstall' in sys.argv +depinstall = '--install-deps' in sys.argv install_options = [ { @@ -48,11 +49,12 @@ else: options = ' ' + ' '.join(options) -if not '.install.json' in os.listdir() or reinstall: - if os.path.isdir('update') and not reinstall: +if not '.install.json' in os.listdir() or reinstall or depinstall: + if os.path.isdir('update') and not reinstall and not depinstall: # unifier was likely updated from v2 or older print('\x1b[33;1mLegacy installation detected, skipping installer.\x1b[0m') with open('.install.json', 'w+') as file: + # noinspection PyTypeChecker json.dump( { 'product': internal["product"], @@ -63,61 +65,77 @@ ) else: # this installation is fresh - if not reinstall: - print('\x1b[33;1mInstallation not detected, running installer...\x1b[0m') + if not depinstall: + if not reinstall: + print('\x1b[33;1mInstallation not detected, running installer...\x1b[0m') - if len(install_options) == 1: - install_option = install_options[0]['id'] - else: - print(f'\x1b[33;1mYou have {len(install_options)} install options available.\x1b[0m\n') + if len(install_options) == 1: + install_option = install_options[0]['id'] + else: + print(f'\x1b[33;1mYou have {len(install_options)} install options available.\x1b[0m\n') - for index in range(len(install_options)): - option = install_options[index] - print(f'{option["color"]};1m{option["name"]} (option {index})\x1b[0m') - print(f'{option["color"]}m{option["description"]}\x1b[0m') + for index in range(len(install_options)): + option = install_options[index] + print(f'{option["color"]};1m{option["name"]} (option {index})\x1b[0m') + print(f'{option["color"]}m{option["description"]}\x1b[0m') - print(f'\n\x1b[33;1mWhich installation option would you like to install? (0-{len(install_options)-1})\x1b[0m') + print(f'\n\x1b[33;1mWhich installation option would you like to install? (0-{len(install_options)-1})\x1b[0m') - try: - install_option = int(input()) + try: + install_option = int(input()) + + if install_option < 0 or install_option >= len(install_options): + raise ValueError() + except: + print(f'\x1b[31;1mAborting.\x1b[0m') + sys.exit(1) - if install_option < 0 or install_option >= len(install_options): - raise ValueError() + install_option = install_options[install_option]['id'] + + print('\x1b[33;1mPlease review the following before continuing:\x1b[0m') + print(f'- Product to install: {internal["product_name"]}') + print(f'- Installation option: {install_option}') + print(f'- Install directory: {os.getcwd()}') + print(f'- Python command/binary: {binary}\n') + print('\x1b[33;1mProceed with installation? (y/n)\x1b[0m') + + try: + answer = input().lower() except: print(f'\x1b[31;1mAborting.\x1b[0m') sys.exit(1) - install_option = install_options[install_option]['id'] - - print('\x1b[33;1mPlease review the following before continuing:\x1b[0m') - print(f'- Product to install: {internal["product_name"]}') - print(f'- Installation option: {install_option}') - print(f'- Install directory: {os.getcwd()}') - print(f'- Python command/binary: {binary}\n') - print('\x1b[33;1mProceed with installation? (y/n)\x1b[0m') + if not answer == 'y': + print(f'\x1b[31;1mAborting.\x1b[0m') + sys.exit(1) + else: + try: + with open('.install.json') as file: + install_data = json.load(file) + except: + print('\x1b[31;1mPlease install Unifier first.\x1b[0m') + sys.exit(1) - try: - answer = input().lower() - except: - print(f'\x1b[31;1mAborting.\x1b[0m') - sys.exit(1) + print('\x1b[33;1mInstalling dependencies...\x1b[0m') - if not answer == 'y': - print(f'\x1b[31;1mAborting.\x1b[0m') - sys.exit(1) + install_option = install_data['option'] exit_code = os.system(f'{binary} boot/dep_installer.py {install_option}{options}') if not exit_code == 0: sys.exit(exit_code) + if depinstall: + print('\x1b[36;1mDependencies installed successfully.\x1b[0m') + sys.exit(0) + exit_code = os.system(f'{binary} boot/installer.py {install_option}{options}') if not exit_code == 0: print('\x1b[31;1mInstaller has crashed or has been aborted.\x1b[0m') sys.exit(exit_code) - print('\x1b[33;1mPlease re-run the run script after configuring the bot to start the bot.\x1b[0m') - sys.exit(0) + # sleep to prevent 429s + time.sleep(5) if not boot_file in os.listdir(): if os.path.isdir('update'): @@ -138,8 +156,10 @@ os.remove('.restart') print('\x1b[33;1mAn incomplete restart was detected.\x1b[0m') +restart_options = '' + while True: - exit_code = os.system(f'{binary} {boot_file}{options}') + exit_code = os.system(f'{binary} {boot_file}{restart_options}{options}') crash_reboot = False if not exit_code == 0: @@ -153,6 +173,11 @@ if crash_reboot or '.restart' in os.listdir(): if '.restart' in os.listdir(): + x = open('.restart', 'r', encoding='utf-8') + data = x.read().split(' ') + x.close() + + restart_options = (' ' + data[1]) if len(data) > 1 else '' os.remove('.restart') print(f'\x1b[33;1mRestarting {internal["product_name"]}...\x1b[0m') diff --git a/boot_config.json b/boot_config.json new file mode 100644 index 0000000..d1ad722 --- /dev/null +++ b/boot_config.json @@ -0,0 +1,4 @@ +{ + "note": "Refer to https://wiki.unifierhq.org/setup-selfhosted/configuring-the-bootloader for more information.", + "bootloader": {} +} \ No newline at end of file