Skip to content

Commit c252fe5

Browse files
authored
Merge pull request #6834 from Turbo87/bye-bye-uploaders
Migrate remaining `Uploaders` code into `Storage` system
2 parents 292bd85 + e232c52 commit c252fe5

File tree

11 files changed

+73
-219
lines changed

11 files changed

+73
-219
lines changed

src/admin/render_readmes.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::{
2-
config, db,
2+
db,
33
models::Version,
44
schema::{crates, readme_renderings, versions},
5-
uploaders::Uploader,
65
};
76
use anyhow::{anyhow, Context};
87
use std::{io::Read, path::Path, sync::Arc, thread};
@@ -40,7 +39,6 @@ pub struct Opts {
4039
}
4140

4241
pub fn run(opts: Opts) -> anyhow::Result<()> {
43-
let base_config = Arc::new(config::Base::from_environment());
4442
let storage = Arc::new(Storage::from_environment());
4543
let conn = &mut db::oneoff_connection().unwrap();
4644

@@ -109,11 +107,10 @@ pub fn run(opts: Opts) -> anyhow::Result<()> {
109107
.context("Couldn't record rendering time")?;
110108

111109
let client = client.clone();
112-
let base_config = base_config.clone();
113110
let storage = storage.clone();
114111
let handle = thread::spawn::<_, anyhow::Result<()>>(move || {
115112
println!("[{}-{}] Rendering README...", krate_name, version.num);
116-
let readme = get_readme(base_config.uploader(), &client, &version, &krate_name)?;
113+
let readme = get_readme(&storage, &client, &version, &krate_name)?;
117114
if !readme.is_empty() {
118115
let rt = tokio::runtime::Builder::new_current_thread()
119116
.enable_all()
@@ -143,19 +140,14 @@ pub fn run(opts: Opts) -> anyhow::Result<()> {
143140

144141
/// Renders the readme of an uploaded crate version.
145142
fn get_readme(
146-
uploader: &Uploader,
143+
storage: &Storage,
147144
client: &Client,
148145
version: &Version,
149146
krate_name: &str,
150147
) -> anyhow::Result<String> {
151148
let pkg_name = format!("{}-{}", krate_name, version.num);
152149

153-
let location = uploader.crate_location(krate_name, &version.num.to_string());
154-
155-
let location = match uploader {
156-
Uploader::S3 { .. } => location,
157-
Uploader::Local => format!("http://localhost:8888/{location}"),
158-
};
150+
let location = storage.crate_location(krate_name, &version.num.to_string());
159151

160152
let mut extra_headers = header::HeaderMap::new();
161153
extra_headers.insert(

src/background_jobs.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::db::ConnectionPool;
1010
use crate::storage::Storage;
1111
use crate::swirl::errors::EnqueueError;
1212
use crate::swirl::PerformError;
13-
use crate::uploaders::Uploader;
1413
use crate::worker;
1514
use crate::worker::cloudfront::CloudFront;
1615
use crate::worker::fastly::Fastly;
@@ -294,7 +293,6 @@ pub struct RenderAndUploadReadmeJob {
294293

295294
pub struct Environment {
296295
index: Arc<Mutex<Repository>>,
297-
pub uploader: Uploader,
298296
http_client: AssertUnwindSafe<Client>,
299297
cloudfront: Option<CloudFront>,
300298
fastly: Option<Fastly>,
@@ -304,15 +302,13 @@ pub struct Environment {
304302
impl Environment {
305303
pub fn new(
306304
index: Repository,
307-
uploader: Uploader,
308305
http_client: Client,
309306
cloudfront: Option<CloudFront>,
310307
fastly: Option<Fastly>,
311308
storage: Arc<Storage>,
312309
) -> Self {
313310
Self::new_shared(
314311
Arc::new(Mutex::new(index)),
315-
uploader,
316312
http_client,
317313
cloudfront,
318314
fastly,
@@ -322,15 +318,13 @@ impl Environment {
322318

323319
pub fn new_shared(
324320
index: Arc<Mutex<Repository>>,
325-
uploader: Uploader,
326321
http_client: Client,
327322
cloudfront: Option<CloudFront>,
328323
fastly: Option<Fastly>,
329324
storage: Arc<Storage>,
330325
) -> Self {
331326
Self {
332327
index,
333-
uploader,
334328
http_client: AssertUnwindSafe(http_client),
335329
cloudfront,
336330
fastly,

src/bin/background-worker.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ fn main() {
4040
info!("Booting runner");
4141

4242
let config = config::Server::default();
43-
let uploader = config.base.uploader();
4443

4544
if config.db.are_all_read_only() {
4645
loop {
@@ -83,14 +82,7 @@ fn main() {
8382
.build()
8483
.expect("Couldn't build client");
8584

86-
let environment = Environment::new_shared(
87-
repository,
88-
uploader.clone(),
89-
client,
90-
cloudfront,
91-
fastly,
92-
storage,
93-
);
85+
let environment = Environment::new_shared(repository, client, cloudfront, fastly, storage);
9486

9587
let environment = Arc::new(Some(environment));
9688

src/config/base.rs

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
//! Base configuration options
22
//!
33
//! - `HEROKU`: Is this instance of crates_io:: currently running on Heroku.
4-
//! - `S3_BUCKET`: The S3 bucket used to store crate files. If not present during development,
5-
//! crates_io:: will fall back to a local uploader.
6-
//! - `S3_REGION`: The region in which the bucket was created. Optional if US standard.
7-
//! - `AWS_ACCESS_KEY`: The access key to interact with S3.
8-
//! - `AWS_SECRET_KEY`: The secret key to interact with S3.
9-
//! - `S3_CDN`: Optional CDN configuration for building public facing URLs.
104
11-
use crate::{env, uploaders::Uploader, Env};
5+
use crate::Env;
126

137
pub struct Base {
148
pub env: Env,
15-
pub uploader: Uploader,
169
}
1710

1811
impl Base {
@@ -22,82 +15,6 @@ impl Base {
2215
_ => Env::Development,
2316
};
2417

25-
let uploader = if env == Env::Production {
26-
// `env` panics if these vars are not set, and in production for a primary instance,
27-
// that's what we want since we don't want to be able to start the server if the
28-
// server doesn't know where to upload crates.
29-
Self::s3_panic_if_missing_keys()
30-
} else if dotenvy::var("S3_BUCKET").is_ok() {
31-
// If we've set the `S3_BUCKET` variable to any value, use all of the values
32-
// for the related S3 environment variables and configure the app to upload to
33-
// and read from S3 like production does. All values except for bucket are
34-
// optional, like production read-only mirrors.
35-
info!("Using S3 uploader");
36-
Self::s3_maybe_read_only()
37-
} else {
38-
// If we don't set the `S3_BUCKET` variable, we'll use a development-only
39-
// uploader that makes it possible to run and publish to a locally-running
40-
// crates.io instance without needing to set up an account and a bucket in S3.
41-
info!("Using local uploader, crate files will be in the local_uploads directory");
42-
Uploader::Local
43-
};
44-
45-
Self { env, uploader }
46-
}
47-
48-
pub fn uploader(&self) -> &Uploader {
49-
&self.uploader
50-
}
51-
52-
fn s3_panic_if_missing_keys() -> Uploader {
53-
let index_bucket = match dotenvy::var("S3_INDEX_BUCKET") {
54-
Ok(name) => Some(Box::new(s3::Bucket::new(
55-
name,
56-
dotenvy::var("S3_INDEX_REGION")
57-
.map_or_else(|_err| s3::Region::Default, s3::Region::Region),
58-
env("AWS_ACCESS_KEY"),
59-
env("AWS_SECRET_KEY"),
60-
"https",
61-
))),
62-
Err(_) => None,
63-
};
64-
Uploader::S3 {
65-
bucket: Box::new(s3::Bucket::new(
66-
env("S3_BUCKET"),
67-
dotenvy::var("S3_REGION")
68-
.map_or_else(|_err| s3::Region::Default, s3::Region::Region),
69-
env("AWS_ACCESS_KEY"),
70-
env("AWS_SECRET_KEY"),
71-
"https",
72-
)),
73-
index_bucket,
74-
cdn: dotenvy::var("S3_CDN").ok(),
75-
}
76-
}
77-
78-
fn s3_maybe_read_only() -> Uploader {
79-
let index_bucket = match dotenvy::var("S3_INDEX_BUCKET") {
80-
Ok(name) => Some(Box::new(s3::Bucket::new(
81-
name,
82-
dotenvy::var("S3_INDEX_REGION")
83-
.map_or_else(|_err| s3::Region::Default, s3::Region::Region),
84-
dotenvy::var("AWS_ACCESS_KEY").unwrap_or_default(),
85-
dotenvy::var("AWS_SECRET_KEY").unwrap_or_default(),
86-
"https",
87-
))),
88-
Err(_) => None,
89-
};
90-
Uploader::S3 {
91-
bucket: Box::new(s3::Bucket::new(
92-
env("S3_BUCKET"),
93-
dotenvy::var("S3_REGION")
94-
.map_or_else(|_err| s3::Region::Default, s3::Region::Region),
95-
dotenvy::var("AWS_ACCESS_KEY").unwrap_or_default(),
96-
dotenvy::var("AWS_SECRET_KEY").unwrap_or_default(),
97-
"https",
98-
)),
99-
index_bucket,
100-
cdn: dotenvy::var("S3_CDN").ok(),
101-
}
18+
Self { env }
10219
}
10320
}

src/config/server.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ipnetwork::IpNetwork;
33
use oauth2::{ClientId, ClientSecret};
44

55
use crate::publish_rate_limit::PublishRateLimit;
6-
use crate::{env, env_optional, uploaders::Uploader, Env};
6+
use crate::{env, env_optional, Env};
77

88
use super::base::Base;
99
use super::database_pools::DatabasePools;
@@ -196,10 +196,6 @@ impl Server {
196196
pub fn env(&self) -> Env {
197197
self.base.env
198198
}
199-
200-
pub fn uploader(&self) -> &Uploader {
201-
self.base.uploader()
202-
}
203199
}
204200

205201
pub(crate) fn domain_name() -> String {

src/controllers/krate/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ pub async fn readme(
312312
Path((crate_name, version)): Path<(String, String)>,
313313
req: Parts,
314314
) -> Response {
315-
let redirect_url = app.config.uploader().readme_location(&crate_name, &version);
315+
let redirect_url = app.storage.readme_location(&crate_name, &version);
316316
if req.wants_json() {
317317
Json(json!({ "url": redirect_url })).into_response()
318318
} else {

src/controllers/version/downloads.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub async fn download(
125125
.await?
126126
};
127127

128-
let redirect_url = app.config.uploader().crate_location(&crate_name, &version);
128+
let redirect_url = app.storage.crate_location(&crate_name, &version);
129129
if wants_json {
130130
Ok(Json(json!({ "url": redirect_url })).into_response())
131131
} else {

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern crate serde_json;
2323
#[macro_use]
2424
extern crate tracing;
2525

26-
pub use crate::{app::App, email::Emails, uploaders::Uploader};
26+
pub use crate::{app::App, email::Emails};
2727
use std::str::FromStr;
2828
use std::sync::Arc;
2929

@@ -52,7 +52,6 @@ pub mod sql;
5252
pub mod ssh;
5353
pub mod swirl;
5454
mod test_util;
55-
pub mod uploaders;
5655
pub mod util;
5756
pub mod worker;
5857

0 commit comments

Comments
 (0)