1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
#![warn(missing_docs)]
//! A library and CLI to help create, run and interact with holochain conductor sandboxes.
//! **Warning this is still WIP and subject to change**
//! There's probably a few bugs. If you find one please open an [issue](https://github.com/holochain/holochain/issues)
//! or make a PR.
//!
//! ## CLI
//! The `hc sandbox` CLI makes it easy to run a dna that you are working on
//! or someone has sent you.
//! It has been designed to use sensible defaults but still give you
//! the configurability when that's required.
//! Sandboxes are stored in tmp directories by default and the paths are
//! persisted in a `.hcXXX` file which is created wherever you are using
//! the CLI.
//! ### Install
//! #### Requirements
//! - [Rust](https://rustup.rs/)
//! - [Holochain](https://github.com/holochain/holochain) binary on the path
//! #### Building
//! From github:
//! ```shell
//! cargo install holochain_cli --git https://github.com/holochain/holochain
//! ```
//! From the holochain repo:
//! ```shell
//! cargo install --path crates/hcYYY
//! ```
//! ### Common usage
//! The best place to start is:
//! ```shell
//! hc sandbox -h
//! ```
//! This will be more up to date then this readme.
//! #### Run
//! This command can be used to generate and run conductor sandboxes.
//! ```shell
//! hc sandbox run -h
//! # or shorter
//! hc sandbox r -h
//! ```
//! In a folder with where your `my-dna.dna` is you can generate and run
//! a new sandbox with:
//! ```shell
//! hc sandbox r
//! ```
//! If you have already created a sandbox previously then it will be reused
//! (usually cleared on reboots).
//! #### Generate
//! Generates new conductor sandboxes and installs apps / dnas.
//! ```shell
//! hc sandbox generate
//! # or shorter
//! hc sandbox g
//! ```
//! For example this will generate 5 sandboxes with app ids set to `my-app`
//! using the `elemental-chat.dna` from the current directory with a quic
//! network sandbox to localhost.
//! _You don't need to specify dnas when they are in the directory._
//! ```shell
//! hc sandbox gen -a "my-app" -n 5 ./elemental-chat.dna network quic
//! ```
//! You can also generate and run in the same command:
//! (Notice the number of conductors and dna path must come before the gen sub-command).
//! ```shell
//! hc sandbox r -n 5 ./elemental-chat.dna gen -a "my-app" network quic
//! ```
//! #### Call
//! Allows calling the [`AdminRequest`] api.
//! If the conductors are not already running they
//! will be run to make the call.
//!
//! ```shell
//! hc sandbox call list-cells
//! ```
//! #### List and Clean
//! These commands allow you to list the persisted sandboxes
//! in the current directory (from the`.hcXXX`) file.
//! You can use the index from:
//! ```shell
//! hc sandbox list
//! ```
//! Output:
//! ```shell
//! hc-sandbox:
//! Sandboxes contained in `.hcXXX`
//! 0: /tmp/KOXgKVLBVvoxe8iKD4iSS
//! 1: /tmp/m8VHwwt93Uh-nF-vr6nf6
//! 2: /tmp/t6adQomMLI5risj8K2Tsd
//! ```
//! To then call or run an individual sandbox (or subset):
//!
//! ```shell
//! hc sandbox r -i=0,2
//! ```
//! You can clean up these sandboxes with:
//! ```shell
//! hc sandbox clean 0 2
//! # Or clean all
//! hc sandbox clean
//! ```
//! ## Library
//! This crate can also be used as a library so you can create more
//! complex sandboxes / admin calls.
//! See the docs:
//! ```shell
//! cargo doc --open
//! ```
//! and the examples.
#![allow(deprecated)]
use std::path::Path;
use std::path::PathBuf;
use holochain_conductor_api::{AdminRequest, AdminResponse};
use holochain_websocket::WebsocketResult;
use holochain_websocket::WebsocketSender;
use ports::get_admin_api;
pub use ports::force_admin_port;
/// Print a msg with `hc-sandbox: ` pre-pended
/// and ansi colors.
macro_rules! msg {
($($arg:tt)*) => ({
use ansi_term::Color::*;
print!("{} ", Blue.bold().paint("hc-sandbox:"));
println!($($arg)*);
})
}
pub mod bundles;
pub mod calls;
pub mod cli;
#[doc(hidden)]
pub mod cmds;
pub mod config;
pub mod generate;
pub mod run;
pub mod sandbox;
pub mod save;
pub use cli::HcSandbox;
mod ports;
/// An active connection to a running conductor.
pub struct CmdRunner {
client: WebsocketSender,
}
impl CmdRunner {
const HOLOCHAIN_PATH: &'static str = "holochain";
/// Create a new connection for calling admin interface commands.
/// Panics if admin port fails to connect.
pub async fn new(port: u16) -> Self {
Self::try_new(port)
.await
.expect("Failed to create CmdRunner because admin port failed to connect")
}
/// Create a new connection for calling admin interface commands.
pub async fn try_new(port: u16) -> WebsocketResult<Self> {
let client = get_admin_api(port).await?;
Ok(Self { client })
}
/// Create a command runner from a sandbox path.
/// This expects holochain to be on the path.
pub async fn from_sandbox(
sandbox_path: PathBuf,
) -> anyhow::Result<(Self, tokio::process::Child)> {
Self::from_sandbox_with_bin_path(Path::new(Self::HOLOCHAIN_PATH), sandbox_path).await
}
/// Create a command runner from a sandbox path and
/// set the path to the holochain binary.
pub async fn from_sandbox_with_bin_path(
holochain_bin_path: &Path,
sandbox_path: PathBuf,
) -> anyhow::Result<(Self, tokio::process::Child)> {
let conductor = run::run_async(holochain_bin_path, sandbox_path, None).await?;
let cmd = CmdRunner::try_new(conductor.0).await?;
Ok((cmd, conductor.1))
}
/// Make an Admin request to this conductor.
pub async fn command(&mut self, cmd: AdminRequest) -> anyhow::Result<AdminResponse> {
let response: Result<AdminResponse, _> = self.client.request(cmd).await;
Ok(response?)
}
}
#[macro_export]
/// Expect that an enum matches a variant and panic if it doesn't.
macro_rules! expect_variant {
($var:expr => $variant:path, $error_msg:expr) => {
match $var {
$variant(v) => v,
_ => panic!(format!("{}: Expected {} but got {:?}", $error_msg, stringify!($variant), $var)),
}
};
($var:expr => $variant:path) => {
expect_variant!($var => $variant, "")
};
}
#[macro_export]
/// Expect that an enum matches a variant and return an error if it doesn't.
macro_rules! expect_match {
($var:expr => $variant:path, $error_msg:expr) => {
match $var {
$variant(v) => v,
_ => anyhow::bail!("{}: Expected {} but got {:?}", $error_msg, stringify!($variant), $var),
}
};
($var:expr => $variant:path) => {
expect_variant!($var => $variant, "")
};
}