|
| 1 | +use clap::{Parser, ValueEnum}; |
| 2 | +use object_store::{ |
| 3 | + aws::AmazonS3Builder, aws::S3ConditionalPut, local::LocalFileSystem, memory::InMemory, |
| 4 | + ObjectStore, Result as ObjectStoreResult, |
| 5 | +}; |
| 6 | +use std::path::PathBuf; |
| 7 | + |
| 8 | +#[derive(Parser)] |
| 9 | +#[command(version, about, long_about=None)] |
| 10 | +pub struct IceHutOpts { |
| 11 | + #[arg( |
| 12 | + short, |
| 13 | + long, |
| 14 | + value_enum, |
| 15 | + env = "OBJECT_STORE_BACKEND", |
| 16 | + help = "Backend to use for state storage" |
| 17 | + )] |
| 18 | + backend: StoreBackend, |
| 19 | + |
| 20 | + #[arg( |
| 21 | + long, |
| 22 | + env = "AWS_ACCESS_KEY_ID", |
| 23 | + required_if_eq("backend", "s3"), |
| 24 | + help = "AWS Access Key ID", |
| 25 | + help_heading = "S3 Backend Options" |
| 26 | + )] |
| 27 | + access_key_id: Option<String>, |
| 28 | + #[arg( |
| 29 | + long, |
| 30 | + env = "AWS_SECRET_ACCESS_KEY", |
| 31 | + required_if_eq("backend", "s3"), |
| 32 | + help = "AWS Secret Access Key", |
| 33 | + help_heading = "S3 Backend Options" |
| 34 | + )] |
| 35 | + secret_access_key: Option<String>, |
| 36 | + #[arg( |
| 37 | + long, |
| 38 | + env = "AWS_REGION", |
| 39 | + required_if_eq("backend", "s3"), |
| 40 | + help = "AWS Region", |
| 41 | + help_heading = "S3 Backend Options" |
| 42 | + )] |
| 43 | + region: Option<String>, |
| 44 | + #[arg( |
| 45 | + long, |
| 46 | + env = "S3_BUCKET", |
| 47 | + required_if_eq("backend", "s3"), |
| 48 | + help = "S3 Bucket Name", |
| 49 | + help_heading = "S3 Backend Options" |
| 50 | + )] |
| 51 | + bucket: Option<String>, |
| 52 | + #[arg( |
| 53 | + long, |
| 54 | + env = "S3_ENDPOINT", |
| 55 | + help = "S3 Endpoint (Optional)", |
| 56 | + help_heading = "S3 Backend Options" |
| 57 | + )] |
| 58 | + endpoint: Option<String>, |
| 59 | + #[arg( |
| 60 | + long, |
| 61 | + env = "S3_ALLOW_HTTP", |
| 62 | + help = "Allow HTTP for S3 (Optional)", |
| 63 | + help_heading = "S3 Backend Options" |
| 64 | + )] |
| 65 | + allow_http: Option<bool>, |
| 66 | + |
| 67 | + #[arg(long, env="FILE_STORAGE_PATH", |
| 68 | + required_if_eq("backend", "file"), |
| 69 | + conflicts_with_all(["access_key_id", "secret_access_key", "region", "bucket", "endpoint", "allow_http"]), |
| 70 | + help_heading="File Backend Options", |
| 71 | + help="Path to the directory where files will be stored" |
| 72 | + )] |
| 73 | + file_storage_path: Option<PathBuf>, |
| 74 | + |
| 75 | + #[arg(short, long, env = "SLATEDB_PREFIX")] |
| 76 | + pub slatedb_prefix: String, |
| 77 | + |
| 78 | + #[arg( |
| 79 | + long, |
| 80 | + env = "ICEHUT_HOST", |
| 81 | + default_value = "127.0.0.1", |
| 82 | + help = "Host to bind to" |
| 83 | + )] |
| 84 | + pub host: Option<String>, |
| 85 | + |
| 86 | + #[arg( |
| 87 | + long, |
| 88 | + env = "ICEHUT_PORT", |
| 89 | + default_value = "3000", |
| 90 | + help = "Port to bind to" |
| 91 | + )] |
| 92 | + pub port: Option<u16>, |
| 93 | + |
| 94 | + #[arg(long, default_value = "false")] |
| 95 | + use_fs: Option<bool>, |
| 96 | +} |
| 97 | + |
| 98 | +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] |
| 99 | +enum StoreBackend { |
| 100 | + S3, |
| 101 | + File, |
| 102 | + Memory, |
| 103 | +} |
| 104 | + |
| 105 | +impl IceHutOpts { |
| 106 | + #[allow(clippy::unwrap_used, clippy::as_conversions)] |
| 107 | + pub fn object_store_backend(self) -> ObjectStoreResult<Box<dyn ObjectStore>> { |
| 108 | + // TODO: Hacky workaround for now, need to figure out a better way to pass this |
| 109 | + // TODO: Really, seriously remove this. This is a hack. |
| 110 | + unsafe { |
| 111 | + let use_fs = self.use_fs.unwrap_or(false); |
| 112 | + std::env::set_var("USE_FILE_SYSTEM_INSTEAD_OF_CLOUD", use_fs.to_string()); |
| 113 | + } |
| 114 | + match self.backend { |
| 115 | + StoreBackend::S3 => { |
| 116 | + let s3_allow_http = self.allow_http.unwrap_or(false); |
| 117 | + |
| 118 | + let s3_builder = AmazonS3Builder::new() |
| 119 | + .with_access_key_id(self.access_key_id.unwrap()) |
| 120 | + .with_secret_access_key(self.secret_access_key.unwrap()) |
| 121 | + .with_region(self.region.unwrap()) |
| 122 | + .with_bucket_name(self.bucket.unwrap()) |
| 123 | + .with_conditional_put(S3ConditionalPut::ETagMatch); |
| 124 | + |
| 125 | + if let Some(endpoint) = self.endpoint { |
| 126 | + s3_builder |
| 127 | + .with_endpoint(&endpoint) |
| 128 | + .with_allow_http(s3_allow_http) |
| 129 | + .build() |
| 130 | + .map(|s3| Box::new(s3) as Box<dyn ObjectStore>) |
| 131 | + } else { |
| 132 | + s3_builder |
| 133 | + .build() |
| 134 | + .map(|s3| Box::new(s3) as Box<dyn ObjectStore>) |
| 135 | + } |
| 136 | + } |
| 137 | + StoreBackend::File => { |
| 138 | + let file_storage_path = self.file_storage_path.unwrap(); |
| 139 | + LocalFileSystem::new_with_prefix(file_storage_path) |
| 140 | + .map(|fs| Box::new(fs) as Box<dyn ObjectStore>) |
| 141 | + } |
| 142 | + StoreBackend::Memory => Ok(Box::new(InMemory::new()) as Box<dyn ObjectStore>), |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments