Module start

Source
Expand description

The start module contains functionality relating to downloading and starting NATS servers and wasmCloud hosts.

§Downloading and Starting NATS and wasmCloud

use anyhow::{anyhow, Result};
use wash_lib::common::CommandGroupUsage;
use wash_lib::start::{
    start_wasmcloud_host,
    start_nats_server,
    ensure_nats_server,
    ensure_wasmcloud,
    NatsConfig
};
use std::path::PathBuf;

#[tokio::main]
async fn main() -> Result<()> {
    let install_dir = PathBuf::from("/tmp");

    // Download NATS if not already installed
    let nats_binary = ensure_nats_server("v2.10.7", &install_dir).await?;

    // Start NATS server, redirecting output to a log file
    let nats_log_path = install_dir.join("nats.log");
    let nats_log_file = tokio::fs::File::create(&nats_log_path).await?.into_std().await;
    let config = NatsConfig::new_standalone("127.0.0.1", 4222, None);
    let mut nats_process = start_nats_server(
        nats_binary,
        nats_log_file,
        config,
        CommandGroupUsage::UseParent,
    ).await?;

    // Download wasmCloud if not already installed
    let wasmcloud_executable = ensure_wasmcloud("v0.57.1", &install_dir).await?;

    // Redirect output (which is on stderr) to a log file
    let log_path = install_dir.join("wasmcloud_stderr.log");
    let log_file = tokio::fs::File::create(&log_path).await?.into_std().await;

    let mut wasmcloud_process = start_wasmcloud_host(
        wasmcloud_executable,
        std::process::Stdio::null(),
        log_file,
        std::collections::HashMap::new(),
    ).await?;

    // Park thread, wasmCloud and NATS are running

    // Terminate processes
    nats_process.kill().await?;
    wasmcloud_process.kill().await?;
    Ok(())
}

Structs§

GitHubRelease
GitHubRelease represents the necessary fields to determine wadm and/or wasmCloud GitHub release (https://developer.github.com/v3/repos/releases/) object has new patch version available. The fields are based on the response from the response schema from the docs.
NatsConfig
Configuration for a NATS server that supports running either in “standalone” or “leaf” mode. See the respective NatsConfig::new_standalone and NatsConfig::new_leaf implementations below for more information.
WadmConfig
Configuration for wadm

Constants§

DOWNLOAD_CLIENT_USER_AGENT
GITHUB_WASMCLOUD_ORG
GITHUB_WASMCLOUD_WADM_REPO
GITHUB_WASMCLOUD_WASMCLOUD_REPO
NATS_SERVER_BINARY
NATS_SERVER_CONF
NATS_SERVER_PID
WADM_BINARY
WADM_PID
WASMCLOUD_HOST_BIN

Functions§

download_binary_from_github
Reusable function to download a release tarball from GitHub and extract an embedded binary to a specified directory
download_nats_server
Downloads the NATS binary for the architecture and operating system of the current host machine.
download_wadm
Downloads the wadm binary for the architecture and operating system of the current host machine.
download_wasmcloud
A wrapper around the download_wasmcloud_for_os_arch_pair function that uses the architecture and operating system of the current host machine.
download_wasmcloud_for_os_arch_pair
Downloads the specified GitHub release version of the wasmCloud host from https://github.com/wasmCloud/wasmcloud-otp/releases/ and unpacking the contents for a specified OS/ARCH pair to a directory. Returns the path to the Elixir executable.
ensure_nats_server
Downloads the NATS binary for the architecture and operating system of the current host machine.
ensure_nats_server_for_os_arch_pair
Ensures the nats-server binary is installed, returning the path to the executable early if it exists or downloading the specified GitHub release version of nats-server from https://github.com/nats-io/nats-server/releases/ and unpacking the binary for a specified OS/ARCH pair to a directory. Returns the path to the NATS executable.
ensure_wadm
Downloads the wadm binary for the architecture and operating system of the current host machine.
ensure_wadm_for_os_arch_pair
Ensures the wadm binary is installed, returning the path to the executable early if it exists or downloading the specified GitHub release version of wadm from https://github.com/wasmcloud/wadm/releases/ and unpacking the binary for a specified OS/ARCH pair to a directory. Returns the path to the wadm executable.
ensure_wasmcloud
A wrapper around the ensure_wasmcloud_for_os_arch_pair function that uses the architecture and operating system of the current host machine.
ensure_wasmcloud_for_os_arch_pair
Ensures the wasmcloud_host application is installed, returning the path to the executable early if it exists or downloading the specified GitHub release version of the wasmCloud host from https://github.com/wasmCloud/wasmcloud-otp/releases/ and unpacking the contents for a specified OS/ARCH pair to a directory. Returns the path to the executable.
find_wasmcloud_binary
Helper function to indicate if the wasmCloud host tarball is successfully installed in a directory. Returns the path to the binary if it exists
get_download_client
Helper function to set up a reqwest client for performing the download
nats_pid_path
Helper function to get the path to the NATS server pid file
new_patch_releases_after
Get a full list of github patch releases that exist after the provided version.
new_patch_version_of_after_string
Returns the latest patch version of the provided version.
start_nats_server
Helper function to execute a NATS server binary with required wasmCloud arguments, e.g. JetStream
start_wadm
Helper function to execute a wadm binary with optional arguments. This function does not check to see if a wadm instance is already running or managing a lattice as wadm does not need to be a singleton.
start_wasmcloud_host
Helper function to start a wasmCloud host given the path to the burrito release application
wait_for_server