download docker compose during launch process
This commit is contained in:
@@ -13,6 +13,7 @@ import secrets
|
|||||||
from flask_login import login_user
|
from flask_login import login_user
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
|
import base64
|
||||||
|
|
||||||
admin_api = Blueprint('admin_api', __name__)
|
admin_api = Blueprint('admin_api', __name__)
|
||||||
|
|
||||||
@@ -1156,3 +1157,77 @@ def create_ssl_certificate(current_user):
|
|||||||
'success': False,
|
'success': False,
|
||||||
'error': str(e)
|
'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
|
||||||
@@ -228,6 +228,18 @@ function initializeSteps() {
|
|||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
stepsContainer.appendChild(portainerStep);
|
stepsContainer.appendChild(portainerStep);
|
||||||
|
|
||||||
|
// Add Docker Compose download step
|
||||||
|
const dockerComposeStep = document.createElement('div');
|
||||||
|
dockerComposeStep.className = 'step-item';
|
||||||
|
dockerComposeStep.innerHTML = `
|
||||||
|
<div class="step-icon"><i class="fas fa-file-code"></i></div>
|
||||||
|
<div class="step-content">
|
||||||
|
<h5>Downloading Docker Compose</h5>
|
||||||
|
<p class="step-status">Fetching docker-compose.yml from repository...</p>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
stepsContainer.appendChild(dockerComposeStep);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function startLaunch(data) {
|
async function startLaunch(data) {
|
||||||
@@ -336,6 +348,37 @@ async function startLaunch(data) {
|
|||||||
portainerStep.classList.add('completed');
|
portainerStep.classList.add('completed');
|
||||||
portainerStep.querySelector('.step-status').textContent = 'Successfully connected to Portainer';
|
portainerStep.querySelector('.step-status').textContent = 'Successfully connected to Portainer';
|
||||||
|
|
||||||
|
// Step 6: Download Docker Compose
|
||||||
|
await updateStep(6, 'Downloading Docker Compose', 'Fetching docker-compose.yml from repository...');
|
||||||
|
const dockerComposeResult = await downloadDockerCompose(data.repository, data.branch);
|
||||||
|
|
||||||
|
if (!dockerComposeResult.success) {
|
||||||
|
throw new Error(dockerComposeResult.error || 'Failed to download docker-compose.yml');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the step to show success
|
||||||
|
const dockerComposeStep = document.querySelectorAll('.step-item')[5];
|
||||||
|
dockerComposeStep.classList.remove('active');
|
||||||
|
dockerComposeStep.classList.add('completed');
|
||||||
|
dockerComposeStep.querySelector('.step-status').textContent = 'Successfully downloaded docker-compose.yml';
|
||||||
|
|
||||||
|
// Add download button
|
||||||
|
const downloadButton = document.createElement('button');
|
||||||
|
downloadButton.className = 'btn btn-sm btn-primary mt-2';
|
||||||
|
downloadButton.innerHTML = '<i class="fas fa-download me-1"></i> Download docker-compose.yml';
|
||||||
|
downloadButton.onclick = () => {
|
||||||
|
const blob = new Blob([dockerComposeResult.content], { type: 'text/yaml' });
|
||||||
|
const url = window.URL.createObjectURL(blob);
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = url;
|
||||||
|
a.download = 'docker-compose.yml';
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
window.URL.revokeObjectURL(url);
|
||||||
|
document.body.removeChild(a);
|
||||||
|
};
|
||||||
|
dockerComposeStep.querySelector('.step-content').appendChild(downloadButton);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showError(error.message);
|
showError(error.message);
|
||||||
}
|
}
|
||||||
@@ -962,5 +1005,39 @@ function retryLaunch() {
|
|||||||
// Reload the page to start over
|
// Reload the page to start over
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add new function to download docker-compose.yml
|
||||||
|
async function downloadDockerCompose(repo, branch) {
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/admin/download-docker-compose', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
repository: repo,
|
||||||
|
branch: branch
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json();
|
||||||
|
throw new Error(error.message || 'Failed to download docker-compose.yml');
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await response.json();
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
content: result.content
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error downloading docker-compose.yml:', error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: error.message
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user