improved launch process using cloudflare
This commit is contained in:
@@ -848,6 +848,125 @@ def deploy_stack():
|
||||
current_app.logger.error(f"Error deploying stack: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@launch_api.route('/check-stack-status', methods=['POST'])
|
||||
@csrf.exempt
|
||||
def check_stack_status():
|
||||
try:
|
||||
data = request.get_json()
|
||||
if not data or 'stack_name' not in data:
|
||||
return jsonify({'error': 'Missing stack_name field'}), 400
|
||||
|
||||
# Get Portainer settings
|
||||
portainer_settings = KeyValueSettings.get_value('portainer_settings')
|
||||
if not portainer_settings:
|
||||
return jsonify({'error': 'Portainer settings not configured'}), 400
|
||||
|
||||
# Get Portainer endpoint ID (assuming it's the first endpoint)
|
||||
endpoint_response = requests.get(
|
||||
f"{portainer_settings['url'].rstrip('/')}/api/endpoints",
|
||||
headers={
|
||||
'X-API-Key': portainer_settings['api_key'],
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
timeout=30
|
||||
)
|
||||
|
||||
if not endpoint_response.ok:
|
||||
return jsonify({'error': 'Failed to get Portainer endpoints'}), 500
|
||||
|
||||
endpoints = endpoint_response.json()
|
||||
if not endpoints:
|
||||
return jsonify({'error': 'No Portainer endpoints found'}), 400
|
||||
|
||||
endpoint_id = endpoints[0]['Id']
|
||||
|
||||
# Get stack information
|
||||
stacks_url = f"{portainer_settings['url'].rstrip('/')}/api/stacks"
|
||||
stacks_response = requests.get(
|
||||
stacks_url,
|
||||
headers={
|
||||
'X-API-Key': portainer_settings['api_key'],
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
params={'filters': json.dumps({'Name': data['stack_name']})},
|
||||
timeout=30
|
||||
)
|
||||
|
||||
if not stacks_response.ok:
|
||||
return jsonify({'error': 'Failed to get stack information'}), 500
|
||||
|
||||
stacks = stacks_response.json()
|
||||
target_stack = None
|
||||
|
||||
for stack in stacks:
|
||||
if stack['Name'] == data['stack_name']:
|
||||
target_stack = stack
|
||||
break
|
||||
|
||||
if not target_stack:
|
||||
return jsonify({'error': f'Stack {data["stack_name"]} not found'}), 404
|
||||
|
||||
# Get stack services to check their status
|
||||
services_url = f"{portainer_settings['url'].rstrip('/')}/api/endpoints/{endpoint_id}/docker/services"
|
||||
services_response = requests.get(
|
||||
services_url,
|
||||
headers={
|
||||
'X-API-Key': portainer_settings['api_key'],
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
params={'filters': json.dumps({'label': f'com.docker.stack.namespace={data["stack_name"]}'})},
|
||||
timeout=30
|
||||
)
|
||||
|
||||
if not services_response.ok:
|
||||
return jsonify({'error': 'Failed to get stack services'}), 500
|
||||
|
||||
services = services_response.json()
|
||||
|
||||
# Check if all services are running
|
||||
all_running = True
|
||||
service_statuses = []
|
||||
|
||||
for service in services:
|
||||
replicas_running = service.get('Spec', {}).get('Mode', {}).get('Replicated', {}).get('Replicas', 0)
|
||||
replicas_actual = service.get('ServiceStatus', {}).get('RunningTasks', 0)
|
||||
|
||||
service_status = {
|
||||
'name': service.get('Spec', {}).get('Name', 'Unknown'),
|
||||
'replicas_expected': replicas_running,
|
||||
'replicas_running': replicas_actual,
|
||||
'status': 'running' if replicas_actual >= replicas_running else 'not_running'
|
||||
}
|
||||
|
||||
service_statuses.append(service_status)
|
||||
|
||||
if replicas_actual < replicas_running:
|
||||
all_running = False
|
||||
|
||||
# Determine overall stack status
|
||||
if all_running and len(services) > 0:
|
||||
status = 'active'
|
||||
elif len(services) > 0:
|
||||
status = 'partial'
|
||||
else:
|
||||
status = 'inactive'
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'data': {
|
||||
'stack_name': data['stack_name'],
|
||||
'stack_id': target_stack['Id'],
|
||||
'status': status,
|
||||
'services': service_statuses,
|
||||
'total_services': len(services),
|
||||
'running_services': len([s for s in service_statuses if s['status'] == 'running'])
|
||||
}
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error checking stack status: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@launch_api.route('/save-instance', methods=['POST'])
|
||||
@csrf.exempt
|
||||
def save_instance():
|
||||
@@ -1559,18 +1678,26 @@ def copy_smtp_settings():
|
||||
if not jwt_token:
|
||||
return jsonify({'error': 'No JWT token received'}), 400
|
||||
|
||||
# Prepare SMTP settings data for the API
|
||||
api_smtp_data = {
|
||||
'smtp_host': smtp_settings.get('smtp_host'),
|
||||
'smtp_port': smtp_settings.get('smtp_port'),
|
||||
'smtp_username': smtp_settings.get('smtp_username'),
|
||||
'smtp_password': smtp_settings.get('smtp_password'),
|
||||
'smtp_security': smtp_settings.get('smtp_security'),
|
||||
'smtp_from_email': smtp_settings.get('smtp_from_email'),
|
||||
'smtp_from_name': smtp_settings.get('smtp_from_name')
|
||||
}
|
||||
|
||||
# Copy SMTP settings to the launched instance
|
||||
smtp_response = requests.post(
|
||||
f"{instance_url.rstrip('/')}/api/admin/key-value",
|
||||
smtp_response = requests.put(
|
||||
f"{instance_url.rstrip('/')}/api/admin/settings",
|
||||
headers={
|
||||
'Authorization': f'Bearer {jwt_token}',
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
json={
|
||||
'key': 'smtp_settings',
|
||||
'value': smtp_settings
|
||||
},
|
||||
json=api_smtp_data,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
@@ -1582,7 +1709,7 @@ def copy_smtp_settings():
|
||||
|
||||
return jsonify({
|
||||
'message': 'SMTP settings copied successfully',
|
||||
'data': smtp_settings
|
||||
'data': api_smtp_data
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user