Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): deploy-service #59

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::net::IpAddr;

use clap::{Parser, Subcommand};
use eyre::Ok;
use proto::clients::CtlClient;
use proto::{
clients::CtlClient,
common::service::{ResourceConfig, ServiceId, ServiceImage, ServiceSpec},
ctl::deployer::RedeploymentPolicy,
};
use tabled::{self, Table, Tabled};

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -39,25 +42,40 @@ pub enum WorkerCmd {
#[derive(Debug, Subcommand)]
pub enum ServiceCmd {
List,
Show { id: String },
Deploy { id: String, image: String },
Terminate { id: String },
Show {
id: String,
},
Deploy {
#[arg(long)]
id: String,
#[arg(long)]
image: String,
#[arg(long)]
public: bool,
#[arg(long)]
concurrency: u32,
#[arg(long)]
cpu_shares: i64,
#[arg(long)]
memory_limit: i64,
},
Terminate {
id: String,
},
}

#[tokio::main]
async fn main() -> eyre::Result<()> {
let cli = Cli::parse();
let ctl_client = CtlClient::new(cli.ctl_addr);

match cli.cmd {
Cmd::Node(cmd) => handle_node(&cmd, ctl_client).await?,
Cmd::Service(cmd) => handle_service(&cmd)?,
Cmd::Node(cmd) => handle_node(cmd, ctl_client).await?,
Cmd::Service(cmd) => handle_service(cmd, ctl_client).await?,
}

Ok(())
}

async fn handle_node(cmd: &NodeCmd, ctl_client: CtlClient) -> eyre::Result<()> {
async fn handle_node(cmd: NodeCmd, ctl_client: CtlClient) -> eyre::Result<()> {
match cmd {
NodeCmd::List => {
let workers = ctl_client.query_workers().await.unwrap().workers;
Expand All @@ -69,11 +87,33 @@ async fn handle_node(cmd: &NodeCmd, ctl_client: CtlClient) -> eyre::Result<()> {
}
}

fn handle_service(cmd: &ServiceCmd) -> eyre::Result<()> {
async fn handle_service(cmd: ServiceCmd, ctl_client: CtlClient) -> eyre::Result<()> {
match cmd {
ServiceCmd::List => todo!(),
ServiceCmd::Show { .. } => todo!(),
ServiceCmd::Deploy { .. } => todo!(),
ServiceCmd::Deploy {
id,
image,
public,
concurrency,
cpu_shares,
memory_limit,
} => {
let spec = ServiceSpec {
service_id: ServiceId(id),
image: ServiceImage(image),
public,
concurrency,
resource_config: ResourceConfig {
cpu_shares,
memory_limit: memory_limit * 1024 * 1024,
},
};
let rd = RedeploymentPolicy::None;
let res = ctl_client.deploy_service(spec, rd).await?;
println!("Successfully deployed service #{}", res.deployment_id);
Ok(())
}
ServiceCmd::Terminate { .. } => todo!(),
}
}
Expand Down
3 changes: 2 additions & 1 deletion ctl/src/deployer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tokio::{
sync::{mpsc, oneshot},
task::JoinSet,
};
use tracing::{error, instrument, warn};
use tracing::{debug, error, instrument, warn};
use uuid::Uuid;

use crate::{
Expand Down Expand Up @@ -110,6 +110,7 @@ impl Deployer {
}

async fn handle_deploy_service(&mut self, spec: ServiceSpec) -> eyre::Result<DeployServiceRes> {
debug!(?spec, "deploying service");
let workers = self.h.worker_mgr.query_workers().await;
let instances = alloc::rand_many(&workers, spec.concurrency);
let deployment_id = DeploymentId(Uuid::now_v7());
Expand Down
8 changes: 7 additions & 1 deletion proto/src/ctl/deployer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, net::IpAddr};
use std::{collections::HashMap, fmt, net::IpAddr};

use serde::{Deserialize, Serialize};
use uuid::Uuid;
Expand All @@ -14,6 +14,12 @@ pub struct RevisionId(pub Uuid);
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Default)]
pub struct DeploymentId(pub Uuid);

impl fmt::Display for DeploymentId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

/// Starts a new deploy in the system.
#[derive(Debug, Serialize, Deserialize)]
pub struct DeployServiceReq {
Expand Down
2 changes: 1 addition & 1 deletion worker/src/runner/container_rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl ContainerRuntime {

let options = Some(CreateContainerOptions {
name,
platform: Some("linux/x86_64".to_string()),
platform: None,
});
let create_response = self.docker.create_container(options, config).await?;

Expand Down