Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add postinstall and data to cargo psibase package #840

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/psidk/src/development/services/rust-service/package.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The available fields are:
- `server`: May be present on any crate that builds a service. The value is a crate which will handle HTTP requests sent to this service. The other crate will be built and included in the current package.
- `plugin`: May be present on any crate that builds a service. The value is a crate that should be built with `cargo component` and uploaded as `/plugin.wasm`
- `flags`: [Flags](../../../specifications/data-formats/package.md#serviceservicejson) for the service.
- `postinstall`: An array of actions to run when the package is installed. May be specified on the top-level crate or on any service. Actions from a single `postinstall` will be run in order. The order of actions from multiple crates is unspecified.
- `data`: An array of `{src, dst}` paths to upload to the service. `src` is relative to the location of Cargo.toml. If `src` is a directory, its contents will be included recursively.
- `dependencies`: Additional packages, not build by cargo, that the package depends on.

Example:
Expand All @@ -29,6 +31,10 @@ flags = []
server = "example"
# Plugin for the front end
plugin = "example-plugin"
# Run the service's init action
postinstall = [{sender="tpack", service="tpack", method="init", rawData="0000"}]
# Upload the UI
data = [{src = "ui/", dst = "/"}]

[package.metadata.psibase.dependencies]
HttpServer = "0.12.0"
Expand Down
143 changes: 109 additions & 34 deletions rust/cargo-psibase/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::{build, build_plugin, Args, SERVICE_POLYFILL};
use anyhow::anyhow;
use cargo_metadata::{Metadata, Node, Package, PackageId};
use psibase::{
AccountNumber, Checksum256, ExactAccountNumber, Meta, PackageInfo, PackageRef, PackagedService,
ServiceInfo,
AccountNumber, Action, Checksum256, ExactAccountNumber, Meta, PackageInfo, PackageRef,
PackagedService, ServiceInfo,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
Expand All @@ -14,6 +14,12 @@ use std::io::{BufReader, Seek, Write};
use std::path::{Path, PathBuf};
use zip::write::{FileOptions, ZipWriter};

#[derive(Serialize, Deserialize, Default)]
struct DataFiles {
src: String,
dst: String,
}

#[derive(Serialize, Deserialize, Default)]
#[serde(default)]
pub struct PsibaseMetadata {
Expand All @@ -24,6 +30,8 @@ pub struct PsibaseMetadata {
flags: Vec<String>,
dependencies: HashMap<String, String>,
services: Option<Vec<String>>,
postinstall: Option<Vec<Action>>,
data: Vec<DataFiles>,
}

impl PsibaseMetadata {
Expand Down Expand Up @@ -103,7 +111,8 @@ fn should_build_package(
filename: &Path,
meta: &Meta,
services: &[(&String, ServiceInfo, PathBuf)],
data: &[(&String, &str, PathBuf)],
data: &[(&String, String, PathBuf)],
postinstall: &[Action],
) -> Result<bool, anyhow::Error> {
let timestamp = if let Ok(metadata) = filename.metadata() {
metadata.modified()?
Expand Down Expand Up @@ -146,16 +155,51 @@ fn should_build_package(
return Ok(true);
}
let account: AccountNumber = service.parse()?;
new_data.push((account.value, *dest));
new_data.push((account.value, dest.as_str()));
}
existing_data.sort();
new_data.sort();
if existing_data != new_data {
return Ok(true);
}
// check postinstall
let mut existing_postinstall = Vec::new();
existing.postinstall(&mut existing_postinstall)?;
if &existing_postinstall[..] != postinstall {
return Ok(true);
}
Ok(false)
}

fn add_files<'a>(
service: &'a String,
src: &Path,
dest: &String,
out: &mut Vec<(&'a String, String, PathBuf)>,
) -> Result<(), anyhow::Error> {
if src.is_file() {
out.push((service, dest.clone(), src.to_path_buf()));
} else if src.is_dir() {
for entry in src.read_dir()? {
let entry = entry?;
add_files(
service,
&entry.path(),
&(dest.clone()
+ "/"
+ entry.file_name().to_str().ok_or_else(|| {
anyhow!(
"non-unicode file name: {}",
entry.file_name().to_string_lossy()
)
})?),
out,
)?;
}
}
Ok(())
}

pub async fn build_package(
args: &Args,
metadata: &MetadataIndex<'_>,
Expand All @@ -168,6 +212,8 @@ pub async fn build_package(
let mut services = Vec::new();
let mut plugins = Vec::new();
let mut data_files = Vec::new();
let mut postinstall = Vec::new();
let mut data_sources = Vec::new();

let get_dep = get_dep_type(|service, dep| {
let r = metadata.resolved.get(&service.id.repr.as_str()).unwrap();
Expand Down Expand Up @@ -203,6 +249,12 @@ pub async fn build_package(
visited.insert(root);
queue.push(root);
}
// Add postinstall from the root whether it is a service or not
if !visited.contains(root) {
if let Some(actions) = &meta.postinstall {
postinstall.extend_from_slice(actions.as_slice());
}
}
} else {
Err(anyhow!("Cannot package a virtual workspace"))?
}
Expand Down Expand Up @@ -242,7 +294,21 @@ pub async fn build_package(
};
plugins.push((plugin, &package.name, "/plugin.wasm", &id.repr));
}
for data in &pmeta.data {
let src = package.manifest_path.parent().unwrap().join(&data.src);
let mut dest = data.dst.clone();
if !dest.starts_with('/') {
dest = "/".to_string() + &dest;
}
if dest.ends_with('/') {
dest.pop();
}
data_sources.push((&package.name, src, dest));
}
services.push((&package.name, info, &package.id.repr));
if let Some(actions) = &pmeta.postinstall {
postinstall.extend_from_slice(actions.as_slice());
}
}
}

Expand Down Expand Up @@ -295,40 +361,49 @@ pub async fn build_package(
plugin
))?
};
data_files.push((service, path, paths.pop().unwrap()))
data_files.push((service, path.to_string(), paths.pop().unwrap()))
}

for (service, src, dest) in data_sources {
add_files(service, src.as_std_path(), &dest, &mut data_files)?;
}

let out_dir = get_package_dir(args, metadata);
let out_path = out_dir.join(package_name.clone() + ".psi");
let mut file = if should_build_package(&out_path, &meta, &service_wasms, &data_files)? {
std::fs::create_dir_all(&out_dir)?;
let mut out = ZipWriter::new(
OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(&out_path)?,
);
let options: FileOptions = FileOptions::default();
out.start_file("meta.json", options)?;
write!(out, "{}", &serde_json::to_string(&meta)?)?;
for (service, info, path) in service_wasms {
out.start_file(format!("service/{}.wasm", service), options)?;
std::io::copy(&mut File::open(path)?, &mut out)?;
out.start_file(format!("service/{}.json", service), options)?;
write!(out, "{}", &serde_json::to_string(&info)?)?;
}
for (service, dest, src) in data_files {
out.start_file(format!("data/{}{}", service, dest), options)?;
std::io::copy(&mut File::open(src)?, &mut out)?;
}
let mut file = out.finish()?;
file.rewind()?;
file
} else {
File::open(out_path)?
};
let mut file =
if should_build_package(&out_path, &meta, &service_wasms, &data_files, &postinstall)? {
std::fs::create_dir_all(&out_dir)?;
let mut out = ZipWriter::new(
OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(&out_path)?,
);
let options: FileOptions = FileOptions::default();
out.start_file("meta.json", options)?;
write!(out, "{}", &serde_json::to_string(&meta)?)?;
for (service, info, path) in service_wasms {
out.start_file(format!("service/{}.wasm", service), options)?;
std::io::copy(&mut File::open(path)?, &mut out)?;
out.start_file(format!("service/{}.json", service), options)?;
write!(out, "{}", &serde_json::to_string(&info)?)?;
}
for (service, dest, src) in data_files {
out.start_file(format!("data/{}{}", service, dest), options)?;
std::io::copy(&mut File::open(src)?, &mut out)?;
}
if !postinstall.is_empty() {
out.start_file("script/postinstall.json", options)?;
write!(out, "{}", &serde_json::to_string(&postinstall)?)?;
}
let mut file = out.finish()?;
file.rewind()?;
file
} else {
File::open(out_path)?
};
// Calculate the package checksum
let mut hasher = Sha256::new();
std::io::copy(&mut file, &mut hasher)?;
Expand Down
2 changes: 2 additions & 0 deletions rust/psibase/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub type BlockNum = u32;
Deserialize,
SimpleObject,
InputObject,
PartialEq,
Eq,
)]
#[fracpack(fracpack_mod = "fracpack")]
#[to_key(psibase_mod = "crate")]
Expand Down
1 change: 1 addition & 0 deletions rust/test_package/query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ publish = false

[package.metadata.psibase]
plugin = "tpack-plugin"
data = [{src = "../ui", dst = "/"}]

[dependencies]
psibase = { path = "../../psibase", version = "0.12.0" }
Expand Down
1 change: 1 addition & 0 deletions rust/test_package/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ publish = false

[package.metadata.psibase]
server = "r-tpack"
postinstall = [{sender="tpack", service="tpack", method="init", rawData="0000"}]

[package.metadata.psibase.dependencies]
HttpServer = "0.12.0"
Expand Down
2 changes: 2 additions & 0 deletions rust/test_package/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod service {
fn foo(value: i32) -> i32 {
value
}
#[action]
fn init() {}
}

#[psibase::test_case(packages("TestPackage"))]
Expand Down
5 changes: 5 additions & 0 deletions rust/test_package/ui/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
Lorem ipsum dolor sit amet
</body>
</html>
Loading