add branch validation to queue
This commit is contained in:
@@ -13,36 +13,119 @@ jobs_bp = Blueprint('jobs', __name__, url_prefix='/jobs')
|
|||||||
def submit():
|
def submit():
|
||||||
return render_template('jobs/submit.html')
|
return render_template('jobs/submit.html')
|
||||||
|
|
||||||
|
@jobs_bp.route('/debug/ssh-test')
|
||||||
|
@login_required
|
||||||
|
def debug_ssh_test():
|
||||||
|
"""Debug endpoint to test SSH connectivity"""
|
||||||
|
if not current_user.is_admin:
|
||||||
|
return jsonify({'error': 'Admin access required'}), 403
|
||||||
|
|
||||||
|
try:
|
||||||
|
ssh_password = os.environ.get('SSH_PASSWORD', 'default_password')
|
||||||
|
ssh_host = os.environ.get('SSH_HOST', 'remote_host')
|
||||||
|
ssh_user = os.environ.get('SSH_USER', 'asf')
|
||||||
|
|
||||||
|
# Test basic SSH connectivity
|
||||||
|
test_cmd = f"sshpass -p '{ssh_password}' ssh -o StrictHostKeyChecking=no {ssh_user}@{ssh_host} 'echo SSH_CONNECTION_SUCCESS'"
|
||||||
|
result = subprocess.run(test_cmd, shell=True, capture_output=True, text=True, timeout=30)
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
'ssh_config': {
|
||||||
|
'user': ssh_user,
|
||||||
|
'host': ssh_host,
|
||||||
|
'password_set': bool(ssh_password and ssh_password != 'default_password')
|
||||||
|
},
|
||||||
|
'test_result': {
|
||||||
|
'returncode': result.returncode,
|
||||||
|
'stdout': result.stdout,
|
||||||
|
'stderr': result.stderr
|
||||||
|
},
|
||||||
|
'success': result.returncode == 0 and 'SSH_CONNECTION_SUCCESS' in result.stdout
|
||||||
|
})
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({
|
||||||
|
'error': str(e),
|
||||||
|
'success': False
|
||||||
|
})
|
||||||
|
|
||||||
@jobs_bp.route('/submit/step1', methods=['POST'])
|
@jobs_bp.route('/submit/step1', methods=['POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def submit_step1():
|
def submit_step1():
|
||||||
branch_name = request.form.get('branch_name')
|
branch_name = request.form.get('branch_name')
|
||||||
|
|
||||||
if not branch_name:
|
if not branch_name:
|
||||||
flash('Branch name is required', 'error')
|
return jsonify({
|
||||||
return redirect(url_for('jobs.submit'))
|
'success': False,
|
||||||
|
'error': 'Branch name is required',
|
||||||
|
'output': ''
|
||||||
|
})
|
||||||
|
|
||||||
# Validate branch exists on remote
|
# Validate branch exists on remote
|
||||||
try:
|
try:
|
||||||
# Get SSH password from environment variable
|
# Get SSH configuration from environment variables
|
||||||
ssh_password = os.environ.get('SSH_PASSWORD', 'default_password')
|
ssh_password = os.environ.get('SSH_PASSWORD', 'default_password')
|
||||||
ssh_host = os.environ.get('SSH_HOST', 'remote_host')
|
ssh_host = os.environ.get('SSH_HOST', 'remote_host')
|
||||||
ssh_user = os.environ.get('SSH_USER', 'asf')
|
ssh_user = os.environ.get('SSH_USER', 'asf')
|
||||||
|
|
||||||
|
print(f"[DEBUG] Starting branch validation for: {branch_name}")
|
||||||
|
print(f"[DEBUG] SSH Config - User: {ssh_user}, Host: {ssh_host}")
|
||||||
|
|
||||||
# First, clone the repository
|
# First, clone the repository
|
||||||
clone_cmd = f"sshpass -p '{ssh_password}' ssh -o StrictHostKeyChecking=no {ssh_user}@{ssh_host} './TPF/gitea_repo_controller.sh clone'"
|
clone_cmd = f"sshpass -p '{ssh_password}' ssh -o StrictHostKeyChecking=no {ssh_user}@{ssh_host} './TPF/gitea_repo_controller.sh clone'"
|
||||||
|
print(f"[DEBUG] Executing clone command: {clone_cmd.replace(ssh_password, '***')}")
|
||||||
|
|
||||||
clone_result = subprocess.run(clone_cmd, shell=True, capture_output=True, text=True, timeout=60)
|
clone_result = subprocess.run(clone_cmd, shell=True, capture_output=True, text=True, timeout=60)
|
||||||
|
|
||||||
|
print(f"[DEBUG] Clone result - Return code: {clone_result.returncode}")
|
||||||
|
print(f"[DEBUG] Clone stdout: {clone_result.stdout}")
|
||||||
|
print(f"[DEBUG] Clone stderr: {clone_result.stderr}")
|
||||||
|
|
||||||
# Then, checkout the branch
|
# Then, checkout the branch
|
||||||
checkout_cmd = f"sshpass -p '{ssh_password}' ssh -o StrictHostKeyChecking=no {ssh_user}@{ssh_host} './TPF/gitea_repo_controller.sh checkout {branch_name}'"
|
checkout_cmd = f"sshpass -p '{ssh_password}' ssh -o StrictHostKeyChecking=no {ssh_user}@{ssh_host} './TPF/gitea_repo_controller.sh checkout {branch_name}'"
|
||||||
|
print(f"[DEBUG] Executing checkout command: {checkout_cmd.replace(ssh_password, '***')}")
|
||||||
|
|
||||||
checkout_result = subprocess.run(checkout_cmd, shell=True, capture_output=True, text=True, timeout=60)
|
checkout_result = subprocess.run(checkout_cmd, shell=True, capture_output=True, text=True, timeout=60)
|
||||||
|
|
||||||
# Check if checkout was successful (no "fatal:" in output)
|
print(f"[DEBUG] Checkout result - Return code: {checkout_result.returncode}")
|
||||||
if "fatal:" in checkout_result.stdout or "fatal:" in checkout_result.stderr or checkout_result.returncode != 0:
|
print(f"[DEBUG] Checkout stdout: {checkout_result.stdout}")
|
||||||
|
print(f"[DEBUG] Checkout stderr: {checkout_result.stderr}")
|
||||||
|
|
||||||
|
# Combine all output for analysis
|
||||||
|
full_output = f"CLONE OUTPUT:\n{clone_result.stdout}\n{clone_result.stderr}\n\nCHECKOUT OUTPUT:\n{checkout_result.stdout}\n{checkout_result.stderr}"
|
||||||
|
|
||||||
|
# Check if checkout was successful
|
||||||
|
# Look for "fatal:" in the output which indicates failure
|
||||||
|
checkout_failed = (
|
||||||
|
"fatal:" in checkout_result.stdout.lower() or
|
||||||
|
"fatal:" in checkout_result.stderr.lower() or
|
||||||
|
checkout_result.returncode != 0
|
||||||
|
)
|
||||||
|
|
||||||
|
print(f"[DEBUG] Checkout failed: {checkout_failed}")
|
||||||
|
|
||||||
|
if checkout_failed:
|
||||||
|
error_msg = f'Branch "{branch_name}" not found on remote. Please push the branch first.'
|
||||||
|
|
||||||
|
# Try to extract more specific error from output
|
||||||
|
if "fatal:" in checkout_result.stdout:
|
||||||
|
fatal_line = [line for line in checkout_result.stdout.split('\n') if 'fatal:' in line.lower()]
|
||||||
|
if fatal_line:
|
||||||
|
error_msg += f" Error: {fatal_line[0].strip()}"
|
||||||
|
elif "fatal:" in checkout_result.stderr:
|
||||||
|
fatal_line = [line for line in checkout_result.stderr.split('\n') if 'fatal:' in line.lower()]
|
||||||
|
if fatal_line:
|
||||||
|
error_msg += f" Error: {fatal_line[0].strip()}"
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'success': False,
|
'success': False,
|
||||||
'error': f'Branch "{branch_name}" not found on remote. Please push the branch first.',
|
'error': error_msg,
|
||||||
'output': checkout_result.stdout + checkout_result.stderr
|
'output': full_output,
|
||||||
|
'debug': {
|
||||||
|
'clone_returncode': clone_result.returncode,
|
||||||
|
'checkout_returncode': checkout_result.returncode,
|
||||||
|
'branch_name': branch_name
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
# If successful, get available scenarios (mock for now)
|
# If successful, get available scenarios (mock for now)
|
||||||
@@ -54,23 +137,35 @@ def submit_step1():
|
|||||||
'Scenario_5_Security_Test'
|
'Scenario_5_Security_Test'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
print(f"[DEBUG] Branch validation successful for: {branch_name}")
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'success': True,
|
'success': True,
|
||||||
'scenarios': scenarios,
|
'scenarios': scenarios,
|
||||||
'message': f'Branch "{branch_name}" validated successfully'
|
'message': f'Branch "{branch_name}" validated successfully',
|
||||||
|
'output': full_output,
|
||||||
|
'debug': {
|
||||||
|
'clone_returncode': clone_result.returncode,
|
||||||
|
'checkout_returncode': checkout_result.returncode,
|
||||||
|
'branch_name': branch_name
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
except subprocess.TimeoutExpired:
|
except subprocess.TimeoutExpired:
|
||||||
|
error_msg = 'Branch validation timed out. Please try again.'
|
||||||
|
print(f"[ERROR] {error_msg}")
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'success': False,
|
'success': False,
|
||||||
'error': 'Branch validation timed out. Please try again.',
|
'error': error_msg,
|
||||||
'output': ''
|
'output': 'Command timed out after 60 seconds'
|
||||||
})
|
})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
error_msg = f'Error validating branch: {str(e)}'
|
||||||
|
print(f"[ERROR] {error_msg}")
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'success': False,
|
'success': False,
|
||||||
'error': f'Error validating branch: {str(e)}',
|
'error': error_msg,
|
||||||
'output': ''
|
'output': f'Exception: {str(e)}'
|
||||||
})
|
})
|
||||||
|
|
||||||
@jobs_bp.route('/submit/step2-validated', methods=['POST'])
|
@jobs_bp.route('/submit/step2-validated', methods=['POST'])
|
||||||
|
|||||||
@@ -91,6 +91,8 @@ function validateBranch() {
|
|||||||
})
|
})
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
|
console.log('Branch validation response:', data); // Debug log
|
||||||
|
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
// Success
|
// Success
|
||||||
validation.className = 'branch-validation success';
|
validation.className = 'branch-validation success';
|
||||||
@@ -100,6 +102,14 @@ function validateBranch() {
|
|||||||
|
|
||||||
validatedBranch = branchName;
|
validatedBranch = branchName;
|
||||||
availableScenarios = data.scenarios;
|
availableScenarios = data.scenarios;
|
||||||
|
|
||||||
|
// Log debug info
|
||||||
|
if (data.debug) {
|
||||||
|
console.log('Debug info:', data.debug);
|
||||||
|
}
|
||||||
|
if (data.output) {
|
||||||
|
console.log('Command output:', data.output);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Error
|
// Error
|
||||||
validation.className = 'branch-validation error';
|
validation.className = 'branch-validation error';
|
||||||
@@ -109,6 +119,15 @@ function validateBranch() {
|
|||||||
|
|
||||||
validatedBranch = null;
|
validatedBranch = null;
|
||||||
availableScenarios = [];
|
availableScenarios = [];
|
||||||
|
|
||||||
|
// Log debug info for troubleshooting
|
||||||
|
console.error('Branch validation failed:', data.error);
|
||||||
|
if (data.debug) {
|
||||||
|
console.error('Debug info:', data.debug);
|
||||||
|
}
|
||||||
|
if (data.output) {
|
||||||
|
console.error('Command output:', data.output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
|||||||
Reference in New Issue
Block a user