add more functionality to git deploy, frontend changes

This commit is contained in:
Barunes Padhy 2024-06-14 23:45:08 +03:00
parent 60beb4d387
commit 018fe37782
5 changed files with 94 additions and 64 deletions

View File

@ -3,7 +3,10 @@ import os
import shutil
import subprocess
import tkinter as tk
from rest_framework import status
from tkinter import simpledialog
from tkinter import messagebox
import urllib.parse
deployment_methods = {
"server_deploy": {
@ -23,10 +26,12 @@ def invokeDialogueBox(title, message, type):
input_data = simpledialog.askstring(title, message)
if type == 'password':
input_data = simpledialog.askstring(title, message, show='*')
if type == 'message':
messagebox.showinfo(title, message)
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.")
@ -41,9 +46,14 @@ def copyData(data_location, deploy_location):
except Exception as e:
print(f"Error occurred: {e}")
def server_deploy():
try:
data_location = f'{settings.BASE_DIR}/deploy/'
deploy_location = settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/server'
copyData(data_location, deploy_location)
return {'message': 'Server deployment successful', 'status': status.HTTP_200_OK}
except Exception as e:
print (f"An error occurred: {str(e)}")
return {'message': str(e), 'status': status.HTTP_500_INTERNAL_SERVER_ERROR}
def github_deploy():
print("Deploying via github")
@ -55,6 +65,8 @@ def github_deploy():
git_commands["git_config_name"] = ['git', 'config', '--local', 'user.name']
git_commands["git_commit"] = ['git', 'commit', '-m', 'Update website']
git_commands["git_branch"] = ['git', 'branch', '-m', 'main']
git_commands["git_get_origin_url"] = ['git', 'remote', 'get-url', 'origin']
git_commands["git_set_origin_url"] = ['git', 'remote', 'set-url', 'origin']
git_commands["git_add_url"] = ['git', 'remote', 'add', 'origin']
git_commands["git_push"] = ['git', 'push', '-u', 'origin', 'main']
@ -65,10 +77,22 @@ def github_deploy():
copyData(data_location, deploy_location)
if not os.path.exists(f'{deploy_location}/.git'):
try:
github_init(deploy_location, git_commands)
gh_pages_deploy(deploy_location, git_commands)
return {'message': 'Github deployment successful', 'status': status.HTTP_200_OK}
except Exception as e:
print (f"An error occurred: {str(e)}")
return {'message': str(e), 'status': status.HTTP_500_INTERNAL_SERVER_ERROR}
else:
try:
gh_pages_deploy(deploy_location, git_commands)
return {'message': 'Github deployment successful', 'status': status.HTTP_200_OK}
except Exception as e:
print (f"An error occurred: {str(e)}")
return {'message': str(e), 'status': status.HTTP_500_INTERNAL_SERVER_ERROR}
def github_init(deploy_location, git_commands):
email = invokeDialogueBox('Github Deploy', 'Enter your github email', 'text')
@ -77,7 +101,6 @@ def github_init(deploy_location, git_commands):
password = invokeDialogueBox('Github Deploy', 'Enter your github token', 'password')
remote_url = f'https://{username}:{password}@github.com/{username}/{username}.github.io.git'
try:
subprocess.run(git_commands["git_init"], cwd=deploy_location, check=True, text=True, capture_output=True)
subprocess.run((git_commands["git_config_email"]).append(email), cwd=deploy_location, check=True, text=True, capture_output=True)
subprocess.run((git_commands["git_config_name"]).append(name), cwd=deploy_location, check=True, text=True, capture_output=True)
@ -86,21 +109,22 @@ def github_init(deploy_location, git_commands):
subprocess.run(git_commands["git_branch"], cwd=deploy_location, check=True, text=True, capture_output=True)
subprocess.run((git_commands["git_add_url"]).append(remote_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_pull"], cwd=deploy_location, check=True, text=True, capture_output=True)
origin_url_subprocess = subprocess.run(git_commands["git_get_origin_url"], cwd=deploy_location, check=True, text=True, capture_output=True)
origin_url = origin_url_subprocess.stdout.strip()
parsed_url = urllib.parse.urlparse(origin_url)
if not '@' in parsed_url.netloc:
username = invokeDialogueBox('Github Deploy', 'Enter your username', 'text')
password = invokeDialogueBox('Github Deploy', 'Enter your github token', 'password')
netloc = f"{username}:{password}@{parsed_url.hostname}"
new_url = urllib.parse.urlunparse(parsed_url._replace(netloc=netloc))
subprocess.run(git_commands["git_set_origin_url"] + [new_url], 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_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):

View File

@ -34,8 +34,8 @@ class Publish(APIView):
storage = CustomStorage()
self.delete_old_data()
self.create_json(storage)
self.execute_deploy(deploy_type)
return Response({"deploy_type": deploy_type}, status=status.HTTP_200_OK)
response = self.execute_deploy(deploy_type)
return Response(response['message'], response['status'])
def delete_old_data(self):
@ -152,7 +152,14 @@ class Publish(APIView):
def execute_deploy(self, deploy_type):
response = {
'message': 'Something failed',
'status': status.HTTP_500_INTERNAL_SERVER_ERROR
}
if deploy_type == "server_deploy":
server_deploy()
response = server_deploy()
if deploy_type == "github_deploy":
github_deploy()
response = github_deploy()
return response

View File

@ -131,6 +131,7 @@ function BlogList(props) {
textColor={ThemeConfig[GlobalTheme].textColor}
bgColor={ThemeConfig[GlobalTheme].background}
borderColor={ThemeConfig[GlobalTheme].borderColor}
buttonColor={ThemeConfig[GlobalTheme].buttonColor}
itemObject={item}
/>
</div>

View File

@ -111,7 +111,9 @@ function CardListViewer(props) {
</CardText>
<CardText>
<Link className={`${props.textColor}`} to={`/${props.resourceType}/${itemObject.id}`}>
<Button color='success'>
Open this resource
</Button>
</Link>
<Button color='danger' onClick={() => showModal()} className='m-2'>Delete Category</Button>
</CardText>
@ -138,21 +140,26 @@ function CardListViewer(props) {
</CardText>
</Link>
</CardBody>
{
itemObject.id === props.featuredBlog ?
<ButtonGroup>
<Button
outline
active={itemObject.id === props.featuredBlog}
color={props.buttonColor}
onClick={() => props.updateFeaturedBlog(itemObject.id)}
>
Set this as featured
</Button>
<Button
outline
color={props.buttonColor}
onClick={() => props.updateFeaturedBlog('')}
>
Unset featured blog
</Button>
</ButtonGroup>
</ButtonGroup> : ''
}
</Card>
)
}

View File

@ -11,17 +11,8 @@ a {
}
.blogContent a{
border-radius: 5px;
padding-left: 5px;
padding-right: 5px;
transition-duration: 0.1s;
}
.blogContent a:hover{
border-radius: 5px;
padding-left: 15px;
padding-right: 15px;
transition-duration: 0.1s;
color: blue !important;
text-decoration: underline !important;
}
.blogContent{