Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pacmancoder committed Jun 21, 2024
1 parent 4a8ecee commit bfe3e54
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
Binary file modified devolutions-gateway/openapi/gateway-api.yaml
Binary file not shown.
Binary file modified devolutions-gateway/openapi/subscriber-api.yaml
Binary file not shown.
2 changes: 1 addition & 1 deletion devolutions-gateway/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn make_router<S>(state: crate::DgwState) -> axum::Router<S> {
.nest("/jet/fwd", fwd::make_router(state.clone()))
.nest("/jet/webapp", webapp::make_router(state.clone()))
.nest("/jet/net", net::make_router(state.clone()))
.route("/jet/update", axum::routing::get(update::start_update));
.route("/jet/update", axum::routing::post(update::trigger_update_check));

if state.conf_handle.get_conf().web_app.enabled {
router = router.route(
Expand Down
32 changes: 19 additions & 13 deletions devolutions-gateway/src/api/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ pub(crate) struct UpdateQueryParam {
#[derive(Serialize)]
pub(crate) struct UpdateResponse {}

/// Starts Devolutions Gateway update process
/// Triggers Devolutions Gateway update process.
///
/// This is done via updating `Agent/update.json` file, which is then read by Devolutions Agent
/// when changes are detected. If the version written to `update.json` is indeed higher than the
/// currently installed version, Devolutions Agent will proceed with the update process.
#[cfg_attr(feature = "openapi", utoipa::path(
get,
post,
operation_id = "Update",
tag = "Update",
path = "/jet/update",
Expand All @@ -33,7 +37,7 @@ pub(crate) struct UpdateResponse {}
),
security(("scope_token" = ["gateway.update"])),
))]
pub(super) async fn start_update(
pub(super) async fn trigger_update_check(
Query(query): Query<UpdateQueryParam>,
_scope: UpdateScope,
) -> Result<Json<UpdateResponse>, HttpError> {
Expand All @@ -42,22 +46,24 @@ pub(super) async fn start_update(
let updater_file_path = get_updater_file_path();

if !updater_file_path.exists() {
error!("Failed to start Gateway update, `update.json` does not exist (should be created by Devolutions Agent)");

return Err(HttpErrorBuilder::new(StatusCode::SERVICE_UNAVAILABLE).msg("Agent updater service is unavailable"));
return Err(HttpErrorBuilder::new(StatusCode::SERVICE_UNAVAILABLE).msg("Agent updater service is not installed"));
}

let update_json = UpdateJson {
gateway: Some(ProductUpdateInfo { target_version }),
};

serde_json::to_string(&update_json)
.ok()
.and_then(|serialized| std::fs::write(updater_file_path, serialized).ok())
.ok_or_else(|| {
error!("Failed to write new Gateway version to `update.json`");
HttpErrorBuilder::new(StatusCode::INTERNAL_SERVER_ERROR).msg("Agent updater service is unavailable")
})?;
let update_json = serde_json::to_string(&update_json).map_err(
HttpError::internal()
.with_msg("failed to serialize the update manifest")
.err(),
)?;

std::fs::write(updater_file_path, update_json).map_err(
HttpError::internal()
.with_msg("failed to write the new `update.json` manifest on disk")
.err(),
)?;

Ok(Json(UpdateResponse {}))
}
2 changes: 1 addition & 1 deletion devolutions-gateway/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use uuid::Uuid;
crate::api::jrec::pull_recording_file,
crate::api::webapp::sign_app_token,
crate::api::webapp::sign_session_token,
crate::api::update::start_update,
crate::api::update::trigger_update_check,
// crate::api::net::get_net_config,
),
components(schemas(
Expand Down

0 comments on commit bfe3e54

Please sign in to comment.