-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathmain.rs
More file actions
216 lines (191 loc) · 7.69 KB
/
main.rs
File metadata and controls
216 lines (191 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::path::PathBuf;
#[cfg(all(feature = "s3", feature = "rattler_config"))]
use anyhow::Context;
use clap::{Parser, Subcommand};
use clap_verbosity_flag::Verbosity;
use rattler_conda_types::Platform;
#[cfg(feature = "rattler_config")]
use rattler_config::config::concurrency::default_max_concurrent_solves;
use rattler_index::{index_fs, IndexFsConfig};
#[cfg(feature = "s3")]
use rattler_index::{index_s3, IndexS3Config, PreconditionChecks};
#[cfg(feature = "s3")]
use rattler_networking::AuthenticationStorage;
#[cfg(feature = "s3")]
use rattler_s3::S3Credentials;
#[cfg(feature = "s3")]
use url::Url;
#[cfg(feature = "s3")]
fn parse_s3_url(value: &str) -> Result<Url, String> {
let url: Url = Url::parse(value).map_err(|e| format!("`{value}` isn't a valid URL: {e}"))?;
if url.scheme() == "s3" && url.host_str().is_some() {
Ok(url)
} else {
Err(format!(
"Only S3 URLs of format s3://bucket/... can be used, not `{value}`"
))
}
}
/// The `rattler-index` CLI.
#[derive(Parser)]
#[command(name = "rattler-index", version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
#[command(flatten)]
verbosity: Verbosity,
/// Whether to write repodata.json.zst.
#[arg(long, default_value = "true", global = true)]
write_zst: Option<bool>,
/// Whether to write sharded repodata.
#[arg(long, default_value = "true", global = true)]
write_shards: Option<bool>,
/// Whether to force the re-indexing of all packages.
/// Note that this will create a new repodata.json instead of updating the
/// existing one.
#[arg(short, long, default_value = "false", global = true)]
force: bool,
/// The maximum number of packages to process in-memory simultaneously.
/// This is necessary to limit memory usage when indexing large channels.
#[arg(long, global = true)]
max_parallel: Option<usize>,
/// A specific platform to index.
/// Defaults to all platforms available in the channel.
#[arg(long, global = true)]
target_platform: Option<Platform>,
/// The name of the conda package (expected to be in the `noarch` subdir)
/// that should be used for repodata patching. For more information, see `https://prefix.dev/blog/repodata_patching`.
#[arg(long, global = true)]
repodata_patch: Option<String>,
/// Disable precondition checks (`ETags`, timestamps) during file operations.
/// Use this flag if your S3 backend doesn't fully support conditional requests,
/// or if you're certain no concurrent indexing processes are running.
/// Warning: Disabling this removes protection against concurrent modifications.
#[cfg(feature = "s3")]
#[arg(long, default_value = "false", global = true)]
disable_precondition_checks: bool,
/// The path to the config file to use to configure rattler-index.
/// Uses the same configuration format as pixi, see `https://pixi.sh/latest/reference/pixi_configuration`.
#[arg(long, global = true)]
config: Option<PathBuf>,
}
/// The subcommands for the `rattler-index` CLI.
#[derive(Subcommand)]
#[allow(clippy::large_enum_variant)]
enum Commands {
/// Index a channel stored on the filesystem.
#[command(name = "fs")]
FileSystem {
/// The path to the channel directory.
#[arg()]
channel: std::path::PathBuf,
},
/// Index a channel stored in an S3 bucket.
#[cfg(feature = "s3")]
S3 {
/// The S3 channel URL, e.g. `s3://my-bucket/my-channel`.
#[arg(value_parser = parse_s3_url)]
channel: Url,
#[clap(flatten)]
credentials: rattler_s3::clap::S3CredentialsOpts,
},
}
/// The configuration type for rattler-index - just extends rattler config and
/// can load the same TOML files as pixi.
#[cfg(feature = "rattler_config")]
pub type Config = rattler_config::config::ConfigBase<()>;
/// Entry point of the `rattler-index` cli.
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Parse the command line arguments
let cli = Cli::parse();
tracing_subscriber::FmtSubscriber::builder()
.with_max_level(cli.verbosity)
.init();
let multi_progress = indicatif::MultiProgress::new();
#[cfg(feature = "rattler_config")]
let config = if let Some(config_path) = cli.config {
Some(Config::load_from_files(vec![config_path])?)
} else {
None
};
#[cfg(feature = "rattler_config")]
let max_parallel = cli
.max_parallel
.or(config.as_ref().map(|c| c.concurrency.downloads))
.unwrap_or_else(default_max_concurrent_solves);
#[cfg(not(feature = "rattler_config"))]
let max_parallel = cli.max_parallel.unwrap_or(10);
#[cfg(feature = "s3")]
let precondition_checks = if cli.disable_precondition_checks {
PreconditionChecks::Disabled
} else {
PreconditionChecks::Enabled
};
match cli.command {
Commands::FileSystem { channel } => {
index_fs(IndexFsConfig {
channel,
target_platform: cli.target_platform,
repodata_patch: cli.repodata_patch,
write_zst: cli.write_zst.unwrap_or(true),
write_shards: cli.write_shards.unwrap_or(true),
force: cli.force,
max_parallel,
multi_progress: Some(multi_progress),
})
.await
}
#[cfg(feature = "s3")]
Commands::S3 {
channel,
#[cfg(feature = "rattler_config")]
mut credentials,
#[cfg(not(feature = "rattler_config"))]
credentials,
} => {
#[cfg(feature = "rattler_config")]
{
let bucket = channel.host().context("Invalid S3 url")?.to_string();
let s3_config = config
.as_ref()
.and_then(|config| config.s3_options.0.get(&bucket));
// Fill in missing credentials from config file if not provided on command line
credentials.region = credentials.region.or(s3_config.map(|c| c.region.clone()));
credentials.endpoint_url = credentials
.endpoint_url
.or(s3_config.map(|c| c.endpoint_url.clone()));
if let Some(s3_config) = s3_config {
credentials.addressing_style = if s3_config.force_path_style {
rattler_s3::clap::S3AddressingStyleOpts::Path
} else {
rattler_s3::clap::S3AddressingStyleOpts::VirtualHost
};
}
}
// Resolve the credentials
let credentials = match Option::<S3Credentials>::from(credentials) {
Some(credentials) => {
let auth_storage = AuthenticationStorage::from_env_and_defaults()?;
credentials.resolve(&channel, &auth_storage).ok_or_else(|| anyhow::anyhow!("Could not find S3 credentials in the authentication storage, and no credentials were provided via the command line."))?
}
None => rattler_s3::ResolvedS3Credentials::from_sdk().await?,
};
index_s3(IndexS3Config {
channel,
credentials,
target_platform: cli.target_platform,
repodata_patch: cli.repodata_patch,
write_zst: cli.write_zst.unwrap_or(true),
write_shards: cli.write_shards.unwrap_or(true),
force: cli.force,
max_parallel,
multi_progress: Some(multi_progress),
precondition_checks,
})
.await
}
}?;
println!("Finished indexing channel.");
Ok(())
}