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

feat: use safenode-manager #56

Merged
merged 8 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions resources/ansible/roles/node/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
is_genesis: False
otlp_endpoint: http://dev-testnet-infra-f2016fb1217aaf30.elb.eu-west-2.amazonaws.com:4317
node_archive_filename: safenode-latest-x86_64-unknown-linux-musl.tar.gz
node_archive_url: https://sn-node.s3.eu-west-2.amazonaws.com/{{ node_archive_filename }}
node_archive_dest_path: /usr/local/bin
node_manager_archive_filename: safenode-manager-latest-x86_64-unknown-linux-musl.tar.gz
node_manager_archive_url: https://sn-node-manager.s3.eu-west-2.amazonaws.com/{{ node_manager_archive_filename }}
node_manager_archive_dest_path: /usr/local/bin
node_port: 12000
node_rpc_port: 12001
node_instance_count: 20
Expand Down
52 changes: 37 additions & 15 deletions resources/ansible/roles/node/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,49 @@
node_rpc_ip: "{{ ansible_host }}"
when: provider == "digital-ocean"

- name: get the latest version or a custom build of the node
- name: download the node manager binary
ansible.builtin.get_url:
url: "{{ node_archive_url }}"
dest: /tmp/{{ node_archive_filename }}
url: "{{ node_manager_archive_url }}"
dest: /tmp/{{ node_manager_archive_filename }}

- name: extract the node binary to /usr/local/bin
- name: extract the node manager binary to /usr/local/bin
become: True
ansible.builtin.unarchive:
src: "/tmp/{{ node_archive_filename }}"
dest: "{{ node_archive_dest_path }}"
src: "/tmp/{{ node_manager_archive_filename }}"
dest: "{{ node_manager_archive_dest_path }}"
remote_src: True

- name: start the node services
include_tasks: start_service.yml
- name: add genesis node service
ansible.builtin.command:
argv: "{{ command_args | reject('equalto', omit) | list }}"
vars:
node_rpc_port: "{{ 12000 + item }}"
loop: "{{ range(1, (node_instance_count | int) + 1)|list }}"
when: not is_genesis
command_args:
- "{{ node_manager_archive_dest_path }}/safenode-manager"
- add
- --first
- --port=12000
b-zee marked this conversation as resolved.
Show resolved Hide resolved
- --rpc-port=12001
# - "{{ ('--version=' + node_version) if node_version is defined else omit }}"
b-zee marked this conversation as resolved.
Show resolved Hide resolved
- "{{ ('--url=' + node_archive_url) if node_archive_url is defined else omit }}"
when: is_genesis

- name: start the genesis node service
include_tasks: start_service.yml
- name: add node services
ansible.builtin.command:
# The `omit` filter is used to remove the `--version` argument if the
# `node_version` variable is not defined. If the argv would be otherwise
# defined as '', the command would fail as it doesn't expect an empty
# argument, so it's omitted instead.
argv: "{{ command_args | reject('equalto', omit) | list }}"
vars:
item: 1
when: is_genesis
command_args:
- "{{ node_manager_archive_dest_path }}/safenode-manager"
- add
- "--count={{ node_instance_count }}"
- "--peer={{ genesis_multiaddr }}"
b-zee marked this conversation as resolved.
Show resolved Hide resolved
# - "{{ ('--version=' + node_version) if node_version is defined else omit }}"
- "{{ ('--url=' + node_archive_url) if node_archive_url is defined else omit }}"
when: not is_genesis

- name: start the node services
become: True
command: safenode-manager start
7 changes: 0 additions & 7 deletions resources/ansible/roles/node/tasks/start_service.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
---
- name: copy service file
become: True
template:
src: sn_node.service.j2
dest: /etc/systemd/system/safenode@{{ item }}.service
register: service_template_created

- name: reload the system manager configuration
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this file actually being used for anything any more? Could we delete it?

become: True
command: systemctl daemon-reload
Expand Down
2 changes: 0 additions & 2 deletions resources/ansible/roles/safenode_rpc_client/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ branch: main
safenode_rpc_client_archive_filename: safenode_rpc_client-x86_64-unknown-linux-musl.tar.gz
safenode_rpc_client_archive_url: https://sn-node.s3.eu-west-2.amazonaws.com/{{ safenode_rpc_client_archive_filename }}
b-zee marked this conversation as resolved.
Show resolved Hide resolved
safenode_rpc_client_archive_dest_path: /usr/local/bin
royalties_cash_notes_dir_path: /home/safe/.local/share/safe/node/royalties-cash-notes
node_rpc_port: 12001

This file was deleted.

30 changes: 0 additions & 30 deletions resources/ansible/roles/safenode_rpc_client/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
---
# The `node_rpc_ip` is used in the service definition file.
- name: get the private IP for the instance
set_fact:
node_rpc_ip: "{{ instance_facts.instances[0].network_interfaces[0].private_ip_address }}"
when: provider == "aws"

- name: use the public IP as the RPC address on DO
set_fact:
node_rpc_ip: "{{ ansible_host }}"
when: provider == "digital-ocean"

- name: get the latest version or a custom build of the RPC client
ansible.builtin.get_url:
url: "{{ safenode_rpc_client_archive_url }}"
Expand All @@ -21,22 +10,3 @@
src: "/tmp/{{ safenode_rpc_client_archive_filename }}"
dest: "{{ safenode_rpc_client_archive_dest_path }}"
remote_src: True

- name: copy safenode_rpc_client service file
become: True
template:
src: safenode_rpc_client.service.j2
dest: /etc/systemd/system/safenode_rpc_client.service
register: service_template_created

- name: reload the system manager configuration
become: True
command: systemctl daemon-reload
when: service_template_created.changed

- name: start the safenode_rpc_client service to store network royalties cash notes to disk
become: True
systemd:
name: safenode_rpc_client
state: started
enabled: yes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
org: maidsafe
branch: main
safenode_rpc_client_archive_dest_path: /usr/local/bin
royalties_cash_notes_dir_path: /home/safe/.local/share/safe/node/royalties-cash-notes
node_rpc_port: 12001
30 changes: 30 additions & 0 deletions resources/ansible/roles/safenode_rpc_client_service/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
# The `node_rpc_ip` is used in the service definition file.
- name: get the private IP for the instance
set_fact:
node_rpc_ip: "{{ instance_facts.instances[0].network_interfaces[0].private_ip_address }}"
when: provider == "aws"

- name: use the public IP as the RPC address on DO
set_fact:
node_rpc_ip: "{{ ansible_host }}"
when: provider == "digital-ocean"

- name: copy safenode_rpc_client service file
become: True
template:
src: safenode_rpc_client.service.j2
dest: /etc/systemd/system/safenode_rpc_client.service
register: service_template_created

- name: reload the system manager configuration
become: True
command: systemctl daemon-reload
when: service_template_created.changed

- name: start the safenode_rpc_client service to store network royalties cash notes to disk
become: True
systemd:
name: safenode_rpc_client
state: started
enabled: yes
6 changes: 6 additions & 0 deletions resources/ansible/safenode_rpc_client_service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: start safenode_rpc_client service on genesis node
hosts: all
become: False
roles:
- safenode_rpc_client_service
56 changes: 50 additions & 6 deletions src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ impl DeployCmd {
err
})?;

// Build
self.provision_safenode_rpc_client().await.map_err(|err| {
println!("Failed to provision safenode rpc client {err:?}");
err
})?;

let (multiaddr, _) = self
.testnet_deploy
.get_genesis_multiaddr(&self.name)
Expand All @@ -87,10 +93,11 @@ impl DeployCmd {
err
})?;

self.provision_safenode_rpc_client(&multiaddr)
// Run service
self.provision_safenode_rpc_client_service(&multiaddr)
.await
.map_err(|err| {
println!("Failed to provision safenode rpc client {err:?}");
println!("Failed to provision safenode rpc client service {err:?}");
err
})?;

Expand Down Expand Up @@ -196,7 +203,7 @@ impl DeployCmd {
Ok(())
}

pub async fn provision_safenode_rpc_client(&self, genesis_multiaddr: &str) -> Result<()> {
pub async fn provision_safenode_rpc_client(&self) -> Result<()> {
let start = Instant::now();
println!("Running ansible against genesis node to deploy safenode_rpc_client...");
self.testnet_deploy.ansible_runner.run_playbook(
Expand All @@ -206,7 +213,26 @@ impl DeployCmd {
self.name
)),
self.testnet_deploy.cloud_provider.get_ssh_user(),
Some(self.build_safenode_rpc_client_extra_vars_doc(genesis_multiaddr)?),
Some(self.build_safenode_rpc_client_extra_vars_doc()?),
)?;
print_duration(start.elapsed());
Ok(())
}

pub async fn provision_safenode_rpc_client_service(
&self,
genesis_multiaddr: &str,
) -> Result<()> {
let start = Instant::now();
println!("Running ansible against genesis node to start safenode_rpc_client service...");
self.testnet_deploy.ansible_runner.run_playbook(
PathBuf::from("safenode_rpc_client_service.yml"),
PathBuf::from("inventory").join(format!(
".{}_genesis_inventory_digital_ocean.yml",
self.name
)),
self.testnet_deploy.cloud_provider.get_ssh_user(),
Some(self.start_safenode_rpc_client_service_extra_vars_doc(genesis_multiaddr)?),
)?;
print_duration(start.elapsed());
Ok(())
Expand Down Expand Up @@ -382,7 +408,7 @@ impl DeployCmd {
Ok(extra_vars)
}

fn build_safenode_rpc_client_extra_vars_doc(&self, genesis_multiaddr: &str) -> Result<String> {
fn build_safenode_rpc_client_extra_vars_doc(&self) -> Result<String> {
let mut extra_vars = String::new();
extra_vars.push_str("{ ");
Self::add_value(
Expand All @@ -391,7 +417,6 @@ impl DeployCmd {
&self.testnet_deploy.cloud_provider.to_string(),
);
Self::add_value(&mut extra_vars, "testnet_name", &self.name);
Self::add_value(&mut extra_vars, "genesis_multiaddr", genesis_multiaddr);
match &self.sn_codebase_type {
SnCodebaseType::Branch {
repo_owner, branch, ..
Expand Down Expand Up @@ -421,6 +446,25 @@ impl DeployCmd {
Ok(extra_vars)
}

fn start_safenode_rpc_client_service_extra_vars_doc(
&self,
genesis_multiaddr: &str,
) -> Result<String> {
let mut extra_vars = String::new();
extra_vars.push_str("{ ");
Self::add_value(
&mut extra_vars,
"provider",
&self.testnet_deploy.cloud_provider.to_string(),
);
Self::add_value(&mut extra_vars, "testnet_name", &self.name);
Self::add_value(&mut extra_vars, "genesis_multiaddr", genesis_multiaddr);

let mut extra_vars = extra_vars.strip_suffix(", ").unwrap().to_string();
extra_vars.push_str(" }");
Ok(extra_vars)
}

fn add_value(document: &mut String, name: &str, value: &str) {
document.push_str(&format!("\"{name}\": \"{value}\", "))
}
Expand Down
23 changes: 19 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use indicatif::{ProgressBar, ProgressStyle};
use log::debug;
use rand::Rng;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use rpc_client::parse_output;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{
Expand Down Expand Up @@ -295,7 +296,7 @@ impl TestnetDeployBuilder {
vault_password_path,
);
let rpc_client = RpcClient::new(
PathBuf::from("./safenode_rpc_client"),
PathBuf::from("/usr/local/bin/safenode_rpc_client"),
working_directory_path.clone(),
);

Expand Down Expand Up @@ -422,9 +423,23 @@ impl TestnetDeploy {
PathBuf::from("inventory").join(format!(".{name}_genesis_inventory_digital_ocean.yml")),
)?;
let genesis_ip = genesis_inventory[0].1;
let node_info = self
.rpc_client
.get_info(format!("{}:12001", genesis_ip).parse()?)?;
// let node_info = self
b-zee marked this conversation as resolved.
Show resolved Hide resolved
// .rpc_client
// // .get_info(format!("{}:12001", genesis_ip).parse()?, "root", &genesis_ip.to_string())?;
// .get_info(
// (Ipv4Addr::LOCALHOST, 12001).into(),
// "root",
// &genesis_ip.to_string(),
// )?;

let output = self.ssh_client.run_command(
&genesis_ip,
"root",
"/usr/local/bin/safenode_rpc_client 127.0.0.1:12001 info",
false,
)?;
let node_info = parse_output(output)?;

let multiaddr = format!("/ip4/{}/tcp/12000/p2p/{}", genesis_ip, node_info.peer_id);
// The genesis_ip is obviously inside the multiaddr, but it's just being returned as a
// separate item for convenience.
Expand Down
32 changes: 2 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,32 +146,6 @@ enum Commands {
/// machine. This information gets used for Slack notifications.
#[arg(long)]
node_count: Option<u16>,
/// Optionally supply a version number to be used for the safe binary. There should be no
/// 'v' prefix.
///
/// You can supply this value to inventory when you are running the process on a different
/// machine from where the testnet was deployed.
///
/// This argument must be used in conjunction with the --safenode-version argument.
///
/// The --safe-version and --safenode-version arguments are mutually exclusive with the
/// --branch and --repo-owner arguments. You can only supply version numbers or a custom
/// branch, not both.
#[arg(long)]
safe_version: Option<String>,
#[arg(long)]
/// Optionally supply a version number to be used for the safenode binary. There should be
/// no 'v' prefix.
///
/// You can supply this value to inventory when you are running the process on a different
/// machine from where the testnet was deployed.
///
/// This argument must be used in conjunction with the --safe-version argument.
///
/// The --safe-version and --safenode-version arguments are mutually exclusive with the
/// --branch and --repo-owner arguments. You can only supply version numbers or a custom
/// branch, not both.
safenode_version: Option<String>,
},
#[clap(name = "logs", subcommand)]
Logs(LogCommands),
Expand Down Expand Up @@ -329,6 +303,7 @@ async fn main() -> Result<()> {
logstash_stack_name,
safe_version,
safenode_version,
..
}) => {
let sn_codebase_type = get_sn_codebase_type(
branch,
Expand Down Expand Up @@ -385,11 +360,8 @@ async fn main() -> Result<()> {
branch,
repo_owner,
node_count,
safe_version,
safenode_version,
}) => {
let sn_codebase_type =
get_sn_codebase_type(branch, repo_owner, safe_version, safenode_version, None)?;
let sn_codebase_type = get_sn_codebase_type(branch, repo_owner, None, None, None)?;

let testnet_deploy = TestnetDeployBuilder::default().provider(provider).build()?;
testnet_deploy
Expand Down