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

feat: run pike without plugin directory #86

Merged
merged 5 commits into from
Mar 5, 2025
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
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[env]
RUST_TEST_THREADS = "1"
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ jobs:
toolchain: "1.85"
components: clippy, rustfmt
- name: Run tests
run: make tests
run: cargo test
env:
RUST_LOG: debug
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ _Используется стандартный процесс открытия

1. **Форкните репозиторий** и создайте новую ветку (`git checkout -b feature-branch`).
2. Внесите изменения и убедитесь, что код проходит проверки (`make style-check`), компилируется и запускается.
3. Запустите тесты (`make tests`).
3. Запустите тесты (`cargo test`).
4. Добавьте информацию о своём изменении в `CHANGELOG.md`.
5. При необходимости внесите изменения в `README.md`.
6. Сделайте коммит, следуя соглашению [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) (см. ниже).
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ keywords = ["picodata", "cargo", "plugin"]
categories = ["development-tools::cargo-plugins"]
readme = "README.md"
rust-version = "1.85"
resolver = "3"

[dependencies]
clap = { version = "4", features = ["derive"] }
Expand Down
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.PHONY: tests
tests:
cargo test -- --test-threads=1

.PHONY: style-check
style-check:
cargo check --all --bins --tests --benches
Expand Down
5 changes: 3 additions & 2 deletions plugin_template/_Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ name = "{{ project_name }}"
version = "0.1.0"
edition = "2024"
publish = false
resolver = "3"

[dependencies]
picodata-plugin = "25.1.1"
serde = { version = "1", features = ["derive"] }
log = "0.4"

[dev-dependencies]
picodata-pike = { git = "https://github.com/picodata/pike.git", branch = "sng_features" } # TODO: change after publish on crates.io
picodata-pike = { git = "https://github.com/picodata/pike.git", branch = "master" } # TODO: change after publish on crates.io
reqwest = { version = "0.12", features = ["blocking"] }

[build-dependencies]
picodata-pike = { git = "https://github.com/picodata/pike.git", branch = "sng_features" } # TODO: change after publish on crates.io
picodata-pike = { git = "https://github.com/picodata/pike.git", branch = "master" } # TODO: change after publish on crates.io
liquid = "0.26"
fs_extra = "1"

Expand Down
20 changes: 16 additions & 4 deletions src/commands/plugin/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ use std::{
use include_dir::{include_dir, Dir, DirEntry};

static PLUGIN_TEMPLATE: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/plugin_template");
static WS_CARGO_MANIFEST_TEMPLATE: &str = r#"[workspace]
resolver = "3"
members = [
"{{ project_name }}",
]
"#;

fn place_file(target_path: &Path, t_ctx: &liquid::Object, entries: &[DirEntry<'_>]) -> Result<()> {
for entry in entries {
Expand Down Expand Up @@ -71,14 +77,19 @@ where
Ok(())
}

fn workspace_init(root_path: &Path, project_name: &str) -> Result<()> {
fn workspace_init(root_path: &Path, project_name: &str, t_ctx: &liquid::Object) -> Result<()> {
let cargo_toml_path = root_path.join("Cargo.toml");

let mut cargo_toml =
File::create(cargo_toml_path).context("failed to create Cargo.toml for workspace")?;

cargo_toml
.write_all(format!("[workspace]\nmembers = [\n \"{project_name}\",\n]").as_bytes())?;
let ws_template = liquid::ParserBuilder::with_stdlib()
.build()
.context("couldn't build from template")?
.parse(WS_CARGO_MANIFEST_TEMPLATE)
.unwrap();

cargo_toml.write_all(ws_template.render(&t_ctx).unwrap().as_bytes())?;

fs::copy(
root_path.join(project_name).join("topology.toml"),
Expand Down Expand Up @@ -138,7 +149,8 @@ pub fn cmd(path: Option<&Path>, without_git: bool, init_workspace: bool) -> Resu
}

if init_workspace {
workspace_init(&path, project_name).context("failed to initiate workspace")?;
workspace_init(&path, project_name, &templates_ctx)
.context("failed to initiate workspace")?;
}

Ok(())
Expand Down
87 changes: 60 additions & 27 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@ fn enable_plugins(topology: &Topology, data_dir: &Path, picodata_path: &PathBuf)
Ok(())
}

fn is_plugin_dir(path: &Path) -> bool {
if !path.is_dir() {
return false;
}
if !path.join("Cargo.toml").exists() {
return false;
}

if path.join("manifest.yaml.template").exists() {
return true;
}

fs::read_dir(path)
.unwrap()
.filter(Result::is_ok)
.map(|e| e.unwrap().path())
.filter(|e| e.is_dir())
.any(|dir| dir.join("manifest.yaml.template").exists())
}

pub struct PicodataInstance {
instance_name: String,
tier: String,
Expand All @@ -192,7 +212,7 @@ impl PicodataInstance {
http_port: u16,
pg_port: u16,
first_instance_bin_port: u16,
plugins_dir: &Path,
plugins_dir: Option<&Path>,
replication_factor: u8,
tier: &str,
run_params: &Params,
Expand Down Expand Up @@ -232,8 +252,6 @@ impl PicodataInstance {
"run",
data_dir_flag,
instance_data_dir.to_str().expect("unreachable"),
"--plugin-dir",
plugins_dir.to_str().unwrap_or("target/debug"),
listen_flag,
&format!("127.0.0.1:{bin_port}"),
"--peer",
Expand All @@ -248,6 +266,13 @@ impl PicodataInstance {
tier,
]);

if let Some(plugins_dir) = plugins_dir {
child.args([
"--plugin-dir",
plugins_dir.to_str().unwrap_or("target/debug"),
]);
}

if run_params.daemon {
child.stdout(Stdio::null()).stderr(Stdio::null());
child.args(["--log", log_file_path.to_str().expect("unreachable")]);
Expand Down Expand Up @@ -417,22 +442,28 @@ pub fn cluster(params: &Params) -> Result<Vec<PicodataInstance>> {
let mut params = params.clone();
params.data_dir = params.plugin_path.join(&params.data_dir);

let plugins_dir = if params.use_release {
cargo_build(
lib::BuildType::Release,
&params.target_dir,
&params.plugin_path,
)?;
params.plugin_path.join(params.target_dir.join("release"))
} else {
cargo_build(
lib::BuildType::Debug,
&params.target_dir,
&params.plugin_path,
)?;
params.plugin_path.join(params.target_dir.join("debug"))
};
params.topology.find_plugin_versions(&plugins_dir)?;
let mut plugins_dir = None;
if is_plugin_dir(&params.plugin_path) {
plugins_dir = if params.use_release {
cargo_build(
lib::BuildType::Release,
&params.target_dir,
&params.plugin_path,
)?;
Some(params.plugin_path.join(params.target_dir.join("release")))
} else {
cargo_build(
lib::BuildType::Debug,
&params.target_dir,
&params.plugin_path,
)?;
Some(params.plugin_path.join(params.target_dir.join("debug")))
};

params
.topology
.find_plugin_versions(plugins_dir.as_ref().unwrap())?;
}

info!("Running the cluster...");

Expand All @@ -451,7 +482,7 @@ pub fn cluster(params: &Params) -> Result<Vec<PicodataInstance>> {
params.base_http_port + instance_id,
params.base_pg_port + instance_id,
first_instance_bin_port,
&plugins_dir,
plugins_dir.as_deref(),
tier.replication_factor,
tier_name,
&params,
Expand All @@ -477,14 +508,16 @@ pub fn cluster(params: &Params) -> Result<Vec<PicodataInstance>> {
thread::sleep(Duration::from_secs(5));
}

let result = enable_plugins(&params.topology, &params.data_dir, &params.picodata_path);
if let Err(e) = result {
for process in &mut picodata_processes {
process.kill().unwrap_or_else(|e| {
error!("failed to kill picodata instances: {:#}", e);
});
if plugins_dir.is_some() {
let result = enable_plugins(&params.topology, &params.data_dir, &params.picodata_path);
if let Err(e) = result {
for process in &mut picodata_processes {
process.kill().unwrap_or_else(|e| {
error!("failed to kill picodata instances: {:#}", e);
});
}
return Err(e.context("failed to enable plugins"));
}
return Err(e.context("failed to enable plugins"));
}
};

Expand Down
23 changes: 13 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,15 @@ fn run_child_killer() {
process::exit(0)
}

fn check_plugin_directory(plugin_dir: &Path) {
if !plugin_dir.join("./topology.toml").exists() {
println!("{CARING_PIKE}");

process::exit(1);
fn is_required_path_exists(plugin_dir: &Path, required_path: &Path) {
if required_path.exists() {
return;
}
if plugin_dir.join(required_path).exists() {
return;
}
println!("{CARING_PIKE}");
process::exit(1);
}

// Add new member to Cargo.toml, additionally checks proper
Expand Down Expand Up @@ -289,7 +292,7 @@ fn main() -> Result<()> {
disable_colors,
plugin_path,
} => {
check_plugin_directory(&plugin_path);
is_required_path_exists(&plugin_path, &topology);

if !daemon {
run_child_killer();
Expand Down Expand Up @@ -323,7 +326,7 @@ fn main() -> Result<()> {
data_dir,
plugin_path,
} => {
check_plugin_directory(&plugin_path);
is_required_path_exists(&plugin_path, &data_dir);

run_child_killer();
let params = commands::stop::ParamsBuilder::default()
Expand All @@ -345,7 +348,7 @@ fn main() -> Result<()> {
target_dir,
plugin_path,
} => {
check_plugin_directory(&plugin_path);
is_required_path_exists(&plugin_path, Path::new("Cargo.toml"));

commands::plugin::pack::cmd(debug, &target_dir, &plugin_path)
.context("failed to execute \"pack\" command")?;
Expand All @@ -355,7 +358,7 @@ fn main() -> Result<()> {
target_dir,
plugin_path,
} => {
check_plugin_directory(&plugin_path);
is_required_path_exists(&plugin_path, Path::new("Cargo.toml"));

commands::plugin::build::cmd(release, &target_dir, &plugin_path)
.context("failed to execute \"build\" command")?;
Expand All @@ -372,7 +375,7 @@ fn main() -> Result<()> {
} => commands::plugin::new::cmd(None, without_git, workspace)
.context("failed to execute \"init\" command")?,
Plugin::Add { path, plugin_path } => {
check_plugin_directory(&plugin_path);
is_required_path_exists(&plugin_path, Path::new("Cargo.toml"));

modify_workspace(path.file_name().unwrap().to_str().unwrap(), &plugin_path)
.context("failed to add new plugin to workspace")?;
Expand Down
26 changes: 13 additions & 13 deletions tests/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod helpers;

use helpers::{build_plugin, check_plugin_version_artefacts, exec_pike, PLUGIN_DIR, TESTS_DIR};
use helpers::{assert_plugin_build_artefacts, build_plugin, exec_pike, PLUGIN_DIR, TESTS_DIR};
use std::{
fs::{self},
path::Path,
Expand All @@ -25,39 +25,39 @@ fn test_cargo_build() {
build_plugin(&helpers::BuildType::Release, "0.1.0");
build_plugin(&helpers::BuildType::Release, "0.1.1");

assert!(check_plugin_version_artefacts(
assert_plugin_build_artefacts(
&Path::new(PLUGIN_DIR)
.join("target")
.join("debug")
.join("test-plugin")
.join("0.1.0"),
false
));
false,
);

assert!(check_plugin_version_artefacts(
assert_plugin_build_artefacts(
&Path::new(PLUGIN_DIR)
.join("target")
.join("debug")
.join("test-plugin")
.join("0.1.1"),
true
));
true,
);

assert!(check_plugin_version_artefacts(
assert_plugin_build_artefacts(
&Path::new(PLUGIN_DIR)
.join("target")
.join("release")
.join("test-plugin")
.join("0.1.0"),
false
));
false,
);

assert!(check_plugin_version_artefacts(
assert_plugin_build_artefacts(
&Path::new(PLUGIN_DIR)
.join("target")
.join("release")
.join("test-plugin")
.join("0.1.1"),
true
));
true,
);
}
Loading