Skip to content
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
36 changes: 31 additions & 5 deletions src/hetzner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct PublicNet {
pub ipv4: Ipv4,
}

#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct IpAddress {
pub ip: String,
Expand Down Expand Up @@ -87,13 +88,22 @@ struct CreateServerRequest {
#[derive(Debug, Deserialize)]
struct CreateServerResponse {
server: Server,
root_password: Option<String>,
}

/// Result of creating a server, includes the server info and root password
pub struct CreateServerResult {
pub server: Server,
pub root_password: Option<String>,
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
struct ErrorResponse {
error: ErrorDetail,
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
struct ErrorDetail {
message: String,
Expand All @@ -112,7 +122,7 @@ impl HetznerClient {
}
}

pub async fn create_server(&self, config: &ServerConfig) -> Result<Server> {
pub async fn create_server(&self, config: &ServerConfig) -> Result<CreateServerResult> {
let url = format!("{}/servers", HETZNER_API_BASE);

let labels = if config.labels.is_empty() {
Expand Down Expand Up @@ -183,7 +193,10 @@ impl HetznerClient {
result.server.name, result.server.id, result.server.public_net.ipv4.ip
);

Ok(result.server)
Ok(CreateServerResult {
server: result.server,
root_password: result.root_password,
})
}

pub async fn delete_server(&self, id: u64) -> Result<()> {
Expand Down Expand Up @@ -338,7 +351,9 @@ impl HetznerClient {
#[derive(Debug, Deserialize)]
struct ServerTypesAvailable {
available: Vec<u64>,
#[allow(dead_code)]
available_for_migration: Vec<u64>,
#[allow(dead_code)]
supported: Vec<u64>,
}

Expand Down Expand Up @@ -370,6 +385,7 @@ impl HetznerClient {
.collect())
}

#[allow(dead_code)]
pub async fn get_server(&self, id: u64) -> Result<Server> {
let url = format!("{}/servers/{}", HETZNER_API_BASE, id);

Expand Down Expand Up @@ -448,6 +464,7 @@ final_message: "FFmpeg worker is ready!"
}

/// Cloud-init config with SSH key access
#[allow(dead_code)]
pub fn worker_cloud_init_with_ssh(
queue_url: &str,
binary_url: &str,
Expand Down Expand Up @@ -496,13 +513,19 @@ final_message: "FFmpeg worker is ready!"
)
}

/// Result of provisioning a worker
pub struct ProvisionResult {
pub ip: String,
pub root_password: Option<String>,
}

pub async fn provision_worker(
hetzner_token: &str,
queue_url: &str,
binary_url: &str,
bg_image_url: &str,
name: Option<String>,
) -> Result<String> {
) -> Result<ProvisionResult> {
let client = HetznerClient::new(hetzner_token.to_string());

let name = name.unwrap_or_else(|| {
Expand All @@ -520,6 +543,9 @@ pub async fn provision_worker(
..Default::default()
};

let server = client.create_server(&config).await?;
Ok(server.public_net.ipv4.ip)
let result = client.create_server(&config).await?;
Ok(ProvisionResult {
ip: result.server.public_net.ipv4.ip,
root_password: result.root_password,
})
}
7 changes: 5 additions & 2 deletions src/hetzner_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,18 @@ async fn main() -> Result<()> {
let binary_url = format!("{}/assets/worker", base);
let bg_image_url = format!("{}/assets/gpc-bg.png", base);

let ip = hetzner::provision_worker(
let result = hetzner::provision_worker(
&token,
&queue_url,
&binary_url,
&bg_image_url,
name,
)
.await?;
println!("Worker provisioned at IP: {}", ip);
println!("Worker provisioned at IP: {}", result.ip);
if let Some(password) = result.root_password {
println!("Root password: {}", password);
}
}
Commands::ListServers { token } => {
let client = hetzner::HetznerClient::new(token);
Expand Down
6 changes: 3 additions & 3 deletions src/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl RemoteLogger {
}

let client = reqwest::Client::new();
let url = format!("{}/jobs/{}/logs", self.queue_url, self.job_id);
let url = format!("{}/api/jobs/{}/logs", self.queue_url, self.job_id);

#[derive(Serialize)]
struct LogsPayload {
Expand Down Expand Up @@ -811,7 +811,7 @@ async fn update_job_progress_remote(
let client = reqwest::Client::new();

client
.patch(format!("{}/jobs/{}/progress", queue_url, job_id))
.patch(format!("{}/api/jobs/{}/progress", queue_url, job_id))
.json(&progress)
.send()
.await
Expand Down Expand Up @@ -845,7 +845,7 @@ async fn update_job_status_remote(
};

client
.patch(format!("{}/jobs/{}", queue_url, job_id))
.patch(format!("{}/api/jobs/{}", queue_url, job_id))
.json(&update)
.send()
.await
Expand Down