|
1 | 1 | //! Endpoint root. |
| 2 | +
|
| 3 | +use api_framework::{ |
| 4 | + framework::{ |
| 5 | + StateError, StateResult, |
| 6 | + queued_async::{QueuedAsyncFramework, QueuedAsyncFrameworkContext}, |
| 7 | + }, |
| 8 | + static_lazy_lock, |
| 9 | +}; |
| 10 | + |
| 11 | +use axum::{Json, http::StatusCode, response::IntoResponse}; |
| 12 | +use config_file::FromConfigFile; |
| 13 | +use serde::Deserialize; |
| 14 | + |
| 15 | +use crate::{ |
| 16 | + config::{ |
| 17 | + Config, |
| 18 | + services::{ServiceConfig, ServicesConfig}, |
| 19 | + }, |
| 20 | + env::DOCKER_WORKSPACE_DIR, |
| 21 | + transactions, |
| 22 | +}; |
| 23 | + |
| 24 | +static_lazy_lock! { |
| 25 | + QUEUED_ASYNC: QueuedAsyncFramework<String> = QueuedAsyncFramework::new(); |
| 26 | +} |
| 27 | + |
| 28 | +/// The payload of the post. |
| 29 | +#[derive(Debug, Clone, Deserialize)] |
| 30 | +pub struct PostPayload { |
| 31 | + /// The label of the service to update. |
| 32 | + pub service_label: String, |
| 33 | +} |
| 34 | + |
| 35 | +/// The client posted an update request. |
| 36 | +/// Responds with [`StatusCode::OK`] right after the deployment is triggered. |
| 37 | +/// |
| 38 | +/// See: [`PostPayload`], [`post_transaction`] |
| 39 | +pub async fn post(Json(payload): Json<PostPayload>) -> impl IntoResponse { |
| 40 | + let services = match ServicesConfig::from_config_file(ServicesConfig::file().path()) { |
| 41 | + Ok(s) => s, |
| 42 | + Err(e) => { |
| 43 | + tracing::error!("failed to read services config: {e:?}"); |
| 44 | + return ( |
| 45 | + StatusCode::INTERNAL_SERVER_ERROR, |
| 46 | + format!("failed to read services config: {e:?}"), |
| 47 | + ) |
| 48 | + .into_response(); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + let service = match services |
| 53 | + .services |
| 54 | + .iter() |
| 55 | + .cloned() |
| 56 | + .find(|s| s.service_label == payload.service_label) |
| 57 | + { |
| 58 | + Some(s) => s, |
| 59 | + None => { |
| 60 | + return ( |
| 61 | + StatusCode::BAD_REQUEST, |
| 62 | + format!("service with label '{}' not found", payload.service_label), |
| 63 | + ) |
| 64 | + .into_response(); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + tokio::spawn(QUEUED_ASYNC.run(payload.service_label.clone(), move |cx| { |
| 69 | + Box::pin(post_transaction(cx, payload.clone(), service.clone())) |
| 70 | + })); |
| 71 | + |
| 72 | + StatusCode::OK.into_response() |
| 73 | +} |
| 74 | + |
| 75 | +async fn post_transaction( |
| 76 | + cx: QueuedAsyncFrameworkContext, |
| 77 | + _payload: PostPayload, |
| 78 | + service: ServiceConfig, |
| 79 | +) -> StateResult<()> { |
| 80 | + async fn inner(cx: &QueuedAsyncFrameworkContext, service: &ServiceConfig) -> StateResult<()> { |
| 81 | + // cd to workspace |
| 82 | + transactions::sys::cd(&*DOCKER_WORKSPACE_DIR) |
| 83 | + .await |
| 84 | + .map_err(|_| StateError::Retry)?; |
| 85 | + |
| 86 | + // login to docker |
| 87 | + transactions::docker::login() |
| 88 | + .await |
| 89 | + .map_err(|_| StateError::Retry)?; |
| 90 | + |
| 91 | + cx.check(())?; |
| 92 | + |
| 93 | + // pull image |
| 94 | + transactions::docker::pull_image(&service.image) |
| 95 | + .await |
| 96 | + .map_err(|_| StateError::Retry)?; |
| 97 | + |
| 98 | + cx.check(())?; |
| 99 | + |
| 100 | + // up container |
| 101 | + transactions::docker::compose_up(&service.container_name) |
| 102 | + .await |
| 103 | + .map_err(|_| StateError::Retry)?; |
| 104 | + |
| 105 | + Ok(()) |
| 106 | + } |
| 107 | + |
| 108 | + match inner(&cx, &service).await { |
| 109 | + Ok(_) => { |
| 110 | + tracing::info!("successfully updated service {}", service.service_label); |
| 111 | + Ok(()) |
| 112 | + } |
| 113 | + err => { |
| 114 | + tracing::error!("failed to update service {}", service.service_label); |
| 115 | + err |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments