Skip to content

Commit

Permalink
Merge pull request #7 from mfactory-lab/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
MGrgr authored Aug 31, 2023
2 parents 56ee273 + 1b4316f commit d159362
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "svt-agent"
description = ""
version = "0.1.62"
version = "0.1.64"
authors = ["Vladyslav Korniienko <vk.tiamo@gmail.com>"]
repository = "https://github.com/mfactory-lab/svt-agent"
edition = "2021"
Expand Down
15 changes: 13 additions & 2 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,16 +477,27 @@ async fn check_balance(client: &MessengerClient) -> Result<u64> {
mod test {
use super::*;
use anchor_client::Cluster;
use solana_sdk::signature::{write_keypair_file, Keypair};
use std::env::temp_dir;
use std::path::PathBuf;

fn tmp_file_path(name: &str) -> String {
use std::env;
let out_dir = temp_dir();
let keypair = Keypair::new();

format!("{}/{}-{}", out_dir.to_str().unwrap(), name, keypair.pubkey())
}

#[tokio::test]
async fn test_agent_init() {
let keypair = PathBuf::from("./keypair.json");
let outfile = tmp_file_path("test_keypair.json");
let serialized_keypair = write_keypair_file(&Keypair::new(), &outfile).unwrap();

pub const CHANNEL_ID: &str = "Bk1EAvKminEwHjyVhG2QA7sQeU1W3zPnFE6rTnLWDKYJ";

let agent = Agent::new(&AgentArgs {
keypair,
keypair: PathBuf::from(&outfile),
cluster: Cluster::Devnet,
channel_id: CHANNEL_ID.parse().unwrap(),
program_id: MESSENGER_PROGRAM_ID.parse().unwrap(),
Expand Down
68 changes: 50 additions & 18 deletions src/task_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use shiplift::{Container, ContainerOptions, Docker, LogsOptions, PullOptions};
use sled::Db;
use std::collections::{HashMap, HashSet, VecDeque};
use std::env;
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::sync::{Mutex, MutexGuard};
Expand Down Expand Up @@ -301,31 +303,55 @@ impl TaskRunner {
format!("{}:{}", ANSIBLE_IMAGE, task.version())
}

fn create_inventory_file(&self, path: impl AsRef<Path>) -> std::io::Result<()> {
let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(path.as_ref())?;

file.write_all(
format!(
"[{}_validators]\nhost ansible_ssh_host={}",
&self.opts.cluster.to_string(),
env::var("DOCKER_HOST_IP").unwrap_or("localhost".to_string())
)
.as_bytes(),
)
}

fn build_cmd(&self, task: &Task) -> Vec<String> {
let host_file = format!("/etc/sv_manager/{}_host", &self.opts.cluster.to_string());

match self.create_inventory_file(&host_file) {
Ok(_) => {
info!("New inventory file `{}` created!", &host_file);
}
Err(e) => {
warn!("Failed to create inventory file. {}", e);
}
}

let mut cmd = vec![
"ansible-playbook".to_string(),
// "--limit=localhost",
// &format!("--inventory=./inventory/{}.yaml", self.opts.cluster),
// "--tags=agent".to_string(),
format!(
"--inventory={},",
env::var("DOCKER_HOST_IP").unwrap_or("localhost".to_string())
),
format!("--inventory={}", &host_file),
];

for &file in TASK_EXTRA_VARS {
let file = file
.replace("{home}", TASK_WORKING_DIR)
.replace("{cluster}", &self.opts.cluster.to_string());

cmd.push(format!("--extra-vars=@{}", file));
}

for &file in TASK_EXTRA_VARS_CHECKED {
if Path::new(file).exists() {
cmd.push(format!("--extra-vars=@{}", file));
}
}
// for &file in TASK_EXTRA_VARS {
// let file = file
// .replace("{home}", TASK_WORKING_DIR)
// .replace("{cluster}", &self.opts.cluster.to_string());
//
// cmd.push(format!("--extra-vars=@{}", file));
// }
//
// for &file in TASK_EXTRA_VARS_CHECKED {
// if Path::new(file).exists() {
// cmd.push(format!("--extra-vars=@{}", file));
// }
// }

if !task.args.is_empty() {
cmd.push(format!("--extra-vars={}", json!(task.args)));
Expand Down Expand Up @@ -418,6 +444,12 @@ mod tests {
use std::env;
use tracing_test::traced_test;

// #[test]
// fn can_create_inventory_file() {
// let runner = get_task_runner();
// runner.create_inventory_file("test.txt")
// }

fn get_task_runner() -> TaskRunner {
TaskRunner::new(TaskRunnerOpts::from(&AgentArgs {
keypair: Default::default(),
Expand Down

0 comments on commit d159362

Please sign in to comment.