Skip to content

Commit 16e6017

Browse files
Add s3 migration command
This will pull 5,000 files (essentially randomly chosen) out of the DB and upload them to the configured S3 instance.
1 parent ff6b23e commit 16e6017

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ schemamama_postgres = "0.2"
3434
rusoto_s3 = "0.40"
3535
rusoto_core = "0.40"
3636
rusoto_credential = "0.40"
37-
37+
futures = "0.1"
38+
tokio = "0.1"
3839

3940
# iron dependencies
4041
iron = "0.5"

src/bin/cratesfyi.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ pub fn main() {
109109
.subcommand(SubCommand::with_name("daemon").about("Starts cratesfyi daemon"))
110110
.subcommand(SubCommand::with_name("database")
111111
.about("Database operations")
112+
.subcommand(SubCommand::with_name("move-to-s3"))
112113
.subcommand(SubCommand::with_name("migrate")
113114
.about("Run database migrations")
114115
.arg(Arg::with_name("VERSION")))
@@ -239,6 +240,9 @@ pub fn main() {
239240
} else if let Some(_) = matches.subcommand_matches("update-search-index") {
240241
let conn = db::connect_db().unwrap();
241242
db::update_search_index(&conn).expect("Failed to update search index");
243+
} else if let Some(_) = matches.subcommand_matches("move-to-s3") {
244+
let conn = db::connect_db().unwrap();
245+
db::file::move_to_s3(&conn, 5_000).expect("Failed to update search index");
242246
}
243247
} else if let Some(matches) = matches.subcommand_matches("start-web-server") {
244248
start_web_server(Some(matches.value_of("SOCKET_ADDR").unwrap_or("0.0.0.0:3000")));

src/db/file.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,52 @@ fn file_list_to_json(file_list: Vec<(String, PathBuf)>) -> Result<Json> {
224224
Ok(file_list_json.to_json())
225225
}
226226

227+
pub fn move_to_s3(conn: &Connection, n: usize) -> Result<()> {
228+
let trans = try!(conn.transaction());
229+
let client = s3_client().expect("configured s3");
230+
231+
let rows = try!(trans.query(
232+
&format!("SELECT path, mime, content FROM files WHERE content != E'in-s3' LIMIT {}", n),
233+
&[]));
234+
235+
let mut rt = ::tokio::runtime::current_thread::Runtime::new().unwrap();
236+
let mut futures = Vec::new();
237+
for row in &rows {
238+
let path: String = row.get(0);
239+
let mime: String = row.get(1);
240+
let content: Vec<u8> = row.get(2);
241+
let path_1 = path.clone();
242+
futures.push(client.put_object(PutObjectRequest {
243+
bucket: "rust-docs-rs".into(),
244+
key: path.clone(),
245+
body: Some(content.into()),
246+
content_type: Some(mime),
247+
..Default::default()
248+
}).map(move |_| {
249+
path_1
250+
}).map_err(move |e| {
251+
panic!("failed to upload to {}: {:?}", path, e)
252+
}));
253+
}
254+
255+
use ::futures::future::Future;
256+
match rt.block_on(::futures::future::join_all(futures)) {
257+
Ok(paths) => {
258+
let statement = trans.prepare("UPDATE files SET content = E'in-s3' WHERE path = $1")
259+
.unwrap();
260+
for path in paths {
261+
statement.execute(&[&path]).unwrap();
262+
}
263+
}
264+
Err(e) => {
265+
panic!("results err: {:?}", e);
266+
}
267+
}
268+
269+
try!(trans.commit());
227270

271+
Ok(())
272+
}
228273

229274
#[cfg(test)]
230275
mod test {

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ extern crate schemamama_postgres;
3333
extern crate rusoto_s3;
3434
extern crate rusoto_core;
3535
extern crate rusoto_credential;
36+
extern crate futures;
37+
extern crate tokio;
3638

3739
pub use self::docbuilder::DocBuilder;
3840
pub use self::docbuilder::ChrootBuilderResult;

0 commit comments

Comments
 (0)