diff --git a/backend/src/routes/updates.rs b/backend/src/routes/updates.rs index 3a150d36..b287db7d 100644 --- a/backend/src/routes/updates.rs +++ b/backend/src/routes/updates.rs @@ -122,10 +122,21 @@ pub async fn install_update( State(_state): State, Json(payload): Json, ) -> Result, AppError> { + tracing::info!( + "Update installation requested for version: {}", + payload.version + ); + let current_version = env!("CARGO_PKG_VERSION").to_string(); + tracing::info!("Current version: {}", current_version); // Safety check: don't downgrade if !version_compare(¤t_version, &payload.version) { + tracing::warn!( + "Update rejected: Cannot install version {} (current: {})", + payload.version, + current_version + ); return Ok(Json(UpdateResponse { success: false, message: "Cannot install an older or same version".to_string(), @@ -133,21 +144,36 @@ pub async fn install_update( } // Find the update script - try multiple locations - let mut possible_paths: Vec = - vec![std::path::PathBuf::from("/opt/csf-core/scripts/update.sh")]; + let mut possible_paths: Vec = vec![ + // Production path (daemon service) + std::path::PathBuf::from("/opt/csf-core/scripts/update.sh"), + ]; + // Development paths if let Ok(dir) = std::env::current_dir() { + // When running from /opt/csf-core/backend possible_paths.push(dir.join("../scripts/update.sh")); + // When running from project root possible_paths.push(dir.join("scripts/update.sh")); + // When running from backend directory + possible_paths.push(dir.join("../../scripts/update.sh")); } - let script_path = possible_paths - .iter() - .find(|&p: &&std::path::PathBuf| p.exists()) - .ok_or_else(|| { - AppError::InternalError("Update script not found in any expected location".to_string()) - })? - .clone(); + tracing::debug!("Searching for update script in: {:?}", possible_paths); + + let script_path = match possible_paths.iter().find(|&p| p.exists()) { + Some(path) => path.clone(), + None => { + let error_msg = format!( + "Update script not found in any expected location. Searched paths: {:?}. Note: Updates can only be performed in production installations, not during local development.", + possible_paths + ); + tracing::error!("{}", error_msg); + return Err(AppError::InternalError( + "Update functionality is only available in production installations. The update script was not found on this system.".to_string() + )); + } + }; tracing::info!("Found update script at: {:?}", script_path); @@ -268,9 +294,15 @@ pub enum AppError { impl IntoResponse for AppError { fn into_response(self) -> Response { - let (status, message) = match self { - AppError::InternalError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg), - AppError::NotFound(msg) => (StatusCode::NOT_FOUND, msg), + let (status, message) = match &self { + AppError::InternalError(msg) => { + tracing::error!("Internal error: {}", msg); + (StatusCode::INTERNAL_SERVER_ERROR, msg.clone()) + } + AppError::NotFound(msg) => { + tracing::warn!("Not found: {}", msg); + (StatusCode::NOT_FOUND, msg.clone()) + } }; (status, Json(serde_json::json!({ "error": message }))).into_response() diff --git a/scripts/install.sh b/scripts/install.sh index bad4de17..466fc59a 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -424,6 +424,18 @@ download_release() { return fi + # Download update script + local update_script_url="https://raw.githubusercontent.com/${GITHUB_REPO}/main/scripts/update.sh" + print_step "Download Update Script..." + + mkdir -p "$INSTALL_DIR/scripts" + if curl -L -f "$update_script_url" -o "$INSTALL_DIR/scripts/update.sh" 2>/dev/null; then + chmod +x "$INSTALL_DIR/scripts/update.sh" + print_success "Update Script heruntergeladen" + else + print_warning "Update Script konnte nicht heruntergeladen werden" + fi + cd - > /dev/null rm -rf "$temp_dir" @@ -635,6 +647,18 @@ EOF return fi + # Copy update script + cd "$temp_dir/csf-core" + print_step "Kopiere Update Script..." + mkdir -p "$INSTALL_DIR/scripts" + if [ -f "scripts/update.sh" ]; then + cp scripts/update.sh "$INSTALL_DIR/scripts/" + chmod +x "$INSTALL_DIR/scripts/update.sh" + print_success "Update Script kopiert" + else + print_warning "Update Script nicht gefunden" + fi + cd - > /dev/null rm -rf "$temp_dir"