From 645e8f20285a97f9a7f8d500e04967fa7696cd65 Mon Sep 17 00:00:00 2001 From: Barunes Padhy Date: Fri, 21 Jun 2024 18:57:59 +0300 Subject: [PATCH 1/7] Added a new functionality to update software --- backend/apimanager/apps.py | 3 + backend/apimanager/check_updates.py | 63 ++++++++++++++++++++ backend/apimanager/publish_methods.py | 22 ++----- backend/apimanager/publish_methods_github.py | 33 +++++----- backend/apimanager/utilities.py | 45 ++++++++++---- backend/backend/settings.py | 1 + 6 files changed, 120 insertions(+), 47 deletions(-) create mode 100644 backend/apimanager/check_updates.py diff --git a/backend/apimanager/apps.py b/backend/apimanager/apps.py index 5092f92..4acf213 100644 --- a/backend/apimanager/apps.py +++ b/backend/apimanager/apps.py @@ -4,3 +4,6 @@ from django.apps import AppConfig class ApimanagerConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'apimanager' + + def ready(self): + import apimanager.check_updates diff --git a/backend/apimanager/check_updates.py b/backend/apimanager/check_updates.py new file mode 100644 index 0000000..5661d36 --- /dev/null +++ b/backend/apimanager/check_updates.py @@ -0,0 +1,63 @@ +from django.conf import settings +import shutil + +from .utilities import ( + copy_content, + run_git_command, + run_npm_command, + run_django_command +) + +from .publish_methods_github import ( + git_update_viewable_ui +) + +from .dialogue_box import ( + draw_dialogue_box +) + + +def update_rangolio(rangolio_location): + run_git_command('git_pull', rangolio_location) + + # Install dependencies and build viewable-ui + run_npm_command('npm_install', f'{rangolio_location}/frontend/viewable-ui') + + # Update server + shutil.move(settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/server', + f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/server.old') + run_npm_command('npm_run', f'{rangolio_location}/frontend/viewable-ui', ['build:server']) + git_update_viewable_ui(f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/server', 'server.old') + + # Update ghpages + shutil.move(settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/ghpages', + f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/ghpages.old') + + run_npm_command('npm_run', f'{rangolio_location}/frontend/viewable-ui', ['build:ghpages']) + git_update_viewable_ui(f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/ghpages', 'ghpages.old') + + # Update editor-ui + run_npm_command('npm_run', f'{rangolio_location}/frontend/editable-ui', ['build']) + run_django_command('collectstatic', f'{rangolio_location}/backend') + copy_content( + f'{rangolio_location}/backend/static/index.html', + f'{rangolio_location}/backend/templates/index.html', + 'file', + 'remove_and_copy' + ) + run_django_command('makemigrations', f'{rangolio_location}/backend') + run_django_command('migrate', f'{rangolio_location}/backend') + + +print ('Checking for updates') +rangolio_location = settings.DEPLOY_CONFIG["RANGOLIO_LOCATION"] + +run_git_command('git_fetch_origin', rangolio_location) +updates = run_git_command('git_diff', rangolio_location) +print (updates) +if updates['subprocess_output'] and updates['subprocess_returncode'] == 0: + update_confirmation = draw_dialogue_box('Software Update', 'Would you like to update rangolio?', 'confirmation') + if update_confirmation: + update_rangolio(rangolio_location) +else: + print('No updates') \ No newline at end of file diff --git a/backend/apimanager/publish_methods.py b/backend/apimanager/publish_methods.py index 74174b0..dd4d235 100644 --- a/backend/apimanager/publish_methods.py +++ b/backend/apimanager/publish_methods.py @@ -37,20 +37,6 @@ def server_deploy(): def github_deploy(): print("Deploying via github") - git_commands = { - "git_init": ['git', 'init'], - "git_add": ['git', 'add', '.'], - "git_pull": ['git', 'pull'], - "git_config_email": ['git', 'config', '--local', 'user.email'], - "git_config_name": ['git', 'config', '--local', 'user.name'], - "git_commit": ['git', 'commit', '-m', 'Update website'], - "git_branch": ['git', 'branch', '-m', 'main'], - "git_get_origin_url": ['git', 'remote', 'get-url', 'origin'], - "git_set_origin_url": ['git', 'remote', 'set-url', 'origin'], - "git_add_url": ['git', 'remote', 'add', 'origin'], - "git_push": ['git', 'push', '-u', 'origin', 'main'], - "git_clone": ['git', 'clone'] - } deploy_location = settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/ghpages' copy_data_and_html('ghpages') @@ -62,10 +48,10 @@ def github_deploy(): 'confirmation' ) if existing_repo: - git_existing_repo_setup(deploy_location, git_commands) + git_existing_repo_setup(deploy_location) else: - github_init(deploy_location, git_commands) - github_pages_deploy(deploy_location, git_commands) + github_init(deploy_location) + github_pages_deploy(deploy_location) return {'message': 'Github deployment successful', 'status': status.HTTP_200_OK} except Exception as e: print(f"An error occurred: {str(e)}") @@ -73,7 +59,7 @@ def github_deploy(): else: try: - github_pages_deploy(deploy_location, git_commands) + github_pages_deploy(deploy_location) return {'message': 'Github deployment successful', 'status': status.HTTP_200_OK} except Exception as e: print(f"An error occurred: {str(e)}") diff --git a/backend/apimanager/publish_methods_github.py b/backend/apimanager/publish_methods_github.py index 9e41719..580ec14 100644 --- a/backend/apimanager/publish_methods_github.py +++ b/backend/apimanager/publish_methods_github.py @@ -1,5 +1,4 @@ from django.conf import settings -import os import shutil import subprocess import urllib.parse @@ -14,10 +13,10 @@ from .dialogue_box import ( ) -def github_init(deploy_location, git_commands): - user_details_defined = git_check_user_details(deploy_location, git_commands) +def github_init(deploy_location): + user_details_defined = git_check_user_details(deploy_location) if not user_details_defined: - git_set_user_details(deploy_location, git_commands) + git_set_user_details(deploy_location) username, password = git_get_username_password() remote_url = f'https://{username}:{password}@github.com/{username}/{username}.github.io.git' @@ -30,7 +29,7 @@ def github_init(deploy_location, git_commands): run_git_command('git_push', deploy_location) -def git_set_user_details(deploy_location, git_commands): +def git_set_user_details(deploy_location): while True: email = draw_dialogue_box('Github Deploy', 'Enter your github email', 'textbox') name = draw_dialogue_box('Github Deploy', 'Enter your name', 'textbox') @@ -46,7 +45,7 @@ def git_set_user_details(deploy_location, git_commands): run_git_command('git_config_name', deploy_location, [name]) -def git_existing_repo_setup(deploy_location, git_commands): +def git_existing_repo_setup(deploy_location): while True: repo_url = draw_dialogue_box('Github Deploy', 'Enter Repository URL', 'textbox') @@ -65,21 +64,17 @@ def git_existing_repo_setup(deploy_location, git_commands): git_update_viewable_ui(deploy_location, dist_folder_name) -def git_check_user_details(deploy_location, git_commands): +def git_check_user_details(deploy_location): config_email = run_git_command('git_config_name', deploy_location) config_name = run_git_command('git_config_email', deploy_location) - if not config_email or not config_name: + if not config_email['subprocess_output'] or not config_name['subprocess_output']: return False return True -def git_update_viewable_ui(deploy_location, dist_folder_name, build_frontend=False): +def git_update_viewable_ui(deploy_location, dist_folder_name): shutil.move(deploy_location, f'{deploy_location}.temp') - if build_frontend: - subprocess.run(["npm", 'run', 'build:ghpages'], cwd=settings.DEPLOY_CONFIG["VIEWABLE_UI_LOCATION"], check=True, - text=True, capture_output=True) - shutil.move(f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/{dist_folder_name}', f'{deploy_location}') copy_content( @@ -131,10 +126,10 @@ def git_get_username_password(): return username, password -def github_pages_deploy(deploy_location, git_commands): - user_details_defined = git_check_user_details(deploy_location, git_commands) +def github_pages_deploy(deploy_location): + user_details_defined = git_check_user_details(deploy_location) if not user_details_defined: - git_set_user_details(deploy_location, git_commands) + git_set_user_details(deploy_location) user_email = run_git_command("git_config_email", deploy_location) user_name = run_git_command("git_config_name", deploy_location) @@ -142,7 +137,7 @@ def github_pages_deploy(deploy_location, git_commands): deploy_confirmation = draw_dialogue_box( 'Github Deploy', - f'Deploying as {user_name} with email {user_email} to {repo_name}', + f'Deploying as {user_name.subprocess_output} with email {user_email.subprocess_output} to {repo_name.subprocess_output}', 'confirmation' ) @@ -150,8 +145,8 @@ def github_pages_deploy(deploy_location, git_commands): run_git_command('git_pull', deploy_location) print("completed git pull") origin_url = run_git_command('git_get_origin_url', deploy_location) - print("Got origin as "+str(origin_url)) - parsed_url = urllib.parse.urlparse(origin_url) + print("Got origin as "+str(origin_url.subprocess_output)) + parsed_url = urllib.parse.urlparse(origin_url.subprocess_output) print(parsed_url) if not '@' in parsed_url.netloc: username = draw_dialogue_box('Github Deploy', 'Enter your username', 'textbox') diff --git a/backend/apimanager/utilities.py b/backend/apimanager/utilities.py index 8c11e6b..0c6cc95 100644 --- a/backend/apimanager/utilities.py +++ b/backend/apimanager/utilities.py @@ -21,12 +21,11 @@ def copy_content(source, destination, content_type, copy_type='merge'): print(f'Error occurred: {e}') -def run_git_command(operation, deploy_location, parameter=None): - if not parameter: - parameter=[] +def run_git_command(operation, command_location, parameter=None): git_commands = { "git_init": ['git', 'init'], "git_add": ['git', 'add', '.'], + "git_fetch_origin": ['git', 'fetch', 'origin'], "git_pull": ['git', 'pull'], "git_config_email": ['git', 'config', '--local', 'user.email'], "git_config_name": ['git', 'config', '--local', 'user.name'], @@ -36,12 +35,38 @@ def run_git_command(operation, deploy_location, parameter=None): "git_set_origin_url": ['git', 'remote', 'set-url', 'origin'], "git_add_url": ['git', 'remote', 'add', 'origin'], "git_push": ['git', 'push', '-u', 'origin', 'main'], - "git_clone": ['git', 'clone'] + "git_clone": ['git', 'clone'], + "git_diff": ['git', 'diff', 'origin/main'] } - try: - subprocess_operation = subprocess.run(git_commands[operation] + parameter, cwd=deploy_location, check=True, text=True, capture_output=True) - subprocess_output = subprocess_operation.stdout.strip() - except subprocess.CalledProcessError as e: - subprocess_output = None + return run_command(operation, command_location, git_commands, parameter) - return subprocess_output + +def run_npm_command(operation, command_location, parameter=None): + npm_commands = { + "npm_install": ['npm', 'install', '-i'], + "npm_run": ['npm', 'run'], + } + return run_command(operation, command_location, npm_commands, parameter) + + +def run_django_command(operation, command_location, parameter=None): + npm_commands = { + "collectstatic": ['python', 'manage.py', 'collectstatic', '--no-input'], + "makemigrations": ['python', 'manage.py', 'makemigrations'], + "migrate": ['python', 'manage.py', 'migrate'], + } + return run_command(operation, command_location, npm_commands, parameter) + + +def run_command(operation, command_location, command_map_list, parameter=None): + if not parameter: + parameter=[] + + try: + subprocess_operation = subprocess.run(command_map_list[operation] + parameter, cwd=command_location, check=True, text=True, capture_output=True) + subprocess_output = subprocess_operation.stdout.strip() + subprocess_returncode = subprocess_operation.returncode + subprocess_result = {'subprocess_output': subprocess_output, 'subprocess_returncode': subprocess_returncode} + return subprocess_result + except subprocess.CalledProcessError as e: + return None diff --git a/backend/backend/settings.py b/backend/backend/settings.py index 99891d4..c7ed9e9 100644 --- a/backend/backend/settings.py +++ b/backend/backend/settings.py @@ -42,6 +42,7 @@ INSTALLED_APPS = [ ] DEPLOY_CONFIG = { + "RANGOLIO_LOCATION": os.path.join(BASE_DIR, '..'), "VIEWABLE_UI_LOCATION": os.path.join(BASE_DIR, '../frontend/viewable-ui'), "DEPLOY_LOCATION": os.path.join(BASE_DIR, '../frontend/viewable-ui/dist'), "EDITOR_HTML_LOCATION": os.path.join(BASE_DIR, 'deploy/html'), From 61701770a87ea95f95893053b3487fccfec7cb1e Mon Sep 17 00:00:00 2001 From: Barunes Padhy Date: Fri, 21 Jun 2024 19:28:39 +0300 Subject: [PATCH 2/7] Added some print statements --- backend/apimanager/check_updates.py | 2 +- backend/apimanager/utilities.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/apimanager/check_updates.py b/backend/apimanager/check_updates.py index 5661d36..08d3b62 100644 --- a/backend/apimanager/check_updates.py +++ b/backend/apimanager/check_updates.py @@ -53,7 +53,7 @@ print ('Checking for updates') rangolio_location = settings.DEPLOY_CONFIG["RANGOLIO_LOCATION"] run_git_command('git_fetch_origin', rangolio_location) -updates = run_git_command('git_diff', rangolio_location) +updates = run_git_command('git_diff', rangolio_location, ['origin/development']) print (updates) if updates['subprocess_output'] and updates['subprocess_returncode'] == 0: update_confirmation = draw_dialogue_box('Software Update', 'Would you like to update rangolio?', 'confirmation') diff --git a/backend/apimanager/utilities.py b/backend/apimanager/utilities.py index 0c6cc95..751e3fa 100644 --- a/backend/apimanager/utilities.py +++ b/backend/apimanager/utilities.py @@ -36,7 +36,7 @@ def run_git_command(operation, command_location, parameter=None): "git_add_url": ['git', 'remote', 'add', 'origin'], "git_push": ['git', 'push', '-u', 'origin', 'main'], "git_clone": ['git', 'clone'], - "git_diff": ['git', 'diff', 'origin/main'] + "git_diff": ['git', 'diff'] } return run_command(operation, command_location, git_commands, parameter) From 6cdf4fad5704d6a043c89ff0c84c9914626a48ac Mon Sep 17 00:00:00 2001 From: Barunes Padhy <148685909+barunespadhy@users.noreply.github.com> Date: Fri, 21 Jun 2024 19:30:18 +0300 Subject: [PATCH 3/7] Update README.md to test update feature --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index a3fbb75..a9e4db8 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,3 @@ Start using Rangolio today to create beautiful, professional portfolio websites ## Instructions Ofcourse I could create instructions here, but what better tool to showcase ability to create content than the tool itself! Head over to [instrctions](https://barunes.io/blog/975fda0e-6f2b-4f7b-9268-2d75dea61b0f) on prepping your github account to setup rangolio. - From b8137d2cb8f426b78392483f688c25183b06cdb2 Mon Sep 17 00:00:00 2001 From: Barunes Padhy <148685909+barunespadhy@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:03:35 +0300 Subject: [PATCH 4/7] Update README.md to test update feature --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index a9e4db8..0d751a0 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,6 @@ Start using Rangolio today to create beautiful, professional portfolio websites ## Instructions Ofcourse I could create instructions here, but what better tool to showcase ability to create content than the tool itself! Head over to [instrctions](https://barunes.io/blog/975fda0e-6f2b-4f7b-9268-2d75dea61b0f) on prepping your github account to setup rangolio. + + + From 841345f15b76c4d194ffac9f41b86827cd564821 Mon Sep 17 00:00:00 2001 From: Barunes Padhy Date: Fri, 21 Jun 2024 20:33:34 +0300 Subject: [PATCH 5/7] made changes to update feature --- backend/apimanager/check_updates.py | 10 +++++-- backend/apimanager/publish_methods_github.py | 31 ++++++++++++-------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/backend/apimanager/check_updates.py b/backend/apimanager/check_updates.py index 08d3b62..5688b15 100644 --- a/backend/apimanager/check_updates.py +++ b/backend/apimanager/check_updates.py @@ -26,15 +26,21 @@ def update_rangolio(rangolio_location): # Update server shutil.move(settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/server', f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/server.old') + run_npm_command('npm_run', f'{rangolio_location}/frontend/viewable-ui', ['build:server']) - git_update_viewable_ui(f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/server', 'server.old') + git_update_viewable_ui(f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/server.old', 'server', False) + shutil.move(settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/server.old', + f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/server') + # Update ghpages shutil.move(settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/ghpages', f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/ghpages.old') run_npm_command('npm_run', f'{rangolio_location}/frontend/viewable-ui', ['build:ghpages']) - git_update_viewable_ui(f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/ghpages', 'ghpages.old') + git_update_viewable_ui(f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/ghpages.old', 'ghpages', False) + shutil.move(settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/ghpages.old', + f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/ghpages') # Update editor-ui run_npm_command('npm_run', f'{rangolio_location}/frontend/editable-ui', ['build']) diff --git a/backend/apimanager/publish_methods_github.py b/backend/apimanager/publish_methods_github.py index 580ec14..45a19da 100644 --- a/backend/apimanager/publish_methods_github.py +++ b/backend/apimanager/publish_methods_github.py @@ -72,23 +72,28 @@ def git_check_user_details(deploy_location): return True -def git_update_viewable_ui(deploy_location, dist_folder_name): +def git_update_viewable_ui(deploy_location, dist_folder_name, copy_index_and_asset=True): + # deploy_location = server.old ; dist_folder_name = server + + # server.old -> server.old.temp shutil.move(deploy_location, f'{deploy_location}.temp') + # server -> server.old shutil.move(f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/{dist_folder_name}', f'{deploy_location}') - copy_content( - f'{deploy_location}.temp/index.html', - deploy_location, - 'file', - 'remove_and_copy' - ) - copy_content( - f'{deploy_location}.temp/assets', - f'{deploy_location}/assets', - 'folder', - 'remove_and_copy' - ) + if copy_index_and_asset: + copy_content( + f'{deploy_location}.temp/index.html', + deploy_location, + 'file', + 'remove_and_copy' + ) + copy_content( + f'{deploy_location}.temp/assets', + f'{deploy_location}/assets', + 'folder', + 'remove_and_copy' + ) copy_content( f'{deploy_location}.temp/data', f'{deploy_location}/data', From 626a625c9820e4b7d6181776dbfe0af8332aa314 Mon Sep 17 00:00:00 2001 From: Barunes Padhy <148685909+barunespadhy@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:44:29 +0300 Subject: [PATCH 6/7] Update README.md to test update feature --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 0d751a0..a9e4db8 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,3 @@ Start using Rangolio today to create beautiful, professional portfolio websites ## Instructions Ofcourse I could create instructions here, but what better tool to showcase ability to create content than the tool itself! Head over to [instrctions](https://barunes.io/blog/975fda0e-6f2b-4f7b-9268-2d75dea61b0f) on prepping your github account to setup rangolio. - - - From bcde14fd60ea6069fab64e9d63463baebc809b11 Mon Sep 17 00:00:00 2001 From: Barunes Padhy <148685909+barunespadhy@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:52:08 +0300 Subject: [PATCH 7/7] Update README.md to test update feature --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index a9e4db8..0d751a0 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,6 @@ Start using Rangolio today to create beautiful, professional portfolio websites ## Instructions Ofcourse I could create instructions here, but what better tool to showcase ability to create content than the tool itself! Head over to [instrctions](https://barunes.io/blog/975fda0e-6f2b-4f7b-9268-2d75dea61b0f) on prepping your github account to setup rangolio. + + +