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 forc index stop #413

Merged
merged 3 commits into from
Dec 9, 2022
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/forc-index/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ anyhow = "1"
clap = { version = "3", features = ["derive", "env"] }
forc-tracing = { version = "0.31", default-features = false }
forc-util = { version = "0.31" }
fuel-indexer-lib = { version = "0.1", path = "../../packages/fuel-indexer-lib" }
fuel-tx = { version = "0.23", features = ["builder"] }
fuels-types = "0.31"
hex = "0.4.3"
Expand Down
3 changes: 3 additions & 0 deletions plugins/forc-index/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub(crate) use crate::commands::{
deploy::Command as DeployCommand, init::Command as InitCommand,
new::Command as NewCommand, start::Command as StartCommand,
stop::Command as StopCommand,
};
use clap::{Parser, Subcommand};
use forc_tracing::{init_tracing_subscriber, TracingSubscriberOptions};
Expand All @@ -19,6 +20,7 @@ enum ForcIndex {
New(NewCommand),
Deploy(DeployCommand),
Start(StartCommand),
Stop(StopCommand),
}

pub async fn run_cli() -> Result<(), anyhow::Error> {
Expand All @@ -34,5 +36,6 @@ pub async fn run_cli() -> Result<(), anyhow::Error> {
ForcIndex::New(command) => crate::commands::new::exec(command),
ForcIndex::Deploy(command) => crate::commands::deploy::exec(command),
ForcIndex::Start(command) => crate::commands::start::exec(command),
ForcIndex::Stop(command) => crate::commands::stop::exec(command),
}
}
2 changes: 1 addition & 1 deletion plugins/forc-index/src/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;

/// Create a new Forc project in an existing directory.
/// Deploy an index asset bundle to a remote or locally running indexer server.
#[derive(Debug, Parser)]
pub struct Command {
/// URL at which to upload index assets
Expand Down
2 changes: 1 addition & 1 deletion plugins/forc-index/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;

/// Create a new Forc project in the current directory.
/// Create a new indexer project in the current directory.
#[derive(Debug, Parser)]
pub struct Command {
/// Name of index
Expand Down
1 change: 1 addition & 0 deletions plugins/forc-index/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod deploy;
pub mod init;
pub mod new;
pub mod start;
pub mod stop;
2 changes: 1 addition & 1 deletion plugins/forc-index/src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;

/// Create a new Forc project in an existing directory.
/// Create a new indexer project in a new directory.
#[derive(Debug, Parser)]
pub struct Command {
/// Name of index
Expand Down
2 changes: 1 addition & 1 deletion plugins/forc-index/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;

/// Create a new Forc project in an existing directory.
/// Start a local indexer service.
#[derive(Debug, Parser)]
pub struct Command {
/// Log level passed to the Fuel Indexer service.
Expand Down
25 changes: 25 additions & 0 deletions plugins/forc-index/src/commands/stop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::{ops::forc_index_stop, utils::defaults};
use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;

/// Stop a running index.
#[derive(Debug, Parser)]
pub struct Command {
/// URL at which index is deployed
#[clap(long, default_value = defaults::DEFAULT_INDEXER_URL, help = "URL at which to upload index assets.")]
pub url: String,

/// Path of the index manifest to be parsed
#[clap(long, help = "Path of the index manifest to be parsed.")]
pub manifest: PathBuf,

/// Authentication header value
#[clap(long, help = "Authentication header value.")]
pub auth: Option<String>,
}

pub fn exec(command: Command) -> Result<()> {
forc_index_stop::init(command)?;
Ok(())
}
41 changes: 1 addition & 40 deletions plugins/forc-index/src/ops/forc_index_deploy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::DeployCommand;
use crate::{cli::DeployCommand, utils::extract_manifest_fields};
use reqwest::{
blocking::{multipart::Form, Client},
header::{HeaderMap, AUTHORIZATION},
Expand All @@ -10,45 +10,6 @@ use std::io::{BufReader, Read};
use std::path::Path;
use tracing::{error, info};

fn extract_manifest_fields(
manifest: serde_yaml::Value,
) -> anyhow::Result<(String, String, String, String)> {
let namespace: String = manifest
.get(&serde_yaml::Value::String("namespace".into()))
.unwrap()
.as_str()
.unwrap()
.to_string();
let identifier: String = manifest
.get(&serde_yaml::Value::String("identifier".into()))
.unwrap()
.as_str()
.unwrap()
.to_string();
let graphql_schema: String = manifest
.get(&serde_yaml::Value::String("graphql_schema".into()))
.unwrap()
.as_str()
.unwrap()
.to_string();
let module: serde_yaml::Value = manifest
.get(&serde_yaml::Value::String("module".into()))
.unwrap()
.to_owned();
let module_path: String = module
.get(&serde_yaml::Value::String("wasm".into()))
.unwrap_or_else(|| {
module
.get(&serde_yaml::Value::String("native".into()))
.unwrap()
})
.as_str()
.unwrap()
.to_string();

Ok((namespace, identifier, graphql_schema, module_path))
}

pub fn init(command: DeployCommand) -> anyhow::Result<()> {
let mut manifest_file = fs::File::open(&command.manifest).unwrap_or_else(|_| {
panic!(
Expand Down
57 changes: 57 additions & 0 deletions plugins/forc-index/src/ops/forc_index_stop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::cli::StopCommand;
use fuel_indexer_lib::manifest::Manifest;
use reqwest::{
blocking::Client,
header::{HeaderMap, AUTHORIZATION},
StatusCode,
};
use serde_json::{to_string_pretty, value::Value, Map};
use tracing::{error, info};

pub fn init(command: StopCommand) -> anyhow::Result<()> {
let manifest: Manifest = Manifest::from_file(command.manifest.as_path())?;

let target = format!(
"{}/api/index/{}/{}",
&command.url, &manifest.namespace, &manifest.identifier
);

let mut headers = HeaderMap::new();
headers.insert(
AUTHORIZATION,
command.auth.unwrap_or_else(|| "fuel".into()).parse()?,
);

info!(
"\n🛑 Stopping index '{}.{}' at {}",
&manifest.namespace, &manifest.identifier, &target
);

let res = Client::new()
.delete(&target)
.headers(headers)
.send()
.expect("Failed to deploy index.");

if res.status() != StatusCode::OK {
error!(
"\n❌ {} returned a non-200 response code: {:?}",
&target,
res.status()
);
return Ok(());
}

let res_json = res
.json::<Map<String, Value>>()
.expect("Failed to read JSON response.");

println!("\n{}", to_string_pretty(&res_json)?);

info!(
"\n✅ Successfully stopped index '{}.{}' at {} \n",
&manifest.namespace, &manifest.identifier, &target
);

Ok(())
}
1 change: 1 addition & 0 deletions plugins/forc-index/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod forc_index_deploy;
pub mod forc_index_init;
pub mod forc_index_new;
pub mod forc_index_start;
pub mod forc_index_stop;
39 changes: 39 additions & 0 deletions plugins/forc-index/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,42 @@ pub mod defaults;
pub(crate) fn dasherize_to_underscore(s: &str) -> String {
str::replace(s, "-", "_")
}

pub(crate) fn extract_manifest_fields(
deekerno marked this conversation as resolved.
Show resolved Hide resolved
manifest: serde_yaml::Value,
) -> anyhow::Result<(String, String, String, String)> {
let namespace: String = manifest
.get(&serde_yaml::Value::String("namespace".into()))
.unwrap()
.as_str()
.unwrap()
.to_string();
let identifier: String = manifest
.get(&serde_yaml::Value::String("identifier".into()))
.unwrap()
.as_str()
.unwrap()
.to_string();
let graphql_schema: String = manifest
.get(&serde_yaml::Value::String("graphql_schema".into()))
.unwrap()
.as_str()
.unwrap()
.to_string();
let module: serde_yaml::Value = manifest
.get(&serde_yaml::Value::String("module".into()))
.unwrap()
.to_owned();
let module_path: String = module
.get(&serde_yaml::Value::String("wasm".into()))
.unwrap_or_else(|| {
module
.get(&serde_yaml::Value::String("native".into()))
.unwrap()
})
.as_str()
.unwrap()
.to_string();

Ok((namespace, identifier, graphql_schema, module_path))
}