checking portainer connection
This commit is contained in:
107
app/routes/admin_api.py
Normal file
107
app/routes/admin_api.py
Normal file
@@ -0,0 +1,107 @@
|
||||
from flask import request, jsonify, current_app
|
||||
from flask_login import login_required
|
||||
import os
|
||||
from datetime import datetime
|
||||
from app.utils.gitea_api import GiteaAPI
|
||||
from app.models.gitea_settings import GiteaSettings
|
||||
|
||||
@admin_api.route('/test-git-connection', methods=['POST'])
|
||||
@login_required
|
||||
def test_git_connection():
|
||||
try:
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return jsonify({'success': False, 'error': 'No data provided'}), 400
|
||||
|
||||
required_fields = ['gitea_url', 'username', 'api_token', 'repo_name']
|
||||
for field in required_fields:
|
||||
if field not in data:
|
||||
return jsonify({'success': False, 'error': f'Missing required field: {field}'}), 400
|
||||
|
||||
# Test the connection using the provided credentials
|
||||
gitea = GiteaAPI(
|
||||
url=data['gitea_url'],
|
||||
token=data['api_token']
|
||||
)
|
||||
|
||||
# Try to get repository information
|
||||
repo = gitea.get_repo(data['username'], data['repo_name'])
|
||||
if not repo:
|
||||
return jsonify({'success': False, 'error': 'Repository not found'}), 404
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': 'Successfully connected to Gitea',
|
||||
'data': {
|
||||
'repo_name': repo.name,
|
||||
'description': repo.description,
|
||||
'default_branch': repo.default_branch,
|
||||
'created_at': repo.created_at,
|
||||
'updated_at': repo.updated_at
|
||||
}
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
|
||||
@admin_api.route('/check-docker-compose', methods=['POST'])
|
||||
@login_required
|
||||
def check_docker_compose():
|
||||
try:
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return jsonify({'success': False, 'error': 'No data provided'}), 400
|
||||
|
||||
required_fields = ['repository', 'branch']
|
||||
for field in required_fields:
|
||||
if field not in data:
|
||||
return jsonify({'success': False, 'error': f'Missing required field: {field}'}), 400
|
||||
|
||||
# Get Gitea settings from database
|
||||
gitea_settings = GiteaSettings.query.first()
|
||||
if not gitea_settings:
|
||||
return jsonify({'success': False, 'error': 'Gitea settings not configured'}), 400
|
||||
|
||||
# Initialize Gitea API
|
||||
gitea = GiteaAPI(
|
||||
url=gitea_settings.server_url,
|
||||
token=gitea_settings.api_token
|
||||
)
|
||||
|
||||
# Split repository into owner and repo name
|
||||
owner, repo_name = data['repository'].split('/')
|
||||
|
||||
# Get the docker-compose.yml file content
|
||||
try:
|
||||
file_content = gitea.get_file_content(
|
||||
owner=owner,
|
||||
repo=repo_name,
|
||||
filepath='docker-compose.yml',
|
||||
ref=data['branch']
|
||||
)
|
||||
except Exception as e:
|
||||
return jsonify({'success': False, 'error': f'Failed to get docker-compose.yml: {str(e)}'}), 404
|
||||
|
||||
# Save the file content to a temporary location
|
||||
temp_dir = os.path.join(current_app.config['UPLOAD_FOLDER'], 'temp')
|
||||
os.makedirs(temp_dir, exist_ok=True)
|
||||
|
||||
file_path = os.path.join(temp_dir, f'docker-compose-{owner}-{repo_name}.yml')
|
||||
with open(file_path, 'w') as f:
|
||||
f.write(file_content)
|
||||
|
||||
# Get file stats
|
||||
file_stats = os.stat(file_path)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': 'Successfully found and downloaded docker-compose.yml',
|
||||
'data': {
|
||||
'file_path': file_path,
|
||||
'size': file_stats.st_size,
|
||||
'last_modified': datetime.fromtimestamp(file_stats.st_mtime).isoformat()
|
||||
}
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
Reference in New Issue
Block a user