Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions backend/src/routes/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,58 @@ pub async fn install_update(
State(_state): State<AppState>,
Json(payload): Json<UpdateRequest>,
) -> Result<Json<UpdateResponse>, 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(&current_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(),
}));
}

// Find the update script - try multiple locations
let mut possible_paths: Vec<std::path::PathBuf> =
vec![std::path::PathBuf::from("/opt/csf-core/scripts/update.sh")];
let mut possible_paths: Vec<std::path::PathBuf> = 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);

Expand Down Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"

Expand Down
Loading