diff --git a/custom/jupyter_server_config.py b/custom/jupyter_server_config.py index bbcefcb5..e87b240c 100644 --- a/custom/jupyter_server_config.py +++ b/custom/jupyter_server_config.py @@ -6,6 +6,7 @@ import logging import threading import time +from glob import glob c = get_config() c.ServerApp.ip = "0.0.0.0" @@ -75,6 +76,7 @@ def naasStarter(): time.sleep(ONE_HOUR) + def naasTemplates(): while True: logging.info("Refreshing templates") @@ -92,6 +94,86 @@ def naasTemplates(): ) time.sleep(ONE_HOUR) +def get_current_commit(dir_path): + commit_hash = None + try: + # Run the Git command to get the current commit hash + commit_hash = subprocess.check_output(['git', '-C', dir_path, 'rev-parse', 'HEAD']).decode().strip() + except subprocess.CalledProcessError as e: + print(f"Failed to get current commit: {e}") + + return commit_hash + +def store_last_installed_version(dir_path, version, install_log_file='.last_installed_version'): + installed_version_file = os.path.join(dir_path, install_log_file) + with open(installed_version_file, 'w') as f: + f.write(version) + return installed_version_file + +def get_last_installed_version(dir_path, install_log_file='.last_installed_version'): + last_installed_version = None + installed_version_file = os.path.join(dir_path, install_log_file) + if os.path.exists(installed_version_file): + with open(installed_version_file, 'r') as f: + last_installed_version = f.read() + return last_installed_version + +def naasABIInstaller(): + while True: + try: + logging.info("Refreshing ABI setup") + + config = [ + { + 'repository': 'https://github.com/jupyter-naas/abi.git', + 'target': '/home/ftp/__abi__', + 'version': 'main' + } + ] + + for c in config: + # Clone repository + os.system( + f"git clone {c['repository']} {c['target']}" + ) + + # Get last installed version + last_installed_version = get_last_installed_version(c['target']) + print(last_installed_version) + + # Reset hard and pull latest version. + os.system( + f"cd {c['target']} && git reset --hard && git checkout {c['version']} && git pull" + ) + + # Grab current commit version + current_commmit = get_current_commit(c['target']) + print(current_commmit) + + # Check if current commit is different than last installed version. + # If yes then we execute all targeted notebooks. + if current_commmit != last_installed_version: + + os.system( + f"cd {c['target']} && pip install --user -r requirements.txt" + ) + + entrypoints = glob(os.path.join(c['target'], '**', '__plugin__.ipynb'), recursive=True) + for entrypoint in entrypoints: + working_directory = '/'.join(entrypoint.split('/')[:-1]) + + execute_cmd = f"cd {working_directory} && papermill {entrypoint.split('/')[-1]} {entrypoint.split('/')[-1]}.setup-execution.ipynb" + + os.system( + execute_cmd + ) + store_last_installed_version(c['target'], current_commmit) + except Exception as e: + logging.error(f'Exception while installing ABI', e) + FIVE_MINUTES = 300 + time.sleep(FIVE_MINUTES) + + runner = threading.Thread(target=naasRunner, args=(naas_port,)) runner.start() @@ -101,3 +183,6 @@ def naasTemplates(): templates = threading.Thread(target=naasTemplates, args=()) templates.start() + +abiInstaller = threading.Thread(target=naasABIInstaller, args=()) +abiInstaller.start() \ No newline at end of file