Added a new functionality to update software
This commit is contained in:
parent
62586ee65c
commit
645e8f2028
@ -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
|
||||
|
||||
63
backend/apimanager/check_updates.py
Normal file
63
backend/apimanager/check_updates.py
Normal file
@ -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')
|
||||
@ -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)}")
|
||||
|
||||
@ -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')
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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'),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user