32 releases

0.0.52-alpha2 Oct 1, 2020
0.0.51-alpha1 Jul 31, 2020
0.0.46-alpha1 Mar 27, 2020
0.0.42-alpha1 Dec 30, 2019
0.0.39-alpha4 Nov 25, 2019

#10 in #conductor

Download history 5/week @ 2024-02-23 3/week @ 2024-03-01 265/week @ 2024-03-29

265 downloads per month

GPL-3.0-only

2MB
40K SLoC

Holochain Conductor API

This crate is a library that provides types and functions that help with building a Holochain Conductor as described in ADR15.

Depending on the specific (application) use-case, the context in which a Holochain instance is run may vary drastically. Application developers may want to bundle Holochain with and statically link the core library into their custom made executable. In such a case, #holochain.rs may be used directly as a wrapper around a single instance.

In the general case, many different DNAs are being executed alongside each other in the context of the same agent, i.e. user. conductor.rs provides a struct that instantiates, holds, manages several Holochain instances. It makes use of config.rs which provides structs for conductor configuration that can be de-/serialized into config files like these.

All of the above is used in the conductor crate.

Example

extern crate holochain_conductor_lib;
extern crate holochain_core_types;
#[macro_use]
extern crate structopt;

use holochain_conductor_lib::{
    config::{load_configuration, Configuration},
    conductor::Conductor,
};
use holochain_core_types::error::HolochainError;
use std::{fs::File, io::prelude::*, path::PathBuf, sync::Arc};
use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(name = "hcc")]
struct Opt {
    /// Path to the toml configuration file for the conductor
    #[structopt(short = "c", long = "config", parse(from_os_str))]
    config: Option<PathBuf>,
}

    let opt = Opt::from_args();
    let config_path = opt.config
        .unwrap_or(PathBuf::from(r"~/.holochain/conductor/conductor_config.toml"));
    let config_path_str = config_path.to_str().unwrap();
    println!("Using config path: {}", config_path_str);
    match bootstrap_from_config(config_path_str) {
        Ok(mut conductor) => {
            if conductor.instances().len() > 0 {
                println!(
                    "Successfully loaded {} instance configurations",
                    conductor.instances().len()
                );
                println!("Starting all of them...");
                conductor.start_all_instances();
                println!("Done.");
                loop {}
            } else {
                println!("No instance started, bailing...");
            }
        }
        Err(error) => println!("Error while trying to boot from config: {:?}", error),
    };

fn bootstrap_from_config(path: &str) -> Result<Conductor, HolochainError> {
    let config = load_config_file(&String::from(path))?;
    config
        .check_consistency(&mut Arc::new(Box::new(Conductor::load_dna)))
        .map_err(|string| HolochainError::ConfigError(string))?;
    let mut conductor = Conductor::from_config(config);
    conductor.boot_from_config()?;
    Ok(conductor)
}

fn load_config_file(path: &String) -> Result<Configuration, HolochainError> {
    let mut f = File::open(path)?;
    let mut contents = String::new();
    f.read_to_string(&mut contents)?;
    Ok(load_configuration::<Configuration>(&contents)?)
}

Dependencies

~78MB
~1.5M SLoC