use hws::{Client, LogQuery};
use serde_json::json;
use std::time::Duration;
#[tokio::main]
async fn main() -> hws::Result<()> {
let lb = Client::builder(
std::env::var("HEYCTL_SERVER").unwrap_or_else(|_| "http://127.0.0.1:9090".into()),
)
.token(std::env::var("HEYCTL_TOKEN").expect("set HEYCTL_TOKEN"))
.build()?;
let me = lb.whoami().await?;
let ns = me.sole_namespace().unwrap_or("default").to_string();
lb.create_deployment(&json!({
"id": "web",
"namespace": ns,
"routes": [{"host": "web.example.com"}],
"vm": {"image": "nginx-fc", "port": 80},
"scaling": {"min_replicas": 1, "max_replicas": 4},
}))
.await?;
lb.wait_for_ready("web")
.timeout(Duration::from_secs(300))
.await?;
lb.install_plugin(&ns, "obs", None).await?;
let obs = lb.obs(&ns);
for d in obs.fleet(Some("1h")).await?.deployments {
println!(
"{}: {} log lines, {} errors",
d.id, d.log_lines, d.error_logs
);
}
for line in obs
.logs("web", &LogQuery::new().level("error").limit(20))
.await?
.rows
{
println!("{} {}", line.ts, line.message);
}
let current = lb.deployment("web").await?;
let mut spec = lb.raw().spec("web").await?;
spec["vm"]["image"] = json!("nginx-fc:1.27");
let op = lb
.start_rollout("web", "web-1-27", ¤t.rollout_revision, &spec)
.await?;
println!("rollout {} is {}", op.operation_id, op.status);
lb.delete_deployment("web").await?;
Ok(())
}