Skip to content

Commit

Permalink
chore(rustfmt): tweak formatting, again
Browse files Browse the repository at this point in the history
  • Loading branch information
CompeyDev committed Apr 10, 2024
1 parent e895d86 commit 2c103ef
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
5 changes: 4 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use uuid::Uuid;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let secret_path = Path::new(&out_dir).join("secret.uuid");
println!("Generating deployment secret to {}...", secret_path.display());
println!(
"Generating deployment secret to {}...",
secret_path.display()
);

let mut secret: String;

Expand Down
4 changes: 0 additions & 4 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
max_width = 150
hard_tabs = true
#normalize_comments = false
#match_block_trailing_comma = true
#closure_block_indent_threshold = 1
37 changes: 27 additions & 10 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ impl Agent {

// TODO: pull from ghcr.io instead of docker hub
let local_images = sock.images();
let mut pull_stream = local_images.pull(&PullOptsBuilder::default().image(image.to_string()).tag("latest").build());
let mut pull_stream = local_images.pull(
&PullOptsBuilder::default()
.image(image.to_string())
.tag("latest")
.build(),
);

while let Some(pull_res) = pull_stream.next().await {
let chunk = pull_res.map_err(anyhow::Error::from)?;
Expand All @@ -53,7 +58,11 @@ impl Agent {
let container_name = format!(
"{}-{}",
image.to_string(),
rand::thread_rng().sample_iter(&Alphanumeric).take(7).map(char::from).collect::<String>()
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(7)
.map(char::from)
.collect::<String>()
);

debug!("Creating container {}", container_name);
Expand Down Expand Up @@ -81,7 +90,8 @@ impl Agent {
debug!("Locking {}", lockfile.display());

if lockfile.exists() {
let deserialized_lockfile = serde_json::from_str::<AgentLockfile>(&fs::read_to_string(lockfile).await?)?;
let deserialized_lockfile =
serde_json::from_str::<AgentLockfile>(&fs::read_to_string(lockfile).await?)?;

if deserialized_lockfile.status == ContainerStatus::Deploying {
let old_container_id = deserialized_lockfile.container_id.clone();
Expand All @@ -92,7 +102,10 @@ impl Agent {
}

// FIXME: remove this clone
self.old_container = Some(Container::new(self.sock.clone(), deserialized_lockfile.container_id))
self.old_container = Some(Container::new(
self.sock.clone(),
deserialized_lockfile.container_id,
))
}

generate_lockfile(lockfile, self.container.id(), ContainerStatus::Deploying).await?;
Expand All @@ -101,11 +114,9 @@ impl Agent {
}

pub async fn deploy(mut self) -> Result<()> {
match self
.old_container
.take()
.ok_or(anyhow::Error::msg("Agent needs to be locked before deploying"))
{
match self.old_container.take().ok_or(anyhow::Error::msg(
"Agent needs to be locked before deploying",
)) {
Ok(container) => {
fs::remove_file(*LOCKFILE).await?;
remove_container(container).await
Expand All @@ -132,7 +143,13 @@ impl Agent {

async fn remove_container(container: Container) -> Result<()> {
container
.remove(&ContainerRemoveOptsBuilder::default().volumes(false).force(true).link(false).build())
.remove(
&ContainerRemoveOptsBuilder::default()
.volumes(false)
.force(true)
.link(false)
.build(),
)
.await?;

debug!("Removed container {}", container.id());
Expand Down
30 changes: 22 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#![allow(clippy::borrow_interior_mutable_const, clippy::declare_interior_mutable_const)]
#![allow(
clippy::borrow_interior_mutable_const,
clippy::declare_interior_mutable_const
)]

use std::{env, net::SocketAddr, time::SystemTime};

Expand Down Expand Up @@ -59,20 +62,28 @@ async fn main() -> Result<()> {
srv.with(tide::log::LogMiddleware::new());

srv.at("/deploy").post(|mut req: Request<()>| async move {
let body = req.body_json::<DeployEndpointBody>().await.map_err(|mut err: tide::Error| {
err.set_status(StatusCode::BadRequest);
err
})?;
let body =
req.body_json::<DeployEndpointBody>()
.await
.map_err(|mut err: tide::Error| {
err.set_status(StatusCode::BadRequest);
err
})?;

if body.secret != DEPLOY_SECRET {
return Err(tide::Error::new(StatusCode::Unauthorized, anyhow::Error::msg("invalid deploy secret")));
return Err(tide::Error::new(
StatusCode::Unauthorized,
anyhow::Error::msg("invalid deploy secret"),
));
}

fn handle_agent_error(err: anyhow::Error) -> tide::Error {
tide::Error::new(StatusCode::InternalServerError, err)
}

let mut agent = Agent::new(DOCKER_UNIX_SOCK, "hello-world").await.map_err(handle_agent_error)?;
let mut agent = Agent::new(DOCKER_UNIX_SOCK, "hello-world")
.await
.map_err(handle_agent_error)?;
agent.lock().await.map_err(handle_agent_error)?;
agent.deploy().await.map_err(handle_agent_error)?;

Expand All @@ -83,7 +94,10 @@ async fn main() -> Result<()> {
srv.at("/status").get(|_| async {
Ok(serde_json::json!(fs::read_to_string(*LOCKFILE)
.await
.map_err(|err| tide::Error::new(StatusCode::InternalServerError, err))?))
.map_err(|err| tide::Error::new(
StatusCode::InternalServerError,
err
))?))
});

srv.listen(SocketAddr::from(([127, 0, 0, 1], PORT))).await?;
Expand Down

0 comments on commit 2c103ef

Please sign in to comment.