version
This commit is contained in:
Binary file not shown.
@@ -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)}")
|
||||
|
||||
@@ -379,18 +379,58 @@ def init_routes(main_bp):
|
||||
|
||||
instances = Instance.query.order_by(Instance.name.asc()).all()
|
||||
|
||||
# Check status for each instance
|
||||
# Get Git settings
|
||||
git_settings = KeyValueSettings.get_value('git_settings')
|
||||
gitea_url = git_settings.get('url') if git_settings else None
|
||||
gitea_token = git_settings.get('token') if git_settings else None
|
||||
gitea_repo = git_settings.get('repo') if git_settings else None
|
||||
|
||||
for instance in instances:
|
||||
# 1. Check status
|
||||
status_info = check_instance_status(instance)
|
||||
instance.status = status_info['status']
|
||||
instance.status_details = status_info['details']
|
||||
|
||||
# 2. Check deployed version
|
||||
deployed_version = None
|
||||
deployed_tag = None
|
||||
deployed_commit = None
|
||||
try:
|
||||
version_url = f"{instance.main_url.rstrip('/')}/api/version"
|
||||
resp = requests.get(version_url, timeout=5)
|
||||
if resp.status_code == 200:
|
||||
version_data = resp.json()
|
||||
deployed_version = version_data.get('version', 'unknown')
|
||||
deployed_tag = version_data.get('tag', 'unknown')
|
||||
deployed_commit = version_data.get('commit', 'unknown')
|
||||
except Exception as e:
|
||||
deployed_version = None
|
||||
deployed_tag = None
|
||||
deployed_commit = None
|
||||
|
||||
instance.deployed_version = deployed_tag or deployed_version or 'unknown'
|
||||
instance.deployed_branch = instance.deployed_branch or 'master'
|
||||
|
||||
# 3. Check latest version from Gitea (if settings available)
|
||||
latest_version = None
|
||||
deployed_branch = instance.deployed_branch or 'master'
|
||||
if gitea_url and gitea_token and gitea_repo:
|
||||
try:
|
||||
headers = {'Accept': 'application/json', 'Authorization': f'token {gitea_token}'}
|
||||
# Gitea API: /api/v1/repos/{owner}/{repo}/commits/{branch}
|
||||
commit_url = f"{gitea_url}/api/v1/repos/{gitea_repo}/commits/{deployed_branch}"
|
||||
commit_resp = requests.get(commit_url, headers=headers, timeout=5)
|
||||
if commit_resp.status_code == 200:
|
||||
latest_version = commit_resp.json().get('sha')
|
||||
except Exception as e:
|
||||
latest_version = None
|
||||
instance.latest_version = latest_version or 'unknown'
|
||||
instance.version_checked_at = datetime.utcnow()
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Get connection settings
|
||||
portainer_settings = KeyValueSettings.get_value('portainer_settings')
|
||||
nginx_settings = KeyValueSettings.get_value('nginx_settings')
|
||||
git_settings = KeyValueSettings.get_value('git_settings')
|
||||
cloudflare_settings = KeyValueSettings.get_value('cloudflare_settings')
|
||||
|
||||
return render_template('main/instances.html',
|
||||
@@ -1975,4 +2015,36 @@ def init_routes(main_bp):
|
||||
flash('This page is only available in master instances.', 'error')
|
||||
return redirect(url_for('main.dashboard'))
|
||||
|
||||
return render_template('wiki/base.html')
|
||||
return render_template('wiki/base.html')
|
||||
|
||||
@main_bp.route('/api/version')
|
||||
def api_version():
|
||||
version_file = os.path.join(current_app.root_path, 'version.txt')
|
||||
version = 'unknown'
|
||||
version_data = {}
|
||||
|
||||
if os.path.exists(version_file):
|
||||
with open(version_file, 'r') as f:
|
||||
content = f.read().strip()
|
||||
|
||||
# Try to parse as JSON first (new format)
|
||||
try:
|
||||
version_data = json.loads(content)
|
||||
version = version_data.get('tag', 'unknown')
|
||||
except json.JSONDecodeError:
|
||||
# Fallback to old format (just commit hash)
|
||||
version = content
|
||||
version_data = {
|
||||
'tag': 'unknown',
|
||||
'commit': content,
|
||||
'branch': 'unknown',
|
||||
'deployed_at': 'unknown'
|
||||
}
|
||||
|
||||
return jsonify({
|
||||
'version': version,
|
||||
'tag': version_data.get('tag', 'unknown'),
|
||||
'commit': version_data.get('commit', 'unknown'),
|
||||
'branch': version_data.get('branch', 'unknown'),
|
||||
'deployed_at': version_data.get('deployed_at', 'unknown')
|
||||
})
|
||||
Reference in New Issue
Block a user