Unverified Commit 913e3936 authored by Vasili Novikov's avatar Vasili Novikov
Browse files

Move docker-related commands to a separate file

parent 4c84a6a9
Showing with 116 additions and 108 deletions
+116 -108
......@@ -12,7 +12,6 @@ use crate::sql_converters::sqlite_rows_to_json;
use crate::sql_converters::validate_property_name;
use chrono::Utc;
use log::debug;
use log::info;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rusqlite::ToSql;
......@@ -21,7 +20,6 @@ use rusqlite::NO_PARAMS;
use serde_json::value::Value::Object;
use serde_json::Value;
use std::collections::HashMap;
use std::process::Command;
use std::str;
use warp::http::status::StatusCode;
......@@ -420,106 +418,3 @@ pub fn get_items_with_edges(
tx.commit()?;
Ok(result)
}
fn docker_arguments() -> Vec<String> {
if std::env::var_os("POD_IS_IN_DOCKER").is_some() {
vec![
"--network=pod_memri-net".to_string(),
"--env=POD_ADDRESS=pod_pod_1".to_string(),
]
} else {
// The indexers/importers/downloaders need to have access to the host
// This is currently done differently on MacOS and Linux
// https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach
let pod_address = if cfg!(target_os = "linux") {
"localhost"
} else {
"host.docker.internal"
};
vec![
format!("--env=POD_ADDRESS={}", pod_address),
"--network=host".to_string(),
]
}
}
pub fn run_downloaders(service: String, data_type: String) -> Result<()> {
info!("Trying to run downloader {} for {}", service, data_type);
match service.as_str() {
"evernote" => match data_type.as_str() {
"note" => {
Command::new("docker")
.arg("run")
.args(&docker_arguments())
.args(&["--rm", "--name=memri-indexers_1", "-it"])
.args(&["memri-downloaders:latest"])
.spawn()
.expect("Failed to run downloader");
}
_ => {
return Err(Error {
code: StatusCode::BAD_REQUEST,
msg: format!("Data type {} not supported", data_type),
})
}
},
_ => {
return Err(Error {
code: StatusCode::BAD_REQUEST,
msg: format!("Service {} not supported", service),
})
}
}
Ok(())
}
pub fn run_importers(data_type: String) -> Result<()> {
info!("Trying to run importer for {}", data_type);
match data_type.as_str() {
"note" => {
Command::new("docker")
.arg("run")
.args(&docker_arguments())
.args(&[
"--rm",
"--volume=download-volume:/usr/src/importers/data",
"--name=memri-importers_1",
])
.args(&["memri-importers:latest"])
.spawn()
.expect("Failed to run importer");
}
_ => {
return Err(Error {
code: StatusCode::BAD_REQUEST,
msg: format!("Data type {} not supported", data_type),
})
}
}
Ok(())
}
pub fn run_indexers(sqlite: &Pool<SqliteConnectionManager>, uid: i64) -> Result<()> {
info!("Trying to run indexer on item {}", uid);
let result = get_item(sqlite, uid)?;
match result.first() {
Some(_item) => {
Command::new("docker")
.arg("run")
.args(&docker_arguments())
.args(&[
"--rm",
"--name=memri-indexers_1",
&format!("--env=RUN_UID={}", uid),
])
.args(&["memri-indexers:latest"])
.spawn()
.expect("Failed to run indexer");
Ok(())
}
None => Err(Error {
code: StatusCode::BAD_REQUEST,
msg: format!("Failed to get item {}", uid),
}),
}
}
......@@ -7,6 +7,7 @@ mod database_migrate_refinery;
pub mod database_migrate_schema;
mod error;
pub mod internal_api;
pub mod services_api;
mod sql_converters;
mod warp_api;
......
use crate::error::Error;
use crate::error::Result;
use crate::internal_api;
use log::info;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use std::process::Command;
use warp::http::status::StatusCode;
fn docker_arguments() -> Vec<String> {
if std::env::var_os("POD_IS_IN_DOCKER").is_some() {
vec![
"--network=pod_memri-net".to_string(),
"--env=POD_ADDRESS=pod_pod_1".to_string(),
]
} else {
// The indexers/importers/downloaders need to have access to the host
// This is currently done differently on MacOS and Linux
// https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach
let pod_address = if cfg!(target_os = "linux") {
"localhost"
} else {
"host.docker.internal"
};
vec![
format!("--env=POD_ADDRESS={}", pod_address),
"--network=host".to_string(),
]
}
}
pub fn run_downloaders(service: String, data_type: String) -> Result<()> {
info!("Trying to run downloader {} for {}", service, data_type);
match service.as_str() {
"evernote" => match data_type.as_str() {
"note" => {
Command::new("docker")
.arg("run")
.args(&docker_arguments())
.args(&["--rm", "--name=memri-indexers_1", "-it"])
.args(&["memri-downloaders:latest"])
.spawn()
.expect("Failed to run downloader");
}
_ => {
return Err(Error {
code: StatusCode::BAD_REQUEST,
msg: format!("Data type {} not supported", data_type),
})
}
},
_ => {
return Err(Error {
code: StatusCode::BAD_REQUEST,
msg: format!("Service {} not supported", service),
})
}
}
Ok(())
}
pub fn run_importers(data_type: String) -> Result<()> {
info!("Trying to run importer for {}", data_type);
match data_type.as_str() {
"note" => {
Command::new("docker")
.arg("run")
.args(&docker_arguments())
.args(&[
"--rm",
"--volume=download-volume:/usr/src/importers/data",
"--name=memri-importers_1",
])
.args(&["memri-importers:latest"])
.spawn()
.expect("Failed to run importer");
}
_ => {
return Err(Error {
code: StatusCode::BAD_REQUEST,
msg: format!("Data type {} not supported", data_type),
})
}
}
Ok(())
}
pub fn run_indexers(sqlite: &Pool<SqliteConnectionManager>, uid: i64) -> Result<()> {
info!("Trying to run indexer on item {}", uid);
let result = internal_api::get_item(sqlite, uid)?;
match result.first() {
Some(_item) => {
Command::new("docker")
.arg("run")
.args(&docker_arguments())
.args(&[
"--rm",
"--name=memri-indexers_1",
&format!("--env=RUN_UID={}", uid),
])
.args(&["memri-indexers:latest"])
.spawn()
.expect("Failed to run indexer");
Ok(())
}
None => Err(Error {
code: StatusCode::BAD_REQUEST,
msg: format!("Failed to get item {}", uid),
}),
}
}
use crate::internal_api;
use crate::services_api;
use bytes::Bytes;
use log::info;
use log::warn;
......@@ -155,7 +156,7 @@ pub async fn run_server(sqlite_pool: Pool<SqliteConnectionManager>) {
.and(warp::path::end())
.and(warp::post())
.map(move |service: String, data_type: String| {
let result = internal_api::run_downloaders(service, data_type);
let result = services_api::run_downloaders(service, data_type);
let result = result.map(|()| warp::reply::json(&serde_json::json!({})));
respond_with_result(result)
});
......@@ -165,7 +166,7 @@ pub async fn run_server(sqlite_pool: Pool<SqliteConnectionManager>) {
.and(warp::path::end())
.and(warp::post())
.map(move |data_type: String| {
let result = internal_api::run_importers(data_type);
let result = services_api::run_importers(data_type);
respond_with_result(result.map(|()| warp::reply::json(&serde_json::json!({}))))
});
......@@ -175,7 +176,7 @@ pub async fn run_server(sqlite_pool: Pool<SqliteConnectionManager>) {
.and(warp::path::end())
.and(warp::post())
.map(move |uid: i64| {
let result = internal_api::run_indexers(&pool, uid);
let result = services_api::run_indexers(&pool, uid);
respond_with_result(result.map(|()| warp::reply::json(&serde_json::json!({}))))
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment