| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use super::{caller, not_found, ApiError, Caller}; |
| 6 | | use crate::auth; |
| 7 | | use crate::backups::{self, Run, Setting}; |
| 8 | | use crate::db::{self, accounts::Account}; |
| 9 | | use crate::web::backups::{setting_of, SettingInput}; |
| 10 | | use crate::Shared; |
| 11 | | use axum::extract::{Path, State}; |
| 12 | | use axum::http::{HeaderMap, StatusCode}; |
| 13 | | use axum::Json; |
| 14 | | use serde::{Deserialize, Serialize}; |
| 15 | |
|
| 16 | |
|
| 17 | | #[derive(Deserialize)] |
| 18 | | pub struct Body { |
| 19 | | pub every: String, |
| 20 | | #[serde(default)] |
| 21 | | pub destination: String, |
| 22 | | |
| 23 | | #[serde(default)] |
| 24 | | pub dir: String, |
| 25 | | |
| 26 | | #[serde(default)] |
| 27 | | pub bucket: String, |
| 28 | | #[serde(default)] |
| 29 | | pub provider: String, |
| 30 | | #[serde(default)] |
| 31 | | pub prefix: String, |
| 32 | | #[serde(default = "thirty")] |
| 33 | | pub keep_days: i64, |
| 34 | | |
| 35 | | #[serde(default)] |
| 36 | | pub s3_bucket: String, |
| 37 | | #[serde(default)] |
| 38 | | pub s3_region: String, |
| 39 | | #[serde(default)] |
| 40 | | pub s3_role: String, |
| 41 | | } |
| 42 | |
|
| 43 | 3× | fn thirty() -> i64 { |
| 44 | 3× | 30 |
| 45 | 3× | } |
| 46 | |
|
| 47 | | #[derive(Serialize)] |
| 48 | | pub struct State_ { |
| 49 | | pub setting: Option<Setting>, |
| 50 | | |
| 51 | | pub place: Option<String>, |
| 52 | | pub runs: Vec<Run>, |
| 53 | | } |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 15× | async fn owner_of(app: &Shared, c: &Caller, org: Option<&str>) -> Result<(Account, String, &'static str), ApiError> { |
| 58 | 15× | let me = c.require("repo:admin")?.clone(); |
| 59 | 14× | match org { |
| 60 | | None => { |
| 61 | 11× | let name = me.username.clone(); |
| 62 | 11× | Ok((me, name, "user")) |
| 63 | | } |
| 64 | 3× | Some(name) => { |
| 65 | 3× | let o = db::accounts::org_by_name(&app.db, name).await?.ok_or_else(|| not_found("no such organisation"))?; |
| 66 | 3× | if !auth::roles::is_org_owner(&app.db, &me, o.id).await? { |
| 67 | 2× | return Err(not_found("no such organisation")); |
| 68 | 1× | } |
| 69 | 1× | Ok((me, o.name, "org")) |
| 70 | | } |
| 71 | | } |
| 72 | 15× | } |
| 73 | |
|
| 74 | 3× | async fn state(app: &Shared, owner: &str) -> Result<State_, ApiError> { |
| 75 | 3× | let setting = backups::setting(&app.db, owner).await?; |
| 76 | 3× | let place = setting.as_ref().map(backups::place); |
| 77 | 3× | Ok(State_ { setting, place, runs: backups::runs(&app.db, owner, 10).await? }) |
| 78 | 3× | } |
| 79 | |
|
| 80 | 2× | async fn get(app: Shared, headers: HeaderMap, org: Option<String>) -> Result<Json<State_>, ApiError> { |
| 81 | 2× | let c = caller(&app, &headers).await?; |
| 82 | 2× | let (_, owner, _) = owner_of(&app, &c, org.as_deref()).await?; |
| 83 | 1× | Ok(Json(state(&app, &owner).await?)) |
| 84 | 2× | } |
| 85 | |
|
| 86 | 5× | async fn put(app: Shared, headers: HeaderMap, org: Option<String>, b: Body) -> Result<Json<State_>, ApiError> { |
| 87 | 5× | let c = caller(&app, &headers).await?; |
| 88 | 5× | let (me, owner, kind) = owner_of(&app, &c, org.as_deref()).await?; |
| 89 | 3× | let input = SettingInput { |
| 90 | 3× | every: b.every, |
| 91 | 3× | destination: b.destination, |
| 92 | 3× | dir: b.dir, |
| 93 | 3× | bucket: b.bucket, |
| 94 | 3× | provider: b.provider, |
| 95 | 3× | prefix: b.prefix, |
| 96 | 3× | keep_days: b.keep_days.to_string(), |
| 97 | 3× | s3_bucket: b.s3_bucket, |
| 98 | 3× | s3_region: b.s3_region, |
| 99 | 3× | s3_role: b.s3_role, |
| 100 | 3× | }; |
| 101 | 3× | let s = setting_of(&app, &owner, kind, &input).map_err(|e| ApiError(StatusCode::BAD_REQUEST, e))?; |
| 102 | 2× | backups::save(&app.db, &s).await?; |
| 103 | 2× | db::accounts::audit(&app.db, Some(&me), "backup.setting", &owner, &format!("{} to {}", s.every, backups::place(&s))).await.ok(); |
| 104 | 2× | Ok(Json(state(&app, &owner).await?)) |
| 105 | 5× | } |
| 106 | |
|
| 107 | 1× | async fn test(app: Shared, headers: HeaderMap, org: Option<String>) -> Result<Json<serde_json::Value>, ApiError> { |
| 108 | 1× | let c = caller(&app, &headers).await?; |
| 109 | 1× | let (_, owner, _) = owner_of(&app, &c, org.as_deref()).await?; |
| 110 | 1× | let set = |
| 111 | 1× | backups::setting(&app.db, &owner).await?.ok_or_else(|| ApiError(StatusCode::CONFLICT, "save where backups go first".into()))?; |
| 112 | 1× | let dest = backups::destination(&app, &set).map_err(|e| ApiError(StatusCode::CONFLICT, format!("{e:#}")))?; |
| 113 | 1× | tokio::task::spawn_blocking(move || backups::probe(dest.as_ref())) |
| 114 | 1× | .await |
| 115 | 1× | .map_err(|e| ApiError(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))? |
| 116 | 1× | .map_err(|e| ApiError(StatusCode::CONFLICT, format!("TEST failed: {e:#}")))?; |
| 117 | 1× | Ok(Json(serde_json::json!({ "ok": true, "place": backups::place(&set) }))) |
| 118 | 1× | } |
| 119 | |
|
| 120 | 2× | async fn run(app: Shared, headers: HeaderMap, org: Option<String>) -> Result<(StatusCode, Json<serde_json::Value>), ApiError> { |
| 121 | 2× | let c = caller(&app, &headers).await?; |
| 122 | 2× | let (_, owner, _) = owner_of(&app, &c, org.as_deref()).await?; |
| 123 | 2× | if backups::setting(&app.db, &owner).await?.is_none() { |
| 124 | 1× | return Err(ApiError(StatusCode::CONFLICT, "save where backups go first".into())); |
| 125 | 1× | } |
| 126 | 1× | let (app2, owner2) = (app.clone(), owner.clone()); |
| 127 | 1× | tokio::spawn(async move { |
| 128 | 1× | if let Err(e) = backups::run(&app2, &owner2, "now").await { |
| 129 | 0× | tracing::warn!("backup of {owner2}: {e:#}"); |
| 130 | 1× | } |
| 131 | 1× | }); |
| 132 | 1× | Ok((StatusCode::ACCEPTED, Json(serde_json::json!({ "owner": owner, "started": true })))) |
| 133 | 2× | } |
| 134 | |
|
| 135 | 5× | async fn runs(app: Shared, headers: HeaderMap, org: Option<String>) -> Result<Json<Vec<Run>>, ApiError> { |
| 136 | 5× | let c = caller(&app, &headers).await?; |
| 137 | 5× | let (_, owner, _) = owner_of(&app, &c, org.as_deref()).await?; |
| 138 | 5× | Ok(Json(backups::runs(&app.db, &owner, 50).await?)) |
| 139 | 5× | } |
| 140 | |
|
| 141 | 1× | pub async fn user_get(State(app): State<Shared>, headers: HeaderMap) -> Result<Json<State_>, ApiError> { |
| 142 | 1× | get(app, headers, None).await |
| 143 | 1× | } |
| 144 | 3× | pub async fn user_put(State(app): State<Shared>, headers: HeaderMap, Json(b): Json<Body>) -> Result<Json<State_>, ApiError> { |
| 145 | 3× | put(app, headers, None, b).await |
| 146 | 3× | } |
| 147 | 1× | pub async fn user_test(State(app): State<Shared>, headers: HeaderMap) -> Result<Json<serde_json::Value>, ApiError> { |
| 148 | 1× | test(app, headers, None).await |
| 149 | 1× | } |
| 150 | 2× | pub async fn user_run(State(app): State<Shared>, headers: HeaderMap) -> Result<(StatusCode, Json<serde_json::Value>), ApiError> { |
| 151 | 2× | run(app, headers, None).await |
| 152 | 2× | } |
| 153 | 5× | pub async fn user_runs(State(app): State<Shared>, headers: HeaderMap) -> Result<Json<Vec<Run>>, ApiError> { |
| 154 | 5× | runs(app, headers, None).await |
| 155 | 5× | } |
| 156 | 1× | pub async fn org_get(State(app): State<Shared>, headers: HeaderMap, Path(name): Path<String>) -> Result<Json<State_>, ApiError> { |
| 157 | 1× | get(app, headers, Some(name)).await |
| 158 | 1× | } |
| 159 | 2× | pub async fn org_put( |
| 160 | 2× | State(app): State<Shared>, |
| 161 | 2× | headers: HeaderMap, |
| 162 | 2× | Path(name): Path<String>, |
| 163 | 2× | Json(b): Json<Body>, |
| 164 | 2× | ) -> Result<Json<State_>, ApiError> { |
| 165 | 2× | put(app, headers, Some(name), b).await |
| 166 | 2× | } |
| 167 | 0× | pub async fn org_test( |
| 168 | 0× | State(app): State<Shared>, |
| 169 | 0× | headers: HeaderMap, |
| 170 | 0× | Path(name): Path<String>, |
| 171 | 0× | ) -> Result<Json<serde_json::Value>, ApiError> { |
| 172 | 0× | test(app, headers, Some(name)).await |
| 173 | 0× | } |
| 174 | 0× | pub async fn org_run( |
| 175 | 0× | State(app): State<Shared>, |
| 176 | 0× | headers: HeaderMap, |
| 177 | 0× | Path(name): Path<String>, |
| 178 | 0× | ) -> Result<(StatusCode, Json<serde_json::Value>), ApiError> { |
| 179 | 0× | run(app, headers, Some(name)).await |
| 180 | 0× | } |
| 181 | 0× | pub async fn org_runs(State(app): State<Shared>, headers: HeaderMap, Path(name): Path<String>) -> Result<Json<Vec<Run>>, ApiError> { |
| 182 | 0× | runs(app, headers, Some(name)).await |
| 183 | 0× | } |