Skip to content

Commit af70fbd

Browse files
authored
feat: call API delete publish endpoint in termination process (#28)
* feat: call API delete publish endpoint in termination process Signed-off-by: Joerg Zeidler <[email protected]> * add sigterm handling Signed-off-by: Joerg Zeidler <[email protected]> * qa Signed-off-by: Joerg Zeidler <[email protected]> * qa Signed-off-by: Joerg Zeidler <[email protected]> --------- Signed-off-by: Joerg Zeidler <[email protected]>
1 parent 62a9577 commit af70fbd

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
77
name = "omnect-ui"
88
readme = "README.md"
99
repository = "[email protected]:omnect/omnect-ui.git"
10-
version = "0.9.2"
10+
version = "0.9.3"
1111
build = "src/build.rs"
1212

1313
[dependencies]

build-and-run-image.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ docker build \
2020
docker run --rm \
2121
-v $(pwd)/temp:/cert \
2222
-v /tmp:/socket \
23+
-v $(pwd)/temp/data:/data \
2324
-u $(id -u):$(id -g) \
2425
-e RUST_LOG=debug \
2526
-e UI_PORT=1977 \
2627
-e SOCKET_PATH=/socket/api.sock \
2728
-e CENTRIFUGO_ADMIN_ENABLED=true \
2829
-e CENTRIFUGO_ADMIN_PASSWORD=123 \
2930
-e CENTRIFUGO_ADMIN_SECRET=123 \
30-
-e DATA_DIR_PATH=$(pwd)/temp/data \
31+
-e DATA_DIR_PATH=/data \
3132
-p "${omnect_ui_port}":"${omnect_ui_port}" \
3233
-p "${centrifugo_port}":"${centrifugo_port}" \
3334
omnect-ui-x86:"local_${omnect_ui_version}"

src/main.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use std::fs::File;
2525
use std::io::Write;
2626
use std::{fs, path::Path};
2727
use tokio::process::Command;
28+
use tokio::signal::unix::{signal, SignalKind};
2829
use uuid::Uuid;
2930

3031
const UPLOAD_LIMIT_BYTES: usize = 250 * 1024 * 1024;
@@ -185,8 +186,6 @@ async fn main() {
185186
let index_html =
186187
std::fs::canonicalize("static/index.html").expect("static/index.html not found");
187188

188-
send_publish_endpoint(&centrifugo_http_api_key, &ods_socket_path).await;
189-
190189
fs::exists(&ods_socket_path).unwrap_or_else(|_| {
191190
panic!(
192191
"omnect device service socket file {} does not exist",
@@ -197,8 +196,10 @@ async fn main() {
197196
fs::exists(&update_os_path!())
198197
.unwrap_or_else(|_| panic!("path {} for os update does not exist", &update_os_path!()));
199198

199+
send_publish_endpoint(&centrifugo_http_api_key, &ods_socket_path).await;
200+
200201
let api_config = Api {
201-
ods_socket_path,
202+
ods_socket_path: ods_socket_path.clone(),
202203
update_os_path: update_os_path!(),
203204
centrifugo_client_token_hmac_secret_key,
204205
index_html,
@@ -280,9 +281,17 @@ async fn main() {
280281

281282
debug!("centrifugo pid: {}", centrifugo.id().unwrap());
282283

284+
let mut sigterm = signal(SignalKind::terminate()).expect("Failed to install SIGTERM handler");
285+
283286
tokio::select! {
284287
_ = tokio::signal::ctrl_c() => {
285288
debug!("ctrl-c");
289+
delete_publish_endpoint(&ods_socket_path).await;
290+
server_handle.stop(true).await;
291+
},
292+
_ = sigterm.recv() => {
293+
debug!("SIGTERM received");
294+
delete_publish_endpoint(&ods_socket_path).await;
286295
server_handle.stop(true).await;
287296
},
288297
_ = server_task => {
@@ -391,6 +400,19 @@ async fn send_publish_endpoint(
391400
HttpResponse::Ok().finish()
392401
}
393402

403+
async fn delete_publish_endpoint(ods_socket_path: &str) -> impl Responder {
404+
let path = format!(
405+
"/publish-endpoint/v1/{}",
406+
String::from(env!("CARGO_PKG_NAME"))
407+
);
408+
if let Err(e) = socket_client::delete_with_empty_body(&path, ods_socket_path).await {
409+
error!("deleting publish endpoint failed: {e:#}");
410+
HttpResponse::InternalServerError().finish();
411+
}
412+
413+
HttpResponse::Ok().finish()
414+
}
415+
394416
pub fn validate_password(password: &str) -> Result<()> {
395417
if password.is_empty() {
396418
error!("password is empty");

src/socket_client.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub async fn post_with_json_body(
2727
.body(serde_json::to_string(&json).unwrap_or_default())
2828
.context("build request failed")?;
2929

30-
post(request, socket_path).await
30+
send_request(request, socket_path).await
3131
}
3232

3333
pub async fn post_with_empty_body(path: &str, socket_path: &str) -> Result<HttpResponse> {
@@ -38,10 +38,21 @@ pub async fn post_with_empty_body(path: &str, socket_path: &str) -> Result<HttpR
3838
.body(String::new())
3939
.context("build request failed")?;
4040

41-
post(request, socket_path).await
41+
send_request(request, socket_path).await
4242
}
4343

44-
async fn post(request: Request<String>, socket_path: &str) -> Result<HttpResponse> {
44+
pub async fn delete_with_empty_body(path: &str, socket_path: &str) -> Result<HttpResponse> {
45+
let request = Request::builder()
46+
.uri(path)
47+
.method("DELETE")
48+
.header("Host", "localhost")
49+
.body(String::new())
50+
.context("build request failed")?;
51+
52+
send_request(request, socket_path).await
53+
}
54+
55+
async fn send_request(request: Request<String>, socket_path: &str) -> Result<HttpResponse> {
4556
let mut sender = match sender(socket_path).await {
4657
Err(e) => {
4758
error!("error creating request sender: {e}. socket might be broken. exit application");

0 commit comments

Comments
 (0)