Merge pull request #1 from barunespadhy/development

Add a feature to update rangolio
This commit is contained in:
Barunes Padhy 2024-06-21 20:55:42 +03:00 committed by GitHub
commit cb8efcb16e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 145 additions and 59 deletions

View File

@ -10,3 +10,5 @@ Start using Rangolio today to create beautiful, professional portfolio websites
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. 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.

View File

@ -4,3 +4,6 @@ from django.apps import AppConfig
class ApimanagerConfig(AppConfig): class ApimanagerConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField' default_auto_field = 'django.db.models.BigAutoField'
name = 'apimanager' name = 'apimanager'
def ready(self):
import apimanager.check_updates

View File

@ -0,0 +1,69 @@
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.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.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'])
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, ['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')
if update_confirmation:
update_rangolio(rangolio_location)
else:
print('No updates')

View File

@ -37,20 +37,6 @@ def server_deploy():
def github_deploy(): def github_deploy():
print("Deploying via github") 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' deploy_location = settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/ghpages'
copy_data_and_html('ghpages') copy_data_and_html('ghpages')
@ -62,10 +48,10 @@ def github_deploy():
'confirmation' 'confirmation'
) )
if existing_repo: if existing_repo:
git_existing_repo_setup(deploy_location, git_commands) git_existing_repo_setup(deploy_location)
else: else:
github_init(deploy_location, git_commands) github_init(deploy_location)
github_pages_deploy(deploy_location, git_commands) github_pages_deploy(deploy_location)
return {'message': 'Github deployment successful', 'status': status.HTTP_200_OK} return {'message': 'Github deployment successful', 'status': status.HTTP_200_OK}
except Exception as e: except Exception as e:
print(f"An error occurred: {str(e)}") print(f"An error occurred: {str(e)}")
@ -73,7 +59,7 @@ def github_deploy():
else: else:
try: try:
github_pages_deploy(deploy_location, git_commands) github_pages_deploy(deploy_location)
return {'message': 'Github deployment successful', 'status': status.HTTP_200_OK} return {'message': 'Github deployment successful', 'status': status.HTTP_200_OK}
except Exception as e: except Exception as e:
print(f"An error occurred: {str(e)}") print(f"An error occurred: {str(e)}")

View File

@ -1,5 +1,4 @@
from django.conf import settings from django.conf import settings
import os
import shutil import shutil
import subprocess import subprocess
import urllib.parse import urllib.parse
@ -14,10 +13,10 @@ from .dialogue_box import (
) )
def github_init(deploy_location, git_commands): def github_init(deploy_location):
user_details_defined = git_check_user_details(deploy_location, git_commands) user_details_defined = git_check_user_details(deploy_location)
if not user_details_defined: 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() username, password = git_get_username_password()
remote_url = f'https://{username}:{password}@github.com/{username}/{username}.github.io.git' 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) 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: while True:
email = draw_dialogue_box('Github Deploy', 'Enter your github email', 'textbox') email = draw_dialogue_box('Github Deploy', 'Enter your github email', 'textbox')
name = draw_dialogue_box('Github Deploy', 'Enter your name', '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]) 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: while True:
repo_url = draw_dialogue_box('Github Deploy', 'Enter Repository URL', 'textbox') repo_url = draw_dialogue_box('Github Deploy', 'Enter Repository URL', 'textbox')
@ -65,35 +64,36 @@ def git_existing_repo_setup(deploy_location, git_commands):
git_update_viewable_ui(deploy_location, dist_folder_name) 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_email = run_git_command('git_config_name', deploy_location)
config_name = run_git_command('git_config_email', 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 False
return True 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, 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') shutil.move(deploy_location, f'{deploy_location}.temp')
if build_frontend: # server -> server.old
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}') shutil.move(f'{settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]}/{dist_folder_name}', f'{deploy_location}')
copy_content( if copy_index_and_asset:
f'{deploy_location}.temp/index.html', copy_content(
deploy_location, f'{deploy_location}.temp/index.html',
'file', deploy_location,
'remove_and_copy' 'file',
) 'remove_and_copy'
copy_content( )
f'{deploy_location}.temp/assets', copy_content(
f'{deploy_location}/assets', f'{deploy_location}.temp/assets',
'folder', f'{deploy_location}/assets',
'remove_and_copy' 'folder',
) 'remove_and_copy'
)
copy_content( copy_content(
f'{deploy_location}.temp/data', f'{deploy_location}.temp/data',
f'{deploy_location}/data', f'{deploy_location}/data',
@ -131,10 +131,10 @@ def git_get_username_password():
return username, password return username, password
def github_pages_deploy(deploy_location, git_commands): def github_pages_deploy(deploy_location):
user_details_defined = git_check_user_details(deploy_location, git_commands) user_details_defined = git_check_user_details(deploy_location)
if not user_details_defined: 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_email = run_git_command("git_config_email", deploy_location)
user_name = run_git_command("git_config_name", deploy_location) user_name = run_git_command("git_config_name", deploy_location)
@ -142,7 +142,7 @@ def github_pages_deploy(deploy_location, git_commands):
deploy_confirmation = draw_dialogue_box( deploy_confirmation = draw_dialogue_box(
'Github Deploy', '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' 'confirmation'
) )
@ -150,8 +150,8 @@ def github_pages_deploy(deploy_location, git_commands):
run_git_command('git_pull', deploy_location) run_git_command('git_pull', deploy_location)
print("completed git pull") print("completed git pull")
origin_url = run_git_command('git_get_origin_url', deploy_location) origin_url = run_git_command('git_get_origin_url', deploy_location)
print("Got origin as "+str(origin_url)) print("Got origin as "+str(origin_url.subprocess_output))
parsed_url = urllib.parse.urlparse(origin_url) parsed_url = urllib.parse.urlparse(origin_url.subprocess_output)
print(parsed_url) print(parsed_url)
if not '@' in parsed_url.netloc: if not '@' in parsed_url.netloc:
username = draw_dialogue_box('Github Deploy', 'Enter your username', 'textbox') username = draw_dialogue_box('Github Deploy', 'Enter your username', 'textbox')

View File

@ -21,12 +21,11 @@ def copy_content(source, destination, content_type, copy_type='merge'):
print(f'Error occurred: {e}') print(f'Error occurred: {e}')
def run_git_command(operation, deploy_location, parameter=None): def run_git_command(operation, command_location, parameter=None):
if not parameter:
parameter=[]
git_commands = { git_commands = {
"git_init": ['git', 'init'], "git_init": ['git', 'init'],
"git_add": ['git', 'add', '.'], "git_add": ['git', 'add', '.'],
"git_fetch_origin": ['git', 'fetch', 'origin'],
"git_pull": ['git', 'pull'], "git_pull": ['git', 'pull'],
"git_config_email": ['git', 'config', '--local', 'user.email'], "git_config_email": ['git', 'config', '--local', 'user.email'],
"git_config_name": ['git', 'config', '--local', 'user.name'], "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_set_origin_url": ['git', 'remote', 'set-url', 'origin'],
"git_add_url": ['git', 'remote', 'add', 'origin'], "git_add_url": ['git', 'remote', 'add', 'origin'],
"git_push": ['git', 'push', '-u', 'origin', 'main'], "git_push": ['git', 'push', '-u', 'origin', 'main'],
"git_clone": ['git', 'clone'] "git_clone": ['git', 'clone'],
"git_diff": ['git', 'diff']
} }
try: return run_command(operation, command_location, git_commands, parameter)
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 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

View File

@ -42,6 +42,7 @@ INSTALLED_APPS = [
] ]
DEPLOY_CONFIG = { DEPLOY_CONFIG = {
"RANGOLIO_LOCATION": os.path.join(BASE_DIR, '..'),
"VIEWABLE_UI_LOCATION": os.path.join(BASE_DIR, '../frontend/viewable-ui'), "VIEWABLE_UI_LOCATION": os.path.join(BASE_DIR, '../frontend/viewable-ui'),
"DEPLOY_LOCATION": os.path.join(BASE_DIR, '../frontend/viewable-ui/dist'), "DEPLOY_LOCATION": os.path.join(BASE_DIR, '../frontend/viewable-ui/dist'),
"EDITOR_HTML_LOCATION": os.path.join(BASE_DIR, 'deploy/html'), "EDITOR_HTML_LOCATION": os.path.join(BASE_DIR, 'deploy/html'),