download docker compose during launch process
This commit is contained in:
@@ -13,6 +13,7 @@ import secrets
|
||||
from flask_login import login_user
|
||||
import requests
|
||||
import json
|
||||
import base64
|
||||
|
||||
admin_api = Blueprint('admin_api', __name__)
|
||||
|
||||
@@ -1155,4 +1156,78 @@ def create_ssl_certificate(current_user):
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}), 500
|
||||
}), 500
|
||||
|
||||
@admin_api.route('/download-docker-compose', methods=['POST'])
|
||||
@csrf.exempt
|
||||
def download_docker_compose():
|
||||
"""Download docker-compose.yml from the repository"""
|
||||
data = request.get_json()
|
||||
if not data or 'repository' not in data or 'branch' not in data:
|
||||
return jsonify({'message': 'Missing required fields'}), 400
|
||||
|
||||
try:
|
||||
# Get Git settings
|
||||
git_settings = KeyValueSettings.get_value('git_settings')
|
||||
if not git_settings:
|
||||
return jsonify({'message': 'Git settings not configured'}), 400
|
||||
|
||||
# Determine the provider and set up the appropriate API call
|
||||
if git_settings['provider'] == 'gitea':
|
||||
# For Gitea
|
||||
headers = {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': f'token {git_settings["token"]}'
|
||||
}
|
||||
|
||||
# Try to get the file content
|
||||
response = requests.get(
|
||||
f'{git_settings["url"]}/api/v1/repos/{data["repository"]}/contents/docker-compose.yml',
|
||||
headers=headers,
|
||||
params={'ref': data['branch']}
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
# Try token as query parameter if header auth fails
|
||||
response = requests.get(
|
||||
f'{git_settings["url"]}/api/v1/repos/{data["repository"]}/contents/docker-compose.yml?token={git_settings["token"]}',
|
||||
headers={'Accept': 'application/json'},
|
||||
params={'ref': data['branch']}
|
||||
)
|
||||
|
||||
elif git_settings['provider'] == 'gitlab':
|
||||
# For GitLab
|
||||
headers = {
|
||||
'PRIVATE-TOKEN': git_settings['token'],
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
|
||||
# Get the file content
|
||||
response = requests.get(
|
||||
f'{git_settings["url"]}/api/v4/projects/{data["repository"].replace("/", "%2F")}/repository/files/docker-compose.yml/raw',
|
||||
headers=headers,
|
||||
params={'ref': data['branch']}
|
||||
)
|
||||
|
||||
else:
|
||||
return jsonify({'message': 'Unsupported Git provider'}), 400
|
||||
|
||||
if response.status_code == 200:
|
||||
# For Gitea, we need to decode the content from base64
|
||||
if git_settings['provider'] == 'gitea':
|
||||
content = base64.b64decode(response.json()['content']).decode('utf-8')
|
||||
else:
|
||||
content = response.text
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'content': content
|
||||
})
|
||||
else:
|
||||
return jsonify({
|
||||
'message': f'Failed to download docker-compose.yml: {response.json().get("message", "Unknown error")}'
|
||||
}), 400
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error downloading docker-compose.yml: {str(e)}")
|
||||
return jsonify({'message': f'Error downloading docker-compose.yml: {str(e)}'}), 500
|
||||
Reference in New Issue
Block a user