Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
David Kosztka
Pod
Commits
913e3936
Unverified
Commit
913e3936
authored
4 years ago
by
Vasili Novikov
Browse files
Options
Download
Email Patches
Plain Diff
Move docker-related commands to a separate file
parent
4c84a6a9
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/internal_api.rs
+0
-105
src/internal_api.rs
src/main.rs
+1
-0
src/main.rs
src/services_api.rs
+111
-0
src/services_api.rs
src/warp_api.rs
+4
-3
src/warp_api.rs
with
116 additions
and
108 deletions
+116
-108
src/internal_api.rs
+
0
-
105
View file @
913e3936
...
...
@@ -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
),
}),
}
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
1
-
0
View file @
913e3936
...
...
@@ -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
;
...
...
This diff is collapsed.
Click to expand it.
src/services_api.rs
0 → 100644
+
111
-
0
View file @
913e3936
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
),
}),
}
}
This diff is collapsed.
Click to expand it.
src/warp_api.rs
+
4
-
3
View file @
913e3936
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!
({}))))
});
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment