version
This commit is contained in:
@@ -13,6 +13,9 @@ from email.utils import formatdate
|
||||
from datetime import datetime
|
||||
import requests
|
||||
import base64
|
||||
from flask_wtf.csrf import CSRFProtect
|
||||
from functools import wraps
|
||||
import os
|
||||
|
||||
launch_api = Blueprint('launch_api', __name__)
|
||||
|
||||
@@ -657,6 +660,60 @@ def download_docker_compose():
|
||||
if not git_settings:
|
||||
return jsonify({'message': 'Git settings not configured'}), 400
|
||||
|
||||
# Get the current commit hash and latest tag for the branch
|
||||
commit_hash = None
|
||||
latest_tag = None
|
||||
if git_settings['provider'] == 'gitea':
|
||||
headers = {
|
||||
'Accept': 'application/json',
|
||||
'Authorization': f'token {git_settings["token"]}'
|
||||
}
|
||||
|
||||
# Get the latest commit for the branch
|
||||
commit_response = requests.get(
|
||||
f'{git_settings["url"]}/api/v1/repos/{data["repository"]}/commits/{data["branch"]}',
|
||||
headers=headers
|
||||
)
|
||||
|
||||
if commit_response.status_code == 200:
|
||||
commit_data = commit_response.json()
|
||||
commit_hash = commit_data.get('sha')
|
||||
else:
|
||||
# Try token as query parameter if header auth fails
|
||||
commit_response = requests.get(
|
||||
f'{git_settings["url"]}/api/v1/repos/{data["repository"]}/commits/{data["branch"]}?token={git_settings["token"]}',
|
||||
headers={'Accept': 'application/json'}
|
||||
)
|
||||
if commit_response.status_code == 200:
|
||||
commit_data = commit_response.json()
|
||||
commit_hash = commit_data.get('sha')
|
||||
|
||||
# Get the latest tag
|
||||
tags_response = requests.get(
|
||||
f'{git_settings["url"]}/api/v1/repos/{data["repository"]}/tags',
|
||||
headers=headers
|
||||
)
|
||||
|
||||
if tags_response.status_code == 200:
|
||||
tags_data = tags_response.json()
|
||||
if tags_data:
|
||||
# Sort tags by commit date (newest first) and get the latest
|
||||
sorted_tags = sorted(tags_data, key=lambda x: x.get('commit', {}).get('created', ''), reverse=True)
|
||||
if sorted_tags:
|
||||
latest_tag = sorted_tags[0].get('name')
|
||||
else:
|
||||
# Try token as query parameter if header auth fails
|
||||
tags_response = requests.get(
|
||||
f'{git_settings["url"]}/api/v1/repos/{data["repository"]}/tags?token={git_settings["token"]}',
|
||||
headers={'Accept': 'application/json'}
|
||||
)
|
||||
if tags_response.status_code == 200:
|
||||
tags_data = tags_response.json()
|
||||
if tags_data:
|
||||
sorted_tags = sorted(tags_data, key=lambda x: x.get('commit', {}).get('created', ''), reverse=True)
|
||||
if sorted_tags:
|
||||
latest_tag = sorted_tags[0].get('name')
|
||||
|
||||
# Determine the provider and set up the appropriate API call
|
||||
if git_settings['provider'] == 'gitea':
|
||||
# For Gitea
|
||||
@@ -704,9 +761,53 @@ def download_docker_compose():
|
||||
else:
|
||||
content = response.text
|
||||
|
||||
# Add version.txt creation to the docker-compose content
|
||||
if commit_hash:
|
||||
# Create version information with both tag and commit hash
|
||||
version_info = {
|
||||
'tag': latest_tag or 'unknown',
|
||||
'commit': commit_hash,
|
||||
'branch': data['branch'],
|
||||
'deployed_at': datetime.utcnow().isoformat()
|
||||
}
|
||||
version_json = json.dumps(version_info, indent=2)
|
||||
|
||||
# Add a command to create version.txt with the version information
|
||||
version_command = f'echo \'{version_json}\' > /app/version.txt'
|
||||
|
||||
# Find the web service and add the command
|
||||
if 'web:' in content:
|
||||
# Add the command to create version.txt before the main command
|
||||
lines = content.split('\n')
|
||||
new_lines = []
|
||||
in_web_service = False
|
||||
command_added = False
|
||||
|
||||
for line in lines:
|
||||
new_lines.append(line)
|
||||
|
||||
if line.strip() == 'web:':
|
||||
in_web_service = True
|
||||
elif in_web_service and line.strip().startswith('command:'):
|
||||
# Add the version.txt creation command before the main command
|
||||
new_lines.append(f' - sh -c "{version_command} && {line.split("command:")[1].strip()}"')
|
||||
command_added = True
|
||||
continue
|
||||
elif in_web_service and line.strip() and not line.startswith(' ') and not line.startswith('#'):
|
||||
# We've left the web service section
|
||||
if not command_added:
|
||||
# If no command was found, add a new command section
|
||||
new_lines.append(f' command: sh -c "{version_command} && python app.py"')
|
||||
command_added = True
|
||||
in_web_service = False
|
||||
|
||||
content = '\n'.join(new_lines)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'content': content
|
||||
'content': content,
|
||||
'commit_hash': commit_hash,
|
||||
'latest_tag': latest_tag
|
||||
})
|
||||
else:
|
||||
return jsonify({
|
||||
@@ -981,26 +1082,77 @@ def save_instance():
|
||||
missing_fields = [field for field in required_fields if field not in data]
|
||||
return jsonify({'error': f'Missing required fields: {", ".join(missing_fields)}'}), 400
|
||||
|
||||
# Save instance data
|
||||
instance_data = {
|
||||
'name': data['name'],
|
||||
'port': data['port'],
|
||||
'domains': data['domains'],
|
||||
'stack_id': data['stack_id'],
|
||||
'stack_name': data['stack_name'],
|
||||
'status': data['status'],
|
||||
'repository': data['repository'],
|
||||
'branch': data['branch'],
|
||||
'created_at': datetime.utcnow().isoformat()
|
||||
}
|
||||
|
||||
# Save to database using KeyValueSettings
|
||||
KeyValueSettings.set_value(f'instance_{data["name"]}', instance_data)
|
||||
|
||||
return jsonify({
|
||||
'message': 'Instance data saved successfully',
|
||||
'data': instance_data
|
||||
})
|
||||
# Check if instance already exists
|
||||
existing_instance = Instance.query.filter_by(name=data['name']).first()
|
||||
|
||||
if existing_instance:
|
||||
# Update existing instance
|
||||
existing_instance.port = data['port']
|
||||
existing_instance.domains = data['domains']
|
||||
existing_instance.stack_id = data['stack_id']
|
||||
existing_instance.stack_name = data['stack_name']
|
||||
existing_instance.status = data['status']
|
||||
existing_instance.repository = data['repository']
|
||||
existing_instance.branch = data['branch']
|
||||
existing_instance.deployed_version = data.get('deployed_version', 'unknown')
|
||||
existing_instance.deployed_branch = data.get('deployed_branch', data['branch'])
|
||||
existing_instance.version_checked_at = datetime.utcnow()
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({
|
||||
'message': 'Instance data updated successfully',
|
||||
'data': {
|
||||
'name': existing_instance.name,
|
||||
'port': existing_instance.port,
|
||||
'domains': existing_instance.domains,
|
||||
'stack_id': existing_instance.stack_id,
|
||||
'stack_name': existing_instance.stack_name,
|
||||
'status': existing_instance.status,
|
||||
'repository': existing_instance.repository,
|
||||
'branch': existing_instance.branch,
|
||||
'deployed_version': existing_instance.deployed_version,
|
||||
'deployed_branch': existing_instance.deployed_branch
|
||||
}
|
||||
})
|
||||
else:
|
||||
# Create new instance
|
||||
instance = Instance(
|
||||
name=data['name'],
|
||||
company='Loading...', # Will be updated later
|
||||
rooms_count=0,
|
||||
conversations_count=0,
|
||||
data_size=0.0,
|
||||
payment_plan='Basic',
|
||||
main_url=f"https://{data['domains'][0]}" if data['domains'] else f"http://localhost:{data['port']}",
|
||||
status=data['status'],
|
||||
port=data['port'],
|
||||
stack_id=data['stack_id'],
|
||||
stack_name=data['stack_name'],
|
||||
repository=data['repository'],
|
||||
branch=data['branch'],
|
||||
deployed_version=data.get('deployed_version', 'unknown'),
|
||||
deployed_branch=data.get('deployed_branch', data['branch'])
|
||||
)
|
||||
|
||||
db.session.add(instance)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({
|
||||
'message': 'Instance data saved successfully',
|
||||
'data': {
|
||||
'name': instance.name,
|
||||
'port': instance.port,
|
||||
'domains': instance.domains,
|
||||
'stack_id': instance.stack_id,
|
||||
'stack_name': instance.stack_name,
|
||||
'status': instance.status,
|
||||
'repository': instance.repository,
|
||||
'branch': instance.branch,
|
||||
'deployed_version': instance.deployed_version,
|
||||
'deployed_branch': instance.deployed_branch
|
||||
}
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error saving instance data: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user