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

Graphman deploy command #4930

Merged
merged 2 commits into from
Oct 24, 2023
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
26 changes: 26 additions & 0 deletions node/src/bin/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,20 @@ pub enum Command {
#[clap(long, short)]
force: bool,
},

// Deploy a subgraph
Deploy {
name: DeploymentSearch,
deployment: DeploymentSearch,

/// The url of the graph-node
#[clap(long, short, default_value = "http://localhost:8020")]
url: String,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be awesome if we would not need a url, but getting away from that might be pretty tough as we would have to instantiate our own SubgraphRegistrar; if the default is ok in most cases, I think it's not a big deal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That didn't cross my mind, just took a look at how that would turn out. We still would need to provide a NodeID when creating a new SubgraphRegistrar.
And the default_value for the url i believe is almost always ok unless the indexer change it explicitly. So we can go with this now and update in the future?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's not hold this up because of the URL; just go ahead and merge


/// Create the subgraph name if it does not exist
#[clap(long, short)]
create: bool,
},
}

impl Command {
Expand Down Expand Up @@ -1513,6 +1527,18 @@ async fn main() -> anyhow::Result<()> {
)
.await
}

Deploy {
deployment,
name,
url,
create,
} => {
let store = ctx.store();
let subgraph_store = store.subgraph_store();

commands::deploy::run(subgraph_store, deployment, name, url, create).await
}
}
}

Expand Down
105 changes: 105 additions & 0 deletions node/src/manager/commands/deploy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use std::sync::Arc;

use graph::prelude::{
anyhow::{anyhow, bail, Result},
reqwest,
serde_json::{json, Value},
SubgraphName, SubgraphStore,
};

use crate::manager::deployment::DeploymentSearch;

// Function to send an RPC request and handle errors
async fn send_rpc_request(url: &str, payload: Value) -> Result<()> {
let client = reqwest::Client::new();
let response = client.post(url).json(&payload).send().await?;

if response.status().is_success() {
Ok(())
} else {
Err(response
.error_for_status()
.expect_err("Failed to parse error response")
.into())
}
}

// Function to send subgraph_create request
async fn send_create_request(name: &str, url: &str) -> Result<()> {
// Construct the JSON payload for subgraph_create
let create_payload = json!({
"jsonrpc": "2.0",
"method": "subgraph_create",
"params": {
"name": name,
},
"id": "1"
});

// Send the subgraph_create request
send_rpc_request(url, create_payload)
.await
.map_err(|e| e.context(format!("Failed to create subgraph with name `{}`", name)))
}

// Function to send subgraph_deploy request
async fn send_deploy_request(name: &str, deployment: &str, url: &str) -> Result<()> {
// Construct the JSON payload for subgraph_deploy
let deploy_payload = json!({
"jsonrpc": "2.0",
"method": "subgraph_deploy",
"params": {
"name": name,
"ipfs_hash": deployment,
},
"id": "1"
});

// Send the subgraph_deploy request
send_rpc_request(url, deploy_payload).await.map_err(|e| {
e.context(format!(
"Failed to deploy subgraph `{}` to `{}`",
deployment, name
))
})
}
pub async fn run(
subgraph_store: Arc<impl SubgraphStore>,
deployment: DeploymentSearch,
search: DeploymentSearch,
url: String,
create: bool,
) -> Result<()> {
let hash = match deployment {
DeploymentSearch::Hash { hash, shard: _ } => hash,
_ => bail!("The `deployment` argument must be a valid IPFS hash"),
};

let name = match search {
DeploymentSearch::Name { name } => name,
_ => bail!("The `name` must be a valid subgraph name"),
};

if create {
println!("Creating subgraph `{}`", name);
let subgraph_name =
SubgraphName::new(name.clone()).map_err(|_| anyhow!("Invalid subgraph name"))?;

let exists = subgraph_store.subgraph_exists(&subgraph_name)?;

if exists {
bail!("Subgraph with name `{}` already exists", name);
}

// Send the subgraph_create request
send_create_request(&name, &url).await?;
println!("Subgraph `{}` created", name);
}

// Send the subgraph_deploy request
println!("Deploying subgraph `{}` to `{}`", hash, name);
send_deploy_request(&name, &hash, &url).await?;
println!("Subgraph `{}` deployed to `{}`", name, url);

Ok(())
}
1 change: 1 addition & 0 deletions node/src/manager/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod config;
pub mod copy;
pub mod create;
pub mod database;
pub mod deploy;
pub mod drop;
pub mod index;
pub mod info;
Expand Down