risedev/task/
task_configure_minio.rs1use std::env;
16use std::process::Command;
17use std::time::Duration;
18
19use anyhow::{Result, anyhow};
20
21use super::{ExecuteContext, Task};
22use crate::MinioConfig;
23
24const HUMMOCK_REMOTE_NAME: &str = "hummock-minio";
25
26pub struct ConfigureMinioTask {
27 mcli_path: String,
28 mcli_config_path: String,
29 config: MinioConfig,
30}
31
32impl ConfigureMinioTask {
33 pub fn new(config: MinioConfig) -> Result<Self> {
34 let prefix_bin = env::var("PREFIX_BIN")?;
35 let prefix_config = env::var("PREFIX_CONFIG")?;
36 Ok(Self {
37 mcli_path: format!("{}/mcli", prefix_bin),
38 mcli_config_path: format!("{}/mcli", prefix_config),
39 config,
40 })
41 }
42
43 fn mcli(&mut self) -> Command {
44 let mut cmd = Command::new(self.mcli_path.clone());
45 cmd.arg("-C").arg(&self.mcli_config_path);
46 cmd
47 }
48}
49
50impl Task for ConfigureMinioTask {
51 fn execute(&mut self, ctx: &mut ExecuteContext<impl std::io::Write>) -> anyhow::Result<()> {
52 ctx.pb.set_message("waiting for online...");
53 let minio_address = format!("{}:{}", self.config.address, self.config.port);
54 let minio_console_address = format!(
55 "{}:{}",
56 self.config.console_address, self.config.console_port
57 );
58 let health_check_addr = format!("http://{}/minio/health/live", minio_address);
59 ctx.wait_http(&health_check_addr)?;
60
61 ctx.pb.set_message("configure...");
62
63 let mut last_result = Err(anyhow!("unreachable"));
64
65 for _ in 0..100 {
68 let mut cmd = self.mcli();
69 cmd.arg("alias")
70 .arg("set")
71 .arg(HUMMOCK_REMOTE_NAME)
72 .arg(format!("http://{}", minio_address))
73 .arg(&self.config.root_user)
74 .arg(&self.config.root_password);
75
76 last_result = ctx.run_command(cmd);
77 if last_result.is_ok() {
78 break;
79 }
80
81 std::thread::sleep(Duration::from_millis(50));
82 }
83
84 last_result?;
85
86 let mut cmd = self.mcli();
108 cmd.arg("mb").arg(format!(
109 "{}/{}",
110 HUMMOCK_REMOTE_NAME, self.config.hummock_bucket
111 ));
112 ctx.run_command(cmd).ok();
113
114 ctx.complete_spin();
115
116 ctx.pb.set_message(format!(
117 "api http://{}/, console http://{}/",
118 minio_address, minio_console_address
119 ));
120
121 Ok(())
122 }
123}