Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

432 feat auto clone refresh and execute chat plugin for abi #436

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions custom/jupyter_server_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import threading
import time
from glob import glob

c = get_config()
c.ServerApp.ip = "0.0.0.0"
Expand Down Expand Up @@ -75,6 +76,7 @@ def naasStarter():
time.sleep(ONE_HOUR)



def naasTemplates():
while True:
logging.info("Refreshing templates")
Expand All @@ -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()
Expand All @@ -101,3 +183,6 @@ def naasTemplates():

templates = threading.Thread(target=naasTemplates, args=())
templates.start()

abiInstaller = threading.Thread(target=naasABIInstaller, args=())
abiInstaller.start()
Loading