Skip to content

Commit ff3fce6

Browse files
authored
Merge pull request #44 from wobcom/refactor/vickyctl
refactor for vickyctl
2 parents 28fb5db + c46698d commit ff3fce6

23 files changed

+1168
-316
lines changed

Diff for: Cargo.lock

+279-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: fairy/src/main.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ pub struct FlakeRef {
8080
#[derive(Debug, Serialize, Deserialize)]
8181
#[serde(tag = "result")]
8282
pub enum TaskResult {
83-
SUCCESS,
84-
ERROR,
83+
Success,
84+
Error,
8585
}
8686

8787
#[derive(Debug, Deserialize)]
8888
#[serde(tag = "state")]
8989
pub enum TaskStatus {
90-
NEW,
91-
RUNNING,
92-
FINISHED(TaskResult),
90+
New,
91+
Running,
92+
Finished(TaskResult),
9393
}
9494

9595
#[derive(Debug, Deserialize)]
@@ -173,9 +173,9 @@ async fn run_task(cfg: Arc<AppConfig>, task: Task) {
173173
let result = match try_run_task(cfg.clone(), &task).await {
174174
Err(e) => {
175175
log::info!("task failed: {} {} {:?}", task.id, task.display_name, e);
176-
TaskResult::ERROR
176+
TaskResult::Error
177177
}
178-
Ok(_) => TaskResult::SUCCESS,
178+
Ok(_) => TaskResult::Success,
179179
};
180180
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
181181
let _ = api::<_, ()>(

Diff for: vicky/src/bin/vicky/locks.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use diesel::PgConnection;
2-
use rocket::get;
2+
use rocket::{get, patch};
33
use rocket::serde::json::Json;
4+
use uuid::Uuid;
45
use vickylib::database::entities::{Database, Lock};
56
use vickylib::database::entities::lock::db_impl::LockDatabase;
7+
use vickylib::database::entities::lock::PoisonedLock;
68
use crate::auth::{Machine, User};
79
use crate::errors::AppError;
810

@@ -11,6 +13,11 @@ async fn locks_get_poisoned(db: &Database) -> Result<Json<Vec<Lock>>, AppError>
1113
Ok(Json(poisoned_locks))
1214
}
1315

16+
async fn locks_get_detailed_poisoned(db: &Database) -> Result<Json<Vec<PoisonedLock>>, AppError> {
17+
let poisoned_locks: Vec<PoisonedLock> = db.run(PgConnection::get_poisoned_locks_with_tasks).await?;
18+
Ok(Json(poisoned_locks))
19+
}
20+
1421
#[get("/poisoned")]
1522
pub async fn locks_get_poisoned_user(
1623
db: Database,
@@ -27,6 +34,23 @@ pub async fn locks_get_poisoned_machine(
2734
locks_get_poisoned(&db).await
2835
}
2936

37+
38+
#[get("/poisoned_detailed")]
39+
pub async fn locks_get_detailed_poisoned_user(
40+
db: Database,
41+
_user: User,
42+
) -> Result<Json<Vec<PoisonedLock>>, AppError> {
43+
locks_get_detailed_poisoned(&db).await
44+
}
45+
46+
#[get("/poisoned_detailed", rank = 2)]
47+
pub async fn locks_get_detailed_poisoned_machine(
48+
db: Database,
49+
_machine: Machine,
50+
) -> Result<Json<Vec<PoisonedLock>>, AppError> {
51+
locks_get_detailed_poisoned(&db).await
52+
}
53+
3054
async fn locks_get_active(db: &Database) -> Result<Json<Vec<Lock>>, AppError> {
3155
let locks: Vec<Lock> = db.run(PgConnection::get_active_locks).await?;
3256
Ok(Json(locks))
@@ -47,3 +71,15 @@ pub async fn locks_get_active_machine(
4771
) -> Result<Json<Vec<Lock>>, AppError> {
4872
locks_get_active(&db).await
4973
}
74+
75+
#[patch("/unlock/<lock_id>")]
76+
pub async fn locks_unlock(
77+
db: Database,
78+
_user: Machine, // TODO: Should actually be user-only, but we don't have that yet
79+
lock_id: String,
80+
) -> Result<(), AppError> {
81+
let lock_uuid = Uuid::try_parse(&lock_id)?;
82+
83+
db.run(move |conn| conn.unlock_lock(&lock_uuid)).await?;
84+
Ok(())
85+
}

Diff for: vicky/src/bin/vicky/main.rs

+24-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ use vickylib::logs::LogDrain;
1616
use vickylib::s3::client::S3Client;
1717

1818
use crate::events::{get_global_events, GlobalEvent};
19-
use crate::locks::{locks_get_active_machine, locks_get_active_user, locks_get_poisoned_machine, locks_get_poisoned_user};
19+
use crate::locks::{
20+
locks_get_active_machine, locks_get_active_user, locks_get_detailed_poisoned_machine,
21+
locks_get_detailed_poisoned_user, locks_get_poisoned_machine, locks_get_poisoned_user,
22+
locks_unlock,
23+
};
2024
use crate::tasks::{
2125
tasks_add, tasks_claim, tasks_finish, tasks_get_logs, tasks_get_machine, tasks_get_user,
2226
tasks_put_logs, tasks_specific_get_machine, tasks_specific_get_user,
@@ -77,19 +81,19 @@ fn run_migrations(connection: &mut impl MigrationHarness<diesel::pg::Pg>) -> Res
7781
Ok(_) => {
7882
log::info!("Migrations successfully completed");
7983
Ok(())
80-
},
84+
}
8185
Err(e) => {
8286
log::error!("Error running migrations {e}");
8387
Err(AppError::MigrationError(e.to_string()))
8488
}
8589
}
8690
}
8791

88-
async fn run_rocket_migrations(rocket: Rocket<Build>) -> Result<Rocket<Build>,Rocket<Build>> {
92+
async fn run_rocket_migrations(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocket<Build>> {
8993
let db: Database = Database::get_one(&rocket).await.unwrap();
9094
match db.run(run_migrations).await {
9195
Ok(_) => Ok(rocket),
92-
Err(_) => Err(rocket)
96+
Err(_) => Err(rocket),
9397
}
9498
}
9599

@@ -117,12 +121,16 @@ async fn main() -> anyhow::Result<()> {
117121

118122
let app_config = build_rocket.figment().extract::<Config>()?;
119123

120-
let oidc_config_resolved: OIDCConfigResolved = reqwest::get(app_config.oidc_config.well_known_uri)
121-
.await?
122-
.json()
123-
.await?;
124+
let oidc_config_resolved: OIDCConfigResolved =
125+
reqwest::get(app_config.oidc_config.well_known_uri)
126+
.await?
127+
.json()
128+
.await?;
124129

125-
log::info!("Fetched OIDC configuration, found jwks_uri={}", oidc_config_resolved.jwks_uri);
130+
log::info!(
131+
"Fetched OIDC configuration, found jwks_uri={}",
132+
oidc_config_resolved.jwks_uri
133+
);
126134

127135
let jwks_verifier = RemoteJwksVerifier::new(
128136
oidc_config_resolved.jwks_uri.clone(),
@@ -162,7 +170,10 @@ async fn main() -> anyhow::Result<()> {
162170
.manage(oidc_config_resolved)
163171
.attach(Database::fairing())
164172
.attach(AdHoc::config::<Config>())
165-
.attach(AdHoc::try_on_ignite("run migrations", run_rocket_migrations))
173+
.attach(AdHoc::try_on_ignite(
174+
"run migrations",
175+
run_rocket_migrations,
176+
))
166177
.mount("/api/v1/web-config", routes![get_web_config])
167178
.mount("/api/v1/user", routes![get_user])
168179
.mount("/api/v1/events", routes![get_global_events])
@@ -185,8 +196,11 @@ async fn main() -> anyhow::Result<()> {
185196
routes![
186197
locks_get_poisoned_user,
187198
locks_get_poisoned_machine,
199+
locks_get_detailed_poisoned_user,
200+
locks_get_detailed_poisoned_machine,
188201
locks_get_active_user,
189202
locks_get_active_machine,
203+
locks_unlock
190204
],
191205
)
192206
.launch()

Diff for: vicky/src/bin/vicky/tasks.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ pub async fn tasks_get_logs<'a>(
107107
EventStream! {
108108

109109
match task.status {
110-
TaskStatus::NEW => {},
111-
TaskStatus::RUNNING => {
110+
TaskStatus::New => {},
111+
TaskStatus::Running => {
112112
let mut recv = log_drain.send_handle.subscribe();
113113
let existing_log_messages = log_drain.get_logs(task_uuid.to_string()).await.unwrap();
114114

@@ -137,7 +137,7 @@ pub async fn tasks_get_logs<'a>(
137137
}
138138
}
139139
},
140-
TaskStatus::FINISHED(_) => {
140+
TaskStatus::Finished(_) => {
141141
let logs = s3.get_logs(&id).await.unwrap();
142142
for element in logs {
143143
yield Event::data(element)
@@ -166,7 +166,7 @@ pub async fn tasks_put_logs(
166166
.ok_or(AppError::HttpError(Status::NotFound))?;
167167

168168
match task.status {
169-
TaskStatus::RUNNING => {
169+
TaskStatus::Running => {
170170
log_drain.push_logs(id, logs.lines.clone())?;
171171
Ok(Json(()))
172172
}
@@ -193,7 +193,7 @@ pub async fn tasks_claim(
193193
.run(move |conn| conn.get_task(next_task.id))
194194
.await?
195195
.ok_or(AppError::HttpError(Status::NotFound))?;
196-
task.status = TaskStatus::RUNNING;
196+
task.status = TaskStatus::Running;
197197
let task2 = task.clone();
198198
db.run(move |conn| conn.update_task(&task2)).await?;
199199
global_events.send(GlobalEvent::TaskUpdate { uuid: task.id })?;
@@ -220,9 +220,9 @@ pub async fn tasks_finish(
220220

221221
log_drain.finish_logs(&id).await?;
222222

223-
task.status = TaskStatus::FINISHED(finish.result.clone());
223+
task.status = TaskStatus::Finished(finish.result.clone());
224224

225-
if finish.result == TaskResult::ERROR {
225+
if finish.result == TaskResult::Error {
226226
task.locks.iter_mut().for_each(|lock| lock.poison(&task.id));
227227
}
228228

@@ -253,7 +253,7 @@ pub async fn tasks_add(
253253
) -> Result<Json<RoTask>, AppError> {
254254
let task_uuid = Uuid::new_v4();
255255

256-
let task_manifest = Task::builder()
256+
let task = Task::builder()
257257
.with_id(task_uuid)
258258
.with_display_name(&task.display_name)
259259
.with_flake(&task.flake_ref.flake)
@@ -262,16 +262,16 @@ pub async fn tasks_add(
262262
.requires_features(task.features.clone())
263263
.build();
264264

265-
if check_lock_conflict(&task_manifest) {
265+
if check_lock_conflict(&task) {
266266
return Err(AppError::HttpError(Status::Conflict));
267267
}
268268

269-
db.run(move |conn| conn.put_task(&task_manifest)).await?;
269+
db.run(move |conn| conn.put_task(task)).await?;
270270
global_events.send(GlobalEvent::TaskAdd)?;
271271

272272
let ro_task = RoTask {
273273
id: task_uuid,
274-
status: TaskStatus::NEW,
274+
status: TaskStatus::New,
275275
};
276276

277277
Ok(Json(ro_task))

0 commit comments

Comments
 (0)