marine/README.md

132 lines
3.5 KiB
Markdown
Raw Normal View History

2020-05-02 09:44:49 +00:00
# Fluence Compute Engine
2020-08-11 12:28:53 +00:00
FCE is intended to run various Wasm binaries. At now, it is in the heavily developing phase. Docs and tutorials are also in the work-in-progress state.
2020-07-16 15:35:52 +00:00
2020-07-27 22:19:15 +00:00
## Installation
2020-08-11 12:28:53 +00:00
- `cargo install fcli`
this will add `fce` binary to your system.
2020-07-16 15:35:52 +00:00
2020-07-27 22:19:15 +00:00
## Usage
2020-07-16 15:35:52 +00:00
- `fce build` in Rust project
2020-07-27 22:19:15 +00:00
## HOW TO: Create App with FCE Modules
2020-07-16 15:35:52 +00:00
2020-07-27 22:19:15 +00:00
### Recommendations:
2020-07-16 15:35:52 +00:00
2020-07-20 09:32:02 +00:00
- Modules architecture should be upwards from `effectors` (modules that persist data and WASI modules) that will work with local binaries, local storage and syscalls to `pure modules` that perform business logic.
2020-10-01 09:23:29 +00:00
- Split app to small FCE modules are easier to support, reuse and distribute
2020-07-16 15:35:52 +00:00
- Each module for its own task (npm like)
2020-07-27 22:19:15 +00:00
### Module project structure
2020-07-16 15:35:52 +00:00
- Init simple rust project `cargo init --bin`
- `Config.toml`:
2020-07-26 18:56:10 +00:00
```toml
2020-07-18 12:44:02 +00:00
[[bin]]
2020-07-16 15:35:52 +00:00
name = "wasm_application"
2020-07-18 12:44:02 +00:00
path = "src/main.rs"
2020-07-16 15:35:52 +00:00
[dependencies]
2020-07-27 22:19:15 +00:00
# logger - if you'll use logging
2020-07-16 15:35:52 +00:00
fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"] }
```
2020-07-20 09:32:02 +00:00
- Methods that will be exported from this module marked with `#[fce]`
2020-07-26 16:38:11 +00:00
```rust
2020-07-16 15:35:52 +00:00
use fluence::fce;
#[fce]
pub fn get(url: String) -> String {
...
}
```
2020-07-20 09:32:02 +00:00
- Multiple arguments with primitive Rust types (`bool, u8, u16, u32, u64, i8, i16, i32, i64, f32, f64, String, Vec<u8>`) and only one return argument could be used
2020-07-16 15:35:52 +00:00
2020-10-01 09:23:29 +00:00
- Build project with `fce build` (supports --release and all other cargo flags as usual)
2020-07-16 15:35:52 +00:00
2020-09-11 12:58:38 +00:00
- Copy wasm file from `target/wasm32-wasi/debug` or `target/wasm32-wasi/release` to directory with other modules
2020-07-16 15:35:52 +00:00
- To import other wasm modules to your project use similar code:
2020-07-26 16:38:11 +00:00
```rust
2020-07-16 15:35:52 +00:00
#[fce]
2020-09-11 12:58:38 +00:00
#[link(wasm_import_module = "curl")]
2020-07-16 15:35:52 +00:00
extern "C" {
#[link_name = "get"]
pub fn curl_get(url: String) -> String;
}
#[fce]
2020-09-11 12:58:38 +00:00
#[link(wasm_import_module = "local_storage")]
2020-07-16 15:35:52 +00:00
extern "C" {
#[link_name = "get"]
pub fn curl_get(url: String) -> String;
}
```
2020-07-27 22:19:15 +00:00
### Combine modules to Application
2020-07-16 15:35:52 +00:00
- Create simple Rust project
- Create `Config.toml` to describe existed wasm modules and give accesses to host binaries and local storage if needed:
2020-07-26 18:56:10 +00:00
```toml
2020-09-11 12:58:38 +00:00
modules_dir = "wasm/artifacts/modules/"
2020-07-16 15:35:52 +00:00
2020-08-23 22:55:52 +00:00
[[module]]
2020-09-11 12:58:38 +00:00
name = "local_storage"
logger_enabled = true
2020-07-16 15:35:52 +00:00
2020-09-11 12:58:38 +00:00
[module.wasi]
preopened_files = ["./wasm/artifacts"]
mapped_dirs = { "sites" = "./sites" }
[[module]]
name = "curl"
logger_enabled = true
2020-10-01 09:23:29 +00:00
[module.mounted_binaries]
2020-07-16 15:35:52 +00:00
curl = "/usr/bin/curl"
2020-09-11 12:58:38 +00:00
[[module]]
name = "site-storage"
mem_pages_count = 10000
logger_enabled = true
2020-10-01 09:23:29 +00:00
2020-08-23 22:55:52 +00:00
[module.wasi]
2020-10-01 09:23:29 +00:00
envs = { "ENV_ONE" = "parameter-one" }
2020-07-16 15:35:52 +00:00
```
2020-08-23 22:55:52 +00:00
`modules_dir` - path to directory with all modules. All subsequent paths will be relative to this path
2020-07-16 15:35:52 +00:00
2020-08-23 22:55:52 +00:00
`[[module]]` - modules list
2020-07-16 15:35:52 +00:00
2020-08-23 22:55:52 +00:00
`name` - wasm file name in `modules_dir`
2020-07-16 15:35:52 +00:00
`mem_pages_count` - a maximum number of Wasm memory pages that loaded module can use. Each Wasm pages is 65536 bytes long
2020-10-01 09:23:29 +00:00
`[module.mounted_binaries]` - list of mounted binary executable files
2020-07-16 15:35:52 +00:00
`curl = "/usr/bin/curl"` - gives possibility to call binary file `/usr/bin/curl` as method `curl` in Rust code
Import example:
2020-07-26 16:38:11 +00:00
```rust
2020-09-11 12:58:38 +00:00
#[fce]
2020-07-16 15:35:52 +00:00
#[link(wasm_import_module = "host")]
extern "C" {
2020-07-20 09:32:02 +00:00
fn curl(args: String) -> String;
2020-07-16 15:35:52 +00:00
}
```
2020-07-20 09:32:02 +00:00
Call binary with arguments: `curl("-vvv ya.ru")`
2020-07-16 15:35:52 +00:00
2020-08-23 22:55:52 +00:00
`[module.wasi]` - this block manages communication with the "outside" world
2020-07-16 15:35:52 +00:00
`env` - environment variables. Usage: `std::env::var("IPFS_ADDR")`
`preopened_files` - list of available directories for loaded modules
`mapped_dirs` - mapping between paths
Working with files as usual:
2020-07-26 16:38:11 +00:00
```rust
2020-07-16 15:35:52 +00:00
fs::write(PathBuf::from("/tmp/somefile"), vec!(1,2,3));
fs::read(...);
2020-10-01 09:23:29 +00:00
```