diff --git a/backend/apimanager/publish_methods.py b/backend/apimanager/publish_methods.py index 25087c8..5c9f441 100644 --- a/backend/apimanager/publish_methods.py +++ b/backend/apimanager/publish_methods.py @@ -1,6 +1,9 @@ from django.conf import settings import os import shutil +import subprocess +import tkinter as tk +from tkinter import simpledialog deployment_methods = { "server_deploy": { @@ -11,20 +14,116 @@ deployment_methods = { } } -def server_deploy(): - data_location = f'{settings.BASE_DIR}/deploy/' - deploy_location = settings.DEPLOY_CONFIG["DEPLOY_LOCATION"] +def invokeDialogueBox(title, message, type): + root = tk.Tk() + root.withdraw() + + input_data = None + if type == 'text': + input_data = simpledialog.askstring(title, message) + if type == 'password': + input_data = simpledialog.askstring(title, message, show='*') + + root.destroy() + + return input_data +def copyData(data_location, deploy_location): if not os.path.exists(data_location): print("The source directory does not exist.") else: try: - shutil.rmtree(f'{deploy_location}/data') + if os.path.exists(f'{deploy_location}/data'): + shutil.rmtree(f'{deploy_location}/data') + else: + pass shutil.copytree(data_location, deploy_location, dirs_exist_ok=True) print(f"Data successfully deployed") except Exception as e: print(f"Error occurred: {e}") - - +def server_deploy(): + data_location = f'{settings.BASE_DIR}/deploy/' + deploy_location = settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/server' + copyData(data_location, deploy_location) + def github_deploy(): - server_deploy() - print("Deploying via github") \ No newline at end of file + print("Deploying via github") + git_commands = {} + git_commands["git_init"] = ['git', 'init'] + git_commands["git_add"] = ['git', 'add', '.'] + git_commands["git_commit"] = ['git', 'commit', '-m', '"Update website"'] + git_commands["git_branch"] = ['git', 'branch', '-m', 'main'] + git_commands["git_push"] = ['git', 'push', '-u', 'origin', 'main'] + + data_location = f'{settings.BASE_DIR}/deploy/' + deploy_location = settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/ghpages' + + run_gh_build = ['npm', 'run', 'build:ghpages'] + + subprocess.run(run_gh_build, cwd=settings.DEPLOY_CONFIG["DEPLOY_LOCATION"].replace('/dist', ''), check=True, text=True, capture_output=True) + create_404_page(deploy_location) + copyData(data_location, deploy_location) + + if not os.path.exists(f'{deploy_location}/.git'): + github_init(deploy_location, git_commands) + gh_pages_deploy(deploy_location, git_commands) + else: + gh_pages_deploy(deploy_location, git_commands) + +def github_init(deploy_location, git_commands): + username = invokeDialogueBox('Github Deploy', 'Enter your username', 'text') + password = invokeDialogueBox('Github Deploy', 'Enter your password', 'password') + remote_url = f'https://{username}:{password}@github.com/{username}/{username}.github.io.git' + git_add_url = ['git', 'remote', 'add', 'origin', remote_url] + + try: + subprocess.run(git_commands["git_init"], cwd=deploy_location, check=True, text=True, capture_output=True) + subprocess.run(git_commands["git_add"], cwd=deploy_location, check=True, text=True, capture_output=True) + subprocess.run(git_commands["git_commit"], cwd=deploy_location, check=True, text=True, capture_output=True) + subprocess.run(git_commands["git_branch"], cwd=deploy_location, check=True, text=True, capture_output=True) + subprocess.run(git_add_url, cwd=deploy_location, check=True, text=True, capture_output=True) + subprocess.run(git_commands["git_push"], cwd=deploy_location, check=True, text=True, capture_output=True) + except subprocess.CalledProcessError as e: + print (f"Failed to add remote: {e.stderr}") + except Exception as e: + print (f"An error occurred: {str(e)}") + + +def gh_pages_deploy(deploy_location, git_commands): + try: + subprocess.run(git_commands["git_add"], cwd=deploy_location, check=True, text=True, capture_output=True) + subprocess.run(git_commands["git_commit"], cwd=deploy_location, check=True, text=True, capture_output=True) + subprocess.run(git_commands["git_push"], cwd=deploy_location, check=True, text=True, capture_output=True) + except subprocess.CalledProcessError as e: + return f"Failed to add remote: {e.stderr}" + except Exception as e: + return f"An error occurred: {str(e)}" + + +def create_404_page(deploy_location): + html_content = """ + + +
+ +